diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 09b65300aa..19361f2895 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -19,8 +19,6 @@ package eu.faircode.email; Copyright 2018-2019 by Marcel Bokhorst (M66B) */ -import android.Manifest; -import android.annotation.TargetApi; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; @@ -30,19 +28,12 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.IntentSender; import android.content.SharedPreferences; -import android.content.pm.ShortcutInfo; -import android.content.pm.ShortcutManager; import android.content.res.Configuration; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.drawable.Icon; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.ParcelFileDescriptor; import android.preference.PreferenceManager; -import android.provider.ContactsContract; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.MenuItem; @@ -404,7 +395,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB pgpService = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain"); pgpService.bindToService(); - updateShortcuts(); + Shortcuts.update(this, this); } private void init() { @@ -782,69 +773,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB }.execute(this, args, "update:check"); } - private void updateShortcuts() { - if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) - return; - - new SimpleTask>() { - @Override - @TargetApi(Build.VERSION_CODES.N_MR1) - protected List onExecute(Context context, Bundle args) { - ShortcutManager sm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE); - int count = sm.getMaxShortcutCountPerActivity() - sm.getManifestShortcuts().size(); - Log.i("Shortcuts count=" + count + - " app=" + sm.getMaxShortcutCountPerActivity() + - " manifest=" + sm.getManifestShortcuts().size()); - - List shortcuts = new ArrayList<>(); - if (count > 0) { - DB db = DB.getInstance(context); - List frequently = db.contact().getFrequentlyContacted(count); - for (EntityContact contact : frequently) { - Intent intent = new Intent(context, ActivityCompose.class); - intent.setAction(Intent.ACTION_SEND); - intent.setData(Uri.parse("mailto:" + contact.email)); - - Icon icon = null; - if (contact.avatar != null && - Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) { - // Create icon from bitmap because launcher might not have contacts permission - InputStream is = ContactsContract.Contacts.openContactPhotoInputStream( - getContentResolver(), Uri.parse(contact.avatar)); - Bitmap bitmap = BitmapFactory.decodeStream(is); - if (bitmap != null) - icon = Icon.createWithBitmap(bitmap); - } - if (icon == null) - icon = Icon.createWithResource(context, R.drawable.ic_shortcut_email); - - shortcuts.add( - new ShortcutInfo.Builder(context, Long.toString(contact.id)) - .setIcon(icon) - .setRank(shortcuts.size() + 1) - .setShortLabel(contact.name == null ? contact.email : contact.name) - .setIntent(intent) - .build()); - } - } - - return shortcuts; - } - - @Override - @TargetApi(Build.VERSION_CODES.N_MR1) - protected void onExecuted(Bundle args, List shortcuts) { - ShortcutManager sm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE); - sm.setDynamicShortcuts(shortcuts); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(ActivityView.this, ActivityView.this, ex); - } - }.execute(this, this, new Bundle(), "shortcuts:get"); - } - private Intent getIntentInvite() { StringBuilder sb = new StringBuilder(); sb.append(getString(R.string.title_try)).append("\n\n"); diff --git a/app/src/main/java/eu/faircode/email/FragmentContacts.java b/app/src/main/java/eu/faircode/email/FragmentContacts.java index 7f9f17da10..206c5bee8b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentContacts.java +++ b/app/src/main/java/eu/faircode/email/FragmentContacts.java @@ -93,6 +93,8 @@ public class FragmentContacts extends FragmentBase { grpReady.setVisibility(View.VISIBLE); } }); + + Shortcuts.update(getContext(), getViewLifecycleOwner()); } @Override diff --git a/app/src/main/java/eu/faircode/email/Shortcuts.java b/app/src/main/java/eu/faircode/email/Shortcuts.java new file mode 100644 index 0000000000..6bc79e69c4 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/Shortcuts.java @@ -0,0 +1,86 @@ +package eu.faircode.email; + +import android.Manifest; +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ShortcutInfo; +import android.content.pm.ShortcutManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.drawable.Icon; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.provider.ContactsContract; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import androidx.lifecycle.LifecycleOwner; + +public class Shortcuts { + static void update(final Context context, final LifecycleOwner owner) { + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) + return; + + new SimpleTask>() { + @Override + @TargetApi(Build.VERSION_CODES.N_MR1) + protected List onExecute(Context context, Bundle args) { + ShortcutManager sm = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); + int count = sm.getMaxShortcutCountPerActivity() - sm.getManifestShortcuts().size(); + Log.i("Shortcuts count=" + count + + " app=" + sm.getMaxShortcutCountPerActivity() + + " manifest=" + sm.getManifestShortcuts().size()); + + List shortcuts = new ArrayList<>(); + if (count > 0) { + DB db = DB.getInstance(context); + List frequently = db.contact().getFrequentlyContacted(count); + for (EntityContact contact : frequently) { + Intent intent = new Intent(context, ActivityCompose.class); + intent.setAction(Intent.ACTION_SEND); + intent.setData(Uri.parse("mailto:" + contact.email)); + + Icon icon = null; + if (contact.avatar != null && + Helper.hasPermission(context, Manifest.permission.READ_CONTACTS)) { + // Create icon from bitmap because launcher might not have contacts permission + InputStream is = ContactsContract.Contacts.openContactPhotoInputStream( + context.getContentResolver(), Uri.parse(contact.avatar)); + Bitmap bitmap = BitmapFactory.decodeStream(is); + if (bitmap != null) + icon = Icon.createWithBitmap(bitmap); + } + if (icon == null) + icon = Icon.createWithResource(context, R.drawable.ic_shortcut_email); + + shortcuts.add( + new ShortcutInfo.Builder(context, Long.toString(contact.id)) + .setIcon(icon) + .setRank(shortcuts.size() + 1) + .setShortLabel(contact.name == null ? contact.email : contact.name) + .setIntent(intent) + .build()); + } + } + + return shortcuts; + } + + @Override + @TargetApi(Build.VERSION_CODES.N_MR1) + protected void onExecuted(Bundle args, List shortcuts) { + ShortcutManager sm = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); + sm.setDynamicShortcuts(shortcuts); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(context, owner, ex); + } + }.execute(context, owner, new Bundle(), "shortcuts:update"); + } +}