From 68bf30dea00a70e642c4506b01fa9e349376a6cc Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 1 Feb 2020 20:33:24 +0100 Subject: [PATCH] Show attachment open app --- .../eu/faircode/email/AdapterAttachment.java | 58 +++++++++++++++++++ app/src/main/res/layout/item_attachment.xml | 7 +-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterAttachment.java b/app/src/main/java/eu/faircode/email/AdapterAttachment.java index 3b19e1678a..e4d8897991 100644 --- a/app/src/main/java/eu/faircode/email/AdapterAttachment.java +++ b/app/src/main/java/eu/faircode/email/AdapterAttachment.java @@ -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() { + @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 diff --git a/app/src/main/res/layout/item_attachment.xml b/app/src/main/res/layout/item_attachment.xml index 3be0eb44e8..0cf1c452f3 100644 --- a/app/src/main/res/layout/item_attachment.xml +++ b/app/src/main/res/layout/item_attachment.xml @@ -27,10 +27,9 @@ app:srcCompat="@drawable/baseline_delete_24" />