Check for contacts permission

This commit is contained in:
M66B 2018-12-17 19:56:18 +01:00
parent 5a49e9a32a
commit d0de9d5ee7
2 changed files with 96 additions and 85 deletions

View File

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B) Copyright 2018 by Marcel Bokhorst (M66B)
*/ */
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -28,6 +29,7 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.IntentSender; import android.content.IntentSender;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -101,6 +103,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
@ -718,72 +721,76 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
ShortcutManager sm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE); ShortcutManager sm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
Cursor cursor = null;
List<ShortcutInfo> shortcuts = new ArrayList<>(); List<ShortcutInfo> shortcuts = new ArrayList<>();
try {
// https://developer.android.com/guide/topics/providers/contacts-provider#ObsoleteData
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.RawContacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.LAST_TIME_CONTACTED
},
ContactsContract.CommonDataKinds.Email.DATA + " <> ''",
null,
ContactsContract.Contacts.STARRED + " DESC" +
", " + ContactsContract.Contacts.TIMES_CONTACTED + " DESC" +
", " + ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC");
while (cursor.moveToNext())
try {
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int starred = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.STARRED));
int times = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
long last = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED));
InternetAddress address = new InternetAddress(email, name); if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
Log.i(Helper.TAG, "Shortcut id=" + id + " address=" + address + == PackageManager.PERMISSION_GRANTED) {
" starred=" + starred + " times=" + times + " last=" + last); Cursor cursor = null;
try {
// https://developer.android.com/guide/topics/providers/contacts-provider#ObsoleteData
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.RawContacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.LAST_TIME_CONTACTED
},
ContactsContract.CommonDataKinds.Email.DATA + " <> ''",
null,
ContactsContract.Contacts.STARRED + " DESC" +
", " + ContactsContract.Contacts.TIMES_CONTACTED + " DESC" +
", " + ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC");
while (cursor.moveToNext())
try {
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int starred = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.STARRED));
int times = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
long last = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED));
if (starred == 0 && times == 0 && last == 0) InternetAddress address = new InternetAddress(email, name);
continue; Log.i(Helper.TAG, "Shortcut id=" + id + " address=" + address +
" starred=" + starred + " times=" + times + " last=" + last);
Uri uri = ContactsContract.Contacts.getLookupUri( if (starred == 0 && times == 0 && last == 0)
cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID)), continue;
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
getContentResolver(), uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
Icon icon = (bitmap == null
? Icon.createWithResource(this, R.drawable.ic_shortcut_email)
: Icon.createWithBitmap(bitmap));
Intent intent = new Intent(this, ActivityCompose.class); Uri uri = ContactsContract.Contacts.getLookupUri(
intent.setAction(Intent.ACTION_SEND); cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID)),
intent.setData(Uri.parse("mailto:" + address)); cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
getContentResolver(), uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
Icon icon = (bitmap == null
? Icon.createWithResource(this, R.drawable.ic_shortcut_email)
: Icon.createWithBitmap(bitmap));
shortcuts.add( Intent intent = new Intent(this, ActivityCompose.class);
new ShortcutInfo.Builder(this, Long.toString(id)) intent.setAction(Intent.ACTION_SEND);
.setIcon(icon) intent.setData(Uri.parse("mailto:" + address));
.setRank(shortcuts.size() + 1)
.setShortLabel(name)
.setIntent(intent)
.build());
if (sm.getManifestShortcuts().size() + shortcuts.size() >= sm.getMaxShortcutCountPerActivity()) shortcuts.add(
break; new ShortcutInfo.Builder(this, Long.toString(id))
} catch (Throwable ex) { .setIcon(icon)
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); .setRank(shortcuts.size() + 1)
} .setShortLabel(name)
} finally { .setIntent(intent)
if (cursor != null) .build());
cursor.close();
if (sm.getManifestShortcuts().size() + shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
break;
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
} finally {
if (cursor != null)
cursor.close();
}
} }
sm.setDynamicShortcuts(shortcuts); sm.setDynamicShortcuts(shortcuts);

View File

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B) Copyright 2018 by Marcel Bokhorst (M66B)
*/ */
import android.Manifest;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -636,34 +637,37 @@ public class ServiceSynchronize extends LifecycleService {
} }
if (!TextUtils.isEmpty(message.avatar)) { if (!TextUtils.isEmpty(message.avatar)) {
Cursor cursor = null; if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
try { == PackageManager.PERMISSION_GRANTED) {
cursor = getContentResolver().query( Cursor cursor = null;
Uri.parse(message.avatar), try {
new String[]{ cursor = getContentResolver().query(
ContactsContract.Contacts._ID, Uri.parse(message.avatar),
ContactsContract.Contacts.LOOKUP_KEY new String[]{
}, ContactsContract.Contacts._ID,
null, null, null); ContactsContract.Contacts.LOOKUP_KEY
if (cursor.moveToNext()) { },
if (true || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { null, null, null);
Uri uri = ContactsContract.Contacts.getLookupUri( if (cursor.moveToNext()) {
cursor.getLong(0), cursor.getString(1)); if (true || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream( Uri uri = ContactsContract.Contacts.getLookupUri(
getContentResolver(), uri); cursor.getLong(0), cursor.getString(1));
mbuilder.setLargeIcon(BitmapFactory.decodeStream(is)); InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
} else { getContentResolver(), uri);
Uri photo = Uri.withAppendedPath( mbuilder.setLargeIcon(BitmapFactory.decodeStream(is));
ContactsContract.Contacts.CONTENT_URI, } else {
cursor.getLong(0) + "/photo"); Uri photo = Uri.withAppendedPath(
mbuilder.setLargeIcon(Icon.createWithContentUri(photo)); ContactsContract.Contacts.CONTENT_URI,
cursor.getLong(0) + "/photo");
mbuilder.setLargeIcon(Icon.createWithContentUri(photo));
}
} }
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
if (cursor != null)
cursor.close();
} }
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
if (cursor != null)
cursor.close();
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)