1 /* 2 * Copyright 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.basicpermissions; 18 19 import com.example.android.basicpermissions.camera.CameraPreviewActivity; 20 21 import android.Manifest; 22 import android.app.Activity; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.os.Bundle; 27 import android.support.design.widget.Snackbar; 28 import android.support.v4.app.ActivityCompat; 29 import android.support.v7.app.AppCompatActivity; 30 import android.view.View; 31 import android.widget.Button; 32 33 /** 34 * Launcher Activity that demonstrates the use of runtime permissions for Android M. 35 * This Activity requests permissions to access the camera 36 * ({@link android.Manifest.permission#CAMERA}) 37 * when the 'Show Camera Preview' button is clicked to start {@link CameraPreviewActivity} once 38 * the permission has been granted. 39 * <p> 40 * First, the status of the Camera permission is checked using {@link 41 * ActivityCompat#checkSelfPermission(Context, String)} 42 * If it has not been granted ({@link PackageManager#PERMISSION_GRANTED}), it is requested by 43 * calling 44 * {@link ActivityCompat#requestPermissions(Activity, String[], int)}. The result of the request is 45 * returned to the 46 * {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}, which starts 47 * {@link 48 * CameraPreviewActivity} if the permission has been granted. 49 * <p> 50 * Note that there is no need to check the API level, the support library 51 * already takes care of this. Similar helper methods for permissions are also available in 52 * ({@link ActivityCompat}, 53 * {@link android.support.v4.content.ContextCompat} and {@link android.support.v4.app.Fragment}). 54 */ 55 public class MainActivity extends AppCompatActivity 56 implements ActivityCompat.OnRequestPermissionsResultCallback { 57 58 private static final int PERMISSION_REQUEST_CAMERA = 0; 59 60 private View mLayout; 61 62 @Override onCreate(Bundle savedInstanceState)63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 setContentView(R.layout.activity_main); 66 mLayout = findViewById(R.id.main_layout); 67 68 // Register a listener for the 'Show Camera Preview' button. 69 Button b = (Button) findViewById(R.id.button_open_camera); 70 b.setOnClickListener(new View.OnClickListener() { 71 @Override 72 public void onClick(View view) { 73 showCameraPreview(); 74 } 75 }); 76 } 77 78 @Override onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)79 public void onRequestPermissionsResult(int requestCode, String[] permissions, 80 int[] grantResults) { 81 // BEGIN_INCLUDE(onRequestPermissionsResult) 82 if (requestCode == PERMISSION_REQUEST_CAMERA) { 83 // Request for camera permission. 84 if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 85 // Permission has been granted. Start camera preview Activity. 86 Snackbar.make(mLayout, "Camera permission was granted. Starting preview.", 87 Snackbar.LENGTH_SHORT) 88 .show(); 89 startCamera(); 90 } else { 91 // Permission request was denied. 92 Snackbar.make(mLayout, "Camera permission request was denied.", 93 Snackbar.LENGTH_SHORT) 94 .show(); 95 } 96 } 97 // END_INCLUDE(onRequestPermissionsResult) 98 } 99 showCameraPreview()100 private void showCameraPreview() { 101 // BEGIN_INCLUDE(startCamera) 102 // Check if the Camera permission has been granted 103 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) 104 == PackageManager.PERMISSION_GRANTED) { 105 // Permission is already available, start camera preview 106 Snackbar.make(mLayout, 107 "Camera permission is available. Starting preview.", 108 Snackbar.LENGTH_SHORT).show(); 109 startCamera(); 110 } else { 111 // Permission is missing and must be requested. 112 requestCameraPermission(); 113 } 114 // END_INCLUDE(startCamera) 115 } 116 117 /** 118 * Requests the {@link android.Manifest.permission#CAMERA} permission. 119 * If an additional rationale should be displayed, the user has to launch the request from 120 * a SnackBar that includes additional information. 121 */ requestCameraPermission()122 private void requestCameraPermission() { 123 // Permission has not been granted and must be requested. 124 if (ActivityCompat.shouldShowRequestPermissionRationale(this, 125 Manifest.permission.CAMERA)) { 126 // Provide an additional rationale to the user if the permission was not granted 127 // and the user would benefit from additional context for the use of the permission. 128 // Display a SnackBar with a button to request the missing permission. 129 Snackbar.make(mLayout, "Camera access is required to display the camera preview.", 130 Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() { 131 @Override 132 public void onClick(View view) { 133 // Request the permission 134 ActivityCompat.requestPermissions(MainActivity.this, 135 new String[]{Manifest.permission.CAMERA}, 136 PERMISSION_REQUEST_CAMERA); 137 } 138 }).show(); 139 140 } else { 141 Snackbar.make(mLayout, 142 "Permission is not available. Requesting camera permission.", 143 Snackbar.LENGTH_SHORT).show(); 144 // Request the permission. The result will be received in onRequestPermissionResult(). 145 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 146 PERMISSION_REQUEST_CAMERA); 147 } 148 } 149 startCamera()150 private void startCamera() { 151 Intent intent = new Intent(this, CameraPreviewActivity.class); 152 startActivity(intent); 153 } 154 155 } 156