1 /*
2  * Copyright (C) 2016 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.Intent;
22 import android.content.pm.PackageManager;
23 import android.os.Bundle;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * Requests permissions that are not absolutely required by the calling Activity;
30  * if permissions are denied, the calling Activity is still restarted.
31  *
32  * Activities that have a set of permissions that must be granted in order for the Activity to
33  * function propertly should call
34  * {@link RequestPermissionsActivity#startPermissionActivity(Activity, String[], Class)}
35  * before calling {@link RequestDesiredPermissionsActivity#startPermissionActivity(Activity)}.
36  */
37 public class RequestDesiredPermissionsActivity extends RequestPermissionsActivityBase {
38 
39     private static String[] sDesiredPermissions;
40 
41     @Override
getPermissions()42     protected String[] getPermissions() {
43         return getPermissions(getPackageManager());
44     }
45 
46     /**
47      * If any desired permission that Contacts app needs are missing, open an Activity
48      * to prompt user for these permissions. After that calling activity is restarted
49      * and in the second run permission check is skipped.
50      *
51      * This is designed to be called inside {@link android.app.Activity#onCreate}
52      */
startPermissionActivity(Activity activity)53     public static boolean startPermissionActivity(Activity activity) {
54         final Bundle extras = activity.getIntent().getExtras();
55         if (extras != null && extras.getBoolean(EXTRA_STARTED_PERMISSIONS_ACTIVITY, false)) {
56             return false;
57         }
58         return startPermissionActivity(activity,
59                 getPermissions(activity.getPackageManager()),
60                 RequestDesiredPermissionsActivity.class);
61     }
62 
getPermissions(PackageManager packageManager)63     private static String[] getPermissions(PackageManager packageManager) {
64         if (sDesiredPermissions == null) {
65             final List<String> permissions = new ArrayList<>();
66             // Calendar group
67             permissions.add(permission.READ_CALENDAR);
68 
69             if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
70                 // SMS group
71                 permissions.add(permission.READ_SMS);
72             }
73             sDesiredPermissions = permissions.toArray(new String[0]);
74         }
75         return sDesiredPermissions;
76     }
77 
78     @Override
onRequestPermissionsResult( int requestCode, String permissions[], int[] grantResults)79     public void onRequestPermissionsResult(
80             int requestCode, String permissions[], int[] grantResults) {
81         mPreviousActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
82         startActivity(mPreviousActivityIntent);
83         overridePendingTransition(0, 0);
84 
85         finish();
86         overridePendingTransition(0, 0);
87     }
88 }