Sync mnc-dev sample prebuilts
Syncing to //developers/samples/android commmit 2be5f5ca32. Change-Id: Ia08c63655bd122c7fbb0cc3a50eec95d8edcb3f1
This commit is contained in:
@@ -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,51 +99,57 @@ 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();
|
||||
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 {
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
// Permission is already available, start camera preview
|
||||
startCamera();
|
||||
Toast.makeText(this,
|
||||
Snackbar.make(mLayout,
|
||||
"Camera permission is available. Starting preview.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
Snackbar.LENGTH_SHORT).show();
|
||||
startCamera();
|
||||
} else {
|
||||
// Permission is missing and must be requested.
|
||||
requestCameraPermission();
|
||||
}
|
||||
// END_INCLUDE(startCamera)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
} else {
|
||||
Snackbar.make(mLayout,
|
||||
"Permission is not available. Requesting camera permission.",
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void startCamera() {
|
||||
Intent intent = new Intent(this, CameraPreviewActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
public static boolean isMNC() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user