aboutsummaryrefslogtreecommitdiff
path: root/system/RuntimePermissionsBasic
diff options
context:
space:
mode:
authorJan-Felix Schmakeit <jfschmakeit@google.com>2015-08-04 18:18:27 +1000
committerJan-Felix Schmakeit <jfschmakeit@google.com>2015-08-17 18:42:44 +1000
commit06c615997a9e3ef2efd1948b515ba5471c699e77 (patch)
treeab526a82a4a1194e4f2bb529d74a2a15fc23d10e /system/RuntimePermissionsBasic
parent6ab42c36e877ca920212b0a1586268a2e855c2e3 (diff)
downloadandroid-06c615997a9e3ef2efd1948b515ba5471c699e77.tar.gz
Update BasicRuntimePermissions sample for API 23.
Use compat calls from v4 support library for all permission checks and requests and lower minSDK to 15. Replace use of Toast with SnackBar. Change-Id: Id126adfa46c71e1e61c992133ad90ab06dee7de4
Diffstat (limited to 'system/RuntimePermissionsBasic')
-rw-r--r--system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/MainActivity.java109
-rw-r--r--system/RuntimePermissionsBasic/Application/src/main/res/layout/activity_main.xml37
-rw-r--r--system/RuntimePermissionsBasic/template-params.xml18
3 files changed, 94 insertions, 70 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 9fa646d7..f534d407 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
@@ -20,13 +20,15 @@ import com.example.android.basicpermissions.camera.CameraPreviewActivity;
import android.Manifest;
import android.app.Activity;
+import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
-import android.os.Build;
import android.os.Bundle;
+import android.support.design.widget.Snackbar;
+import android.support.v4.app.ActivityCompat;
+import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
-import android.widget.Toast;
/**
* Launcher Activity that demonstrates the use of runtime permissions for Android M.
@@ -36,22 +38,32 @@ import android.widget.Toast;
* the permission has been granted.
* <p>
* First, the status of the Camera permission is checked using {@link
- * Activity#checkSelfPermission(String)}.
+ * ActivityCompat#checkSelfPermission(Context, String)}
* If it has not been granted ({@link PackageManager#PERMISSION_GRANTED}), it is requested by
* calling
- * {@link Activity#requestPermissions(String[], int)}. The result of the request is returned in
- * {@link Activity#onRequestPermissionsResult(int, String[], int[])}, which starts {@link
- * CameraPreviewActivity}
- * if the permission has been granted.
+ * {@link ActivityCompat#requestPermissions(Activity, String[], int)}. The result of the request is
+ * returned to the
+ * {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}, which starts
+ * {@link
+ * CameraPreviewActivity} if the permission has been granted.
+ * <p>
+ * Note that there is no need to check the API level, the support library
+ * already takes care of this. Similar helper methods for permissions are also available in
+ * ({@link ActivityCompat},
+ * {@link android.support.v4.content.ContextCompat} and {@link android.support.v4.app.Fragment}).
*/
-public class MainActivity extends Activity {
+public class MainActivity extends AppCompatActivity
+ implements ActivityCompat.OnRequestPermissionsResultCallback {
private static final int PERMISSION_REQUEST_CAMERA = 0;
+ private View mLayout;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
+ mLayout = findViewById(R.id.main_layout);
// Register a listener for the 'Show Camera Preview' button.
Button b = (Button) findViewById(R.id.button_open_camera);
@@ -69,15 +81,16 @@ public class MainActivity extends Activity {
// BEGIN_INCLUDE(onRequestPermissionsResult)
if (requestCode == PERMISSION_REQUEST_CAMERA) {
// Request for camera permission.
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
+ if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission has been granted. Start camera preview Activity.
- Toast.makeText(this, "Camera permission was granted. Starting preview.",
- Toast.LENGTH_SHORT)
+ Snackbar.make(mLayout, "Camera permission was granted. Starting preview.",
+ Snackbar.LENGTH_SHORT)
.show();
startCamera();
} else {
// Permission request was denied.
- Toast.makeText(this, "Camera permission request was denied.", Toast.LENGTH_SHORT)
+ Snackbar.make(mLayout, "Camera permission request was denied.",
+ Snackbar.LENGTH_SHORT)
.show();
}
}
@@ -86,43 +99,52 @@ public class MainActivity extends Activity {
private void showCameraPreview() {
// BEGIN_INCLUDE(startCamera)
- 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();
+ // Check if the Camera permission has been granted
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
+ == PackageManager.PERMISSION_GRANTED) {
+ // Permission is already available, start camera preview
+ Snackbar.make(mLayout,
+ "Camera permission is available. Starting preview.",
+ Snackbar.LENGTH_SHORT).show();
startCamera();
- return;
+ } else {
+ // Permission is missing and must be requested.
+ requestCameraPermission();
}
+ // END_INCLUDE(startCamera)
+ }
- // 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.
+ /**
+ * Requests the {@link android.Manifest.permission#CAMERA} permission.
+ * If an additional rationale should be displayed, the user has to launch the request from
+ * a SnackBar that includes additional information.
+ */
+ private void requestCameraPermission() {
+ // Permission has not been granted and must be requested.
+ if (ActivityCompat.shouldShowRequestPermissionRationale(this,
+ 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.
+ // Display a SnackBar with a button to request the missing permission.
+ Snackbar.make(mLayout, "Camera access is required to display the camera preview.",
+ Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ // Request the permission
+ ActivityCompat.requestPermissions(MainActivity.this,
+ new String[]{Manifest.permission.CAMERA},
+ PERMISSION_REQUEST_CAMERA);
+ }
+ }).show();
- 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,
+ } else {
+ Snackbar.make(mLayout,
"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},
+ Snackbar.LENGTH_SHORT).show();
+ // Request the permission. The result will be received in onRequestPermissionResult().
+ ActivityCompat.requestPermissions(this, 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)
}
private void startCamera() {
@@ -130,7 +152,4 @@ public class MainActivity extends Activity {
startActivity(intent);
}
- public static boolean isMNC() {
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
- }
}
diff --git a/system/RuntimePermissionsBasic/Application/src/main/res/layout/activity_main.xml b/system/RuntimePermissionsBasic/Application/src/main/res/layout/activity_main.xml
index c3bf99c5..146b8b1e 100644
--- a/system/RuntimePermissionsBasic/Application/src/main/res/layout/activity_main.xml
+++ b/system/RuntimePermissionsBasic/Application/src/main/res/layout/activity_main.xml
@@ -14,27 +14,28 @@
limitations under the License.
-->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/horizontal_page_margin"
- android:paddingRight="@dimen/horizontal_page_margin"
- android:paddingTop="@dimen/vertical_page_margin"
- android:paddingBottom="@dimen/vertical_page_margin"
- android:orientation="vertical"
- tools:context=".MainActivity">
+<LinearLayout android:id="@+id/main_layout"
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical"
+ android:paddingBottom="@dimen/vertical_page_margin"
+ android:paddingLeft="@dimen/horizontal_page_margin"
+ android:paddingRight="@dimen/horizontal_page_margin"
+ android:paddingTop="@dimen/vertical_page_margin"
+ tools:context=".MainActivity">
<TextView
- android:text="@string/intro"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="@dimen/horizontal_page_margin"/>
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="@dimen/horizontal_page_margin"
+ android:text="@string/intro" />
<Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Open Camera Preview"
- android:id="@+id/button_open_camera"/>
+ android:id="@+id/button_open_camera"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Open Camera Preview" />
</LinearLayout>
diff --git a/system/RuntimePermissionsBasic/template-params.xml b/system/RuntimePermissionsBasic/template-params.xml
index 7794fda0..7b078936 100644
--- a/system/RuntimePermissionsBasic/template-params.xml
+++ b/system/RuntimePermissionsBasic/template-params.xml
@@ -19,14 +19,15 @@
<group>System</group>
<package>com.example.android.basicpermissions</package>
- <minSdk>23</minSdk>
+ <minSdk>15</minSdk>
- <dependency>com.android.support:appcompat-v7:21.+</dependency>
+ <dependency>com.android.support:appcompat-v7:23.0.0</dependency>
+ <dependency>com.android.support:support-v4:23.0.0</dependency>
+ <dependency>com.android.support:design:23.0.0</dependency>
<strings>
<intro>
<![CDATA[
- This sample shows runtime permissions available in the Android M and above.
This sample shows a basic implementation for requesting permissions at runtime. Click the button to request the Camera permission and open a full-screen camera preview.
Note: The "RuntimePermissions" sample provides a more complete overview over the runtime permission features available.
]]>
@@ -54,7 +55,8 @@ Note: The "RuntimePermissions" sample provides a more complete overview over the
<description>
<![CDATA[
This basic sample shows runtime permissions available in the Android M and above.
-It shows how to use the new runtime permission API to check for and request permissions.
+It shows how to use the new runtime permissions API to check and request permissions through the
+support library.
]]>
</description>
@@ -62,9 +64,11 @@ It shows how to use the new runtime permission API to check for and request perm
<![CDATA[
Android M introduced runtime permissions. Applications targeting M and above need to request their
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).
+This sample introduces the basic use of the runtime permissions API through the support library by
+verifying permissions (ActivityCompat#checkSelfPermission(Context, String)), requesting permissions (ActivityCompat#requestPermissions(Activity, String[], int))
+and handling the permission request callback (ActivityCompat.OnRequestPermissionsResultCallback).
+An application can display additional context and justification for a permission after calling
+ActivityCompat#shouldShowRequestPermissionRationale#shouldShowRequestPermissionRationale(Activity, String).
See the "RuntimePermissions" sample for a more complete description and reference implementation.
]]>