Browse Source

Progress bar for attachment downloads

main
M66B 6 years ago
parent
commit
b460c06d1b
9 changed files with 98 additions and 61 deletions
  1. +44
    -39
      app/src/main/java/eu/faircode/email/AdapterAttachment.java
  2. +10
    -2
      app/src/main/java/eu/faircode/email/DaoAttachment.java
  3. +3
    -0
      app/src/main/java/eu/faircode/email/DaoFolder.java
  4. +2
    -2
      app/src/main/java/eu/faircode/email/FragmentMessage.java
  5. +0
    -1
      app/src/main/java/eu/faircode/email/MessageHelper.java
  6. +6
    -2
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java
  7. +19
    -0
      app/src/main/java/eu/faircode/email/TupleAttachment.java
  8. +2
    -1
      app/src/main/res/layout/fragment_message.xml
  9. +12
    -14
      app/src/main/res/layout/item_attachment.xml

+ 44
- 39
app/src/main/java/eu/faircode/email/AdapterAttachment.java View File

@ -35,6 +35,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@ -51,15 +52,15 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
private Context context;
private ExecutorService executor = Executors.newCachedThreadPool();
private List<EntityAttachment> all = new ArrayList<>();
private List<EntityAttachment> filtered = new ArrayList<>();
private List<TupleAttachment> all = new ArrayList<>();
private List<TupleAttachment> filtered = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View itemView;
TextView tvName;
TextView tvSize;
TextView tvProgress;
ImageView ivStatus;
ProgressBar progressbar;
ViewHolder(View itemView) {
super(itemView);
@ -67,8 +68,8 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
this.itemView = itemView;
tvName = itemView.findViewById(R.id.tvName);
tvSize = itemView.findViewById(R.id.tvSize);
tvProgress = itemView.findViewById(R.id.tvProgress);
ivStatus = itemView.findViewById(R.id.ivStatus);
progressbar = itemView.findViewById(R.id.progressbar);
}
private void wire() {
@ -81,26 +82,14 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
@Override
public void onClick(View view) {
final EntityAttachment attachment = filtered.get(getLayoutPosition());
final TupleAttachment attachment = filtered.get(getLayoutPosition());
if (attachment != null)
if (attachment.content == null) {
if (attachment.progress == null)
// Download
executor.submit(new Runnable() {
@Override
public void run() {
DB db = DB.getInstance(context);
attachment.progress = 0;
db.attachment().updateAttachment(attachment);
EntityMessage message = db.message().getMessage(attachment.message);
EntityOperation.queue(context, message, EntityOperation.ATTACHMENT, attachment.sequence);
}
});
} else {
if (attachment.content) {
// Build file name
final File dir = new File(context.getCacheDir(), "attachments");
final File file = new File(dir, TextUtils.isEmpty(attachment.name)
? "attachment_" + attachment.id : attachment.name);
? "attachment_" + attachment.id
: attachment.name.toLowerCase().replaceAll("[^a-zA-Z0-9-.]", "_"));
// https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, "eu.faircode.email", file);
@ -133,10 +122,14 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
dir.mkdir();
file.createNewFile();
// Get attachment content
byte[] content = DB.getInstance(context).attachment().getContent(attachment.id);
// Write attachment content to file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(attachment.content);
fos.write(content);
} finally {
if (fos != null)
fos.close();
@ -150,6 +143,18 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
}
}
});
} else {
if (attachment.progress == null)
// Download
executor.submit(new Runnable() {
@Override
public void run() {
DB db = DB.getInstance(context);
db.attachment().setProgress(attachment.id, 0);
EntityMessage message = db.message().getMessage(attachment.message);
EntityOperation.queue(context, message, EntityOperation.ATTACHMENT, attachment.sequence);
}
});
}
}
}
@ -159,12 +164,12 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
setHasStableIds(true);
}
public void set(List<EntityAttachment> attachments) {
public void set(List<TupleAttachment> attachments) {
Log.i(Helper.TAG, "Set attachments=" + attachments.size());
Collections.sort(attachments, new Comparator<EntityAttachment>() {
Collections.sort(attachments, new Comparator<TupleAttachment>() {
@Override
public int compare(EntityAttachment a1, EntityAttachment a2) {
public int compare(TupleAttachment a1, TupleAttachment a2) {
return a1.sequence.compareTo(a2.sequence);
}
});
@ -202,10 +207,10 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
}
private class MessageDiffCallback extends DiffUtil.Callback {
private List<EntityAttachment> prev;
private List<EntityAttachment> next;
private List<TupleAttachment> prev;
private List<TupleAttachment> next;
MessageDiffCallback(List<EntityAttachment> prev, List<EntityAttachment> next) {
MessageDiffCallback(List<TupleAttachment> prev, List<TupleAttachment> next) {
this.prev = prev;
this.next = next;
}
@ -222,15 +227,15 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
EntityAttachment a1 = prev.get(oldItemPosition);
EntityAttachment a2 = next.get(newItemPosition);
TupleAttachment a1 = prev.get(oldItemPosition);
TupleAttachment a2 = next.get(newItemPosition);
return a1.id.equals(a2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
EntityAttachment a1 = prev.get(oldItemPosition);
EntityAttachment a2 = next.get(newItemPosition);
TupleAttachment a1 = prev.get(oldItemPosition);
TupleAttachment a2 = next.get(newItemPosition);
return a1.equals(a2);
}
}
@ -255,7 +260,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
EntityAttachment attachment = filtered.get(position);
TupleAttachment attachment = filtered.get(position);
holder.tvName.setText(attachment.name);
if (attachment.size != null)
@ -263,19 +268,19 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
holder.tvSize.setVisibility(attachment.size == null ? View.GONE : View.VISIBLE);
if (attachment.progress != null)
holder.tvProgress.setText(String.format("%d %%", attachment.progress));
holder.tvProgress.setVisibility(
attachment.progress == null || attachment.content != null ? View.GONE : View.VISIBLE);
holder.progressbar.setProgress(attachment.progress);
holder.progressbar.setVisibility(
attachment.progress == null || attachment.content ? View.GONE : View.VISIBLE);
if (attachment.content == null) {
if (attachment.content) {
holder.ivStatus.setImageResource(R.drawable.baseline_visibility_24);
holder.ivStatus.setVisibility(View.VISIBLE);
} else {
if (attachment.progress == null) {
holder.ivStatus.setImageResource(R.drawable.baseline_get_app_24);
holder.ivStatus.setVisibility(View.VISIBLE);
} else
holder.ivStatus.setVisibility(View.GONE);
} else {
holder.ivStatus.setImageResource(R.drawable.baseline_visibility_24);
holder.ivStatus.setVisibility(View.VISIBLE);
}
holder.wire();


+ 10
- 2
app/src/main/java/eu/faircode/email/DaoAttachment.java View File

@ -30,12 +30,20 @@ import java.util.List;
@Dao
public interface DaoAttachment {
@Query("SELECT * FROM attachment WHERE message = :message")
LiveData<List<EntityAttachment>> liveAttachments(long message);
@Query("SELECT id,message,sequence,name,type,size,progress" +
", (NOT content IS NULL) as content" +
" FROM attachment WHERE message = :message")
LiveData<List<TupleAttachment>> liveAttachments(long message);
@Query("SELECT * FROM attachment WHERE message = :message AND sequence = :sequence")
EntityAttachment getAttachment(long message, int sequence);
@Query("UPDATE attachment SET progress = :progress WHERE id = :id")
void setProgress(long id, int progress);
@Query("SELECT content FROM attachment WHERE id = :id")
byte[] getContent(long id);
@Insert(onConflict = OnConflictStrategy.REPLACE)
long insertAttachment(EntityAttachment attachment);


+ 3
- 0
app/src/main/java/eu/faircode/email/DaoFolder.java View File

@ -30,6 +30,9 @@ import java.util.List;
@Dao
public interface DaoFolder {
@Query("SELECT * FROM folder WHERE account = :account")
List<EntityFolder> getFolders(long account);
@Query("SELECT * FROM folder WHERE account = :account AND synchronize = :synchronize")
List<EntityFolder> getFolders(long account, boolean synchronize);


+ 2
- 2
app/src/main/java/eu/faircode/email/FragmentMessage.java View File

@ -237,9 +237,9 @@ public class FragmentMessage extends Fragment {
DB.getInstance(getContext()).attachment().liveAttachments(id).removeObservers(FragmentMessage.this);
DB.getInstance(getContext()).attachment().liveAttachments(id).observe(FragmentMessage.this,
new Observer<List<EntityAttachment>>() {
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<EntityAttachment> attachments) {
public void onChanged(@Nullable List<TupleAttachment> attachments) {
adapter.set(attachments);
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
}


+ 0
- 1
app/src/main/java/eu/faircode/email/MessageHelper.java View File

@ -310,7 +310,6 @@ public class MessageHelper {
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || !TextUtils.isEmpty(part.getFileName())) {
ContentType ct = new ContentType(part.getContentType());
EntityAttachment attachment = new EntityAttachment();
attachment.sequence = result.size() + 1;
attachment.name = part.getFileName();
attachment.type = ct.getBaseType();
attachment.size = part.getSize();


+ 6
- 2
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -338,7 +338,7 @@ public class ServiceSynchronize extends LifecycleService {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(ServiceSynchronize.this);
lbm.registerReceiver(processReceiver, new IntentFilter(ACTION_PROCESS_FOLDER));
Log.i(Helper.TAG, "listen process folder");
for (final EntityFolder folder : db.folder().getFolders(account.id, false))
for (final EntityFolder folder : db.folder().getFolders(account.id))
if (!EntityFolder.TYPE_OUTBOX.equals(folder.type))
lbm.sendBroadcast(new Intent(ACTION_PROCESS_FOLDER).putExtra("folder", folder.id));
@ -1002,9 +1002,13 @@ public class ServiceSynchronize extends LifecycleService {
message.id = db.message().insertMessage(message);
Log.i(Helper.TAG, folder.name + " added id=" + message.id);
int sequence = 0;
for (EntityAttachment attachment : helper.getAttachments()) {
Log.i(Helper.TAG, "attachment name=" + attachment.name + " type=" + attachment.type);
sequence++;
Log.i(Helper.TAG, "attachment seq=" + sequence +
" name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
attachment.sequence = sequence;
attachment.id = db.attachment().insertAttachment(attachment);
}


+ 19
- 0
app/src/main/java/eu/faircode/email/TupleAttachment.java View File

@ -0,0 +1,19 @@
package eu.faircode.email;
import android.support.annotation.NonNull;
public class TupleAttachment {
@NonNull
public Long id;
@NonNull
public Long message;
@NonNull
public Integer sequence;
public String name;
@NonNull
public String type;
public Integer size;
public Integer progress;
@NonNull
public boolean content;
}

+ 2
- 1
app/src/main/res/layout/fragment_message.xml View File

@ -153,10 +153,11 @@
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="3dp"
android:maxHeight="90dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="150dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vSeparatorAttachments" />


+ 12
- 14
app/src/main/res/layout/item_attachment.xml View File

@ -36,20 +36,8 @@
android:text="10 kB"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@id/ivAttachments"
app:layout_constraintEnd_toStartOf="@+id/tvProgress"
app:layout_constraintStart_toEndOf="@id/tvName"
app:layout_constraintTop_toTopOf="@id/ivAttachments" />
<TextView
android:id="@+id/tvProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="50 %"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@id/ivAttachments"
app:layout_constraintEnd_toStartOf="@+id/ivStatus"
app:layout_constraintStart_toEndOf="@id/tvSize"
app:layout_constraintStart_toEndOf="@id/tvName"
app:layout_constraintTop_toTopOf="@id/ivAttachments" />
<ImageView
@ -59,6 +47,16 @@
android:layout_marginStart="6dp"
android:src="@drawable/baseline_get_app_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvProgress"
app:layout_constraintStart_toEndOf="@id/tvSize"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressbar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="50"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ivAttachments" />
</android.support.constraint.ConstraintLayout>

Loading…
Cancel
Save