This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[camerax] Adds functionality to bind UseCases to a lifecycle #6939
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bea50d3
Copy over code from proof of concept
camsim99 154eed6
Add dart tests
camsim99 7c31863
Fix dart tests
camsim99 8855f00
Add java tests
camsim99 147c4f6
Add me as owner and changelog change
camsim99 53a3521
Fix analyzer
camsim99 66265ef
Add instance manager fix
camsim99 af38240
Update comment
camsim99 55c82d3
Undo instance manager changes
camsim99 f73ba55
Merge remote-tracking branch 'upstream/main' into camx_s1
camsim99 fa70664
Formatting
camsim99 b4f890c
Fix analyze
camsim99 452b5c0
Address review
camsim99 7dae914
Fix analyze
camsim99 d08702a
Add import
camsim99 33f2612
Fix assertion error
camsim99 b7b4b98
Remove unecessary this keywrod
camsim99 fcad911
Update packages/camera/camera_android_camerax/android/src/main/java/i…
camsim99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ndroid_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraFlutterApiImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.camerax; | ||
|
|
||
| import androidx.camera.core.Camera; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugins.camerax.GeneratedCameraXLibrary.CameraFlutterApi; | ||
|
|
||
| public class CameraFlutterApiImpl extends CameraFlutterApi { | ||
| private final InstanceManager instanceManager; | ||
|
|
||
| public CameraFlutterApiImpl(BinaryMessenger binaryMessenger, InstanceManager instanceManager) { | ||
| super(binaryMessenger); | ||
| this.instanceManager = instanceManager; | ||
| } | ||
|
|
||
| void create(Camera camera, Reply<Void> reply) { | ||
| create(instanceManager.addHostCreatedInstance(camera), reply); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,13 @@ | |
|
|
||
| import android.content.Context; | ||
| import androidx.annotation.NonNull; | ||
| import androidx.camera.core.Camera; | ||
| import androidx.camera.core.CameraInfo; | ||
| import androidx.camera.core.CameraSelector; | ||
| import androidx.camera.core.UseCase; | ||
| import androidx.camera.lifecycle.ProcessCameraProvider; | ||
| import androidx.core.content.ContextCompat; | ||
| import androidx.lifecycle.LifecycleOwner; | ||
| import com.google.common.util.concurrent.ListenableFuture; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugins.camerax.GeneratedCameraXLibrary.ProcessCameraProviderHostApi; | ||
|
|
@@ -20,6 +24,7 @@ public class ProcessCameraProviderHostApiImpl implements ProcessCameraProviderHo | |
| private final InstanceManager instanceManager; | ||
|
|
||
| private Context context; | ||
| private LifecycleOwner lifecycleOwner; | ||
|
|
||
| public ProcessCameraProviderHostApiImpl( | ||
| BinaryMessenger binaryMessenger, InstanceManager instanceManager, Context context) { | ||
|
|
@@ -28,6 +33,10 @@ public ProcessCameraProviderHostApiImpl( | |
| this.context = context; | ||
| } | ||
|
|
||
| public void setLifecycleOwner(LifecycleOwner lifecycleOwner) { | ||
| this.lifecycleOwner = lifecycleOwner; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the context that the {@code ProcessCameraProvider} will use to attach the lifecycle of the | ||
| * camera to. | ||
|
|
@@ -40,8 +49,8 @@ public void setContext(Context context) { | |
| } | ||
|
|
||
| /** | ||
| * Returns the instance of the ProcessCameraProvider to manage the lifecycle of the camera for the | ||
| * current {@code Context}. | ||
| * Returns the instance of the {@code ProcessCameraProvider} to manage the lifecycle of the camera | ||
| * for the current {@code Context}. | ||
| */ | ||
| @Override | ||
| public void getInstance(GeneratedCameraXLibrary.Result<Long> result) { | ||
|
|
@@ -54,11 +63,9 @@ public void getInstance(GeneratedCameraXLibrary.Result<Long> result) { | |
| // Camera provider is now guaranteed to be available. | ||
| ProcessCameraProvider processCameraProvider = processCameraProviderFuture.get(); | ||
|
|
||
| if (!instanceManager.containsInstance(processCameraProvider)) { | ||
| final ProcessCameraProviderFlutterApiImpl flutterApi = | ||
| new ProcessCameraProviderFlutterApiImpl(binaryMessenger, instanceManager); | ||
| flutterApi.create(processCameraProvider, reply -> {}); | ||
| } | ||
| final ProcessCameraProviderFlutterApiImpl flutterApi = | ||
| new ProcessCameraProviderFlutterApiImpl(binaryMessenger, instanceManager); | ||
| flutterApi.create(processCameraProvider, reply -> {}); | ||
| result.success(instanceManager.getIdentifierForStrongReference(processCameraProvider)); | ||
| } catch (Exception e) { | ||
| result.error(e); | ||
|
|
@@ -67,7 +74,7 @@ public void getInstance(GeneratedCameraXLibrary.Result<Long> result) { | |
| ContextCompat.getMainExecutor(context)); | ||
| } | ||
|
|
||
| /** Returns cameras available to the ProcessCameraProvider. */ | ||
| /** Returns cameras available to the {@code ProcessCameraProvider}. */ | ||
| @Override | ||
| public List<Long> getAvailableCameraInfos(@NonNull Long identifier) { | ||
| ProcessCameraProvider processCameraProvider = | ||
|
|
@@ -84,4 +91,52 @@ public List<Long> getAvailableCameraInfos(@NonNull Long identifier) { | |
| } | ||
| return availableCamerasIds; | ||
| } | ||
|
|
||
| /** | ||
| * Binds specified {@code UseCase}s to the lifecycle of the {@code LifecycleOwner} that | ||
| * corresponds to this instance and returns the instance of the {@code Camera} whose lifecycle | ||
| * that {@code LifecycleOwner} reflects. | ||
| */ | ||
| @Override | ||
| public Long bindToLifecycle( | ||
| @NonNull Long identifier, | ||
| @NonNull Long cameraSelectorIdentifier, | ||
| @NonNull List<Long> useCaseIds) { | ||
| ProcessCameraProvider processCameraProvider = | ||
| (ProcessCameraProvider) instanceManager.getInstance(identifier); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you wrap
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @gmackall just a heads up about this |
||
| CameraSelector cameraSelector = | ||
| (CameraSelector) instanceManager.getInstance(cameraSelectorIdentifier); | ||
| UseCase[] useCases = new UseCase[useCaseIds.size()]; | ||
| for (int i = 0; i < useCaseIds.size(); i++) { | ||
| useCases[i] = (UseCase) instanceManager.getInstance(((Number) useCaseIds.get(i)).longValue()); | ||
| } | ||
|
|
||
| Camera camera = | ||
| processCameraProvider.bindToLifecycle( | ||
| (LifecycleOwner) lifecycleOwner, cameraSelector, useCases); | ||
|
|
||
| final CameraFlutterApiImpl camraFlutterApi = | ||
| new CameraFlutterApiImpl(binaryMessenger, instanceManager); | ||
| camraFlutterApi.create(camera, result -> {}); | ||
|
camsim99 marked this conversation as resolved.
Outdated
|
||
|
|
||
| return instanceManager.getIdentifierForStrongReference(camera); | ||
| } | ||
|
|
||
| @Override | ||
| public void unbind(@NonNull Long identifier, @NonNull List<Long> useCaseIds) { | ||
| ProcessCameraProvider processCameraProvider = | ||
| (ProcessCameraProvider) instanceManager.getInstance(identifier); | ||
| UseCase[] useCases = new UseCase[useCaseIds.size()]; | ||
| for (int i = 0; i < useCaseIds.size(); i++) { | ||
| useCases[i] = (UseCase) instanceManager.getInstance(((Number) useCaseIds.get(i)).longValue()); | ||
| } | ||
| processCameraProvider.unbind(useCases); | ||
| } | ||
|
|
||
| @Override | ||
| public void unbindAll(@NonNull Long identifier) { | ||
| ProcessCameraProvider processCameraProvider = | ||
| (ProcessCameraProvider) instanceManager.getInstance(identifier); | ||
| processCameraProvider.unbindAll(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I missed this in the last PR. But I don't think it is necessary to create a new
CameraAndroidCameraxPlugininstance. You should be able to just callsetUpon the current instance, right?