1 /* 2 * Copyright (C) 2015 The Android Open Source Project 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.android.inputmethod.latin.permissions; 18 19 20 import android.app.Activity; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.support.annotation.NonNull; 25 import android.support.v4.app.ActivityCompat; 26 27 /** 28 * An activity to help request permissions. It's used when no other activity is available, e.g. in 29 * InputMethodService. This activity assumes that all permissions are not granted yet. 30 */ 31 public final class PermissionsActivity 32 extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback { 33 34 /** 35 * Key to retrieve requested permissions from the intent. 36 */ 37 public static final String EXTRA_PERMISSION_REQUESTED_PERMISSIONS = "requested_permissions"; 38 39 /** 40 * Key to retrieve request code from the intent. 41 */ 42 public static final String EXTRA_PERMISSION_REQUEST_CODE = "request_code"; 43 44 private static final int INVALID_REQUEST_CODE = -1; 45 46 private int mPendingRequestCode = INVALID_REQUEST_CODE; 47 48 /** 49 * Starts a PermissionsActivity and checks/requests supplied permissions. 50 */ 51 public static void run( 52 @NonNull Context context, int requestCode, @NonNull String... permissionStrings) { 53 Intent intent = new Intent(context.getApplicationContext(), PermissionsActivity.class); 54 intent.putExtra(EXTRA_PERMISSION_REQUESTED_PERMISSIONS, permissionStrings); 55 intent.putExtra(EXTRA_PERMISSION_REQUEST_CODE, requestCode); 56 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 57 context.startActivity(intent); 58 } 59 60 @Override 61 protected void onCreate(Bundle savedInstanceState) { 62 super.onCreate(savedInstanceState); 63 mPendingRequestCode = (savedInstanceState != null) 64 ? savedInstanceState.getInt(EXTRA_PERMISSION_REQUEST_CODE, INVALID_REQUEST_CODE) 65 : INVALID_REQUEST_CODE; 66 } 67 68 @Override 69 protected void onSaveInstanceState(Bundle outState) { 70 super.onSaveInstanceState(outState); 71 outState.putInt(EXTRA_PERMISSION_REQUEST_CODE, mPendingRequestCode); 72 } 73 74 @Override 75 protected void onResume() { 76 super.onResume(); 77 // Only do request when there is no pending request to avoid duplicated requests. 78 if (mPendingRequestCode == INVALID_REQUEST_CODE) { 79 final Bundle extras = getIntent().getExtras(); 80 final String[] permissionsToRequest = 81 extras.getStringArray(EXTRA_PERMISSION_REQUESTED_PERMISSIONS); 82 mPendingRequestCode = extras.getInt(EXTRA_PERMISSION_REQUEST_CODE); 83 // Assuming that all supplied permissions are not granted yet, so that we don't need to 84 // check them again. 85 PermissionsUtil.requestPermissions(this, mPendingRequestCode, permissionsToRequest); 86 } 87 } 88 89 @Override 90 public void onRequestPermissionsResult( 91 int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 92 mPendingRequestCode = INVALID_REQUEST_CODE; 93 PermissionsManager.get(this).onRequestPermissionsResult( 94 requestCode, permissions, grantResults); 95 finish(); 96 } 97 } 98