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