Check for contacts permission

pull/147/head
M66B 6 years ago
parent 5a49e9a32a
commit d0de9d5ee7

@ -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 if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
cursor = getContentResolver().query( == PackageManager.PERMISSION_GRANTED) {
ContactsContract.CommonDataKinds.Email.CONTENT_URI, Cursor cursor = null;
new String[]{ try {
ContactsContract.RawContacts._ID, // https://developer.android.com/guide/topics/providers/contacts-provider#ObsoleteData
ContactsContract.Contacts.LOOKUP_KEY, cursor = getContentResolver().query(
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Email.CONTENT_URI,
ContactsContract.CommonDataKinds.Email.DATA, new String[]{
ContactsContract.Contacts.STARRED, ContactsContract.RawContacts._ID,
ContactsContract.Contacts.TIMES_CONTACTED, ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.LAST_TIME_CONTACTED ContactsContract.Contacts.DISPLAY_NAME,
}, ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.DATA + " <> ''", ContactsContract.Contacts.STARRED,
null, ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.STARRED + " DESC" + ContactsContract.Contacts.LAST_TIME_CONTACTED
", " + ContactsContract.Contacts.TIMES_CONTACTED + " DESC" + },
", " + ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC"); ContactsContract.CommonDataKinds.Email.DATA + " <> ''",
while (cursor.moveToNext()) null,
try { ContactsContract.Contacts.STARRED + " DESC" +
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID)); ", " + ContactsContract.Contacts.TIMES_CONTACTED + " DESC" +
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); ", " + ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC");
String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); while (cursor.moveToNext())
int starred = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.STARRED)); try {
int times = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED)); long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
long last = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
InternetAddress address = new InternetAddress(email, name); int starred = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.STARRED));
Log.i(Helper.TAG, "Shortcut id=" + id + " address=" + address + int times = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
" starred=" + starred + " times=" + times + " last=" + last); 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(
cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID)), if (starred == 0 && times == 0 && last == 0)
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY))); continue;
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
getContentResolver(), uri); Uri uri = ContactsContract.Contacts.getLookupUri(
Bitmap bitmap = BitmapFactory.decodeStream(is); cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts._ID)),
Icon icon = (bitmap == null cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
? Icon.createWithResource(this, R.drawable.ic_shortcut_email) InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(
: Icon.createWithBitmap(bitmap)); getContentResolver(), uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
Intent intent = new Intent(this, ActivityCompose.class); Icon icon = (bitmap == null
intent.setAction(Intent.ACTION_SEND); ? Icon.createWithResource(this, R.drawable.ic_shortcut_email)
intent.setData(Uri.parse("mailto:" + address)); : 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) shortcuts.add(
.setIntent(intent) new ShortcutInfo.Builder(this, Long.toString(id))
.build()); .setIcon(icon)
.setRank(shortcuts.size() + 1)
if (sm.getManifestShortcuts().size() + shortcuts.size() >= sm.getMaxShortcutCountPerActivity()) .setShortLabel(name)
break; .setIntent(intent)
} catch (Throwable ex) { .build());
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} if (sm.getManifestShortcuts().size() + shortcuts.size() >= sm.getMaxShortcutCountPerActivity())
} finally { break;
if (cursor != null) } catch (Throwable ex) {
cursor.close(); Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
} finally {
if (cursor != null)
cursor.close();
}
} }
sm.setDynamicShortcuts(shortcuts); sm.setDynamicShortcuts(shortcuts);

@ -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)

Loading…
Cancel
Save