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.Binder; 31 import android.os.UserHandle; 32 import android.util.Log; 33 import android.util.Slog; 34 35 import java.util.List; 36 import java.util.function.Consumer; 37 38 /** Utility methods for accessing {@link RoleManager} APIs. */ 39 @SuppressLint("LongLogTag") 40 final class RolesUtils { 41 isRoleHolder(@onNull Context context, @UserIdInt int userId, @NonNull String packageName, @NonNull String role)42 static boolean isRoleHolder(@NonNull Context context, @UserIdInt int userId, 43 @NonNull String packageName, @NonNull String role) { 44 final RoleManager roleManager = context.getSystemService(RoleManager.class); 45 final List<String> roleHolders = roleManager.getRoleHoldersAsUser( 46 role, UserHandle.of(userId)); 47 return roleHolders.contains(packageName); 48 } 49 addRoleHolderForAssociation( @onNull Context context, @NonNull AssociationInfo associationInfo, @NonNull Consumer<Boolean> roleGrantResult)50 static void addRoleHolderForAssociation( 51 @NonNull Context context, @NonNull AssociationInfo associationInfo, 52 @NonNull Consumer<Boolean> roleGrantResult) { 53 if (DEBUG) { 54 Log.d(TAG, "addRoleHolderForAssociation() associationInfo=" + associationInfo); 55 } 56 57 final String deviceProfile = associationInfo.getDeviceProfile(); 58 if (deviceProfile == null) return; 59 60 final RoleManager roleManager = context.getSystemService(RoleManager.class); 61 62 final String packageName = associationInfo.getPackageName(); 63 final int userId = associationInfo.getUserId(); 64 final UserHandle userHandle = UserHandle.of(userId); 65 66 roleManager.addRoleHolderAsUser(deviceProfile, packageName, 67 MANAGE_HOLDERS_FLAG_DONT_KILL_APP, userHandle, context.getMainExecutor(), 68 roleGrantResult); 69 } 70 removeRoleHolderForAssociation( @onNull Context context, @NonNull AssociationInfo associationInfo)71 static void removeRoleHolderForAssociation( 72 @NonNull Context context, @NonNull AssociationInfo associationInfo) { 73 if (DEBUG) { 74 Log.d(TAG, "removeRoleHolderForAssociation() associationInfo=" + associationInfo); 75 } 76 77 final String deviceProfile = associationInfo.getDeviceProfile(); 78 if (deviceProfile == null) return; 79 80 final RoleManager roleManager = context.getSystemService(RoleManager.class); 81 82 final String packageName = associationInfo.getPackageName(); 83 final int userId = associationInfo.getUserId(); 84 final UserHandle userHandle = UserHandle.of(userId); 85 86 Slog.i(TAG, "Removing CDM role holder, role=" + deviceProfile 87 + ", package=u" + userId + "\\" + packageName); 88 final long identity = Binder.clearCallingIdentity(); 89 try { 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 } finally { 99 Binder.restoreCallingIdentity(identity); 100 } 101 } 102 RolesUtils()103 private RolesUtils() {}; 104 } 105