From 4a2a4390a282ee782ed5c838391d77c4af3795da Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Sun, 24 Nov 2024 16:36:15 +0100 Subject: [PATCH] feat(torch): add support for ML Kit Barcode Scanning (#337) --- .changeset/hip-emus-attack.md | 5 ++ packages/torch/README.md | 14 +++-- packages/torch/android/build.gradle | 2 + .../capacitorjs/plugins/torch/Torch.java | 62 ++++++++++++++----- packages/torch/src/definitions.ts | 6 +- 5 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 .changeset/hip-emus-attack.md diff --git a/.changeset/hip-emus-attack.md b/.changeset/hip-emus-attack.md new file mode 100644 index 00000000..936881b6 --- /dev/null +++ b/.changeset/hip-emus-attack.md @@ -0,0 +1,5 @@ +--- +'@capawesome/capacitor-torch': minor +--- + +feat(android): add compatibility for the ML Kit Barcode Scanning plugin diff --git a/packages/torch/README.md b/packages/torch/README.md index 8d94387a..b00eebf6 100644 --- a/packages/torch/README.md +++ b/packages/torch/README.md @@ -20,6 +20,12 @@ This API requires the following permissions be added to your `AndroidManifest.xm ``` +#### Variables + +This plugin will use the following project variables (defined in your app’s `variables.gradle` file): + +- `$androidxCameraCoreVersion` version of `androidx.camera:camera-core` (default: `1.1.0`) + ## Configuration No configuration required for this plugin. @@ -80,7 +86,7 @@ enable() => Promise Enable the torch. -Only available on Android and iOS. +Only available on Android (SDK 23+) and iOS. **Since:** 6.0.0 @@ -95,7 +101,7 @@ disable() => Promise Disable the torch. -Only available on Android and iOS. +Only available on Android (SDK 23+) and iOS. **Since:** 6.0.0 @@ -144,7 +150,7 @@ toggle() => Promise Toggle the torch. -Only available on Android and iOS. +Only available on Android (SDK 23+) and iOS. **Since:** 6.0.0 @@ -175,4 +181,4 @@ See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/mai ## License -See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/torch/LICENSE). \ No newline at end of file +See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/torch/LICENSE). diff --git a/packages/torch/android/build.gradle b/packages/torch/android/build.gradle index 7df405a5..3e2d01a5 100644 --- a/packages/torch/android/build.gradle +++ b/packages/torch/android/build.gradle @@ -2,6 +2,7 @@ ext { junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1' androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5' + androidxCameraCoreVersion = project.hasProperty('androidxCameraCoreVersion') ? rootProject.ext.androidxCameraCoreVersion : '1.1.0' androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1' } @@ -52,6 +53,7 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(':capacitor-android') implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" + implementation "androidx.camera:camera-core:$androidxCameraCoreVersion" testImplementation "junit:junit:$junitVersion" androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" diff --git a/packages/torch/android/src/main/java/io/capawesome/capacitorjs/plugins/torch/Torch.java b/packages/torch/android/src/main/java/io/capawesome/capacitorjs/plugins/torch/Torch.java index efa850a8..7df8d87d 100644 --- a/packages/torch/android/src/main/java/io/capawesome/capacitorjs/plugins/torch/Torch.java +++ b/packages/torch/android/src/main/java/io/capawesome/capacitorjs/plugins/torch/Torch.java @@ -4,6 +4,10 @@ import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraManager; import android.os.Build; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.camera.core.CameraControl; +import java.lang.reflect.Method; public class Torch { @@ -16,26 +20,12 @@ public Torch(TorchPlugin plugin) { } public void enable() throws CameraAccessException { - CameraManager camera = (CameraManager) plugin.getContext().getSystemService(plugin.getContext().CAMERA_SERVICE); - String cameraId = camera.getCameraIdList()[0]; - if (cameraId == null) { - return; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - camera.setTorchMode(cameraId, true); - } + setTorchMode(true); isTorchEnabled = true; } public void disable() throws CameraAccessException { - CameraManager camera = (CameraManager) plugin.getContext().getSystemService(plugin.getContext().CAMERA_SERVICE); - String cameraId = camera.getCameraIdList()[0]; - if (cameraId == null) { - return; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - camera.setTorchMode(cameraId, false); - } + setTorchMode(false); isTorchEnabled = false; } @@ -57,4 +47,44 @@ public void toggle() throws CameraAccessException { enable(); } } + + private void setTorchMode(boolean enabled) throws CameraAccessException { + CameraControl cameraControl = getCameraControl(); + if (cameraControl == null) { + CameraManager cameraManager = getCameraManager(); + setTorchMode(cameraManager, enabled); + } else { + setTorchMode(cameraControl, enabled); + } + } + + private void setTorchMode(@NonNull CameraManager cameraManager, boolean enabled) throws CameraAccessException { + String cameraId = cameraManager.getCameraIdList()[0]; + if (cameraId == null) { + return; + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + cameraManager.setTorchMode(cameraId, enabled); + } + } + + private void setTorchMode(@NonNull CameraControl cameraControl, boolean enabled) { + cameraControl.enableTorch(enabled); + } + + @Nullable + private CameraControl getCameraControl() { + try { + Class cls = Class.forName("io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.BarcodeScanner"); + Method method = cls.getMethod("getCameraControl"); + return (CameraControl) method.invoke(null); + } catch (Exception e) { + return null; + } + } + + @NonNull + private CameraManager getCameraManager() { + return (CameraManager) plugin.getContext().getSystemService(plugin.getContext().CAMERA_SERVICE); + } } diff --git a/packages/torch/src/definitions.ts b/packages/torch/src/definitions.ts index 4abfef3a..99146fc6 100644 --- a/packages/torch/src/definitions.ts +++ b/packages/torch/src/definitions.ts @@ -2,7 +2,7 @@ export interface TorchPlugin { /** * Enable the torch. * - * Only available on Android and iOS. + * Only available on Android (SDK 23+) and iOS. * * @since 6.0.0 */ @@ -10,7 +10,7 @@ export interface TorchPlugin { /** * Disable the torch. * - * Only available on Android and iOS. + * Only available on Android (SDK 23+) and iOS. * * @since 6.0.0 */ @@ -34,7 +34,7 @@ export interface TorchPlugin { /** * Toggle the torch. * - * Only available on Android and iOS. + * Only available on Android (SDK 23+) and iOS. * * @since 6.0.0 */