1 /*
2  * Copyright (C) 2022 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.bedstead.nene.roles;
18 
19 import static android.app.role.RoleManager.ROLE_BROWSER;
20 
21 import static com.android.bedstead.permissions.CommonPermissions.BYPASS_ROLE_QUALIFICATION;
22 import static com.android.bedstead.permissions.CommonPermissions.INTERACT_ACROSS_USERS_FULL;
23 import static com.android.bedstead.permissions.CommonPermissions.MANAGE_ROLE_HOLDERS;
24 import static com.android.bedstead.nene.utils.Versions.T;
25 
26 import android.annotation.TargetApi;
27 import android.app.role.RoleManager;
28 import android.content.Context;
29 import android.os.Build;
30 import android.os.UserHandle;
31 
32 import com.android.bedstead.nene.TestApis;
33 import com.android.bedstead.nene.annotations.Experimental;
34 import com.android.bedstead.nene.packages.Package;
35 import com.android.bedstead.permissions.PermissionContext;
36 import com.android.bedstead.nene.users.UserReference;
37 import com.android.bedstead.nene.utils.Versions;
38 
39 import java.util.HashSet;
40 import java.util.Set;
41 
42 /**
43  * Test APIs related to roles.
44  *
45  * <p>To add or remove a role to or from a specific package, see
46  * {@link Package#setAsRoleHolder(String)} and {@link Package#removeAsRoleHolder(String)}.
47  */
48 @TargetApi(Build.VERSION_CODES.TIRAMISU)
49 public class Roles {
50     public static final Roles sInstance = new Roles();
51 
52     private static final Context sContext = TestApis.context().instrumentedContext();
53 
Roles()54     private Roles() {}
55 
56     /**
57      * @see RoleManager#setBypassingRoleQualification(boolean)
58      */
59     @Experimental
setBypassingRoleQualification(boolean bypassingRoleQualification)60     public void setBypassingRoleQualification(boolean bypassingRoleQualification) {
61         if (!Versions.meetsMinimumSdkVersionRequirement(T)) {
62             return;
63         }
64         try (PermissionContext p = TestApis.permissions().withPermission(
65                 BYPASS_ROLE_QUALIFICATION)) {
66             sContext.getSystemService(RoleManager.class)
67                     .setBypassingRoleQualification(bypassingRoleQualification);
68         }
69     }
70 
71     /**
72      * @see RoleManager#getRoleHolders(String)
73      */
74     @Experimental
getRoleHolders(String role)75     public Set<String> getRoleHolders(String role) {
76         try (PermissionContext p = TestApis.permissions().withPermission(
77                 MANAGE_ROLE_HOLDERS)) {
78             return new HashSet<>(sContext.getSystemService(RoleManager.class).getRoleHolders(role));
79         }
80     }
81 
82     /**
83      * @see RoleManager#getRoleHoldersAsUser(String, UserHandle)
84      */
85     @Experimental
getRoleHoldersAsUser(UserReference user, String role)86     public Set<String> getRoleHoldersAsUser(UserReference user, String role) {
87         try (PermissionContext p = TestApis.permissions().withPermission(
88                 MANAGE_ROLE_HOLDERS).withPermission(INTERACT_ACROSS_USERS_FULL)) {
89             return new HashSet<>(
90                     sContext.getSystemService(RoleManager.class).getRoleHoldersAsUser(role,
91                             user.userHandle()));
92         }
93     }
94 
95     /**
96      * Returns true if any package holds {@link RoleManager#ROLE_BROWSER}
97      */
98     @Experimental
hasBrowserRoleHolder()99     public boolean hasBrowserRoleHolder() {
100         return hasBrowserRoleHolderAsUser(/* user= */ TestApis.users().instrumented());
101     }
102 
103     /**
104      * Returns true if any package holds {@link RoleManager#ROLE_BROWSER} for a given user.
105      */
106     @Experimental
hasBrowserRoleHolderAsUser(UserReference user)107     public boolean hasBrowserRoleHolderAsUser(UserReference user) {
108         return getRoleHoldersAsUser(user, ROLE_BROWSER).isEmpty();
109 
110     }
111 }
112