Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[camerax] Allow instance manager to create identical objects #7034

Merged
merged 6 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/camera/camera_android_camerax/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public List<Long> filter(@NonNull Long identifier, @NonNull List<Long> cameraInf
List<Long> filteredCameraInfosIds = new ArrayList<Long>();

for (CameraInfo cameraInfo : filteredCameraInfos) {
cameraInfoFlutterApiImpl.create(cameraInfo, result -> {});
if (!instanceManager.containsInstance(cameraInfo)) {
cameraInfoFlutterApiImpl.create(cameraInfo, result -> {});
}
Long filteredCameraInfoId = instanceManager.getIdentifierForStrongReference(cameraInfo);
filteredCameraInfosIds.add(filteredCameraInfoId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,15 @@ public void addDartCreatedInstance(Object instance, long identifier) {
/**
* Adds a new instance that was instantiated from the host platform.
*
* <p>If an instance has already been added, the identifier of the instance will be returned.
* <p>If an instance has already been added, this will recreate it. {@code #containsInstance} can
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
* 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.
*/
public long addHostCreatedInstance(Object instance) {
assertManagerIsNotClosed();
if (containsInstance(instance)) {
return getIdentifierForStrongReference(instance);
}

final long identifier = nextIdentifier++;
addInstance(instance, identifier);
return identifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void getInstance(GeneratedCameraXLibrary.Result<Long> 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);
Expand All @@ -87,7 +89,9 @@ public List<Long> 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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 -> {});
Expand Down