diff --git a/app/src/debug/res/xml/shortcuts.xml b/app/src/debug/res/xml/shortcuts.xml
index 8a3fe854d9..5d10043733 100644
--- a/app/src/debug/res/xml/shortcuts.xml
+++ b/app/src/debug/res/xml/shortcuts.xml
@@ -34,4 +34,9 @@
android:targetPackage="eu.faircode.email.debug" />
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/eu/faircode/email/ActivityCompose.java b/app/src/main/java/eu/faircode/email/ActivityCompose.java
index 94ef1db320..a320a5f632 100644
--- a/app/src/main/java/eu/faircode/email/ActivityCompose.java
+++ b/app/src/main/java/eu/faircode/email/ActivityCompose.java
@@ -97,6 +97,17 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
args.putString("body", body);
}
+ if (intent.hasExtra(Intent.EXTRA_SHORTCUT_ID)) {
+ String to = intent.getStringExtra(Intent.EXTRA_SHORTCUT_ID);
+ if (to != null)
+ try {
+ InternetAddress.parse(to);
+ args.putString("to", to);
+ } catch (AddressException ex) {
+ Log.w(ex);
+ }
+ }
+
if (intent.hasExtra(Intent.EXTRA_EMAIL)) {
String[] to = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
if (to != null)
diff --git a/app/src/main/java/eu/faircode/email/DaoContact.java b/app/src/main/java/eu/faircode/email/DaoContact.java
index 72db9b1b7e..67521fa8fb 100644
--- a/app/src/main/java/eu/faircode/email/DaoContact.java
+++ b/app/src/main/java/eu/faircode/email/DaoContact.java
@@ -41,15 +41,14 @@ public interface DaoContact {
" ORDER BY times_contacted DESC, last_contacted DESC")
LiveData> liveContacts();
- @Query("SELECT * FROM contact" +
+ @Query("SELECT email, name, avatar FROM contact" +
" WHERE state <> " + EntityContact.STATE_IGNORE +
" ORDER BY" +
" CASE WHEN state = " + EntityContact.STATE_FAVORITE + " THEN 0 ELSE 1 END" +
", CASE WHEN avatar IS NULL THEN 1 ELSE 0 END" +
", times_contacted DESC" +
- ", last_contacted DESC" +
- " LIMIT :count")
- List getFrequentlyContacted(int count);
+ ", last_contacted DESC")
+ Cursor getFrequentlyContacted();
@Query("SELECT *" +
" FROM contact" +
diff --git a/app/src/main/java/eu/faircode/email/Shortcuts.java b/app/src/main/java/eu/faircode/email/Shortcuts.java
index 3070d248a4..a9f346bddd 100644
--- a/app/src/main/java/eu/faircode/email/Shortcuts.java
+++ b/app/src/main/java/eu/faircode/email/Shortcuts.java
@@ -24,13 +24,13 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutManager;
+import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
-import android.text.TextUtils;
import android.widget.Toast;
import androidx.core.app.Person;
@@ -41,7 +41,10 @@ import androidx.lifecycle.LifecycleOwner;
import java.io.InputStream;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
public class Shortcuts {
static void update(final Context context, final LifecycleOwner owner) {
@@ -58,23 +61,34 @@ public class Shortcuts {
int count = app - manifest;
Log.i("Shortcuts count=" + count + " app=" + app + " manifest=" + manifest);
+ DB db = DB.getInstance(context);
+ List emails = new ArrayList<>();
List shortcuts = new ArrayList<>();
- if (count > 0) {
- DB db = DB.getInstance(context);
- List frequently = db.contact().getFrequentlyContacted(count);
- for (EntityContact contact : frequently) {
+ try (Cursor cursor = db.contact().getFrequentlyContacted()) {
+ int colEmail = cursor.getColumnIndex("email");
+ int colName = cursor.getColumnIndex("name");
+ int colAvatar = cursor.getColumnIndex("avatar");
+ while (shortcuts.size() < count && cursor.moveToNext()) {
+ String email = cursor.getString(colEmail);
+ String name = (cursor.isNull(colName) ? null : cursor.getString(colName));
+ String avatar = (cursor.isNull(colAvatar) ? null : cursor.getString(colAvatar));
+
+ if (emails.contains(email))
+ continue;
+ emails.add(email);
+
Intent intent = new Intent(context, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setAction(Intent.ACTION_SEND);
intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setData(Uri.parse("mailto:" + contact.email));
+ intent.setData(Uri.parse("mailto:" + email));
IconCompat icon = null;
- if (contact.avatar != null &&
+ if (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));
+ context.getContentResolver(), Uri.parse(avatar));
Bitmap bitmap = BitmapFactory.decodeStream(is);
if (bitmap != null)
icon = IconCompat.createWithBitmap(bitmap);
@@ -82,25 +96,24 @@ public class Shortcuts {
if (icon == null)
icon = IconCompat.createWithResource(context, R.drawable.ic_shortcut_email);
- String name = (TextUtils.isEmpty(contact.name) ? contact.email : contact.name);
-
- //Set categories = new HashSet<>(Arrays.asList(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION));
+ Set categories = new HashSet<>(Arrays.asList("eu.faircode.email.TEXT_SHARE_TARGET"));
- ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(context, Long.toString(contact.id))
+ String id = (name == null ? email : "\"" + name + "\" <" + email + ">");
+ ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(context, id)
.setIcon(icon)
.setRank(shortcuts.size() + 1)
- .setShortLabel(name)
- .setLongLabel(name)
- //.setCategories(categories)
+ .setShortLabel(name == null ? email : name)
+ .setLongLabel(name == null ? email : name)
+ .setCategories(categories)
.setIntent(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Person.Builder person = new Person.Builder()
.setIcon(icon)
- .setName(name)
+ .setName(name == null ? email : name)
.setImportant(true);
- if (contact.avatar != null)
- person.setUri(contact.avatar);
+ if (avatar != null)
+ person.setUri(avatar);
builder.setPerson(person.build());
}
@@ -114,6 +127,7 @@ public class Shortcuts {
@Override
@TargetApi(Build.VERSION_CODES.N_MR1)
protected void onExecuted(Bundle args, List shortcuts) {
+ ShortcutManagerCompat.removeAllDynamicShortcuts(context);
ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts);
}
diff --git a/app/src/main/res/xml/shortcuts.xml b/app/src/main/res/xml/shortcuts.xml
index 5938db0366..5a047042ad 100644
--- a/app/src/main/res/xml/shortcuts.xml
+++ b/app/src/main/res/xml/shortcuts.xml
@@ -34,4 +34,9 @@
android:targetPackage="eu.faircode.email" />
+
+
+
+
+
\ No newline at end of file