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
1 changed files with 44 additions and 26 deletions

View File

@ -36,16 +36,14 @@ public class PhoneHelper {
+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
+ "\")";
CursorLoader mCursorLoader = new CursorLoader(context,
CursorLoader mCursorLoader = new NotThrowCursorLoader(context,
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
null);
mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {
@Override
public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
if (cursor == null) {
return;
}
if (cursor != null) {
while (cursor.moveToNext()) {
Bundle contact = new Bundle();
contact.putInt("phoneid", cursor.getInt(cursor
@ -65,10 +63,12 @@ public class PhoneHelper {
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
phoneContacts.add(contact);
}
cursor.close();
}
if (listener != null) {
listener.onPhoneContactsLoaded(phoneContacts);
}
cursor.close();
}
});
try {
@ -80,6 +80,24 @@ 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) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {