catch security exception when user prevents access to address book

This commit is contained in:
Daniel Gultsch 2016-01-20 16:18:15 +01:00
parent 0619685e55
commit e71acdef29

View file

@ -18,17 +18,17 @@ import java.util.concurrent.RejectedExecutionException;
public class PhoneHelper { public class PhoneHelper {
public static void loadPhoneContacts(Context context,final List<Bundle> phoneContacts, final OnPhoneContactsLoadedListener listener) { public static void loadPhoneContacts(Context context, final List<Bundle> phoneContacts, final OnPhoneContactsLoadedListener listener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
listener.onPhoneContactsLoaded(phoneContacts); listener.onPhoneContactsLoaded(phoneContacts);
return; return;
} }
final String[] PROJECTION = new String[] { ContactsContract.Data._ID, final String[] PROJECTION = new String[]{ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_URI, ContactsContract.Data.PHOTO_URI,
ContactsContract.Data.LOOKUP_KEY, ContactsContract.Data.LOOKUP_KEY,
ContactsContract.CommonDataKinds.Im.DATA }; ContactsContract.CommonDataKinds.Im.DATA};
final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\"" final String SELECTION = "(" + ContactsContract.Data.MIMETYPE + "=\""
+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
@ -36,39 +36,39 @@ public class PhoneHelper {
+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER + "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
+ "\")"; + "\")";
CursorLoader mCursorLoader = new CursorLoader(context, CursorLoader mCursorLoader = new NotThrowCursorLoader(context,
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
null); null);
mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() { mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {
@Override @Override
public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) { public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
if (cursor == null) { if (cursor != null) {
return; while (cursor.moveToNext()) {
} Bundle contact = new Bundle();
while (cursor.moveToNext()) { contact.putInt("phoneid", cursor.getInt(cursor
Bundle contact = new Bundle(); .getColumnIndex(ContactsContract.Data._ID)));
contact.putInt("phoneid", cursor.getInt(cursor contact.putString(
.getColumnIndex(ContactsContract.Data._ID))); "displayname",
contact.putString( cursor.getString(cursor
"displayname", .getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
cursor.getString(cursor contact.putString("photouri", cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))); .getColumnIndex(ContactsContract.Data.PHOTO_URI)));
contact.putString("photouri", cursor.getString(cursor contact.putString("lookup", cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.PHOTO_URI))); .getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));
contact.putString("lookup", cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.LOOKUP_KEY)));
contact.putString( contact.putString(
"jid", "jid",
cursor.getString(cursor cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA))); .getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
phoneContacts.add(contact); phoneContacts.add(contact);
}
cursor.close();
} }
if (listener != null) { if (listener != null) {
listener.onPhoneContactsLoaded(phoneContacts); listener.onPhoneContactsLoaded(phoneContacts);
} }
cursor.close();
} }
}); });
try { try {
@ -80,12 +80,30 @@ public class PhoneHelper {
} }
} }
private static class NotThrowCursorLoader extends CursorLoader {
public NotThrowCursorLoader(Context c, Uri u, String[] p, String s, String[] sa, String so) {
super(c, u, p, s, sa, so);
}
@Override
public Cursor loadInBackground() {
try {
return (super.loadInBackground());
} catch (SecurityException e) {
return(null);
}
}
}
public static Uri getSefliUri(Context context) { public static Uri getSefliUri(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
return null; return null;
} }
String[] mProjection = new String[] { Profile._ID, Profile.PHOTO_URI }; String[] mProjection = new String[]{Profile._ID, Profile.PHOTO_URI};
Cursor mProfileCursor = context.getContentResolver().query( Cursor mProfileCursor = context.getContentResolver().query(
Profile.CONTENT_URI, mProjection, null, null, null); Profile.CONTENT_URI, mProjection, null, null, null);