forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[camerax] Implements torch mode (flutter#4903)
Implements the torch flash mode. Also wraps classes necessary for the implementation (a method in `Camera`, `CameraControl`). Fixes flutter/flutter#120715. Fixes flutter/flutter#115846. Part of flutter/flutter#115847.
- Loading branch information
1 parent
a3a330a
commit d965258
Showing
26 changed files
with
1,337 additions
and
305 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.5.0+19 | ||
|
||
* Implements torch flash mode. | ||
|
||
## 0.5.0+18 | ||
|
||
* Implements `startVideoCapturing`. | ||
|
This file contains 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 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
29 changes: 29 additions & 0 deletions
29
...camerax/android/src/main/java/io/flutter/plugins/camerax/CameraControlFlutterApiImpl.java
This file contains 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,29 @@ | ||
// 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.annotation.NonNull; | ||
import androidx.camera.core.CameraControl; | ||
import io.flutter.plugin.common.BinaryMessenger; | ||
import io.flutter.plugins.camerax.GeneratedCameraXLibrary.CameraControlFlutterApi; | ||
|
||
public class CameraControlFlutterApiImpl extends CameraControlFlutterApi { | ||
private final @NonNull InstanceManager instanceManager; | ||
|
||
public CameraControlFlutterApiImpl( | ||
@NonNull BinaryMessenger binaryMessenger, @NonNull InstanceManager instanceManager) { | ||
super(binaryMessenger); | ||
this.instanceManager = instanceManager; | ||
} | ||
|
||
/** | ||
* Creates a {@link CameraControl} instance in Dart. {@code reply} is not used so it can be empty. | ||
*/ | ||
void create(CameraControl cameraControl, Reply<Void> reply) { | ||
if (!instanceManager.containsInstance(cameraControl)) { | ||
create(instanceManager.addHostCreatedInstance(cameraControl), reply); | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
...id_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraControlHostApiImpl.java
This file contains 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,101 @@ | ||
// 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 android.content.Context; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import androidx.camera.core.CameraControl; | ||
import androidx.core.content.ContextCompat; | ||
import com.google.common.util.concurrent.FutureCallback; | ||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import io.flutter.plugins.camerax.GeneratedCameraXLibrary.CameraControlHostApi; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Host API implementation for {@link CameraControl}. | ||
* | ||
* <p>This class handles instantiating and adding native object instances that are attached to a | ||
* Dart instance or handle method calls on the associated native class or an instance of the class. | ||
*/ | ||
public class CameraControlHostApiImpl implements CameraControlHostApi { | ||
private final InstanceManager instanceManager; | ||
private final CameraControlProxy proxy; | ||
|
||
/** Proxy for constructors and static method of {@link CameraControl}. */ | ||
@VisibleForTesting | ||
public static class CameraControlProxy { | ||
Context context; | ||
|
||
/** Enables or disables the torch of the specified {@link CameraControl} instance. */ | ||
@NonNull | ||
public void enableTorch( | ||
@NonNull CameraControl cameraControl, | ||
@NonNull Boolean torch, | ||
@NonNull GeneratedCameraXLibrary.Result<Void> result) { | ||
ListenableFuture<Void> enableTorchFuture = cameraControl.enableTorch(torch); | ||
|
||
Futures.addCallback( | ||
enableTorchFuture, | ||
new FutureCallback<Void>() { | ||
public void onSuccess(Void voidResult) { | ||
result.success(null); | ||
} | ||
|
||
public void onFailure(Throwable t) { | ||
result.error(t); | ||
} | ||
}, | ||
ContextCompat.getMainExecutor(context)); | ||
} | ||
} | ||
|
||
/** | ||
* Constructs an {@link CameraControlHostApiImpl}. | ||
* | ||
* @param instanceManager maintains instances stored to communicate with attached Dart objects | ||
*/ | ||
public CameraControlHostApiImpl( | ||
@NonNull InstanceManager instanceManager, @NonNull Context context) { | ||
this(instanceManager, new CameraControlProxy(), context); | ||
} | ||
|
||
/** | ||
* Constructs an {@link CameraControlHostApiImpl}. | ||
* | ||
* @param instanceManager maintains instances stored to communicate with attached Dart objects | ||
* @param proxy proxy for constructors and static method of {@link CameraControl} | ||
* @param context {@link Context} used to retrieve {@code Executor} used to enable torch mode | ||
*/ | ||
@VisibleForTesting | ||
CameraControlHostApiImpl( | ||
@NonNull InstanceManager instanceManager, | ||
@NonNull CameraControlProxy proxy, | ||
@NonNull Context context) { | ||
this.instanceManager = instanceManager; | ||
this.proxy = proxy; | ||
proxy.context = context; | ||
} | ||
|
||
/** | ||
* Sets the context that the {@code ProcessCameraProvider} will use to enable/disable torch mode. | ||
* | ||
* <p>If using the camera plugin in an add-to-app context, ensure that a new instance of the | ||
* {@code CameraControl} is fetched via {@code #enableTorch} anytime the context changes. | ||
*/ | ||
public void setContext(@NonNull Context context) { | ||
this.proxy.context = context; | ||
} | ||
|
||
@Override | ||
public void enableTorch( | ||
@NonNull Long identifier, | ||
@NonNull Boolean torch, | ||
@NonNull GeneratedCameraXLibrary.Result<Void> result) { | ||
proxy.enableTorch( | ||
Objects.requireNonNull(instanceManager.getInstance(identifier)), torch, result); | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.