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.contacts.activities;
18 
19 import android.Manifest.permission;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.widget.Toast;
25 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
26 import com.android.contacts.R;
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * Activity that requests permissions needed for activities exported from Contacts.
32  */
33 public class RequestPermissionsActivity extends RequestPermissionsActivityBase {
34 
35     public static final String BROADCAST_PERMISSIONS_GRANTED = "broadcastPermissionsGranted";
36 
37     private static String[] sRequiredPermissions;
38 
39     @Override
getPermissions()40     protected String[] getPermissions() {
41         return getPermissions(getPackageManager());
42     }
43 
44     /**
45      * Method to check if the required permissions are given.
46      */
hasRequiredPermissions(Context context)47     public static boolean hasRequiredPermissions(Context context) {
48         return hasPermissions(context, getPermissions(context.getPackageManager()));
49     }
50 
startPermissionActivityIfNeeded(Activity activity)51     public static boolean startPermissionActivityIfNeeded(Activity activity) {
52         return startPermissionActivity(activity,
53                 getPermissions(activity.getPackageManager()),
54                 RequestPermissionsActivity.class);
55     }
56 
getPermissions(PackageManager packageManager)57     private static String[] getPermissions(PackageManager packageManager) {
58         if (sRequiredPermissions == null) {
59             final List<String> permissions = new ArrayList<>();
60             // Contacts group
61             permissions.add(permission.GET_ACCOUNTS);
62             permissions.add(permission.READ_CONTACTS);
63             permissions.add(permission.WRITE_CONTACTS);
64 
65             if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
66                 // Phone group
67                 // These are only used in a few places such as QuickContactActivity and
68                 // ImportExportDialogFragment.  We work around missing this permission when
69                 // telephony is not available on the device (i.e. on tablets).
70                 permissions.add(permission.CALL_PHONE);
71                 permissions.add(permission.READ_PHONE_NUMBERS);
72                 permissions.add(permission.READ_PHONE_STATE);
73                 permissions.add(permission.READ_CALL_LOG);
74             }
75             sRequiredPermissions = permissions.toArray(new String[0]);
76         }
77         return sRequiredPermissions;
78     }
79 
80     @Override
onRequestPermissionsResult( int requestCode, String permissions[], int[] grantResults)81     public void onRequestPermissionsResult(
82             int requestCode, String permissions[], int[] grantResults) {
83         if (permissions != null && permissions.length > 0
84                 && isAllGranted(permissions, grantResults)) {
85             mPreviousActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
86             if (mIsCallerSelf) {
87                 startActivityForResult(mPreviousActivityIntent, 0);
88             } else {
89                 startActivity(mPreviousActivityIntent);
90             }
91             finish();
92             overridePendingTransition(0, 0);
93 
94             LocalBroadcastManager.getInstance(this).sendBroadcast(
95                     new Intent(BROADCAST_PERMISSIONS_GRANTED));
96         } else {
97             Toast.makeText(this, R.string.missing_required_permission, Toast.LENGTH_SHORT).show();
98             finish();
99         }
100     }
101 }
102