Skip to content

Commit

Permalink
Handle runtime permission in the demo app
Browse files Browse the repository at this point in the history
Also move a test layout resource to where it should have been.

Change-Id: Ifdc7868bd86defb71324f5f6d500eff999856043
  • Loading branch information
yaraki committed May 30, 2016
1 parent 6befea4 commit 7ddd7f9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@

import com.google.android.cameraview.CameraView;

import android.Manifest;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements
ActivityCompat.OnRequestPermissionsResultCallback {

private static final String TAG = "MainActivity";
private static final int REQUEST_CAMERA_PERMISSION = 1;
private static final String FRAGMENT_DIALOG = "dialog";

private CameraView mCameraView;

Expand All @@ -41,7 +54,16 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onResume() {
super.onResume();
mCameraView.start();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
mCameraView.start();
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
new ConfirmationDialogFragment().show(getSupportFragmentManager(), FRAGMENT_DIALOG);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
}

@Override
Expand All @@ -50,6 +72,20 @@ protected void onPause() {
super.onPause();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (permissions.length != 1 || grantResults.length != 1) {
throw new RuntimeException("Error on requesting permission.");
}
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, R.string.permission_not_granted, Toast.LENGTH_SHORT).show();
// No need to start camera here; it is handled by onResume
}
}
}

private CameraView.Callback mCallback
= new CameraView.Callback() {

Expand All @@ -65,4 +101,33 @@ public void onCameraClosed(CameraView cameraView) {

};

public static class ConfirmationDialogFragment extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage(R.string.permission_confirmation)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), R.string.permission_not_granted,
Toast.LENGTH_SHORT).show();
}
})
.create();
}

}

}
2 changes: 2 additions & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
-->
<resources>
<string name="app_name">CameraView Demo</string>
<string name="permission_confirmation">This app demonstrates the usage of CameraView. In order to do that, it needs permission to access camera.</string>
<string name="permission_not_granted">Camera app cannot do anything without camera permission.</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Camera1 extends CameraViewImpl {

private int mDisplayOrientation;

private boolean mShowingPreview;

private static class PreviewInfo {
SurfaceTexture surface;
int width;
Expand Down Expand Up @@ -113,12 +115,16 @@ void start() {
if (mPreviewInfo.surface != null) {
setUpPreview();
}
mShowingPreview = true;
mCamera.startPreview();
}

@Override
void stop() {
mCamera.stopPreview();
if (mCamera != null) {
mCamera.stopPreview();
}
mShowingPreview = false;
releaseCamera();
}

Expand Down Expand Up @@ -220,8 +226,17 @@ private void adjustPreviewSize() {
mAspectRatio = chooseAspectRatio();
}
Size size = chooseOptimalSize(sizes);
mCameraParameters.setPreviewSize(size.getWidth(), size.getHeight());
mCamera.setParameters(mCameraParameters);
final Camera.Size currentSize = mCameraParameters.getPictureSize();
if (currentSize.width != size.getWidth() || currentSize.height != size.getHeight()) {
if (mShowingPreview) {
mCamera.stopPreview();
}
mCameraParameters.setPreviewSize(size.getWidth(), size.getHeight());
mCamera.setParameters(mCameraParameters);
if (mShowingPreview) {
mCamera.startPreview();
}
}
}

@SuppressWarnings("SuspiciousNameCombination")
Expand Down

0 comments on commit 7ddd7f9

Please sign in to comment.