Add CameraActivity utility class to automate Camera permission request handling
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package org.opencv.android;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static android.Manifest.permission.CAMERA;
|
||||
|
||||
public class CameraActivity extends Activity {
|
||||
|
||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 200;
|
||||
|
||||
protected List<? extends CameraBridgeViewBase> getCameraViewList() {
|
||||
return new ArrayList<CameraBridgeViewBase>();
|
||||
}
|
||||
|
||||
protected void onCameraPermissionGranted() {
|
||||
List<? extends CameraBridgeViewBase> cameraViews = getCameraViewList();
|
||||
if (cameraViews == null) {
|
||||
return;
|
||||
}
|
||||
for (CameraBridgeViewBase cameraBridgeViewBase: cameraViews) {
|
||||
if (cameraBridgeViewBase != null) {
|
||||
cameraBridgeViewBase.setCameraPermissionGranted();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
boolean havePermission = true;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (checkSelfPermission(CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
requestPermissions(new String[]{CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
|
||||
havePermission = false;
|
||||
}
|
||||
}
|
||||
if (havePermission) {
|
||||
onCameraPermissionGranted();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0
|
||||
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
onCameraPermissionGranted();
|
||||
}
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
protected int mPreviewFormat = RGBA;
|
||||
protected int mCameraIndex = CAMERA_ID_ANY;
|
||||
protected boolean mEnabled;
|
||||
protected boolean mCameraPermissionGranted = false;
|
||||
protected FpsMeter mFpsMeter = null;
|
||||
|
||||
public static final int CAMERA_ID_ANY = -1;
|
||||
@@ -219,9 +220,24 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method is provided for clients, so they can signal camera permission has been granted.
|
||||
* The actual onCameraViewStarted callback will be delivered only after setCameraPermissionGranted
|
||||
* and enableView have been called and surface is available
|
||||
*/
|
||||
public void setCameraPermissionGranted() {
|
||||
synchronized(mSyncObject) {
|
||||
mCameraPermissionGranted = true;
|
||||
checkCurrentState();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method is provided for clients, so they can enable the camera connection.
|
||||
* The actual onCameraViewStarted callback will be delivered only after both this method is called and surface is available
|
||||
* The actual onCameraViewStarted callback will be delivered only after setCameraPermissionGranted
|
||||
* and enableView have been called and surface is available
|
||||
*/
|
||||
public void enableView() {
|
||||
synchronized(mSyncObject) {
|
||||
@@ -300,7 +316,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
|
||||
Log.d(TAG, "call checkCurrentState");
|
||||
int targetState;
|
||||
|
||||
if (mEnabled && mSurfaceExist && getVisibility() == VISIBLE) {
|
||||
if (mEnabled && mCameraPermissionGranted && mSurfaceExist && getVisibility() == VISIBLE) {
|
||||
targetState = STARTED;
|
||||
} else {
|
||||
targetState = STOPPED;
|
||||
|
||||
Reference in New Issue
Block a user