Update RuntimePermissions sample to latest API.

Include a call to shouldShowRequestPermissionRationale(..).

Change-Id: I9846a2b9858da2d7127d7cf7b37c2615cecc3dda
diff --git a/system/RuntimePermissions/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java b/system/RuntimePermissions/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java
index 43436aa..5f38bad 100644
--- a/system/RuntimePermissions/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java
+++ b/system/RuntimePermissions/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java
@@ -54,6 +54,10 @@
  * {@link Activity#requestPermissions(String[], int)} and the return value checked in {@link
  * Activity#onRequestPermissionsResult(int, String[], int[])}.
  * <p>
+ * Before requesting permissions, {@link Activity#shouldShowRequestPermissionRationale(String)}
+ * should be called to provide the user with additional context for the use of permissions if they
+ * have been denied previously.
+ * <p>
  * If this sample is executed on a device running a platform version below M, all permissions
  * declared
  * in the Android manifest file are always granted at install time and cannot be requested at run
@@ -102,14 +106,26 @@
         // BEGIN_INCLUDE(camera_permission)
         // Check if the Camera permission is already available.
         if (PermissionUtil.hasSelfPermission(this, Manifest.permission.CAMERA)) {
+            // Camera permissions is already available, show the camera preview.
             Log.i(TAG,
                     "CAMERA permission has already been granted. Displaying camera preview.");
-            // Camera permissions is already available, show the camera preview.
             showCameraPreview();
         } else {
+            // Camera permission has not been granted.
             Log.i(TAG, "CAMERA permission has NOT been granted. Requesting permission.");
-            // Camera permission has not been granted. Request it.
-            requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_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.
+            if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
+                Log.i(TAG,
+                        "Displaying camera permission rationale to provide additional context.");
+                Toast.makeText(this, R.string.permission_camera_rationale, Toast.LENGTH_SHORT)
+                        .show();
+            }
+
+            // Request Camera permission
+            requestPermissions(new String[]{Manifest.permission.CAMERA},
+                    REQUEST_CAMERA);
         }
         // END_INCLUDE(camera_permission)
 
@@ -128,7 +144,18 @@
             // Contact permissions have been granted. Show the contacts fragment.
             showContactDetails();
         } else {
+            // Contacts permissions have not been granted.
             Log.i(TAG, "Contact permissions has NOT been granted. Requesting permission.");
+
+            // 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.
+            if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
+                Log.i(TAG,
+                        "Displaying contacts permission rationale to provide additional context.");
+                Toast.makeText(this, R.string.permission_contacts_rationale, Toast.LENGTH_SHORT)
+                        .show();
+            }
+
             // contact permissions has not been granted (read and write contacts). Request them.
             requestPermissions(PERMISSIONS_CONTACT, REQUEST_CONTACTS);
         }
diff --git a/system/RuntimePermissions/Application/src/main/res/values/strings.xml b/system/RuntimePermissions/Application/src/main/res/values/strings.xml
index 941f059..82d7b71 100644
--- a/system/RuntimePermissions/Application/src/main/res/values/strings.xml
+++ b/system/RuntimePermissions/Application/src/main/res/values/strings.xml
@@ -16,4 +16,6 @@
     <string name="permision_available_camera">Camera Permission has been granted. Preview can now be opened.</string>
     <string name="permision_available_contacts">Contacts Permissions have been granted. Contacts screen can now be opened.</string>
     <string name="permissions_not_granted">Permissions were not granted.</string>
-</resources>
\ No newline at end of file
+    <string name="permission_camera_rationale">Camera permission is needed to show the camera preview.</string>
+    <string name="permission_contacts_rationale">Contacts permissions are needed to demonstrate access to the contacts database.</string>
+</resources>
diff --git a/system/RuntimePermissions/template-params.xml b/system/RuntimePermissions/template-params.xml
index 0610712..e33c608 100644
--- a/system/RuntimePermissions/template-params.xml
+++ b/system/RuntimePermissions/template-params.xml
@@ -75,6 +75,10 @@
 Activity#requestPermissions(String[], int) and Fragment#requestPermissions(String[], int).
 Permission requests are sent through Activity#requestPermissions(String[]), and the response
 received in the callback Activity#onRequestPermissionsResult(int, permissions[], int[]).
+Applications can provide an additional rational for the use of permissions after calling
+Activity#shouldShowRequestPermissionRationale(String). This call will return true if the
+application should provide the user with more context on why the requested permissions is needed,
+for example if the permission request has been denied before.
 
 If an application targets an SDK below M, all permissions are granted at runtime and are available
 when the application is running. However, if permissions have been turned off in the system settings