Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.voicemail; |
| 18 | |
| 19 | import android.Manifest.permission; |
| 20 | import android.content.Context; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.support.annotation.NonNull; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Handles permission checking for the voicemail module. Currently "phone" and "sms" permissions are |
| 28 | * required. |
| 29 | */ |
| 30 | public class VoicemailPermissionHelper { |
| 31 | |
| 32 | /** *_VOICEMAIL permissions are auto-granted by being the default dialer. */ |
| 33 | private static final String[] VOICEMAIL_PERMISSIONS = { |
| 34 | permission.ADD_VOICEMAIL, |
| 35 | permission.WRITE_VOICEMAIL, |
| 36 | permission.READ_VOICEMAIL, |
| 37 | permission.READ_PHONE_STATE, |
| 38 | permission.SEND_SMS |
| 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * Returns {@code true} if the app has all permissions required for the voicemail module to |
| 43 | * operate. |
| 44 | */ |
| 45 | public static boolean hasPermissions(Context context) { |
| 46 | return getMissingPermissions(context).isEmpty(); |
| 47 | } |
| 48 | |
| 49 | /** Returns a list of permission that is missing for the voicemail module to operate. */ |
| 50 | @NonNull |
| 51 | public static List<String> getMissingPermissions(Context context) { |
| 52 | List<String> result = new ArrayList<>(); |
| 53 | for (String permission : VOICEMAIL_PERMISSIONS) { |
| 54 | if (context.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { |
| 55 | result.add(permission); |
| 56 | } |
| 57 | } |
| 58 | return result; |
| 59 | } |
| 60 | } |