• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STATE);
72             }
73             sRequiredPermissions = permissions.toArray(new String[0]);
74         }
75         return sRequiredPermissions;
76     }
77 
78     @Override
onRequestPermissionsResult( int requestCode, String permissions[], int[] grantResults)79     public void onRequestPermissionsResult(
80             int requestCode, String permissions[], int[] grantResults) {
81         if (permissions != null && permissions.length > 0
82                 && isAllGranted(permissions, grantResults)) {
83             mPreviousActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
84             if (mIsCallerSelf) {
85                 startActivityForResult(mPreviousActivityIntent, 0);
86             } else {
87                 startActivity(mPreviousActivityIntent);
88             }
89             finish();
90             overridePendingTransition(0, 0);
91 
92             LocalBroadcastManager.getInstance(this).sendBroadcast(
93                     new Intent(BROADCAST_PERMISSIONS_GRANTED));
94         } else {
95             Toast.makeText(this, R.string.missing_required_permission, Toast.LENGTH_SHORT).show();
96             finish();
97         }
98     }
99 }
100