1 /* 2 * Copyright (C) 2011 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.providers.contacts; 18 19 import com.android.providers.contacts.util.ContactsPermissions; 20 21 import android.content.Context; 22 import android.telecom.DefaultDialerManager; 23 24 /** 25 * Provides method related to check various voicemail permissions under the 26 * specified context. 27 * <p> This is an immutable object. 28 */ 29 public class VoicemailPermissions { 30 private final Context mContext; 31 VoicemailPermissions(Context context)32 public VoicemailPermissions(Context context) { 33 mContext = context; 34 } 35 36 /** Determines if the calling process has access to its own voicemails. */ callerHasOwnVoicemailAccess()37 public boolean callerHasOwnVoicemailAccess() { 38 return callerHasPermission(android.Manifest.permission.ADD_VOICEMAIL); 39 } 40 41 /** Determine if the calling process has full read access to all voicemails. */ callerHasReadAccess(String callingPackage)42 public boolean callerHasReadAccess(String callingPackage) { 43 if (DefaultDialerManager.isDefaultOrSystemDialer(mContext, callingPackage)) { 44 return true; 45 } 46 return callerHasPermission(android.Manifest.permission.READ_VOICEMAIL); 47 } 48 49 /** Determine if the calling process has the permission required to update and remove all 50 * voicemails */ callerHasWriteAccess(String callingPackage)51 public boolean callerHasWriteAccess(String callingPackage) { 52 if (DefaultDialerManager.isDefaultOrSystemDialer(mContext, callingPackage)) { 53 return true; 54 } 55 return callerHasPermission(android.Manifest.permission.WRITE_VOICEMAIL); 56 } 57 58 /** 59 * Checks that the caller has permissions to access its own voicemails. 60 * 61 * @throws SecurityException if the caller does not have the voicemail source permission. 62 */ checkCallerHasOwnVoicemailAccess()63 public void checkCallerHasOwnVoicemailAccess() { 64 if (!callerHasOwnVoicemailAccess()) { 65 throw new SecurityException("The caller must have permission: " + 66 android.Manifest.permission.ADD_VOICEMAIL); 67 } 68 } 69 70 /** 71 * Checks that the caller has permissions to read ALL voicemails. 72 * 73 * @throws SecurityException if the caller does not have the voicemail source permission. 74 */ checkCallerHasReadAccess(String callingPackage)75 public void checkCallerHasReadAccess(String callingPackage) { 76 if (!callerHasReadAccess(callingPackage)) { 77 throw new SecurityException(String.format("The caller must be the default or system " 78 + "dialer, or have the system-only %s permission: ", 79 android.Manifest.permission.READ_VOICEMAIL)); 80 } 81 } 82 checkCallerHasWriteAccess(String callingPackage)83 public void checkCallerHasWriteAccess(String callingPackage) { 84 if (!callerHasWriteAccess(callingPackage)) { 85 throw new SecurityException(String.format("The caller must be the default or system " 86 + "dialer, or have the system-only %s permission: ", 87 android.Manifest.permission.WRITE_VOICEMAIL)); 88 } 89 } 90 91 /** Determines if the given package has access to its own voicemails. */ packageHasOwnVoicemailAccess(String packageName)92 public boolean packageHasOwnVoicemailAccess(String packageName) { 93 return packageHasPermission(packageName, 94 android.Manifest.permission.ADD_VOICEMAIL); 95 } 96 97 /** Determines if the given package has read access. */ packageHasReadAccess(String packageName)98 public boolean packageHasReadAccess(String packageName) { 99 return packageHasPermission(packageName, android.Manifest.permission.READ_VOICEMAIL); 100 } 101 102 /** Determines if the given package has write access. */ packageHasWriteAccess(String packageName)103 public boolean packageHasWriteAccess(String packageName) { 104 return packageHasPermission(packageName, android.Manifest.permission.WRITE_VOICEMAIL); 105 } 106 107 /** Determines if the given package has the given permission. */ packageHasPermission(String packageName, String permission)108 private boolean packageHasPermission(String packageName, String permission) { 109 return ContactsPermissions.hasPackagePermission(mContext, permission, packageName); 110 } 111 112 /** Determines if the calling process has the given permission. */ callerHasPermission(String permission)113 private boolean callerHasPermission(String permission) { 114 return ContactsPermissions.hasCallerOrSelfPermission(mContext, permission); 115 } 116 } 117