Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class CameraPlugin implements MethodCallHandler {
private Camera camera;
private Activity activity;
private Registrar registrar;
private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks;
// The code to run after requesting camera permissions.
private Runnable cameraPermissionContinuation;
private boolean requestingPermission;
Expand All @@ -76,53 +77,51 @@ private CameraPlugin(Registrar registrar, FlutterView view, Activity activity) {

registrar.addRequestPermissionsResultListener(new CameraRequestPermissionsListener());

activity
.getApplication()
.registerActivityLifecycleCallbacks(
new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
this.activityLifecycleCallbacks =
new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

@Override
public void onActivityStarted(Activity activity) {}
@Override
public void onActivityStarted(Activity activity) {}

@Override
public void onActivityResumed(Activity activity) {
if (requestingPermission) {
requestingPermission = false;
return;
}
if (activity == CameraPlugin.this.activity) {
if (camera != null) {
camera.open(null);
}
}
@Override
public void onActivityResumed(Activity activity) {
if (requestingPermission) {
requestingPermission = false;
return;
}
if (activity == CameraPlugin.this.activity) {
if (camera != null) {
camera.open(null);
}
}
}

@Override
public void onActivityPaused(Activity activity) {
if (activity == CameraPlugin.this.activity) {
if (camera != null) {
camera.close();
}
}
@Override
public void onActivityPaused(Activity activity) {
if (activity == CameraPlugin.this.activity) {
if (camera != null) {
camera.close();
}
}
}

@Override
public void onActivityStopped(Activity activity) {
if (activity == CameraPlugin.this.activity) {
if (camera != null) {
camera.close();
}
}
@Override
public void onActivityStopped(Activity activity) {
if (activity == CameraPlugin.this.activity) {
if (camera != null) {
camera.close();
}
}
}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

@Override
public void onActivityDestroyed(Activity activity) {}
});
@Override
public void onActivityDestroyed(Activity activity) {}
};
}

public static void registerWith(Registrar registrar) {
Expand Down Expand Up @@ -181,6 +180,9 @@ public void onMethodCall(MethodCall call, final Result result) {
camera.close();
}
camera = new Camera(cameraName, resolutionPreset, result);
this.activity
.getApplication()
.registerActivityLifecycleCallbacks(this.activityLifecycleCallbacks);
break;
}
case "takePicture":
Expand All @@ -204,6 +206,11 @@ public void onMethodCall(MethodCall call, final Result result) {
if (camera != null) {
camera.dispose();
}
if (this.activity != null && this.activityLifecycleCallbacks != null) {
this.activity
.getApplication()
.unregisterActivityLifecycleCallbacks(this.activityLifecycleCallbacks);
}
result.success(null);
break;
}
Expand Down Expand Up @@ -376,7 +383,8 @@ private void computeBestPreviewAndRecordingSize(
} else {
previewSize = goodEnough.get(0);

// Video capture size should not be greater than 1080 because MediaRecorder cannot handle higher resolutions.
// Video capture size should not be greater than 1080 because MediaRecorder cannot handle
// higher resolutions.
videoSize = goodEnough.get(0);
for (int i = goodEnough.size() - 1; i >= 0; i--) {
if (goodEnough.get(i).getHeight() <= 1080) {
Expand Down