Skip to content

Commit 89cd25b

Browse files
Merge pull request #36 from taras-omelchuk/media-capture
Media-Capture plugin for 12.0 release
2 parents bcbbe2d + 769ec1e commit 89cd25b

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed

plugin.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
Copyright (c) 2022 BlackBerry Limited. All Rights Reserved.
3+
Copyright (c) 2023 BlackBerry Limited. All Rights Reserved.
44
Some modifications to the original cordova-plugin-file
55
from https://github.com/apache/cordova-plugin-media-capture
66
@@ -88,10 +88,11 @@ xmlns:android="http://schemas.android.com/apk/res/android"
8888
</config-file>
8989

9090
<config-file target="AndroidManifest.xml" parent="/manifest">
91+
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
9192
<uses-permission android:name="android.permission.RECORD_AUDIO" />
9293
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
93-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
94-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
94+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
95+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
9596
</config-file>
9697

9798
<source-file src="src/android/BBDCapture.java" target-dir="src/com/blackberry/bbd/cordova/plugins/mediacapture" />

src/android/BBDCapture.java

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2022 BlackBerry Limited. All Rights Reserved.
2+
Copyright (c) 2023 BlackBerry Limited. All Rights Reserved.
33
Some modifications to the original Cordova Media Capture plugin
44
from https://github.com/apache/cordova-plugin-media-capture
55
@@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one
3030
import java.lang.reflect.InvocationTargetException;
3131
import java.lang.reflect.Field;
3232
import java.lang.reflect.Method;
33+
import java.util.ArrayList;
3334
import java.util.Arrays;
3435

3536
import android.content.ActivityNotFoundException;
@@ -84,6 +85,19 @@ public class BBDCapture extends CordovaPlugin {
8485
private static final int CAPTURE_PERMISSION_DENIED = 4;
8586
private static final int CAPTURE_NOT_SUPPORTED = 20;
8687

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+
87101
private boolean cameraPermissionInManifest; // Whether or not the CAMERA permission is declared in AndroidManifest.xml
88102

89103
private final PendingRequests pendingRequests = new PendingRequests();
@@ -225,6 +239,41 @@ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean vi
225239
return obj;
226240
}
227241

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+
228277
/**
229278
* Sets up an intent to capture audio. Result handled by onActivityResult()
230279
*/
@@ -277,35 +326,21 @@ private String getTempDirectoryPath() {
277326
* Sets up an intent to capture images. Result handled by onActivityResult()
278327
*/
279328
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;
282330

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();
285333

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);
297335

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);
304340

305-
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
341+
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
306342

307-
this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode);
308-
}
343+
this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode);
309344
}
310345

311346
private static void createWritableFile(File file) throws IOException {

src/ios/CDVCaptureBD.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2022 BlackBerry Limited. All Rights Reserved.
2+
Copyright (c) 2023 BlackBerry Limited. All Rights Reserved.
33
Some modifications to the original Cordova Media Capture plugin
44
from https://github.com/apache/cordova-plugin-media-capture/
55
@@ -810,6 +810,8 @@ - (void)viewDidLoad
810810
}
811811
}
812812

813+
[self.avSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
814+
813815
NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath]; // use file system temporary directory
814816
NSError* err = nil;
815817
GDFileManager* fileMgr = [[GDFileManager alloc] init];
@@ -869,7 +871,6 @@ - (void)processButton:(id)sender
869871
__weak CDVAudioRecorderViewControllerBD* weakSelf = self;
870872

871873
void (^startRecording)(void) = ^{
872-
[weakSelf.avSession setCategory:AVAudioSessionCategoryRecord error:&error];
873874
[weakSelf.avSession setActive:YES error:&error];
874875
if (error) {
875876
// can't continue without active audio session

0 commit comments

Comments
 (0)