Skip to content

Commit

Permalink
Merge pull request #36 from taras-omelchuk/media-capture
Browse files Browse the repository at this point in the history
Media-Capture plugin for 12.0 release
  • Loading branch information
volodymyrtaliar committed Nov 16, 2023
2 parents bcbbe2d + 769ec1e commit 89cd25b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
7 changes: 4 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 BlackBerry Limited. All Rights Reserved.
Copyright (c) 2023 BlackBerry Limited. All Rights Reserved.
Some modifications to the original cordova-plugin-file
from https://github.com/apache/cordova-plugin-media-capture
Expand Down Expand Up @@ -88,10 +88,11 @@ xmlns:android="http://schemas.android.com/apk/res/android"
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
</config-file>

<source-file src="src/android/BBDCapture.java" target-dir="src/com/blackberry/bbd/cordova/plugins/mediacapture" />
Expand Down
85 changes: 60 additions & 25 deletions src/android/BBDCapture.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2022 BlackBerry Limited. All Rights Reserved.
Copyright (c) 2023 BlackBerry Limited. All Rights Reserved.
Some modifications to the original Cordova Media Capture plugin
from https://github.com/apache/cordova-plugin-media-capture
Expand Down Expand Up @@ -30,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;

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

private static String[] storagePermissions;
static {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
storagePermissions = new String[]{
};
} else {
storagePermissions = new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
}
}

private boolean cameraPermissionInManifest; // Whether or not the CAMERA permission is declared in AndroidManifest.xml

private final PendingRequests pendingRequests = new PendingRequests();
Expand Down Expand Up @@ -225,6 +239,41 @@ private JSONObject getAudioVideoData(String filePath, JSONObject obj, boolean vi
return obj;
}

private boolean isMissingPermissions(Request req, ArrayList<String> permissions) {
ArrayList<String> missingPermissions = new ArrayList<>();
for (String permission: permissions) {
if (!PermissionHelper.hasPermission(this, permission)) {
missingPermissions.add(permission);
}
}

boolean isMissingPermissions = missingPermissions.size() > 0;
if (isMissingPermissions) {
String[] missing = missingPermissions.toArray(new String[missingPermissions.size()]);
PermissionHelper.requestPermissions(this, req.requestCode, missing);
}
return isMissingPermissions;
}

private boolean isMissingPermissions(Request req, String mediaPermission) {
ArrayList<String> permissions = new ArrayList<>(Arrays.asList(storagePermissions));
if (mediaPermission != null && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permissions.add(mediaPermission);
}
return isMissingPermissions(req, permissions);
}

private boolean isMissingCameraPermissions(Request req, String mediaPermission) {
ArrayList<String> cameraPermissions = new ArrayList<>(Arrays.asList(storagePermissions));
if (cameraPermissionInManifest) {
cameraPermissions.add(Manifest.permission.CAMERA);
}
if (mediaPermission != null && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
cameraPermissions.add(mediaPermission);
}
return isMissingPermissions(req, cameraPermissions);
}

/**
* Sets up an intent to capture audio. Result handled by onActivityResult()
*/
Expand Down Expand Up @@ -277,35 +326,21 @@ private String getTempDirectoryPath() {
* Sets up an intent to capture images. Result handled by onActivityResult()
*/
private void captureImage(Request req) {
boolean needExternalStoragePermission =
!PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (isMissingCameraPermissions(req, Manifest.permission.READ_MEDIA_IMAGES)) return;

boolean needCameraPermission = cameraPermissionInManifest &&
!PermissionHelper.hasPermission(this, Manifest.permission.CAMERA);
// Save the number of images currently on disk for later
this.numPics = queryImgDB(whichContentStore()).getCount();

if (needExternalStoragePermission || needCameraPermission) {
if (needExternalStoragePermission && needCameraPermission) {
PermissionHelper.requestPermissions(this, req.requestCode, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA});
} else if (needExternalStoragePermission) {
PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE);
} else {
PermissionHelper.requestPermission(this, req.requestCode, Manifest.permission.CAMERA);
}
} else {
// Save the number of images currently on disk for later
this.numPics = queryImgDB(whichContentStore()).getCount();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

ContentResolver contentResolver = this.cordova.getContext().getContentResolver();
ContentValues cv = new ContentValues();
cv.put(MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG);
imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);
ContentResolver contentResolver = this.cordova.getContext().getContentResolver();
ContentValues cv = new ContentValues();
cv.put(MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG);
imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv);

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);

this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode);
}
this.cordova.startActivityForResult((CordovaPlugin) this, intent, req.requestCode);
}

private static void createWritableFile(File file) throws IOException {
Expand Down
5 changes: 3 additions & 2 deletions src/ios/CDVCaptureBD.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2022 BlackBerry Limited. All Rights Reserved.
Copyright (c) 2023 BlackBerry Limited. All Rights Reserved.
Some modifications to the original Cordova Media Capture plugin
from https://github.com/apache/cordova-plugin-media-capture/
Expand Down Expand Up @@ -810,6 +810,8 @@ - (void)viewDidLoad
}
}

[self.avSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath]; // use file system temporary directory
NSError* err = nil;
GDFileManager* fileMgr = [[GDFileManager alloc] init];
Expand Down Expand Up @@ -869,7 +871,6 @@ - (void)processButton:(id)sender
__weak CDVAudioRecorderViewControllerBD* weakSelf = self;

void (^startRecording)(void) = ^{
[weakSelf.avSession setCategory:AVAudioSessionCategoryRecord error:&error];
[weakSelf.avSession setActive:YES error:&error];
if (error) {
// can't continue without active audio session
Expand Down

0 comments on commit 89cd25b

Please sign in to comment.