From 5e15c482069dc7be7526b41e5925853f2463c62b Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 17 Nov 2017 10:28:51 +0100 Subject: [PATCH] refactore phone helper to ignore security exception --- .../ui/PublishProfilePictureActivity.java | 2 +- .../conversations/utils/PhoneHelper.java | 66 +++++++++---------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java index 9a699b518..fe18b5478 100644 --- a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java @@ -141,7 +141,7 @@ public class PublishProfilePictureActivity extends XmppActivity { } }); - this.defaultUri = PhoneHelper.getSelfiUri(getApplicationContext()); + this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext()); } private void chooseAvatar(boolean crop) { diff --git a/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java b/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java index 3242cfcf5..380afc578 100644 --- a/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/PhoneHelper.java @@ -82,45 +82,23 @@ 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 (Throwable 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) { + public static Uri getProfilePictureUri(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { return null; } - String[] mProjection = new String[]{Profile._ID, Profile.PHOTO_URI}; - Cursor mProfileCursor = context.getContentResolver().query( - Profile.CONTENT_URI, mProjection, null, null, null); - - if (mProfileCursor == null || mProfileCursor.getCount() == 0) { + final String[] projection = new String[]{Profile._ID, Profile.PHOTO_URI}; + final Cursor cursor; + try { + cursor = context.getContentResolver().query(Profile.CONTENT_URI, projection, null, null, null); + } catch (SecurityException e) { return null; - } else { - mProfileCursor.moveToFirst(); - String uri = mProfileCursor.getString(1); - mProfileCursor.close(); - if (uri == null) { - return null; - } else { - return Uri.parse(uri); - } } + if (cursor == null) { + return null; + } + final String uri = cursor.moveToFirst() ? cursor.getString(1) : null; + cursor.close(); + return uri == null ? null : Uri.parse(uri); } public static String getVersionName(Context context) { @@ -135,4 +113,22 @@ public class PhoneHelper { 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); + } + } + + } }