diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-04-01 17:17:26 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-04-01 17:17:26 +0200 |
commit | a69c98c5c58da0babe570a1b7eebaacf58ae5a05 (patch) | |
tree | 174ca68ec0fb95079b0307da1c81439e91bfd675 /src/main/java | |
parent | 5670a7a735387c4371af1b27aa1e59e927a9a175 (diff) |
request camera permissions when attaching taking picture or recording video
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/ConversationFragment.java | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index 7942c14b7..4ddc318ab 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -23,6 +23,8 @@ import android.os.SystemClock; import android.preference.PreferenceManager; import android.provider.MediaStore; import android.support.annotation.IdRes; +import android.support.annotation.NonNull; +import android.support.annotation.StringRes; import android.support.media.ExifInterface; import android.support.v13.view.inputmethod.InputConnectionCompat; import android.support.v13.view.inputmethod.InputContentInfoCompat; @@ -1435,7 +1437,11 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } public void attachFile(final int attachmentChoice) { - if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) { + if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_FROM_CAMERA) { + if (!hasStorageAndCameraPermission(attachmentChoice)) { + return; + } + } else if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) { if (!Config.ONLY_INTERNAL_STORAGE && !hasStoragePermission(attachmentChoice)) { return; } @@ -1512,9 +1518,9 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } @Override - public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { if (grantResults.length > 0) { - if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (allGranted(grantResults)) { if (requestCode == REQUEST_START_DOWNLOAD) { if (this.mPendingDownloadableMessage != null) { startDownloadable(this.mPendingDownloadableMessage); @@ -1527,9 +1533,33 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke attachFile(requestCode); } } else { - getActivity().runOnUiThread(() -> Toast.makeText(getActivity(), R.string.no_permission, Toast.LENGTH_SHORT).show()); + @StringRes int res; + if (Manifest.permission.CAMERA.equals(getFirstDenied(grantResults, permissions))) { + res = R.string.no_camera_permission; + } else { + res = R.string.no_storage_permission; + } + Toast.makeText(getActivity(), res, Toast.LENGTH_SHORT).show(); + } + } + } + + private static boolean allGranted(int[] grantResults) { + for (int grantResult : grantResults) { + if (grantResult != PackageManager.PERMISSION_GRANTED) { + return false; + } + } + return true; + } + + private static String getFirstDenied(int[] grantResults, String[] permissions) { + for (int i = 0; i < grantResults.length; ++i) { + if (grantResults[i] == PackageManager.PERMISSION_DENIED) { + return permissions[i]; } } + return null; } public void startDownloadable(Message message) { @@ -1547,6 +1577,26 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } + private boolean hasStorageAndCameraPermission(int requestCode) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + List<String> missingPermissions = new ArrayList<>(); + if (!Config.ONLY_INTERNAL_STORAGE && activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + missingPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); + } + if (activity.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { + missingPermissions.add(Manifest.permission.CAMERA); + } + if (missingPermissions.size() == 0) { + return true; + } else { + requestPermissions(missingPermissions.toArray(new String[missingPermissions.size()]), requestCode); + return false; + } + } else { + return true; + } + } + @SuppressLint("InflateParams") protected void clearHistoryDialog(final Conversation conversation) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |