diff options
author | Jan-Felix Schmakeit <jfschmakeit@google.com> | 2015-06-30 16:04:31 +1000 |
---|---|---|
committer | Jan-Felix Schmakeit <jfschmakeit@google.com> | 2015-07-02 12:06:06 +1000 |
commit | 9b2b063961464f3ea696b2ca9384834247a8d2c2 (patch) | |
tree | 75c24c7344e77a560e98140e0fa7814b9d4373f1 /system/RuntimePermissionsBasic | |
parent | e032d5034bb8e2f96509e14004fb0dd41b9e9764 (diff) | |
download | android-9b2b063961464f3ea696b2ca9384834247a8d2c2.tar.gz |
Update RuntimePermissionsBasic sample to latest API.
Include a call to shouldShowRequestPermissionRationale(..).
Change-Id: Ifc97077d5b16358e21254f115528ceb8c2b73bab
Diffstat (limited to 'system/RuntimePermissionsBasic')
-rw-r--r-- | system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/MainActivity.java | 53 | ||||
-rw-r--r-- | system/RuntimePermissionsBasic/template-params.xml | 1 |
2 files changed, 31 insertions, 23 deletions
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 9de3b018..4e758a81 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 @@ public class MainActivity extends Activity { 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 c794d50d..25191b13 100644 --- a/system/RuntimePermissionsBasic/template-params.xml +++ b/system/RuntimePermissionsBasic/template-params.xml @@ -66,6 +66,7 @@ Android M introduced runtime permissions. Applications targeting M and above nee 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. ]]> |