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.role.controller.util; 18 19 import android.app.role.RoleManager; 20 import android.content.Context; 21 import android.content.SharedPreferences; 22 import android.os.UserHandle; 23 24 import androidx.annotation.NonNull; 25 26 import com.android.modules.utils.build.SdkLevel; 27 import com.android.role.controller.model.Role; 28 29 /** 30 * Helper for accessing features in {@link RoleManager}. 31 */ 32 public class RoleManagerCompat { 33 34 /** 35 * Key in the generic shared preferences that stores if the user manually selected the "none" 36 * role holder for a role. 37 */ 38 private static final String IS_NONE_ROLE_HOLDER_SELECTED_KEY = "is_none_role_holder_selected:"; 39 40 /** 41 * Name of generic shared preferences file. 42 */ 43 private static final String PREFERENCES_FILE = "preferences"; 44 RoleManagerCompat()45 private RoleManagerCompat() {} 46 47 /** 48 * @see RoleManager#isBypassingRoleQualification() 49 */ isBypassingRoleQualification(@onNull RoleManager roleManager)50 public static boolean isBypassingRoleQualification(@NonNull RoleManager roleManager) { 51 if (SdkLevel.isAtLeastS()) { 52 return roleManager.isBypassingRoleQualification(); 53 } else { 54 return false; 55 } 56 } 57 58 /** 59 * Get a device protected storage based shared preferences. Avoid storing sensitive data in it. 60 * 61 * @param context the context to get the shared preferences 62 * @return a device protected storage based shared preferences 63 */ 64 @NonNull getDeviceProtectedSharedPreferences(@onNull Context context)65 private static SharedPreferences getDeviceProtectedSharedPreferences(@NonNull Context context) { 66 if (!context.isDeviceProtectedStorage()) { 67 context = context.createDeviceProtectedStorageContext(); 68 } 69 return context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); 70 } 71 72 /** 73 * Check whether the role has the fallback holder enabled. 74 * 75 * @return whether the "none" role holder is not selected 76 */ isRoleFallbackEnabledAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)77 public static boolean isRoleFallbackEnabledAsUser(@NonNull Role role, @NonNull UserHandle user, 78 @NonNull Context context) { 79 Context userContext = UserUtils.getUserContext(context, user); 80 boolean isNoneHolderSelected = getDeviceProtectedSharedPreferences(userContext) 81 .getBoolean(IS_NONE_ROLE_HOLDER_SELECTED_KEY + role.getName(), false); 82 return !isNoneHolderSelected; 83 } 84 85 /** 86 * Set whether the role has fallback holder enabled. 87 * 88 */ setRoleFallbackEnabledAsUser(@onNull Role role, boolean fallbackEnabled, @NonNull UserHandle user, @NonNull Context context)89 public static void setRoleFallbackEnabledAsUser(@NonNull Role role, 90 boolean fallbackEnabled, @NonNull UserHandle user, @NonNull Context context) { 91 Context userContext = UserUtils.getUserContext(context, user); 92 if (fallbackEnabled) { 93 getDeviceProtectedSharedPreferences(userContext).edit() 94 .remove(IS_NONE_ROLE_HOLDER_SELECTED_KEY + role.getName()) 95 .apply(); 96 } else { 97 getDeviceProtectedSharedPreferences(userContext).edit() 98 .putBoolean(IS_NONE_ROLE_HOLDER_SELECTED_KEY + role.getName(), true) 99 .apply(); 100 } 101 } 102 } 103