Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 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 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 17 | package com.android.contacts.activities; |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 18 | |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 19 | import android.Manifest.permission; |
| 20 | import android.app.Activity; |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 21 | import android.content.Intent; |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 23 | import android.support.v4.content.LocalBroadcastManager; |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 24 | import android.widget.Toast; |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 25 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 26 | import com.android.contacts.R; |
| 27 | |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 31 | /** |
Brian Attwell | 62f8007 | 2015-06-23 23:26:27 -0700 | [diff] [blame] | 32 | * Activity that requests permissions needed for activities exported from Contacts. |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 33 | */ |
Brian Attwell | 62f8007 | 2015-06-23 23:26:27 -0700 | [diff] [blame] | 34 | public class RequestPermissionsActivity extends RequestPermissionsActivityBase { |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 35 | |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 36 | public static final String BROADCAST_PERMISSIONS_GRANTED = "broadcastPermissionsGranted"; |
| 37 | |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 38 | private static String[] sRequiredPermissions; |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 39 | |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 40 | @Override |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 41 | protected String[] getPermissions() { |
| 42 | return getPermissions(getPackageManager()); |
Brian Attwell | 62f8007 | 2015-06-23 23:26:27 -0700 | [diff] [blame] | 43 | } |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 44 | |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 45 | public static boolean startPermissionActivity(Activity activity) { |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 46 | return startPermissionActivity(activity, |
| 47 | getPermissions(activity.getPackageManager()), |
Brian Attwell | 62f8007 | 2015-06-23 23:26:27 -0700 | [diff] [blame] | 48 | RequestPermissionsActivity.class); |
Brian Attwell | 3c77546 | 2015-05-08 16:55:32 -0700 | [diff] [blame] | 49 | } |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 50 | |
Walter Jang | bf6fcf9 | 2016-07-08 10:06:17 -0700 | [diff] [blame] | 51 | 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 Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 73 | @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 Jang | 915ccdd | 2016-10-24 12:18:20 -0700 | [diff] [blame] | 79 | if (mIsCallerSelf) { |
| 80 | startActivityForResult(mPreviousActivityIntent, 0); |
| 81 | } else { |
| 82 | startActivity(mPreviousActivityIntent); |
| 83 | } |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 84 | finish(); |
| 85 | overridePendingTransition(0, 0); |
Marcus Hagerott | 8ac989c | 2016-10-04 08:45:58 -0700 | [diff] [blame] | 86 | |
| 87 | LocalBroadcastManager.getInstance(this).sendBroadcast( |
| 88 | new Intent(BROADCAST_PERMISSIONS_GRANTED)); |
Walter Jang | 93ea728 | 2016-02-22 21:24:42 +0000 | [diff] [blame] | 89 | } else { |
| 90 | Toast.makeText(this, R.string.missing_required_permission, Toast.LENGTH_SHORT).show(); |
| 91 | finish(); |
| 92 | } |
| 93 | } |
Carlos Valdivia | 4348852 | 2015-06-11 20:12:08 +0000 | [diff] [blame] | 94 | } |