1 /*
2  * Copyright (C) 2023 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.permissioncontroller.permission.ui.model.grantPermissions
18 
19 import com.android.permissioncontroller.permission.model.livedatatypes.LightAppPermGroup
20 import com.android.permissioncontroller.permission.ui.model.DenyButton
21 import com.android.permissioncontroller.permission.ui.model.Prompt
22 
23 /** The base behavior all grant behavior objects inherit from. */
24 abstract class GrantBehavior {
25     val LOG_TAG = "GrantPermissionsViewModel"
26 
27     /**
28      * Get the prompt type for the given set of requested permissions
29      *
30      * @param group The LightAppPermGroup representing the state of the app and its permissions
31      * @param requestedPerms The permissions requested by the app, after filtering
32      * @param isSystemTriggeredPrompt Whether the prompt was triggered by the system, instead of by
33      *   the app.
34      */
getPromptnull35     abstract fun getPrompt(
36         group: LightAppPermGroup,
37         requestedPerms: Set<String>,
38         isSystemTriggeredPrompt: Boolean = false
39     ): Prompt
40 
41     /**
42      * Get the deny button type for the given set of requested permissions. This is separate from
43      * the prompt, because the same prompt can have multiple deny behaviors, based on if the user
44      * has seen it before.
45      *
46      * @param group The LightAppPermGroup representing the state of the app and its permissions
47      * @param requestedPerms The permissions requested by the app, after filtering
48      * @param prompt The prompt determined by the behavior.
49      */
50     abstract fun getDenyButton(
51         group: LightAppPermGroup,
52         requestedPerms: Set<String>,
53         prompt: Prompt
54     ): DenyButton
55 
56     /**
57      * Whether the group is considered "fully granted". If it is, any remaining permissions in the
58      * group not already granted will be granted.
59      */
60     open fun isGroupFullyGranted(group: LightAppPermGroup, requestedPerms: Set<String>): Boolean {
61         return group.foreground.isGrantedExcludingRWROrAllRWR
62     }
63 
64     /**
65      * Whether or not all foreground permissions in the group are granted. Only different in groups
66      * that respect background and foreground permissions.
67      */
isForegroundFullyGrantednull68     open fun isForegroundFullyGranted(
69         group: LightAppPermGroup,
70         requestedPerms: Set<String>
71     ): Boolean {
72         return isGroupFullyGranted(group, requestedPerms)
73     }
74 
75     /**
76      * Whether or not the permission should be considered as "user fixed". If it is, it is removed
77      * from the request.
78      */
isPermissionFixednull79     open fun isPermissionFixed(group: LightAppPermGroup, perm: String): Boolean {
80         return group.isUserFixed
81     }
82 }
83