From 83cd02e7e9d043629bbd29982bfed6f184030ebc Mon Sep 17 00:00:00 2001 From: camsim99 Date: Wed, 25 Jan 2023 16:54:55 -0800 Subject: [PATCH 1/5] Make changes amd add test --- .../camerax/CameraSelectorHostApiImpl.java | 4 +++- .../flutter/plugins/camerax/InstanceManager.java | 4 +--- .../camerax/ProcessCameraProviderHostApiImpl.java | 12 +++++++++--- .../plugins/camerax/InstanceManagerTest.java | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraSelectorHostApiImpl.java b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraSelectorHostApiImpl.java index 87c69dea9e1c..0528d584d26e 100644 --- a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraSelectorHostApiImpl.java +++ b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraSelectorHostApiImpl.java @@ -60,7 +60,9 @@ public List filter(@NonNull Long identifier, @NonNull List cameraInf List filteredCameraInfosIds = new ArrayList(); for (CameraInfo cameraInfo : filteredCameraInfos) { - cameraInfoFlutterApiImpl.create(cameraInfo, result -> {}); + if (!instanceManager.containsInstance(cameraInfo)) { + cameraInfoFlutterApiImpl.create(cameraInfo, result -> {}); + } Long filteredCameraInfoId = instanceManager.getIdentifierForStrongReference(cameraInfo); filteredCameraInfosIds.add(filteredCameraInfoId); } diff --git a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java index 9b549d7bd1ea..5ae907711b19 100644 --- a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java +++ b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java @@ -129,9 +129,7 @@ public void addDartCreatedInstance(Object instance, long identifier) { */ public long addHostCreatedInstance(Object instance) { assertManagerIsNotClosed(); - if (containsInstance(instance)) { - return getIdentifierForStrongReference(instance); - } + final long identifier = nextIdentifier++; addInstance(instance, identifier); return identifier; diff --git a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/ProcessCameraProviderHostApiImpl.java b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/ProcessCameraProviderHostApiImpl.java index f82f18f054c9..e7036e7090c1 100644 --- a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/ProcessCameraProviderHostApiImpl.java +++ b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/ProcessCameraProviderHostApiImpl.java @@ -66,7 +66,9 @@ public void getInstance(GeneratedCameraXLibrary.Result result) { final ProcessCameraProviderFlutterApiImpl flutterApi = new ProcessCameraProviderFlutterApiImpl(binaryMessenger, instanceManager); - flutterApi.create(processCameraProvider, reply -> {}); + if (!instanceManager.containsInstance(processCameraProvider)) { + flutterApi.create(processCameraProvider, reply -> {}); + } result.success(instanceManager.getIdentifierForStrongReference(processCameraProvider)); } catch (Exception e) { result.error(e); @@ -87,7 +89,9 @@ public List getAvailableCameraInfos(@NonNull Long identifier) { new CameraInfoFlutterApiImpl(binaryMessenger, instanceManager); for (CameraInfo cameraInfo : availableCameras) { - cameraInfoFlutterApi.create(cameraInfo, result -> {}); + if (!instanceManager.containsInstance(cameraInfo)) { + cameraInfoFlutterApi.create(cameraInfo, result -> {}); + } availableCamerasIds.add(instanceManager.getIdentifierForStrongReference(cameraInfo)); } return availableCamerasIds; @@ -122,7 +126,9 @@ public Long bindToLifecycle( final CameraFlutterApiImpl cameraFlutterApi = new CameraFlutterApiImpl(binaryMessenger, instanceManager); - cameraFlutterApi.create(camera, result -> {}); + if (!instanceManager.containsInstance(camera)) { + cameraFlutterApi.create(camera, result -> {}); + } return instanceManager.getIdentifierForStrongReference(camera); } diff --git a/packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/InstanceManagerTest.java b/packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/InstanceManagerTest.java index 3878e05a40e8..e2e012dc35fb 100644 --- a/packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/InstanceManagerTest.java +++ b/packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/InstanceManagerTest.java @@ -5,6 +5,7 @@ package io.flutter.plugins.camerax; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -40,6 +41,20 @@ public void addHostCreatedInstance() { instanceManager.close(); } + @Test + public void addHostCreatedInstance_createsSameInstanceTwice() { + final InstanceManager instanceManager = InstanceManager.open(identifier -> {}); + + final Object object = new Object(); + long firstIdentifier = instanceManager.addHostCreatedInstance(object); + long secondIdentifier = instanceManager.addHostCreatedInstance(object); + + assertNotEquals(firstIdentifier, secondIdentifier); + assertTrue(instanceManager.containsInstance(object)); + + instanceManager.close(); + } + @Test public void remove() { final InstanceManager instanceManager = InstanceManager.open(identifier -> {}); From 857d4183f2c770444d65f1cb7347349b8ae293fc Mon Sep 17 00:00:00 2001 From: camsim99 Date: Thu, 26 Jan 2023 10:14:15 -0800 Subject: [PATCH 2/5] Update changelog --- packages/camera/camera_android_camerax/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/camera/camera_android_camerax/CHANGELOG.md b/packages/camera/camera_android_camerax/CHANGELOG.md index 389fc31e26e7..69b2ce22f7b5 100644 --- a/packages/camera/camera_android_camerax/CHANGELOG.md +++ b/packages/camera/camera_android_camerax/CHANGELOG.md @@ -6,3 +6,4 @@ * Adds ProcessCameraProvider class. * Bump CameraX version to 1.3.0-alpha02. * Adds Camera and UseCase classes, along with methods for binding UseCases to a lifecycle with the ProcessCameraProvider. +* Changes instance manager to allow the separate creation of identical objects. From e6d8c746dec32db66c1654927cb6f0925e288003 Mon Sep 17 00:00:00 2001 From: camsim99 Date: Thu, 26 Jan 2023 10:17:10 -0800 Subject: [PATCH 3/5] Add comment --- .../main/java/io/flutter/plugins/camerax/InstanceManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java index 5ae907711b19..f3adf0b7a1ad 100644 --- a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java +++ b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java @@ -122,7 +122,8 @@ public void addDartCreatedInstance(Object instance, long identifier) { /** * Adds a new instance that was instantiated from the host platform. * - *

If an instance has already been added, the identifier of the instance will be returned. + *

If an instance has already been added, this will re-create it. {@code #containsInstance} can + * be used to check if the object has already been added to avoid this. * * @param instance the instance to be stored. * @return the unique identifier stored with instance. From 111b39421e7ba865020dfb18be9100fba781b8eb Mon Sep 17 00:00:00 2001 From: camsim99 Date: Thu, 26 Jan 2023 10:18:02 -0800 Subject: [PATCH 4/5] Fix spelling --- .../main/java/io/flutter/plugins/camerax/InstanceManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java index f3adf0b7a1ad..75f2ef205dec 100644 --- a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java +++ b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java @@ -122,7 +122,7 @@ public void addDartCreatedInstance(Object instance, long identifier) { /** * Adds a new instance that was instantiated from the host platform. * - *

If an instance has already been added, this will re-create it. {@code #containsInstance} can + *

If an instance has already been added, this will recreate it. {@code #containsInstance} can * be used to check if the object has already been added to avoid this. * * @param instance the instance to be stored. From 398c1b04ad035120db64001e870438d6ca6e9947 Mon Sep 17 00:00:00 2001 From: Camille Simon <43054281+camsim99@users.noreply.github.com> Date: Mon, 30 Jan 2023 13:30:04 -0800 Subject: [PATCH 5/5] Update packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java Co-authored-by: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> --- .../main/java/io/flutter/plugins/camerax/InstanceManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java index 75f2ef205dec..8212d1267a19 100644 --- a/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java +++ b/packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/InstanceManager.java @@ -122,7 +122,7 @@ public void addDartCreatedInstance(Object instance, long identifier) { /** * Adds a new instance that was instantiated from the host platform. * - *

If an instance has already been added, this will recreate it. {@code #containsInstance} can + *

If an instance has already been added, this will replace it. {@code #containsInstance} can * be used to check if the object has already been added to avoid this. * * @param instance the instance to be stored.