Merge "Update RuntimePermissions sample to latest API." into mnc-dev
diff --git a/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/MainActivity.java b/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/MainActivity.java
index 9de3b01..4e758a8 100644
--- a/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/MainActivity.java
+++ b/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/MainActivity.java
@@ -86,34 +86,41 @@
private void showCameraPreview() {
// BEGIN_INCLUDE(startCamera)
- if (isMNC()) {
- // On Android M and above, need to check if permission has been granted at runtime.
- if (checkSelfPermission(Manifest.permission.CAMERA)
- == PackageManager.PERMISSION_GRANTED) {
- // Permission is available, start camera preview
- startCamera();
- Toast.makeText(this,
- "Camera permission has already been granted. Starting preview.",
- Toast.LENGTH_SHORT).show();
- } else {
- // Permission has not been granted and must be requested.
- Toast.makeText(this,
- "Permission is not available. Requesting camera permission.",
- Toast.LENGTH_SHORT).show();
- requestPermissions(new String[]{Manifest.permission.CAMERA},
- PERMISSION_REQUEST_CAMERA);
- }
- } else {
- /*
- Below Android M all permissions have already been grated at install time and do not
- need to verified or requested.
- If a permission has been disabled in the system settings, the API will return
- unavailable or empty data instead. */
+ if (!isMNC()) {
+ // Below Android M there is no need to check for runtime permissions
Toast.makeText(this,
"Requested permissions are granted at install time below M and are always "
+ "available at runtime.",
Toast.LENGTH_SHORT).show();
startCamera();
+ return;
+ }
+
+ // Check if the Camera permission has been granted
+ if (checkSelfPermission(Manifest.permission.CAMERA)
+ != PackageManager.PERMISSION_GRANTED) {
+ // Permission has not been granted and must be requested.
+
+ if (shouldShowRequestPermissionRationale(
+ Manifest.permission.CAMERA)) {
+ // Provide an additional rationale to the user if the permission was not granted
+ // and the user would benefit from additional context for the use of the permission.
+ Toast.makeText(this, "Camera access is required to display a camera preview.",
+ Toast.LENGTH_SHORT).show();
+ }
+ Toast.makeText(this,
+ "Permission is not available. Requesting camera permission.",
+ Toast.LENGTH_SHORT).show();
+
+ // Request the permission. The result will be received in onRequestPermissionResult()
+ requestPermissions(new String[]{Manifest.permission.CAMERA},
+ PERMISSION_REQUEST_CAMERA);
+ } else {
+ // Permission is already available, start camera preview
+ startCamera();
+ Toast.makeText(this,
+ "Camera permission is available. Starting preview.",
+ Toast.LENGTH_SHORT).show();
}
// END_INCLUDE(startCamera)
}
diff --git a/system/RuntimePermissionsBasic/template-params.xml b/system/RuntimePermissionsBasic/template-params.xml
index c794d50..25191b1 100644
--- a/system/RuntimePermissionsBasic/template-params.xml
+++ b/system/RuntimePermissionsBasic/template-params.xml
@@ -66,6 +66,7 @@
permissions at runtime.
This sample introduces the basic use of the runtime permissions API by checking for permissions (Activity#checkSelfPermission(String)), requesting permissions (Activity#requestPermissions(String[],int))
and handling the permission request callback (Activity#onRequestPermissionsResult(int, permissions[], int[])).
+An application can display additional context and justification for a permission after calling Activity#shouldShowRequestPermissionRationale(String).
See the "RuntimePermissions" sample for a more complete description and reference implementation.
]]>