Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 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 | */ |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame] | 16 | package com.android.launcher3 |
| 17 | |
| 18 | import android.content.Context |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 19 | import android.content.Context.MODE_PRIVATE |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame] | 20 | import android.content.SharedPreferences |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 21 | import androidx.annotation.VisibleForTesting |
fbaron | e58aaf1 | 2023-09-25 11:34:56 -0700 | [diff] [blame] | 22 | import com.android.launcher3.BuildConfig.WIDGET_ON_FIRST_SCREEN |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 23 | import com.android.launcher3.LauncherFiles.DEVICE_PREFERENCES_KEY |
| 24 | import com.android.launcher3.LauncherFiles.SHARED_PREFERENCES_KEY |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 25 | import com.android.launcher3.model.DeviceGridState |
| 26 | import com.android.launcher3.pm.InstallSessionHelper |
| 27 | import com.android.launcher3.provider.RestoreDbTask |
Charlie Anderson | 511421c | 2023-10-26 11:22:26 -0400 | [diff] [blame] | 28 | import com.android.launcher3.provider.RestoreDbTask.FIRST_LOAD_AFTER_RESTORE_KEY |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 29 | import com.android.launcher3.states.RotationHelper |
| 30 | import com.android.launcher3.util.DisplayController |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 31 | import com.android.launcher3.util.MainThreadInitializedObject |
Sunny Goyal | 10fa016 | 2024-04-21 00:13:35 -0700 | [diff] [blame] | 32 | import com.android.launcher3.util.SafeCloseable |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 33 | import com.android.launcher3.util.Themes |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame] | 34 | |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 35 | /** |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 36 | * Manages Launcher [SharedPreferences] through [Item] instances. |
Jordan Demeulenaere | be82bc6 | 2023-03-01 09:11:48 +0000 | [diff] [blame] | 37 | * |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 38 | * TODO(b/262721340): Replace all direct SharedPreference refs with LauncherPrefs / Item methods. |
| 39 | */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 40 | abstract class LauncherPrefs : SafeCloseable { |
| 41 | |
| 42 | /** Returns the value with type [T] for [item]. */ |
| 43 | abstract fun <T> get(item: ContextualItem<T>): T |
| 44 | |
| 45 | /** Returns the value with type [T] for [item]. */ |
| 46 | abstract fun <T> get(item: ConstantItem<T>): T |
| 47 | |
| 48 | /** Stores the values for each item in preferences. */ |
| 49 | abstract fun put(vararg itemsToValues: Pair<Item, Any>) |
| 50 | |
| 51 | /** Stores the [value] with type [T] for [item] in preferences. */ |
| 52 | abstract fun <T : Any> put(item: Item, value: T) |
| 53 | |
| 54 | /** Synchronous version of [put]. */ |
| 55 | abstract fun putSync(vararg itemsToValues: Pair<Item, Any>) |
| 56 | |
| 57 | /** Registers [listener] for [items]. */ |
| 58 | abstract fun addListener(listener: LauncherPrefChangeListener, vararg items: Item) |
| 59 | |
| 60 | /** Unregisters [listener] for [items]. */ |
| 61 | abstract fun removeListener(listener: LauncherPrefChangeListener, vararg items: Item) |
| 62 | |
| 63 | /** Returns `true` iff all [items] have a value. */ |
| 64 | abstract fun has(vararg items: Item): Boolean |
| 65 | |
| 66 | /** Removes the value for each item in [items]. */ |
| 67 | abstract fun remove(vararg items: Item) |
| 68 | |
| 69 | /** Synchronous version of [remove]. */ |
| 70 | abstract fun removeSync(vararg items: Item) |
| 71 | |
| 72 | companion object { |
| 73 | @VisibleForTesting const val BOOT_AWARE_PREFS_KEY = "boot_aware_prefs" |
| 74 | |
| 75 | @JvmField |
| 76 | var INSTANCE = MainThreadInitializedObject<LauncherPrefs> { LauncherPrefsImpl(it) } |
| 77 | |
| 78 | @JvmStatic fun get(context: Context): LauncherPrefs = INSTANCE.get(context) |
| 79 | |
| 80 | const val TASKBAR_PINNING_KEY = "TASKBAR_PINNING_KEY" |
| 81 | const val TASKBAR_PINNING_DESKTOP_MODE_KEY = "TASKBAR_PINNING_DESKTOP_MODE_KEY" |
| 82 | const val SHOULD_SHOW_SMARTSPACE_KEY = "SHOULD_SHOW_SMARTSPACE_KEY" |
| 83 | @JvmField |
| 84 | val ICON_STATE = nonRestorableItem("pref_icon_shape_path", "", EncryptionType.ENCRYPTED) |
| 85 | |
| 86 | @JvmField |
| 87 | val ENABLE_TWOLINE_ALLAPPS_TOGGLE = backedUpItem("pref_enable_two_line_toggle", false) |
| 88 | @JvmField |
| 89 | val THEMED_ICONS = backedUpItem(Themes.KEY_THEMED_ICONS, false, EncryptionType.ENCRYPTED) |
| 90 | @JvmField val PROMISE_ICON_IDS = backedUpItem(InstallSessionHelper.PROMISE_ICON_IDS, "") |
| 91 | @JvmField val WORK_EDU_STEP = backedUpItem("showed_work_profile_edu", 0) |
| 92 | @JvmField |
| 93 | val WORKSPACE_SIZE = |
| 94 | backedUpItem(DeviceGridState.KEY_WORKSPACE_SIZE, "", EncryptionType.ENCRYPTED) |
| 95 | @JvmField |
| 96 | val HOTSEAT_COUNT = |
| 97 | backedUpItem(DeviceGridState.KEY_HOTSEAT_COUNT, -1, EncryptionType.ENCRYPTED) |
| 98 | @JvmField |
| 99 | val TASKBAR_PINNING = |
| 100 | backedUpItem(TASKBAR_PINNING_KEY, false, EncryptionType.DEVICE_PROTECTED) |
| 101 | @JvmField |
| 102 | val TASKBAR_PINNING_IN_DESKTOP_MODE = |
| 103 | backedUpItem(TASKBAR_PINNING_DESKTOP_MODE_KEY, true, EncryptionType.DEVICE_PROTECTED) |
| 104 | |
| 105 | @JvmField |
| 106 | val DEVICE_TYPE = |
| 107 | backedUpItem( |
| 108 | DeviceGridState.KEY_DEVICE_TYPE, |
| 109 | InvariantDeviceProfile.TYPE_PHONE, |
| 110 | EncryptionType.ENCRYPTED, |
| 111 | ) |
| 112 | @JvmField |
| 113 | val DB_FILE = backedUpItem(DeviceGridState.KEY_DB_FILE, "", EncryptionType.ENCRYPTED) |
| 114 | @JvmField |
| 115 | val SHOULD_SHOW_SMARTSPACE = |
| 116 | backedUpItem( |
| 117 | SHOULD_SHOW_SMARTSPACE_KEY, |
| 118 | WIDGET_ON_FIRST_SCREEN, |
| 119 | EncryptionType.DEVICE_PROTECTED, |
| 120 | ) |
| 121 | @JvmField |
| 122 | val RESTORE_DEVICE = |
| 123 | backedUpItem( |
| 124 | RestoreDbTask.RESTORED_DEVICE_TYPE, |
| 125 | InvariantDeviceProfile.TYPE_PHONE, |
| 126 | EncryptionType.ENCRYPTED, |
| 127 | ) |
| 128 | @JvmField |
Stefan Andonian | 8c0a787 | 2024-10-21 14:26:27 -0700 | [diff] [blame^] | 129 | val NO_DB_FILES_RESTORED = |
| 130 | nonRestorableItem("no_db_files_restored", false, EncryptionType.DEVICE_PROTECTED) |
| 131 | @JvmField |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 132 | val IS_FIRST_LOAD_AFTER_RESTORE = |
| 133 | nonRestorableItem(FIRST_LOAD_AFTER_RESTORE_KEY, false, EncryptionType.ENCRYPTED) |
| 134 | @JvmField val APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_IDS, "") |
| 135 | @JvmField val OLD_APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_OLD_IDS, "") |
| 136 | @JvmField |
| 137 | val GRID_NAME = |
| 138 | ConstantItem( |
| 139 | "idp_grid_name", |
| 140 | isBackedUp = true, |
| 141 | defaultValue = null, |
| 142 | encryptionType = EncryptionType.ENCRYPTED, |
| 143 | type = String::class.java, |
| 144 | ) |
| 145 | @JvmField |
| 146 | val ALLOW_ROTATION = |
| 147 | backedUpItem(RotationHelper.ALLOW_ROTATION_PREFERENCE_KEY, Boolean::class.java) { |
| 148 | RotationHelper.getAllowRotationDefaultValue(DisplayController.INSTANCE.get(it).info) |
| 149 | } |
| 150 | |
| 151 | // Preferences for widget configurations |
| 152 | @JvmField |
| 153 | val RECONFIGURABLE_WIDGET_EDUCATION_TIP_SEEN = |
| 154 | backedUpItem("launcher.reconfigurable_widget_education_tip_seen", false) |
| 155 | |
| 156 | @JvmStatic |
| 157 | fun <T> backedUpItem( |
| 158 | sharedPrefKey: String, |
| 159 | defaultValue: T, |
| 160 | encryptionType: EncryptionType = EncryptionType.ENCRYPTED, |
| 161 | ): ConstantItem<T> = |
| 162 | ConstantItem(sharedPrefKey, isBackedUp = true, defaultValue, encryptionType) |
| 163 | |
| 164 | @JvmStatic |
| 165 | fun <T> backedUpItem( |
| 166 | sharedPrefKey: String, |
| 167 | type: Class<out T>, |
| 168 | encryptionType: EncryptionType = EncryptionType.ENCRYPTED, |
| 169 | defaultValueFromContext: (c: Context) -> T, |
| 170 | ): ContextualItem<T> = |
| 171 | ContextualItem( |
| 172 | sharedPrefKey, |
| 173 | isBackedUp = true, |
| 174 | defaultValueFromContext, |
| 175 | encryptionType, |
| 176 | type, |
| 177 | ) |
| 178 | |
| 179 | @JvmStatic |
| 180 | fun <T> nonRestorableItem( |
| 181 | sharedPrefKey: String, |
| 182 | defaultValue: T, |
| 183 | encryptionType: EncryptionType = EncryptionType.ENCRYPTED, |
| 184 | ): ConstantItem<T> = |
| 185 | ConstantItem(sharedPrefKey, isBackedUp = false, defaultValue, encryptionType) |
| 186 | |
| 187 | @Deprecated("Don't use shared preferences directly. Use other LauncherPref methods.") |
| 188 | @JvmStatic |
| 189 | fun getPrefs(context: Context): SharedPreferences { |
| 190 | // Use application context for shared preferences, so we use single cached instance |
| 191 | return context.applicationContext.getSharedPreferences( |
| 192 | SHARED_PREFERENCES_KEY, |
| 193 | MODE_PRIVATE, |
| 194 | ) |
| 195 | } |
| 196 | |
| 197 | @Deprecated("Don't use shared preferences directly. Use other LauncherPref methods.") |
| 198 | @JvmStatic |
| 199 | fun getDevicePrefs(context: Context): SharedPreferences { |
| 200 | // Use application context for shared preferences, so we use a single cached instance |
| 201 | return context.applicationContext.getSharedPreferences( |
| 202 | DEVICE_PREFERENCES_KEY, |
| 203 | MODE_PRIVATE, |
| 204 | ) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private class LauncherPrefsImpl(private val encryptedContext: Context) : LauncherPrefs() { |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 210 | private val deviceProtectedStorageContext = |
| 211 | encryptedContext.createDeviceProtectedStorageContext() |
| 212 | |
| 213 | private val bootAwarePrefs |
| 214 | get() = |
| 215 | deviceProtectedStorageContext.getSharedPreferences(BOOT_AWARE_PREFS_KEY, MODE_PRIVATE) |
| 216 | |
| 217 | private val Item.encryptedPrefs |
| 218 | get() = encryptedContext.getSharedPreferences(sharedPrefFile, MODE_PRIVATE) |
| 219 | |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 220 | private fun chooseSharedPreferences(item: Item): SharedPreferences = |
Stefan Andonian | 1a626d2 | 2024-04-03 18:36:16 +0000 | [diff] [blame] | 221 | if (item.encryptionType == EncryptionType.DEVICE_PROTECTED) bootAwarePrefs |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 222 | else item.encryptedPrefs |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame] | 223 | |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 224 | /** Wrapper around `getInner` for a `ContextualItem` */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 225 | override fun <T> get(item: ContextualItem<T>): T = |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 226 | getInner(item, item.defaultValueFromContext(encryptedContext)) |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 227 | |
| 228 | /** Wrapper around `getInner` for an `Item` */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 229 | override fun <T> get(item: ConstantItem<T>): T = getInner(item, item.defaultValue) |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 230 | |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 231 | /** |
| 232 | * Retrieves the value for an [Item] from [SharedPreferences]. It handles method typing via the |
| 233 | * default value type, and will throw an error if the type of the item provided is not a |
| 234 | * `String`, `Boolean`, `Float`, `Int`, `Long`, or `Set<String>`. |
| 235 | */ |
| 236 | @Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST") |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 237 | private fun <T> getInner(item: Item, default: T): T { |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 238 | val sp = chooseSharedPreferences(item) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 239 | |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 240 | return when (item.type) { |
| 241 | String::class.java -> sp.getString(item.sharedPrefKey, default as? String) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 242 | Boolean::class.java, |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 243 | java.lang.Boolean::class.java -> sp.getBoolean(item.sharedPrefKey, default as Boolean) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 244 | Int::class.java, |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 245 | java.lang.Integer::class.java -> sp.getInt(item.sharedPrefKey, default as Int) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 246 | Float::class.java, |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 247 | java.lang.Float::class.java -> sp.getFloat(item.sharedPrefKey, default as Float) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 248 | Long::class.java, |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 249 | java.lang.Long::class.java -> sp.getLong(item.sharedPrefKey, default as Long) |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 250 | Set::class.java -> sp.getStringSet(item.sharedPrefKey, default as? Set<String>) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 251 | else -> |
| 252 | throw IllegalArgumentException( |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 253 | "item type: ${item.type}" + " is not compatible with sharedPref methods" |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 254 | ) |
| 255 | } |
| 256 | as T |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 259 | /** |
| 260 | * Stores each of the values provided in `SharedPreferences` according to the configuration |
| 261 | * contained within the associated items provided. Internally, it uses apply, so the caller |
| 262 | * cannot assume that the values that have been put are immediately available for use. |
| 263 | * |
| 264 | * The forEach loop is necessary here since there is 1 `SharedPreference.Editor` returned from |
| 265 | * prepareToPutValue(itemsToValues) for every distinct `SharedPreferences` file present in the |
| 266 | * provided item configurations. |
| 267 | */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 268 | override fun put(vararg itemsToValues: Pair<Item, Any>): Unit = |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 269 | prepareToPutValues(itemsToValues).forEach { it.apply() } |
| 270 | |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 271 | /** See referenced `put` method above. */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 272 | override fun <T : Any> put(item: Item, value: T): Unit = put(item.to(value)) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * Synchronously stores all the values provided according to their associated Item |
| 276 | * configuration. |
| 277 | */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 278 | override fun putSync(vararg itemsToValues: Pair<Item, Any>): Unit = |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 279 | prepareToPutValues(itemsToValues).forEach { it.commit() } |
| 280 | |
| 281 | /** |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 282 | * Updates the values stored in `SharedPreferences` for each corresponding Item-value pair. If |
| 283 | * the item is boot aware, this method updates both the boot aware and the encrypted files. This |
| 284 | * is done because: 1) It allows for easy roll-back if the data is already in encrypted prefs |
| 285 | * and we need to turn off the boot aware data feature & 2) It simplifies Backup/Restore, which |
| 286 | * already points to encrypted storage. |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 287 | * |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 288 | * Returns a list of editors with all transactions added so that the caller can determine to use |
| 289 | * .apply() or .commit() |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 290 | */ |
| 291 | private fun prepareToPutValues( |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 292 | updates: Array<out Pair<Item, Any>> |
| 293 | ): List<SharedPreferences.Editor> { |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 294 | val updatesPerPrefFile = |
| 295 | updates |
| 296 | .filter { it.first.encryptionType != EncryptionType.DEVICE_PROTECTED } |
| 297 | .groupBy { it.first.encryptedPrefs } |
| 298 | .toMutableMap() |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 299 | |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 300 | val bootAwareUpdates = |
Stefan Andonian | 1a626d2 | 2024-04-03 18:36:16 +0000 | [diff] [blame] | 301 | updates.filter { it.first.encryptionType == EncryptionType.DEVICE_PROTECTED } |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 302 | if (bootAwareUpdates.isNotEmpty()) { |
| 303 | updatesPerPrefFile[bootAwarePrefs] = bootAwareUpdates |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return updatesPerPrefFile.map { prefToItemValueList -> |
| 307 | prefToItemValueList.key.edit().apply { |
| 308 | prefToItemValueList.value.forEach { itemToValue: Pair<Item, Any> -> |
| 309 | putValue(itemToValue.first, itemToValue.second) |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * Handles adding values to `SharedPreferences` regardless of type. This method is especially |
| 317 | * helpful for updating `SharedPreferences` values for `List<<Item>Any>` that have multiple |
| 318 | * types of Item values. |
| 319 | */ |
| 320 | @Suppress("UNCHECKED_CAST") |
| 321 | private fun SharedPreferences.Editor.putValue( |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 322 | item: Item, |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 323 | value: Any?, |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 324 | ): SharedPreferences.Editor = |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 325 | when (item.type) { |
| 326 | String::class.java -> putString(item.sharedPrefKey, value as? String) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 327 | Boolean::class.java, |
| 328 | java.lang.Boolean::class.java -> putBoolean(item.sharedPrefKey, value as Boolean) |
| 329 | Int::class.java, |
| 330 | java.lang.Integer::class.java -> putInt(item.sharedPrefKey, value as Int) |
| 331 | Float::class.java, |
| 332 | java.lang.Float::class.java -> putFloat(item.sharedPrefKey, value as Float) |
| 333 | Long::class.java, |
| 334 | java.lang.Long::class.java -> putLong(item.sharedPrefKey, value as Long) |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 335 | Set::class.java -> putStringSet(item.sharedPrefKey, value as? Set<String>) |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 336 | else -> |
| 337 | throw IllegalArgumentException( |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 338 | "item type: ${item.type} is not compatible with sharedPref methods" |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 339 | ) |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * After calling this method, the listener will be notified of any future updates to the |
| 344 | * `SharedPreferences` files associated with the provided list of items. The listener will need |
| 345 | * to filter update notifications so they don't activate for non-relevant updates. |
| 346 | */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 347 | override fun addListener(listener: LauncherPrefChangeListener, vararg items: Item) { |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 348 | items |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 349 | .map { chooseSharedPreferences(it) } |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 350 | .distinct() |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 351 | .forEach { it.registerOnSharedPreferenceChangeListener(listener) } |
Thales Lima | 03ac377 | 2023-01-06 15:16:41 +0000 | [diff] [blame] | 352 | } |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * Stops the listener from getting notified of any more updates to any of the |
| 356 | * `SharedPreferences` files associated with any of the provided list of [Item]. |
| 357 | */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 358 | override fun removeListener(listener: LauncherPrefChangeListener, vararg items: Item) { |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 359 | // If a listener is not registered to a SharedPreference, unregistering it does nothing |
| 360 | items |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 361 | .map { chooseSharedPreferences(it) } |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 362 | .distinct() |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 363 | .forEach { it.unregisterOnSharedPreferenceChangeListener(listener) } |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Checks if all the provided [Item] have values stored in their corresponding |
| 368 | * `SharedPreferences` files. |
| 369 | */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 370 | override fun has(vararg items: Item): Boolean { |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 371 | items |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 372 | .groupBy { chooseSharedPreferences(it) } |
| 373 | .forEach { (prefs, itemsSublist) -> |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 374 | if (!itemsSublist.none { !prefs.contains(it.sharedPrefKey) }) return false |
| 375 | } |
| 376 | return true |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Asynchronously removes the [Item]'s value from its corresponding `SharedPreferences` file. |
| 381 | */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 382 | override fun remove(vararg items: Item) = prepareToRemove(items).forEach { it.apply() } |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 383 | |
| 384 | /** Synchronously removes the [Item]'s value from its corresponding `SharedPreferences` file. */ |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 385 | override fun removeSync(vararg items: Item) = prepareToRemove(items).forEach { it.commit() } |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 386 | |
| 387 | /** |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 388 | * Removes the key value pairs stored in `SharedPreferences` for each corresponding Item. If the |
| 389 | * item is boot aware, this method removes the data from both the boot aware and encrypted |
| 390 | * files. |
| 391 | * |
| 392 | * @return a list of editors with all transactions added so that the caller can determine to use |
Stefan Andonian | 956b88e | 2023-03-20 20:38:46 +0000 | [diff] [blame] | 393 | * .apply() or .commit() |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 394 | */ |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 395 | private fun prepareToRemove(items: Array<out Item>): List<SharedPreferences.Editor> { |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 396 | val itemsPerFile = |
| 397 | items |
| 398 | .filter { it.encryptionType != EncryptionType.DEVICE_PROTECTED } |
| 399 | .groupBy { it.encryptedPrefs } |
| 400 | .toMutableMap() |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 401 | |
Stefan Andonian | 1a626d2 | 2024-04-03 18:36:16 +0000 | [diff] [blame] | 402 | val bootAwareUpdates = items.filter { it.encryptionType == EncryptionType.DEVICE_PROTECTED } |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 403 | if (bootAwareUpdates.isNotEmpty()) { |
| 404 | itemsPerFile[bootAwarePrefs] = bootAwareUpdates |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return itemsPerFile.map { (prefs, items) -> |
| 408 | prefs.edit().also { editor -> |
| 409 | items.forEach { item -> editor.remove(item.sharedPrefKey) } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Sunny Goyal | 10fa016 | 2024-04-21 00:13:35 -0700 | [diff] [blame] | 414 | override fun close() {} |
Stefan Andonian | d1b33b3 | 2022-12-16 21:22:27 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 417 | abstract class Item { |
| 418 | abstract val sharedPrefKey: String |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 419 | abstract val isBackedUp: Boolean |
| 420 | abstract val type: Class<*> |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 421 | abstract val encryptionType: EncryptionType |
Stefan Andonian | 246eeaa | 2023-02-21 22:14:47 +0000 | [diff] [blame] | 422 | val sharedPrefFile: String |
| 423 | get() = if (isBackedUp) SHARED_PREFERENCES_KEY else DEVICE_PREFERENCES_KEY |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 424 | |
| 425 | fun <T> to(value: T): Pair<Item, T> = Pair(this, value) |
| 426 | } |
| 427 | |
| 428 | data class ConstantItem<T>( |
| 429 | override val sharedPrefKey: String, |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 430 | override val isBackedUp: Boolean, |
| 431 | val defaultValue: T, |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 432 | override val encryptionType: EncryptionType, |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 433 | // The default value can be null. If so, the type needs to be explicitly stated, or else NPE |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 434 | override val type: Class<out T> = defaultValue!!::class.java, |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 435 | ) : Item() { |
Sunny Goyal | cd44740 | 2023-10-06 13:47:50 -0700 | [diff] [blame] | 436 | |
| 437 | fun get(c: Context): T = LauncherPrefs.get(c).get(this) |
Stefan Andonian | 5bd9a22 | 2023-02-23 00:58:33 +0000 | [diff] [blame] | 438 | } |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 439 | |
| 440 | data class ContextualItem<T>( |
| 441 | override val sharedPrefKey: String, |
Stefan Andonian | 4c9612b | 2023-02-22 00:00:03 +0000 | [diff] [blame] | 442 | override val isBackedUp: Boolean, |
| 443 | private val defaultSupplier: (c: Context) -> T, |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 444 | override val encryptionType: EncryptionType, |
Brian Isganitis | 9cbc478 | 2024-10-08 14:47:17 -0400 | [diff] [blame] | 445 | override val type: Class<out T>, |
Stefan Andonian | 1d7f703 | 2023-01-23 21:55:04 +0000 | [diff] [blame] | 446 | ) : Item() { |
| 447 | private var default: T? = null |
| 448 | |
| 449 | fun defaultValueFromContext(context: Context): T { |
| 450 | if (default == null) { |
| 451 | default = defaultSupplier(context) |
| 452 | } |
| 453 | return default!! |
| 454 | } |
Sunny Goyal | cd44740 | 2023-10-06 13:47:50 -0700 | [diff] [blame] | 455 | |
| 456 | fun get(c: Context): T = LauncherPrefs.get(c).get(this) |
Thales Lima | 03ac377 | 2023-01-06 15:16:41 +0000 | [diff] [blame] | 457 | } |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 458 | |
| 459 | enum class EncryptionType { |
| 460 | ENCRYPTED, |
| 461 | DEVICE_PROTECTED, |
Stefan Andonian | 54495f3 | 2023-09-29 23:41:26 +0000 | [diff] [blame] | 462 | } |