1 /*
2  * Copyright (C) 2018 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.packageinstaller.role.model;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ApplicationInfo;
22 import android.os.UserHandle;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.preference.Preference;
27 
28 import com.android.packageinstaller.role.ui.TwoTargetPreference;
29 
30 import java.util.Collections;
31 import java.util.List;
32 
33 /**
34  * Interface for behavior of a role.
35  */
36 public interface RoleBehavior {
37 
38     /**
39      * @see Role#onRoleAdded(Context)
40      */
onRoleAdded(@onNull Role role, @NonNull Context context)41     default void onRoleAdded(@NonNull Role role, @NonNull Context context) {}
42 
43     /**
44      * @see Role#isAvailableAsUser(UserHandle, Context)
45      */
isAvailableAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)46     default boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
47             @NonNull Context context) {
48         return true;
49     }
50 
51     /**
52      * @see Role#getDefaultHolders(Context)
53      */
54     @NonNull
getDefaultHolders(@onNull Role role, @NonNull Context context)55     default List<String> getDefaultHolders(@NonNull Role role, @NonNull Context context) {
56         return Collections.emptyList();
57     }
58 
59     /**
60      * @see Role#getFallbackHolder(Context)
61      */
62     @Nullable
getFallbackHolder(@onNull Role role, @NonNull Context context)63     default String getFallbackHolder(@NonNull Role role, @NonNull Context context) {
64         return null;
65     }
66 
67     /**
68      * @see Role#isVisibleAsUser(UserHandle, Context)
69      */
isVisibleAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)70     default boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
71             @NonNull Context context) {
72         return true;
73     }
74 
75     /**
76      * @see Role#getManageIntentAsUser(UserHandle, Context)
77      */
78     @Nullable
getManageIntentAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)79     default Intent getManageIntentAsUser(@NonNull Role role, @NonNull UserHandle user,
80             @NonNull Context context) {
81         return null;
82     }
83 
84     /**
85      * @see Role#preparePreferenceAsUser(TwoTargetPreference, UserHandle, Context)
86      */
preparePreferenceAsUser(@onNull Role role, @NonNull TwoTargetPreference preference, @NonNull UserHandle user, @NonNull Context context)87     default void preparePreferenceAsUser(@NonNull Role role,
88             @NonNull TwoTargetPreference preference, @NonNull UserHandle user,
89             @NonNull Context context) {}
90 
91     /**
92      * @see Role#isApplicationVisibleAsUser(ApplicationInfo, UserHandle, Context)
93      */
isApplicationVisibleAsUser(@onNull Role role, @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user, @NonNull Context context)94     default boolean isApplicationVisibleAsUser(@NonNull Role role,
95             @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user,
96             @NonNull Context context) {
97         return true;
98     }
99 
100     /**
101      * @see Role#prepareApplicationPreferenceAsUser(Preference, ApplicationInfo, UserHandle,
102      *      Context)
103      */
prepareApplicationPreferenceAsUser(@onNull Role role, @NonNull Preference preference, @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user, @NonNull Context context)104     default void prepareApplicationPreferenceAsUser(@NonNull Role role,
105             @NonNull Preference preference, @NonNull ApplicationInfo applicationInfo,
106             @NonNull UserHandle user, @NonNull Context context) {}
107 
108     /**
109      * @see Role#getConfirmationMessage(String, Context)
110      */
111     @Nullable
getConfirmationMessage(@onNull Role role, @NonNull String packageName, @NonNull Context context)112     default CharSequence getConfirmationMessage(@NonNull Role role, @NonNull String packageName,
113             @NonNull Context context) {
114         return null;
115     }
116 
117     /**
118      * @see Role#getQualifyingPackagesAsUser(UserHandle, Context)
119      */
120     @Nullable
getQualifyingPackagesAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)121     default List<String> getQualifyingPackagesAsUser(@NonNull Role role, @NonNull UserHandle user,
122             @NonNull Context context) {
123         return null;
124     }
125 
126     /**
127      * @see Role#isPackageQualified(String, Context)
128      */
129     @Nullable
isPackageQualified(@onNull Role role, @NonNull String packageName, @NonNull Context context)130     default Boolean isPackageQualified(@NonNull Role role, @NonNull String packageName,
131             @NonNull Context context) {
132         return null;
133     }
134 
135     /**
136      * @see Role#grant(String, boolean, boolean, boolean, Context)
137      */
grant(@onNull Role role, @NonNull String packageName, @NonNull Context context)138     default void grant(@NonNull Role role, @NonNull String packageName, @NonNull Context context) {}
139 
140     /**
141      * @see Role#revoke(String, boolean, boolean, Context)
142      */
revoke(@onNull Role role, @NonNull String packageName, @NonNull Context context)143     default void revoke(@NonNull Role role, @NonNull String packageName,
144             @NonNull Context context) {}
145 
146     /**
147      * @see Role#onHolderSelectedAsUser(String, UserHandle, Context)
148      */
onHolderSelectedAsUser(@onNull Role role, @NonNull String packageName, @NonNull UserHandle user, @NonNull Context context)149     default void onHolderSelectedAsUser(@NonNull Role role, @NonNull String packageName,
150             @NonNull UserHandle user, @NonNull Context context) {}
151 
152     /**
153      * @see Role#onHolderChangedAsUser(String, UserHandle, Context)
154      */
onHolderChangedAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)155     default void onHolderChangedAsUser(@NonNull Role role, @NonNull UserHandle user,
156             @NonNull Context context) {}
157 }
158