Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 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; |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
Walter Jang | 58dc5be | 2016-02-27 07:04:33 -0800 | [diff] [blame] | 23 | import android.os.Bundle; |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 24 | |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 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 | |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 39 | private static String[] sDesiredPermissions; |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 40 | |
| 41 | @Override |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 42 | protected String[] getPermissions() { |
| 43 | return getPermissions(getPackageManager()); |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
guanxiongliu | fdb68ec | 2016-05-18 14:42:49 -0700 | [diff] [blame] | 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. |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 50 | * |
| 51 | * This is designed to be called inside {@link android.app.Activity#onCreate} |
| 52 | */ |
| 53 | public static boolean startPermissionActivity(Activity activity) { |
Walter Jang | 58dc5be | 2016-02-27 07:04:33 -0800 | [diff] [blame] | 54 | final Bundle extras = activity.getIntent().getExtras(); |
guanxiongliu | fdb68ec | 2016-05-18 14:42:49 -0700 | [diff] [blame] | 55 | if (extras != null && extras.getBoolean(EXTRA_STARTED_PERMISSIONS_ACTIVITY, false)) { |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 56 | return false; |
| 57 | } |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 58 | return startPermissionActivity(activity, |
| 59 | getPermissions(activity.getPackageManager()), |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 60 | RequestDesiredPermissionsActivity.class); |
| 61 | } |
| 62 | |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 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 | |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 78 | @Override |
| 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 | } |