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
17package com.android.providers.contacts;
18
19import android.content.Context;
20import 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 */
27public 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    /** Determines if the calling process has access to all voicemails. */
40    public boolean callerHasFullAccess() {
41        return callerHasPermission(android.Manifest.permission.ADD_VOICEMAIL) &&
42                callerHasPermission(Manifest.permission.READ_WRITE_ALL_VOICEMAIL);
43    }
44
45    /**
46     * Checks that the caller has permissions to access its own voicemails.
47     *
48     * @throws SecurityException if the caller does not have the voicemail source permission.
49     */
50    public void checkCallerHasOwnVoicemailAccess() {
51        if (!callerHasOwnVoicemailAccess()) {
52            throw new SecurityException("The caller must have permission: " +
53                    android.Manifest.permission.ADD_VOICEMAIL);
54        }
55    }
56
57    /**
58     * Checks that the caller has permissions to access ALL voicemails.
59     *
60     * @throws SecurityException if the caller does not have the voicemail source permission.
61     */
62    public void checkCallerHasFullAccess() {
63        if (!callerHasFullAccess()) {
64            throw new SecurityException(String.format("The caller must have permissions %s AND %s",
65                    android.Manifest.permission.ADD_VOICEMAIL,
66                    Manifest.permission.READ_WRITE_ALL_VOICEMAIL));
67        }
68    }
69
70    /** Determines if the given package has access to its own voicemails. */
71    public boolean packageHasOwnVoicemailAccess(String packageName) {
72        return packageHasPermission(packageName,
73                android.Manifest.permission.ADD_VOICEMAIL);
74    }
75
76    /** Determines if the given package has full access. */
77    public boolean packageHasFullAccess(String packageName) {
78        return packageHasPermission(
79                packageName, android.Manifest.permission.ADD_VOICEMAIL) &&
80                packageHasPermission(packageName, Manifest.permission.READ_WRITE_ALL_VOICEMAIL);
81    }
82
83    /** Determines if the given package has the given permission. */
84    private boolean packageHasPermission(String packageName, String permission) {
85        return mContext.getPackageManager().checkPermission(permission, packageName)
86                == PackageManager.PERMISSION_GRANTED;
87    }
88
89    /** Determines if the calling process has the given permission. */
90    private boolean callerHasPermission(String permission) {
91        // We need to check against both the calling or self permission in order for the Contacts
92        // app to be allowed access.
93        // The reason is that the Contacts app shares its process with the ContactsProvider and
94        // therefore its requests are not considered to be IPCs, since they are coming from the same
95        // process, even if, technically, from a different package.
96        return mContext.checkCallingOrSelfPermission(permission)
97                == PackageManager.PERMISSION_GRANTED;
98    }
99}
100