1 /* 2 * Copyright (C) 2015 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.wearable.runtimepermissions; 18 19 import android.Manifest; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.os.Bundle; 23 import android.support.annotation.NonNull; 24 import android.support.v4.app.ActivityCompat; 25 import android.support.v7.app.AppCompatActivity; 26 import android.util.Log; 27 import android.view.View; 28 29 /** 30 * This is a simple splash screen (activity) for giving more details on why the user should approve 31 * phone permissions for storage. If they choose to move forward, the permission screen 32 * is brought up. Either way (approve or disapprove), this will exit to the MainPhoneActivity after 33 * they are finished with their final decision. 34 * 35 * If this activity is started by our service (IncomingRequestPhoneService) it is marked via an 36 * extra (MainPhoneActivity.EXTRA_PROMPT_PERMISSION_FROM_WEAR). That service only starts 37 * this activity if the phone permission hasn't been approved for the data wear is trying to access. 38 * When the user decides within this Activity what to do with the permission request, it closes and 39 * opens the MainPhoneActivity (to maintain the app experience). It also again passes along the same 40 * extra (MainPhoneActivity.EXTRA_PROMPT_PERMISSION_FROM_WEAR) to alert MainPhoneActivity to 41 * send the results of the user's decision to the wear device. 42 */ 43 public class PhonePermissionRequestActivity extends AppCompatActivity implements 44 ActivityCompat.OnRequestPermissionsResultCallback { 45 46 private static final String TAG = "PhoneRationale"; 47 48 /* Id to identify Location permission request. */ 49 private static final int PERMISSION_REQUEST_READ_STORAGE = 1; 50 51 @Override onCreate(Bundle savedInstanceState)52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 55 // If permissions granted, we start the main activity (shut this activity down). 56 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) 57 == PackageManager.PERMISSION_GRANTED) { 58 startMainActivity(); 59 } 60 61 setContentView(R.layout.activity_phone_permission_request); 62 } 63 onClickApprovePermissionRequest(View view)64 public void onClickApprovePermissionRequest(View view) { 65 Log.d(TAG, "onClickApprovePermissionRequest()"); 66 67 // On 23+ (M+) devices, External storage permission not granted. Request permission. 68 ActivityCompat.requestPermissions( 69 this, 70 new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 71 PERMISSION_REQUEST_READ_STORAGE); 72 } 73 onClickDenyPermissionRequest(View view)74 public void onClickDenyPermissionRequest(View view) { 75 Log.d(TAG, "onClickDenyPermissionRequest()"); 76 startMainActivity(); 77 } 78 79 /* 80 * Callback received when a permissions request has been completed. 81 */ 82 @Override onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)83 public void onRequestPermissionsResult( 84 int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 85 86 String permissionResult = "Request code: " + requestCode + ", Permissions: " + permissions 87 + ", Results: " + grantResults; 88 Log.d(TAG, "onRequestPermissionsResult(): " + permissionResult); 89 90 if (requestCode == PERMISSION_REQUEST_READ_STORAGE) { 91 // Close activity regardless of user's decision (decision picked up in main activity). 92 startMainActivity(); 93 } 94 } 95 startMainActivity()96 private void startMainActivity() { 97 98 Intent mainActivityIntent = new Intent(this, MainPhoneActivity.class); 99 100 /* 101 * If service started this Activity (b/c wear requested data where permissions were not 102 * approved), tells MainPhoneActivity to send results to wear device (via this extra). 103 */ 104 boolean serviceStartedActivity = getIntent().getBooleanExtra( 105 MainPhoneActivity.EXTRA_PROMPT_PERMISSION_FROM_WEAR, false); 106 107 if (serviceStartedActivity) { 108 mainActivityIntent.putExtra( 109 MainPhoneActivity.EXTRA_PROMPT_PERMISSION_FROM_WEAR, true); 110 } 111 112 startActivity(mainActivityIntent); 113 } 114 }