1 /* 2 * Copyright (C) 2023 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.devicelockcontroller.storage; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.os.Build; 22 23 import androidx.annotation.Nullable; 24 25 import com.android.devicelockcontroller.policy.DeviceStateController.DeviceState; 26 27 /** 28 * Stores per-user local parameters. 29 * Unlike {@link GlobalParameters}, this class can be directly accessed. 30 */ 31 public final class UserParameters { 32 private static final String FILENAME = "user-params"; 33 private static final String KEY_DEVICE_STATE = "device_state"; 34 private static final String KEY_HOME_PACKAGE_OVERRIDE = "home_override_package"; 35 private static final String TAG = "UserParameters"; 36 UserParameters()37 private UserParameters() { 38 } 39 getSharedPreferences(Context context)40 private static SharedPreferences getSharedPreferences(Context context) { 41 final Context deviceContext = context.createDeviceProtectedStorageContext(); 42 43 return deviceContext.getSharedPreferences(FILENAME, Context.MODE_PRIVATE); 44 } 45 46 47 /** 48 * Gets the current device state. 49 * 50 * @param context Context used to get the shared preferences. 51 * @return the current device state. 52 */ 53 @DeviceState getDeviceState(Context context)54 public static int getDeviceState(Context context) { 55 return getSharedPreferences(context).getInt(KEY_DEVICE_STATE, DeviceState.UNPROVISIONED); 56 } 57 58 /** 59 * Sets the current device state. 60 * 61 * @param context Context used to get the shared preferences. 62 * @param state New state. 63 */ setDeviceState(Context context, @DeviceState int state)64 public static void setDeviceState(Context context, @DeviceState int state) { 65 getSharedPreferences(context).edit().putInt(KEY_DEVICE_STATE, state).apply(); 66 } 67 68 /** 69 * Sets the current device state and synchronously write to storage. This call will block until 70 * write is complete. 71 * 72 * @param state new {@link DeviceState} 73 * @return true if state is set successfully; otherwise, false. 74 */ setDeviceStateSync(Context context, @DeviceState int state)75 public static boolean setDeviceStateSync(Context context, @DeviceState int state) { 76 return getSharedPreferences(context).edit().putInt(KEY_DEVICE_STATE, state).commit(); 77 } 78 79 /** 80 * Gets the name of the package overriding home. 81 * 82 * @param context Context used to get the shared preferences. 83 * @return Package overriding home. 84 */ 85 @Nullable getPackageOverridingHome(Context context)86 public static String getPackageOverridingHome(Context context) { 87 return getSharedPreferences(context).getString(KEY_HOME_PACKAGE_OVERRIDE, null); 88 } 89 90 /** 91 * Sets the name of the package overriding home. 92 * 93 * @param context Context used to get the shared preferences. 94 * @param packageName Package overriding home. 95 */ setPackageOverridingHome(Context context, @Nullable String packageName)96 public static void setPackageOverridingHome(Context context, @Nullable String packageName) { 97 getSharedPreferences(context).edit() 98 .putString(KEY_HOME_PACKAGE_OVERRIDE, packageName).apply(); 99 } 100 101 /** 102 * Clear all user parameters. 103 */ clear(Context context)104 public static void clear(Context context) { 105 if (!Build.isDebuggable()) { 106 throw new SecurityException("Clear is not allowed in non-debuggable build!"); 107 } 108 getSharedPreferences(context).edit().clear().commit(); 109 } 110 } 111