blob: 1c11905dc00d000512cb66dbc0d80ab86f9ca6c4 [file] [log] [blame]
Brian Attwell62f80072015-06-23 23:26:27 -07001/*
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
Gary Mai0a49afa2016-12-05 15:53:58 -080017package com.android.contacts.activities;
Brian Attwell62f80072015-06-23 23:26:27 -070018
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.os.Bundle;
24import android.os.Trace;
Nancy Chen054bd1e2015-11-18 18:53:30 -080025import android.support.v4.app.ActivityCompat;
Brian Attwell62f80072015-06-23 23:26:27 -070026
Gary Mai0a49afa2016-12-05 15:53:58 -080027import com.android.contacts.model.AccountTypeManager;
28import com.android.contacts.util.PermissionsUtil;
29
Brian Attwell62f80072015-06-23 23:26:27 -070030import java.util.ArrayList;
31import java.util.Arrays;
32
33/**
Walter Jangbf6fcf92016-07-08 10:06:17 -070034 * Activity that asks the user for all {@link #getPermissions} if any are missing.
Brian Attwell62f80072015-06-23 23:26:27 -070035 *
36 * NOTE: As a result of b/22095159, this can behave oddly in the case where the final permission
37 * you are requesting causes an application restart.
38 */
Nancy Chen054bd1e2015-11-18 18:53:30 -080039public abstract class RequestPermissionsActivityBase extends Activity
40 implements ActivityCompat.OnRequestPermissionsResultCallback {
Walter Jang93ea7282016-02-22 21:24:42 +000041
Brian Attwell62f80072015-06-23 23:26:27 -070042 public static final String PREVIOUS_ACTIVITY_INTENT = "previous_intent";
Walter Jang93ea7282016-02-22 21:24:42 +000043
44 /** Whether the permissions activity was already started. */
guanxiongliufdb68ec2016-05-18 14:42:49 -070045 protected static final String EXTRA_STARTED_PERMISSIONS_ACTIVITY =
46 "started_permissions_activity";
Walter Jang93ea7282016-02-22 21:24:42 +000047
Walter Jang915ccdd2016-10-24 12:18:20 -070048 protected static final String EXTRA_IS_CALLER_SELF = "is_caller_self";
49
Brian Attwell62f80072015-06-23 23:26:27 -070050 private static final int PERMISSIONS_REQUEST_ALL_PERMISSIONS = 1;
51
52 /**
Walter Jangbf6fcf92016-07-08 10:06:17 -070053 * @return list of permissions that are needed in order for {@link #PREVIOUS_ACTIVITY_INTENT}
54 * to operate. You only need to return a single permission per permission group you care about.
Brian Attwell62f80072015-06-23 23:26:27 -070055 */
Walter Jangbf6fcf92016-07-08 10:06:17 -070056 protected abstract String[] getPermissions();
Brian Attwell62f80072015-06-23 23:26:27 -070057
Walter Jang93ea7282016-02-22 21:24:42 +000058 protected Intent mPreviousActivityIntent;
Brian Attwell62f80072015-06-23 23:26:27 -070059
Walter Jang915ccdd2016-10-24 12:18:20 -070060 /** If true then start the target activity "for result" after permissions are granted. */
61 protected boolean mIsCallerSelf;
62
Brian Attwell62f80072015-06-23 23:26:27 -070063 @Override
64 protected void onCreate(Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
66 mPreviousActivityIntent = (Intent) getIntent().getExtras().get(PREVIOUS_ACTIVITY_INTENT);
Walter Jang915ccdd2016-10-24 12:18:20 -070067 mIsCallerSelf = getIntent().getBooleanExtra(EXTRA_IS_CALLER_SELF, false);
Brian Attwell62f80072015-06-23 23:26:27 -070068
69 // Only start a requestPermissions() flow when first starting this activity the first time.
70 // The process is likely to be restarted during the permission flow (necessary to enable
71 // permissions) so this is important to track.
72 if (savedInstanceState == null) {
73 requestPermissions();
74 }
75 }
76
77 /**
78 * If any permissions the Contacts app needs are missing, open an Activity
79 * to prompt the user for these permissions. Moreover, finish the current activity.
80 *
81 * This is designed to be called inside {@link android.app.Activity#onCreate}
82 */
83 protected static boolean startPermissionActivity(Activity activity,
84 String[] requiredPermissions, Class<?> newActivityClass) {
Walter Jang915ccdd2016-10-24 12:18:20 -070085 return startPermissionActivity(activity, requiredPermissions, /* isCallerSelf */ false,
86 newActivityClass);
87 }
88
89 protected static boolean startPermissionActivity(Activity activity,
90 String[] requiredPermissions, boolean isCallerSelf, Class<?> newActivityClass) {
Walter Jang93ea7282016-02-22 21:24:42 +000091 if (!hasPermissions(activity, requiredPermissions)) {
Brian Attwell62f80072015-06-23 23:26:27 -070092 final Intent intent = new Intent(activity, newActivityClass);
guanxiongliufdb68ec2016-05-18 14:42:49 -070093 activity.getIntent().putExtra(EXTRA_STARTED_PERMISSIONS_ACTIVITY, true);
Brian Attwell62f80072015-06-23 23:26:27 -070094 intent.putExtra(PREVIOUS_ACTIVITY_INTENT, activity.getIntent());
Walter Jang915ccdd2016-10-24 12:18:20 -070095 intent.putExtra(EXTRA_IS_CALLER_SELF, isCallerSelf);
Brian Attwell62f80072015-06-23 23:26:27 -070096 activity.startActivity(intent);
97 activity.finish();
98 return true;
99 }
Walter Jang1dd67352015-07-23 11:05:30 -0700100
101 // Account type initialization must be delayed until the Contacts permission group
102 // has been granted (since GET_ACCOUNTS) falls under that groups. Previously it
103 // was initialized in ContactApplication which would cause problems as
104 // AccountManager.getAccounts would return an empty array. See b/22690336
105 AccountTypeManager.getInstance(activity);
106
Brian Attwell62f80072015-06-23 23:26:27 -0700107 return false;
108 }
109
Walter Jang93ea7282016-02-22 21:24:42 +0000110 protected boolean isAllGranted(String permissions[], int[] grantResult) {
Brian Attwell62f80072015-06-23 23:26:27 -0700111 for (int i = 0; i < permissions.length; i++) {
112 if (grantResult[i] != PackageManager.PERMISSION_GRANTED
113 && isPermissionRequired(permissions[i])) {
114 return false;
115 }
116 }
117 return true;
118 }
119
120 private boolean isPermissionRequired(String p) {
Walter Jangbf6fcf92016-07-08 10:06:17 -0700121 return Arrays.asList(getPermissions()).contains(p);
Brian Attwell62f80072015-06-23 23:26:27 -0700122 }
123
124 private void requestPermissions() {
125 Trace.beginSection("requestPermissions");
126 try {
127 // Construct a list of missing permissions
128 final ArrayList<String> unsatisfiedPermissions = new ArrayList<>();
Walter Jangbf6fcf92016-07-08 10:06:17 -0700129 for (String permission : getPermissions()) {
guanxiongliufdb68ec2016-05-18 14:42:49 -0700130 if (!PermissionsUtil.hasPermission(this, permission)) {
Brian Attwell62f80072015-06-23 23:26:27 -0700131 unsatisfiedPermissions.add(permission);
132 }
133 }
134 if (unsatisfiedPermissions.size() == 0) {
135 throw new RuntimeException("Request permission activity was called even"
136 + " though all permissions are satisfied.");
137 }
Nancy Chen054bd1e2015-11-18 18:53:30 -0800138 ActivityCompat.requestPermissions(
139 this,
Brian Attwell62f80072015-06-23 23:26:27 -0700140 unsatisfiedPermissions.toArray(new String[unsatisfiedPermissions.size()]),
141 PERMISSIONS_REQUEST_ALL_PERMISSIONS);
142 } finally {
143 Trace.endSection();
144 }
145 }
146
147 protected static boolean hasPermissions(Context context, String[] permissions) {
148 Trace.beginSection("hasPermission");
149 try {
150 for (String permission : permissions) {
guanxiongliufdb68ec2016-05-18 14:42:49 -0700151 if (!PermissionsUtil.hasPermission(context, permission)) {
Brian Attwell62f80072015-06-23 23:26:27 -0700152 return false;
153 }
154 }
155 return true;
156 } finally {
157 Trace.endSection();
158 }
159 }
160}