From c0cc969bfe5600590df53c101d492c9caa0a83d4 Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Mon, 23 Jan 2017 12:04:51 -0500 Subject: [PATCH] Automatic permissions --- .../com/flurgle/camerakit/CameraView.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index 93e497ae..ad213461 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -1,6 +1,10 @@ package com.flurgle.camerakit; +import android.Manifest; +import android.app.Activity; import android.content.Context; +import android.content.ContextWrapper; +import android.content.pm.PackageManager; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -9,6 +13,8 @@ import android.os.Parcelable; import android.support.annotation.IntDef; import android.support.annotation.NonNull; 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.os.ParcelableCompat; import android.support.v4.os.ParcelableCompatCreatorCallbacks; @@ -23,6 +29,8 @@ import java.lang.annotation.RetentionPolicy; 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_FRONT = Constants.FACING_FRONT; @@ -60,6 +68,8 @@ public class CameraView extends FrameLayout { private boolean mAdjustViewBounds; + private boolean mWaitingForPermission; + private CameraListener mCameraListener; private DisplayOrientationDetector mDisplayOrientationDetector; @@ -154,7 +164,13 @@ public class CameraView extends FrameLayout { } public void start() { - mCameraImpl.start(); + int permissionCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA); + if (permissionCheck == PackageManager.PERMISSION_GRANTED) { + mWaitingForPermission = false; + mCameraImpl.start(); + } else { + requestCameraPermission(); + } } 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 { @Facing