• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.role.controller.behavior.v36;
18 
19 import android.app.role.RoleManager;
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.os.Build;
23 import android.os.UserHandle;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.annotation.RequiresApi;
28 
29 import com.android.role.controller.model.Role;
30 import com.android.role.controller.model.RoleBehavior;
31 import com.android.role.controller.util.PackageUtils;
32 import com.android.role.controller.util.RoleFlags;
33 import com.android.role.controller.util.UserUtils;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 @RequiresApi(Build.VERSION_CODES.BAKLAVA)
39 public class ReservedForTestingProfileGroupExclusivityRoleBehavior implements RoleBehavior {
40     @Nullable
41     @Override
getDefaultHoldersAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)42     public List<String> getDefaultHoldersAsUser(@NonNull Role role, @NonNull UserHandle user,
43             @NonNull Context context) {
44         if (RoleFlags.isProfileGroupExclusivityAvailable()) {
45             Context userContext = UserUtils.getUserContext(context, user);
46             RoleManager userRoleManager = userContext.getSystemService(RoleManager.class);
47             return userRoleManager.getDefaultHoldersForTest(role.getName());
48         } else {
49             return null;
50         }
51     }
52 
53     @Override
isVisibleAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)54     public Boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
55             @NonNull Context context) {
56         if (RoleFlags.isProfileGroupExclusivityAvailable()) {
57             Context userContext = UserUtils.getUserContext(context, user);
58             RoleManager userRoleManager = userContext.getSystemService(RoleManager.class);
59             return userRoleManager.isRoleVisibleForTest(role.getName());
60         } else {
61             return false;
62         }
63     }
64 
65     @Nullable
66     @Override
getQualifyingPackagesAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)67     public List<String> getQualifyingPackagesAsUser(@NonNull Role role, @NonNull UserHandle user,
68             @NonNull Context context) {
69         if (RoleFlags.isProfileGroupExclusivityAvailable()) {
70             Context userContext = UserUtils.getUserContext(context, user);
71             RoleManager userRoleManager = userContext.getSystemService(RoleManager.class);
72             List<String> qualifyingPackageNames =
73                     userRoleManager.getDefaultHoldersForTest(role.getName());
74 
75             // When getQualifyingPackagesAsUser returns a package that isn't installed, Default App
76             // Settings fails to load. Only return available packages.
77             List<String> availableQualifyingPackageNames = new ArrayList<>();
78             for (int i = 0; i < qualifyingPackageNames.size(); i++) {
79                 String qualifyingPackage = qualifyingPackageNames.get(i);
80                 ApplicationInfo applicationInfo =
81                         PackageUtils.getApplicationInfoAsUser(qualifyingPackage, user, context);
82                 if (applicationInfo != null) {
83                     availableQualifyingPackageNames.add(qualifyingPackage);
84                 }
85             }
86             return availableQualifyingPackageNames;
87         } else {
88             return null;
89         }
90     }
91 }
92