summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/role/model/UserDeniedManager.java
blob: 51de3142237dc1de7298a28b6f6d367e5c57b263 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2019 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.packageinstaller.role.model;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.ArraySet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.packageinstaller.Constants;

import java.util.Collections;
import java.util.Set;

/**
 * Manages user denied status for requesting roles.
 */
public class UserDeniedManager {

    @Nullable
    private static UserDeniedManager sInstance;

    private final SharedPreferences mPreferences;

    /**
     * Get a singleton instance of this class
     *
     * @param context the context for retrieving shared preferences.
     *
     * @return the singleton instance of this class
     */
    @NonNull
    public static UserDeniedManager getInstance(@NonNull Context context) {
        if (sInstance == null) {
            sInstance = new UserDeniedManager(context);
        }
        return sInstance;
    }

    private UserDeniedManager(@NonNull Context context) {
        context = context.getApplicationContext();
        mPreferences = context.getSharedPreferences(Constants.REQUEST_ROLE_USER_DENIED_FILE,
                Context.MODE_PRIVATE);
    }

    /**
     * Check whether an application has been denied for a role once.
     *
     * @param roleName the name of the role
     * @param packageName the package name of the application
     *
     * @return whether the application has been denied for the role once
     */
    public boolean isDeniedOnce(@NonNull String roleName, @NonNull String packageName) {
        return isDenied(roleName, packageName, false);
    }

    /**
     * Remember that an application has been denied for a role once.
     *
     * @param roleName the name of the role
     * @param packageName the package name of the application
     */
    public void setDeniedOnce(@NonNull String roleName, @NonNull String packageName) {
        setDenied(roleName, packageName, false, true);
    }

    /**
     * Check whether an application is always denied for a role.
     *
     * @param roleName the name of the role
     * @param packageName the package name of the application
     *
     * @return whether the application is always denied for the role
     */
    public boolean isDeniedAlways(@NonNull String roleName, @NonNull String packageName) {
        return isDenied(roleName, packageName, true);
    }

    /**
     * Remember that an application is always denied for a role.
     *
     * @param roleName the name of the role
     * @param packageName the package name of the application
     */
    public void setDeniedAlways(@NonNull String roleName, @NonNull String packageName) {
        setDenied(roleName, packageName, true, true);
    }

    /**
     * Forget about whether an application is denied for a role, once or always.
     *
     * @param roleName the name of the role
     * @param packageName the package name of the application
     */
    public void clearDenied(@NonNull String roleName, @NonNull String packageName) {
        setDenied(roleName, packageName, false, false);
        setDenied(roleName, packageName, true, false);
    }

    /**
     * Forget about whether an application is denied for any of the roles, once or always.
     *
     * @param packageName the package name of the application
     */
    public void clearPackageDenied(@NonNull String packageName) {
        mPreferences.edit()
                .remove(getKey(packageName, false))
                .remove(getKey(packageName, true))
                .apply();
    }

    @NonNull
    private static String getKey(@NonNull String packageName, boolean always) {
        return (always ? Constants.REQUEST_ROLE_USER_DENIED_ALWAYS_KEY_PREFIX
                : Constants.REQUEST_ROLE_USER_DENIED_ONCE_KEY_PREFIX) + packageName;
    }

    private boolean isDenied(@NonNull String roleName, @NonNull String packageName,
            boolean always) {
        String key = getKey(packageName, always);
        return mPreferences.getStringSet(key, Collections.emptySet()).contains(roleName);
    }

    private void setDenied(@NonNull String roleName, @NonNull String packageName, boolean always,
            boolean denied) {
        String key = getKey(packageName, always);
        Set<String> roleNames = mPreferences.getStringSet(key, Collections.emptySet());
        if (roleNames.contains(roleName) == denied) {
            return;
        }
        roleNames = new ArraySet<>(roleNames);
        if (denied) {
            roleNames.add(roleName);
        } else {
            roleNames.remove(roleName);
        }
        if (roleName.isEmpty()) {
            mPreferences.edit().remove(key).apply();
        } else {
            mPreferences.edit().putStringSet(key, roleNames).apply();
        }
    }
}