mirror of
https://github.com/M66B/FairEmail.git
synced 2025-01-01 04:35:57 +00:00
Get shortcuts on the background
This commit is contained in:
parent
e5d4a42c69
commit
013cb9e965
1 changed files with 80 additions and 59 deletions
|
@ -20,6 +20,7 @@ package eu.faircode.email;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
|
@ -37,6 +38,7 @@ import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.drawable.Icon;
|
import android.graphics.drawable.Icon;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
|
@ -781,74 +783,93 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
|
||||||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1)
|
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ShortcutManager sm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
|
new SimpleTask<List<ShortcutInfo>>() {
|
||||||
|
@Override
|
||||||
|
@TargetApi(Build.VERSION_CODES.N_MR1)
|
||||||
|
protected List<ShortcutInfo> 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<ShortcutInfo> shortcuts = new ArrayList<>();
|
List<ShortcutInfo> shortcuts = new ArrayList<>();
|
||||||
|
|
||||||
if (hasPermission(Manifest.permission.READ_CONTACTS)) {
|
if (hasPermission(Manifest.permission.READ_CONTACTS)) {
|
||||||
// https://developer.android.com/guide/topics/providers/contacts-provider#ObsoleteData
|
// https://developer.android.com/guide/topics/providers/contacts-provider#ObsoleteData
|
||||||
try (Cursor cursor = getContentResolver().query(
|
try (Cursor cursor = getContentResolver().query(
|
||||||
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
||||||
new String[]{
|
new String[]{
|
||||||
ContactsContract.RawContacts._ID,
|
ContactsContract.RawContacts._ID,
|
||||||
ContactsContract.Contacts.LOOKUP_KEY,
|
ContactsContract.Contacts.LOOKUP_KEY,
|
||||||
ContactsContract.Contacts.DISPLAY_NAME,
|
ContactsContract.Contacts.DISPLAY_NAME,
|
||||||
ContactsContract.CommonDataKinds.Email.DATA,
|
ContactsContract.CommonDataKinds.Email.DATA,
|
||||||
ContactsContract.Contacts.STARRED,
|
ContactsContract.Contacts.STARRED,
|
||||||
ContactsContract.Contacts.TIMES_CONTACTED,
|
ContactsContract.Contacts.TIMES_CONTACTED,
|
||||||
ContactsContract.Contacts.LAST_TIME_CONTACTED
|
ContactsContract.Contacts.LAST_TIME_CONTACTED
|
||||||
},
|
},
|
||||||
ContactsContract.CommonDataKinds.Email.DATA + " <> ''",
|
ContactsContract.CommonDataKinds.Email.DATA + " <> ''",
|
||||||
null,
|
null,
|
||||||
ContactsContract.Contacts.STARRED + " DESC" +
|
ContactsContract.Contacts.STARRED + " DESC" +
|
||||||
", " + ContactsContract.Contacts.TIMES_CONTACTED + " DESC" +
|
", " + ContactsContract.Contacts.TIMES_CONTACTED + " DESC" +
|
||||||
", " + ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC")) {
|
", " + ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC")) {
|
||||||
while (cursor != null && cursor.moveToNext())
|
while (cursor != null && cursor.moveToNext() && shortcuts.size() < count)
|
||||||
try {
|
try {
|
||||||
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
|
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
|
||||||
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||||
String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
|
String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
|
||||||
int starred = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.STARRED));
|
int starred = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.STARRED));
|
||||||
int times = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
|
int times = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
|
||||||
long last = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED));
|
long last = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED));
|
||||||
|
|
||||||
Log.i("Shortcut id=" + id + " email=" + email +
|
Log.i("Shortcut id=" + id + " email=" + email +
|
||||||
" starred=" + starred + " times=" + times + " last=" + last);
|
" starred=" + starred + " times=" + times + " last=" + last);
|
||||||
|
|
||||||
if (starred == 0 && times == 0 && last == 0)
|
if (starred == 0 && times == 0 && last == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Uri uri = ContactsContract.Contacts.getLookupUri(
|
Uri uri = ContactsContract.Contacts.getLookupUri(
|
||||||
cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID)),
|
cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID)),
|
||||||
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
|
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
|
||||||
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
|
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
|
||||||
getContentResolver(), uri);
|
getContentResolver(), uri);
|
||||||
Bitmap bitmap = BitmapFactory.decodeStream(is);
|
Bitmap bitmap = BitmapFactory.decodeStream(is);
|
||||||
Icon icon = (bitmap == null
|
Icon icon = (bitmap == null
|
||||||
? Icon.createWithResource(this, R.drawable.ic_shortcut_email)
|
? Icon.createWithResource(context, R.drawable.ic_shortcut_email)
|
||||||
: Icon.createWithBitmap(bitmap));
|
: Icon.createWithBitmap(bitmap));
|
||||||
|
|
||||||
Intent intent = new Intent(this, ActivityCompose.class);
|
Intent intent = new Intent(context, ActivityCompose.class);
|
||||||
intent.setAction(Intent.ACTION_SEND);
|
intent.setAction(Intent.ACTION_SEND);
|
||||||
intent.setData(Uri.parse("mailto:" + email));
|
intent.setData(Uri.parse("mailto:" + email));
|
||||||
|
|
||||||
shortcuts.add(
|
shortcuts.add(
|
||||||
new ShortcutInfo.Builder(this, Long.toString(id))
|
new ShortcutInfo.Builder(context, Long.toString(id))
|
||||||
.setIcon(icon)
|
.setIcon(icon)
|
||||||
.setRank(shortcuts.size() + 1)
|
.setRank(shortcuts.size() + 1)
|
||||||
.setShortLabel(name)
|
.setShortLabel(name)
|
||||||
.setIntent(intent)
|
.setIntent(intent)
|
||||||
.build());
|
.build());
|
||||||
|
} catch (Throwable ex) {
|
||||||
if (sm.getManifestShortcuts().size() + shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
|
Log.e(ex);
|
||||||
break;
|
}
|
||||||
} catch (Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
sm.setDynamicShortcuts(shortcuts);
|
return shortcuts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@TargetApi(Build.VERSION_CODES.N_MR1)
|
||||||
|
protected void onExecuted(Bundle args, List<ShortcutInfo> 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() {
|
private Intent getIntentInvite() {
|
||||||
|
|
Loading…
Reference in a new issue