Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 3ce4c34

Browse files
Auto-update
1 parent b8e5e3a commit 3ce4c34

File tree

14 files changed

+46
-7
lines changed

14 files changed

+46
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#Fri May 22 14:58:06 PDT 2015
17 Bytes
Binary file not shown.
81.9 KB
Binary file not shown.
287 KB
Binary file not shown.
18.6 KB
Binary file not shown.
24.3 KB
Binary file not shown.

Application/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.1.0'
8+
classpath 'com.android.tools.build:gradle:1.2.0'
99
}
1010
}
1111

Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
* {@link Activity#requestPermissions(String[], int)} and the return value checked in {@link
5555
* Activity#onRequestPermissionsResult(int, String[], int[])}.
5656
* <p>
57+
* Before requesting permissions, {@link Activity#shouldShowRequestPermissionRationale(String)}
58+
* should be called to provide the user with additional context for the use of permissions if they
59+
* have been denied previously.
60+
* <p>
5761
* If this sample is executed on a device running a platform version below M, all permissions
5862
* declared
5963
* in the Android manifest file are always granted at install time and cannot be requested at run
@@ -102,14 +106,26 @@ public void showCamera(View view) {
102106
// BEGIN_INCLUDE(camera_permission)
103107
// Check if the Camera permission is already available.
104108
if (PermissionUtil.hasSelfPermission(this, Manifest.permission.CAMERA)) {
109+
// Camera permissions is already available, show the camera preview.
105110
Log.i(TAG,
106111
"CAMERA permission has already been granted. Displaying camera preview.");
107-
// Camera permissions is already available, show the camera preview.
108112
showCameraPreview();
109113
} else {
114+
// Camera permission has not been granted.
110115
Log.i(TAG, "CAMERA permission has NOT been granted. Requesting permission.");
111-
// Camera permission has not been granted. Request it.
112-
requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
116+
117+
// Provide an additional rationale to the user if the permission was not granted
118+
// and the user would benefit from additional context for the use of the permission.
119+
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
120+
Log.i(TAG,
121+
"Displaying camera permission rationale to provide additional context.");
122+
Toast.makeText(this, R.string.permission_camera_rationale, Toast.LENGTH_SHORT)
123+
.show();
124+
}
125+
126+
// Request Camera permission
127+
requestPermissions(new String[]{Manifest.permission.CAMERA},
128+
REQUEST_CAMERA);
113129
}
114130
// END_INCLUDE(camera_permission)
115131

@@ -128,7 +144,18 @@ public void showContacts(View v) {
128144
// Contact permissions have been granted. Show the contacts fragment.
129145
showContactDetails();
130146
} else {
147+
// Contacts permissions have not been granted.
131148
Log.i(TAG, "Contact permissions has NOT been granted. Requesting permission.");
149+
150+
// Provide an additional rationale to the user if the permission was not granted
151+
// and the user would benefit from additional context for the use of the permission.
152+
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
153+
Log.i(TAG,
154+
"Displaying contacts permission rationale to provide additional context.");
155+
Toast.makeText(this, R.string.permission_contacts_rationale, Toast.LENGTH_SHORT)
156+
.show();
157+
}
158+
132159
// contact permissions has not been granted (read and write contacts). Request them.
133160
requestPermissions(PERMISSIONS_CONTACT, REQUEST_CONTACTS);
134161
}

Application/src/main/java/com/example/android/system/runtimepermissions/camera/CameraPreviewFragment.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
6161

6262
// Open an instance of the first camera and retrieve its info.
6363
mCamera = getCameraInstance(CAMERA_ID);
64-
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
65-
Camera.getCameraInfo(CAMERA_ID, cameraInfo);
64+
Camera.CameraInfo cameraInfo = null;
65+
66+
if (mCamera != null) {
67+
// Get camera info only if the camera is available
68+
cameraInfo = new Camera.CameraInfo();
69+
Camera.getCameraInfo(CAMERA_ID, cameraInfo);
70+
}
6671

6772
if (mCamera == null || cameraInfo == null) {
6873
// Camera is not available, display error message

Application/src/main/res/values/strings.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
<string name="permision_available_camera">Camera Permission has been granted. Preview can now be opened.</string>
1717
<string name="permision_available_contacts">Contacts Permissions have been granted. Contacts screen can now be opened.</string>
1818
<string name="permissions_not_granted">Permissions were not granted.</string>
19-
</resources>
19+
<string name="permission_camera_rationale">Camera permission is needed to show the camera preview.</string>
20+
<string name="permission_contacts_rationale">Contacts permissions are needed to demonstrate access to the contacts database.</string>
21+
</resources>

0 commit comments

Comments
 (0)