summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/util/PermissionsUtil.java
blob: a3140225b61284333209733c7f40e2b723353d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.common.util;

import android.Manifest.permission;
import android.content.Context;
import android.content.pm.PackageManager;

/**
 * Utility class to help with runtime permissions.
 */
public class PermissionsUtil {
    // Each permission in this list is a cherry-picked permission from a particular permission
    // group. Granting a permission group enables access to all permissions in that group so we
    // only need to check a single permission in each group.
    // Note: This assumes that the app has correctly requested for all the relevant permissions
    // in its Manifest file.
    public static final String PHONE = permission.CALL_PHONE;
    public static final String CONTACTS = permission.READ_CONTACTS;
    public static final String LOCATION = permission.ACCESS_FINE_LOCATION;

    private static Boolean sHasPhonePermissions;
    private static Boolean sHasContactsPermissions;
    private static Boolean sHasLocationPermissions;

    public static boolean hasPhonePermissions(Context context) {
        if (sHasPhonePermissions == null) {
            sHasPhonePermissions = hasPermission(context, PHONE);
        }
        return sHasPhonePermissions;
    }

    public static boolean hasContactsPermissions(Context context) {
        if (sHasContactsPermissions == null) {
            sHasContactsPermissions = hasPermission(context, CONTACTS);
        }
        return sHasContactsPermissions;
    }

    public static boolean hasLocationPermissions(Context context) {
        if (sHasLocationPermissions == null) {
            sHasLocationPermissions = hasPermission(context, LOCATION);
        }
        return sHasLocationPermissions;
    }

    /**
     * To be called during various activity lifecycle events to update the cached versions of the
     * permissions.
     *
     * @param context A valid context.
     */
    public static void updateCachedPermissions(Context context) {
        sHasPhonePermissions = hasPermission(context, PHONE);
        sHasContactsPermissions = hasPermission(context, CONTACTS);
        sHasLocationPermissions = hasPermission(context, LOCATION);
    }

    public static boolean hasPermission(Context context, String permission) {
        return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
    }
}