refactore phone helper to ignore security exception

This commit is contained in:
Daniel Gultsch 2017-11-17 10:28:51 +01:00
parent a337be982f
commit 5e15c48206
2 changed files with 32 additions and 36 deletions

View file

@ -141,7 +141,7 @@ public class PublishProfilePictureActivity extends XmppActivity {
} }
}); });
this.defaultUri = PhoneHelper.getSelfiUri(getApplicationContext()); this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext());
} }
private void chooseAvatar(boolean crop) { private void chooseAvatar(boolean crop) {

View file

@ -82,45 +82,23 @@ public class PhoneHelper {
} }
} }
private static class NotThrowCursorLoader extends CursorLoader { public static Uri getProfilePictureUri(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
public NotThrowCursorLoader(Context c, Uri u, String[] p, String s, String[] sa, String so) { return null;
super(c, u, p, s, sa, so);
} }
final String[] projection = new String[]{Profile._ID, Profile.PHOTO_URI};
@Override final Cursor cursor;
public Cursor loadInBackground() {
try { try {
return (super.loadInBackground()); cursor = context.getContentResolver().query(Profile.CONTENT_URI, projection, null, null, null);
} catch (Throwable e) { } catch (SecurityException e) {
return(null);
}
}
}
public static Uri getSelfiUri(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
return null; return null;
} }
String[] mProjection = new String[]{Profile._ID, Profile.PHOTO_URI}; if (cursor == null) {
Cursor mProfileCursor = context.getContentResolver().query(
Profile.CONTENT_URI, mProjection, null, null, null);
if (mProfileCursor == null || mProfileCursor.getCount() == 0) {
return null; return null;
} else {
mProfileCursor.moveToFirst();
String uri = mProfileCursor.getString(1);
mProfileCursor.close();
if (uri == null) {
return null;
} else {
return Uri.parse(uri);
}
} }
final String uri = cursor.moveToFirst() ? cursor.getString(1) : null;
cursor.close();
return uri == null ? null : Uri.parse(uri);
} }
public static String getVersionName(Context context) { public static String getVersionName(Context context) {
@ -135,4 +113,22 @@ public class PhoneHelper {
return "unknown"; return "unknown";
} }
} }
private static class NotThrowCursorLoader extends CursorLoader {
private 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 (Throwable e) {
return (null);
}
}
}
} }