|
1 | 1 | /*
|
2 |
| - Copyright (c) 2022 BlackBerry Limited. All Rights Reserved. |
| 2 | + Copyright (c) 2023 BlackBerry Limited. All Rights Reserved. |
3 | 3 | Some modifications to the original Cordova Media Capture plugin
|
4 | 4 | from https://github.com/apache/cordova-plugin-media-capture
|
5 | 5 |
|
@@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one
|
30 | 30 | import java.lang.reflect.InvocationTargetException;
|
31 | 31 | import java.lang.reflect.Field;
|
32 | 32 | import java.lang.reflect.Method;
|
| 33 | +import java.util.ArrayList; |
33 | 34 | import java.util.Arrays;
|
34 | 35 |
|
35 | 36 | import android.content.ActivityNotFoundException;
|
@@ -84,6 +85,19 @@ public class BBDCapture extends CordovaPlugin {
|
84 | 85 | private static final int CAPTURE_PERMISSION_DENIED = 4;
|
85 | 86 | private static final int CAPTURE_NOT_SUPPORTED = 20;
|
86 | 87 |
|
| 88 | + private static String[] storagePermissions; |
| 89 | + static { |
| 90 | + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 91 | + storagePermissions = new String[]{ |
| 92 | + }; |
| 93 | + } else { |
| 94 | + storagePermissions = new String[] { |
| 95 | + Manifest.permission.READ_EXTERNAL_STORAGE, |
| 96 | + Manifest.permission.WRITE_EXTERNAL_STORAGE |
| 97 | + }; |
| 98 | + } |
| 99 | + } |
| 100 | + |
87 | 101 | private boolean cameraPermissionInManifest; // Whether or not the CAMERA permission is declared in AndroidManifest.xml
|
88 | 102 |
|
89 | 103 | private final PendingRequests pendingRequests = new PendingRequests();
|
@@ -225,6 +239,41 @@ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean vi
|
225 | 239 | return obj;
|
226 | 240 | }
|
227 | 241 |
|
| 242 | + private boolean isMissingPermissions(Request req, ArrayList<String> permissions) { |
| 243 | + ArrayList<String> missingPermissions = new ArrayList<>(); |
| 244 | + for (String permission: permissions) { |
| 245 | + if (!PermissionHelper.hasPermission(this, permission)) { |
| 246 | + missingPermissions.add(permission); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + boolean isMissingPermissions = missingPermissions.size() > 0; |
| 251 | + if (isMissingPermissions) { |
| 252 | + String[] missing = missingPermissions.toArray(new String[missingPermissions.size()]); |
| 253 | + PermissionHelper.requestPermissions(this, req.requestCode, missing); |
| 254 | + } |
| 255 | + return isMissingPermissions; |
| 256 | + } |
| 257 | + |
| 258 | + private boolean isMissingPermissions(Request req, String mediaPermission) { |
| 259 | + ArrayList<String> permissions = new ArrayList<>(Arrays.asList(storagePermissions)); |
| 260 | + if (mediaPermission != null && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 261 | + permissions.add(mediaPermission); |
| 262 | + } |
| 263 | + return isMissingPermissions(req, permissions); |
| 264 | + } |
| 265 | + |
| 266 | + private boolean isMissingCameraPermissions(Request req, String mediaPermission) { |
| 267 | + ArrayList<String> cameraPermissions = new ArrayList<>(Arrays.asList(storagePermissions)); |
| 268 | + if (cameraPermissionInManifest) { |
| 269 | + cameraPermissions.add(Manifest.permission.CAMERA); |
| 270 | + } |
| 271 | + if (mediaPermission != null && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 272 | + cameraPermissions.add(mediaPermission); |
| 273 | + } |
| 274 | + return isMissingPermissions(req, cameraPermissions); |
| 275 | + } |
| 276 | + |
228 | 277 | /**
|
229 | 278 | * Sets up an intent to capture audio. Result handled by onActivityResult()
|
230 | 279 | */
|
@@ -277,35 +326,21 @@ private String getTempDirectoryPath() {
|
277 | 326 | * Sets up an intent to capture images. Result handled by onActivityResult()
|
278 | 327 | */
|
279 | 328 | private void captureImage(Request req) {
|
280 |
| - boolean needExternalStoragePermission = |
281 |
| - !PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 329 | + if (isMissingCameraPermissions(req, Manifest.permission.READ_MEDIA_IMAGES)) return; |
282 | 330 |
|
283 |
| - boolean needCameraPermission = cameraPermissionInManifest && |
284 |
| - !PermissionHelper.hasPermission(this, Manifest.permission.CAMERA); |
| 331 | + // Save the number of images currently on disk for later |
| 332 | + this.numPics = queryImgDB(whichContentStore()).getCount(); |
285 | 333 |
|
286 |
| - if (needExternalStoragePermission || needCameraPermission) { |
287 |
| - if (needExternalStoragePermission && needCameraPermission) { |
288 |
| - PermissionHelper.requestPermissions(this, req.requestCode, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}); |
289 |
| - } else if (needExternalStoragePermission) { |
290 |
| - PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); |
291 |
| - } else { |
292 |
| - PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.CAMERA); |
293 |
| - } |
294 |
| - } else { |
295 |
| - // Save the number of images currently on disk for later |
296 |
| - this.numPics = queryImgDB(whichContentStore()).getCount(); |
| 334 | + Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); |
297 | 335 |
|
298 |
| - Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); |
299 |
| - |
300 |
| - ContentResolver contentResolver = this.cordova.getContext().getContentResolver(); |
301 |
| - ContentValues cv = new ContentValues(); |
302 |
| - cv.put(MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG); |
303 |
| - imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv); |
| 336 | + ContentResolver contentResolver = this.cordova.getContext().getContentResolver(); |
| 337 | + ContentValues cv = new ContentValues(); |
| 338 | + cv.put(MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG); |
| 339 | + imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv); |
304 | 340 |
|
305 |
| - intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri); |
| 341 | + intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri); |
306 | 342 |
|
307 |
| - this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode); |
308 |
| - } |
| 343 | + this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode); |
309 | 344 | }
|
310 | 345 |
|
311 | 346 | private static void createWritableFile(File file) throws IOException {
|
|
0 commit comments