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.common.activity;
18 
19 import android.Manifest.permission;
20 import android.app.Activity;
21 import android.content.Intent;
22 import android.os.Bundle;
23 
24 /**
25  * Requests permissions that are not absolutely required by the calling Activity;
26  * if permissions are denied, the calling Activity is still restarted.
27  *
28  * Activities that have a set of permissions that must be granted in order for the Activity to
29  * function propertly should call
30  * {@link RequestPermissionsActivity#startPermissionActivity(Activity, String[], Class)}
31  * before calling {@link RequestDesiredPermissionsActivity#startPermissionActivity(Activity)}.
32  */
33 public class RequestDesiredPermissionsActivity extends RequestPermissionsActivityBase {
34 
35     private static final String[] DESIRED_PERMISSIONS = new String[] {
36             permission.ACCESS_FINE_LOCATION,
37             permission.READ_CALENDAR,
38             permission.READ_SMS,
39     };
40 
41     @Override
getRequiredPermissions()42     protected String[] getRequiredPermissions() {
43         return DESIRED_PERMISSIONS;
44     }
45 
46     @Override
getDesiredPermissions()47     protected String[] getDesiredPermissions() {
48         return DESIRED_PERMISSIONS;
49     }
50 
51     /**
52      * If any desired permissions the Contacts app needs are missing, open an Activity
53      * to prompt the user for these permissions.
54      *
55      * This is designed to be called inside {@link android.app.Activity#onCreate}
56      */
startPermissionActivity(Activity activity)57     public static boolean startPermissionActivity(Activity activity) {
58         final Bundle extras = activity.getIntent().getExtras();
59         if (extras != null && extras.getBoolean(STARTED_PERMISSIONS_ACTIVITY, false)) {
60             return false;
61         }
62         return startPermissionActivity(activity, DESIRED_PERMISSIONS,
63                 RequestDesiredPermissionsActivity.class);
64     }
65 
66     @Override
onRequestPermissionsResult( int requestCode, String permissions[], int[] grantResults)67     public void onRequestPermissionsResult(
68             int requestCode, String permissions[], int[] grantResults) {
69         mPreviousActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
70         startActivity(mPreviousActivityIntent);
71         overridePendingTransition(0, 0);
72 
73         finish();
74         overridePendingTransition(0, 0);
75     }
76 }