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