Automatic permissions

pull/1/head
Dylan McIntyre 8 years ago
parent 0534a3358f
commit c0cc969bfe
  1. 42
      camerakit/src/main/java/com/flurgle/camerakit/CameraView.java

@ -1,6 +1,10 @@
package com.flurgle.camerakit; package com.flurgle.camerakit;
import android.Manifest;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -9,6 +13,8 @@ import android.os.Parcelable;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.hardware.display.DisplayManagerCompat; import android.support.v4.hardware.display.DisplayManagerCompat;
import android.support.v4.os.ParcelableCompat; import android.support.v4.os.ParcelableCompat;
import android.support.v4.os.ParcelableCompatCreatorCallbacks; import android.support.v4.os.ParcelableCompatCreatorCallbacks;
@ -23,6 +29,8 @@ import java.lang.annotation.RetentionPolicy;
public class CameraView extends FrameLayout { public class CameraView extends FrameLayout {
private static final int PERMISSION_REQUEST_CAMERA = 16;
public static final int FACING_BACK = Constants.FACING_BACK; public static final int FACING_BACK = Constants.FACING_BACK;
public static final int FACING_FRONT = Constants.FACING_FRONT; public static final int FACING_FRONT = Constants.FACING_FRONT;
@ -60,6 +68,8 @@ public class CameraView extends FrameLayout {
private boolean mAdjustViewBounds; private boolean mAdjustViewBounds;
private boolean mWaitingForPermission;
private CameraListener mCameraListener; private CameraListener mCameraListener;
private DisplayOrientationDetector mDisplayOrientationDetector; private DisplayOrientationDetector mDisplayOrientationDetector;
@ -154,7 +164,13 @@ public class CameraView extends FrameLayout {
} }
public void start() { public void start() {
int permissionCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
mWaitingForPermission = false;
mCameraImpl.start(); mCameraImpl.start();
} else {
requestCameraPermission();
}
} }
public void stop() { public void stop() {
@ -237,6 +253,32 @@ public class CameraView extends FrameLayout {
} }
private void requestCameraPermission() {
Activity activity = null;
Context context = getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
activity = (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
if (activity != null) {
ActivityCompat.requestPermissions(activity, new String[]{ Manifest.permission.CAMERA }, PERMISSION_REQUEST_CAMERA);
mWaitingForPermission = true;
}
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if (mWaitingForPermission) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
start();
}
}
}
protected static class SavedState extends BaseSavedState { protected static class SavedState extends BaseSavedState {
@Facing @Facing

Loading…
Cancel
Save