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