|
|
|
@ -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);
|
|
|
|
|
|
|
|
|
|
List<ShortcutInfoCompat> shortcuts = new ArrayList<>();
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
List<EntityContact> frequently = db.contact().getFrequentlyContacted(count);
|
|
|
|
|
for (EntityContact contact : frequently) {
|
|
|
|
|
List<String> emails = new ArrayList<>();
|
|
|
|
|
List<ShortcutInfoCompat> shortcuts = new ArrayList<>();
|
|
|
|
|
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<String> categories = new HashSet<>(Arrays.asList(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION));
|
|
|
|
|
Set<String> 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<ShortcutInfoCompat> shortcuts) {
|
|
|
|
|
ShortcutManagerCompat.removeAllDynamicShortcuts(context);
|
|
|
|
|
ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|