1 /* 2 * Copyright (C) 2021 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.server.companion; 18 19 import static android.app.role.RoleManager.MANAGE_HOLDERS_FLAG_DONT_KILL_APP; 20 21 import static com.android.server.companion.CompanionDeviceManagerService.DEBUG; 22 import static com.android.server.companion.CompanionDeviceManagerService.TAG; 23 24 import android.annotation.NonNull; 25 import android.annotation.SuppressLint; 26 import android.annotation.UserIdInt; 27 import android.app.role.RoleManager; 28 import android.companion.AssociationInfo; 29 import android.content.Context; 30 import android.os.UserHandle; 31 import android.util.Log; 32 import android.util.Slog; 33 34 import java.util.List; 35 36 /** Utility methods for accessing {@link RoleManager} APIs. */ 37 @SuppressLint("LongLogTag") 38 final class RolesUtils { 39 isRoleHolder(@onNull Context context, @UserIdInt int userId, @NonNull String packageName, @NonNull String role)40 static boolean isRoleHolder(@NonNull Context context, @UserIdInt int userId, 41 @NonNull String packageName, @NonNull String role) { 42 final RoleManager roleManager = context.getSystemService(RoleManager.class); 43 final List<String> roleHolders = roleManager.getRoleHoldersAsUser( 44 role, UserHandle.of(userId)); 45 return roleHolders.contains(packageName); 46 } 47 addRoleHolderForAssociation( @onNull Context context, @NonNull AssociationInfo associationInfo)48 static void addRoleHolderForAssociation( 49 @NonNull Context context, @NonNull AssociationInfo associationInfo) { 50 if (DEBUG) { 51 Log.d(TAG, "addRoleHolderForAssociation() associationInfo=" + associationInfo); 52 } 53 54 final String deviceProfile = associationInfo.getDeviceProfile(); 55 if (deviceProfile == null) return; 56 57 final RoleManager roleManager = context.getSystemService(RoleManager.class); 58 59 final String packageName = associationInfo.getPackageName(); 60 final int userId = associationInfo.getUserId(); 61 final UserHandle userHandle = UserHandle.of(userId); 62 63 roleManager.addRoleHolderAsUser(deviceProfile, packageName, 64 MANAGE_HOLDERS_FLAG_DONT_KILL_APP, userHandle, context.getMainExecutor(), 65 success -> { 66 if (!success) { 67 Slog.e(TAG, "Failed to add u" + userId + "\\" + packageName 68 + " to the list of " + deviceProfile + " holders."); 69 } 70 }); 71 } 72 removeRoleHolderForAssociation( @onNull Context context, @NonNull AssociationInfo associationInfo)73 static void removeRoleHolderForAssociation( 74 @NonNull Context context, @NonNull AssociationInfo associationInfo) { 75 if (DEBUG) { 76 Log.d(TAG, "removeRoleHolderForAssociation() associationInfo=" + associationInfo); 77 } 78 79 final String deviceProfile = associationInfo.getDeviceProfile(); 80 if (deviceProfile == null) return; 81 82 final RoleManager roleManager = context.getSystemService(RoleManager.class); 83 84 final String packageName = associationInfo.getPackageName(); 85 final int userId = associationInfo.getUserId(); 86 final UserHandle userHandle = UserHandle.of(userId); 87 88 Slog.i(TAG, "Removing CDM role holder, role=" + deviceProfile 89 + ", package=u" + userId + "\\" + packageName); 90 roleManager.removeRoleHolderAsUser(deviceProfile, packageName, 91 MANAGE_HOLDERS_FLAG_DONT_KILL_APP, userHandle, context.getMainExecutor(), 92 success -> { 93 if (!success) { 94 Slog.e(TAG, "Failed to remove u" + userId + "\\" + packageName 95 + " from the list of " + deviceProfile + " holders."); 96 } 97 }); 98 } 99 RolesUtils()100 private RolesUtils() {}; 101 } 102