Show attachment open app

pull/172/head
M66B 5 years ago
parent d06c4c6fc0
commit 68bf30dea0

@ -19,9 +19,13 @@ package eu.faircode.email;
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -32,6 +36,7 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
@ -43,6 +48,7 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -63,6 +69,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private View view;
private ImageButton ibDelete;
private ImageView ivType;
private TextView tvName;
private TextView tvSize;
private ImageView ivStatus;
@ -76,6 +83,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
view = itemView.findViewById(R.id.clItem);
ibDelete = itemView.findViewById(R.id.ibDelete);
ivType = itemView.findViewById(R.id.ivType);
tvName = itemView.findViewById(R.id.tvName);
tvSize = itemView.findViewById(R.id.tvSize);
ivStatus = itemView.findViewById(R.id.ivStatus);
@ -101,6 +109,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
view.setAlpha(attachment.isInline() && attachment.isImage() ? Helper.LOW_LIGHT : 1.0f);
ibDelete.setVisibility(readonly ? View.GONE : attachment.isInline() ? View.INVISIBLE : View.VISIBLE);
ivType.setImageDrawable(null);
tvName.setText(attachment.name);
if (attachment.size != null)
@ -139,6 +148,55 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
tvError.setText(attachment.error);
tvError.setVisibility(attachment.error == null ? View.GONE : View.VISIBLE);
Bundle args = new Bundle();
args.putLong("id", attachment.id);
args.putSerializable("file", attachment.getFile(context));
args.putString("type", attachment.getMimeType());
new SimpleTask<Drawable>() {
@Override
protected Drawable onExecute(Context context, Bundle args) throws Throwable {
File file = (File) args.getSerializable("file");
String type = args.getString("type");
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndTypeAndNormalize(uri, type);
PackageManager pm = context.getPackageManager();
ComponentName component = intent.resolveActivity(pm);
if (component == null)
return null;
return pm.getApplicationIcon(component.getPackageName());
}
@Override
protected void onExecuted(Bundle args, Drawable icon) {
long id = args.getLong("id");
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
EntityAttachment attachment = items.get(pos);
if (attachment == null || !attachment.id.equals(id))
return;
if (icon == null)
ivType.setImageResource(R.drawable.baseline_attachment_24);
else
ivType.setImageDrawable(icon);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "attachment:icon");
}
@Override

@ -27,10 +27,9 @@
app:srcCompat="@drawable/baseline_delete_24" />
<ImageView
android:id="@+id/ivAttachments"
android:id="@+id/ivType"
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@string/title_legend_attachment"
app:layout_constraintBottom_toBottomOf="@+id/tvType"
app:layout_constraintStart_toEndOf="@id/ibDelete"
app:layout_constraintTop_toTopOf="parent"
@ -48,7 +47,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toStartOf="@+id/tvSize"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toEndOf="@id/ivAttachments"
app:layout_constraintStart_toEndOf="@id/ivType"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
@ -108,7 +107,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="?attr/colorWarning"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/ivAttachments"
app:layout_constraintStart_toEndOf="@id/ivType"
app:layout_constraintTop_toBottomOf="@id/tvType" />
<ProgressBar

Loading…
Cancel
Save