blob: 02ddf34e1ba7ed76f93852b3ee501fde960d0b77 [file] [log] [blame]
Brian Attwell3c775462015-05-08 16:55:32 -07001/*
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
Gary Mai0a49afa2016-12-05 15:53:58 -080017package com.android.contacts.activities;
Walter Jang93ea7282016-02-22 21:24:42 +000018
Brian Attwell3c775462015-05-08 16:55:32 -070019import android.Manifest.permission;
20import android.app.Activity;
Walter Jang93ea7282016-02-22 21:24:42 +000021import android.content.Intent;
Walter Jangbf6fcf92016-07-08 10:06:17 -070022import android.content.pm.PackageManager;
Marcus Hagerott8ac989c2016-10-04 08:45:58 -070023import android.support.v4.content.LocalBroadcastManager;
Walter Jang93ea7282016-02-22 21:24:42 +000024import android.widget.Toast;
Brian Attwell3c775462015-05-08 16:55:32 -070025
Gary Mai0a49afa2016-12-05 15:53:58 -080026import com.android.contacts.R;
27
Walter Jangbf6fcf92016-07-08 10:06:17 -070028import java.util.ArrayList;
29import java.util.List;
30
Brian Attwell3c775462015-05-08 16:55:32 -070031/**
Brian Attwell62f80072015-06-23 23:26:27 -070032 * Activity that requests permissions needed for activities exported from Contacts.
Brian Attwell3c775462015-05-08 16:55:32 -070033 */
Brian Attwell62f80072015-06-23 23:26:27 -070034public class RequestPermissionsActivity extends RequestPermissionsActivityBase {
Brian Attwell3c775462015-05-08 16:55:32 -070035
Marcus Hagerott8ac989c2016-10-04 08:45:58 -070036 public static final String BROADCAST_PERMISSIONS_GRANTED = "broadcastPermissionsGranted";
37
Walter Jangbf6fcf92016-07-08 10:06:17 -070038 private static String[] sRequiredPermissions;
Brian Attwell3c775462015-05-08 16:55:32 -070039
Brian Attwell3c775462015-05-08 16:55:32 -070040 @Override
Walter Jangbf6fcf92016-07-08 10:06:17 -070041 protected String[] getPermissions() {
42 return getPermissions(getPackageManager());
Brian Attwell62f80072015-06-23 23:26:27 -070043 }
Walter Jang93ea7282016-02-22 21:24:42 +000044
Brian Attwell3c775462015-05-08 16:55:32 -070045 public static boolean startPermissionActivity(Activity activity) {
Walter Jangbf6fcf92016-07-08 10:06:17 -070046 return startPermissionActivity(activity,
47 getPermissions(activity.getPackageManager()),
Brian Attwell62f80072015-06-23 23:26:27 -070048 RequestPermissionsActivity.class);
Brian Attwell3c775462015-05-08 16:55:32 -070049 }
Walter Jang93ea7282016-02-22 21:24:42 +000050
Walter Jangbf6fcf92016-07-08 10:06:17 -070051 private static String[] getPermissions(PackageManager packageManager) {
52 if (sRequiredPermissions == null) {
53 final List<String> permissions = new ArrayList<>();
54 // Contacts group
55 permissions.add(permission.GET_ACCOUNTS);
56 permissions.add(permission.READ_CONTACTS);
57 permissions.add(permission.WRITE_CONTACTS);
58
59 if (packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
60 // Phone group
61 // These are only used in a few places such as QuickContactActivity and
62 // ImportExportDialogFragment. We work around missing this permission when
63 // telephony is not available on the device (i.e. on tablets).
64 permissions.add(permission.CALL_PHONE);
65 permissions.add(permission.READ_CALL_LOG);
66 permissions.add(permission.READ_PHONE_STATE);
67 }
68 sRequiredPermissions = permissions.toArray(new String[0]);
69 }
70 return sRequiredPermissions;
71 }
72
Walter Jang93ea7282016-02-22 21:24:42 +000073 @Override
74 public void onRequestPermissionsResult(
75 int requestCode, String permissions[], int[] grantResults) {
76 if (permissions != null && permissions.length > 0
77 && isAllGranted(permissions, grantResults)) {
78 mPreviousActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Walter Jang915ccdd2016-10-24 12:18:20 -070079 if (mIsCallerSelf) {
80 startActivityForResult(mPreviousActivityIntent, 0);
81 } else {
82 startActivity(mPreviousActivityIntent);
83 }
Walter Jang93ea7282016-02-22 21:24:42 +000084 finish();
85 overridePendingTransition(0, 0);
Marcus Hagerott8ac989c2016-10-04 08:45:58 -070086
87 LocalBroadcastManager.getInstance(this).sendBroadcast(
88 new Intent(BROADCAST_PERMISSIONS_GRANTED));
Walter Jang93ea7282016-02-22 21:24:42 +000089 } else {
90 Toast.makeText(this, R.string.missing_required_permission, Toast.LENGTH_SHORT).show();
91 finish();
92 }
93 }
Carlos Valdivia43488522015-06-11 20:12:08 +000094}