blob: 9a0d02a59e6ffa229fd645fabe443c207d5c9e38 [file] [log] [blame]
Stefan Andonian5bd9a222023-02-23 00:58:33 +00001/*
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 Andonian146701c2022-11-10 23:07:40 +000016package com.android.launcher3
17
18import android.content.Context
Stefan Andonian5bd9a222023-02-23 00:58:33 +000019import android.content.Context.MODE_PRIVATE
Stefan Andonian146701c2022-11-10 23:07:40 +000020import android.content.SharedPreferences
Stefan Andoniand1b33b32022-12-16 21:22:27 +000021import android.content.SharedPreferences.OnSharedPreferenceChangeListener
Stefan Andonian5bd9a222023-02-23 00:58:33 +000022import android.util.Log
Andy Wickham9fbc52f2023-09-22 17:44:23 -070023import android.view.ViewConfiguration
Stefan Andoniand1b33b32022-12-16 21:22:27 +000024import androidx.annotation.VisibleForTesting
fbarone58aaf12023-09-25 11:34:56 -070025import com.android.launcher3.BuildConfig.WIDGET_ON_FIRST_SCREEN
Stefan Andonian4c9612b2023-02-22 00:00:03 +000026import com.android.launcher3.LauncherFiles.DEVICE_PREFERENCES_KEY
27import com.android.launcher3.LauncherFiles.SHARED_PREFERENCES_KEY
Stefan Andoniand1b33b32022-12-16 21:22:27 +000028import com.android.launcher3.model.DeviceGridState
29import com.android.launcher3.pm.InstallSessionHelper
30import com.android.launcher3.provider.RestoreDbTask
Stefan Andonian1d7f7032023-01-23 21:55:04 +000031import com.android.launcher3.states.RotationHelper
32import com.android.launcher3.util.DisplayController
Stefan Andoniand1b33b32022-12-16 21:22:27 +000033import com.android.launcher3.util.MainThreadInitializedObject
34import com.android.launcher3.util.Themes
Stefan Andonian146701c2022-11-10 23:07:40 +000035
Stefan Andoniand1b33b32022-12-16 21:22:27 +000036/**
37 * Use same context for shared preferences, so that we use a single cached instance
Jordan Demeulenaerebe82bc62023-03-01 09:11:48 +000038 *
Stefan Andoniand1b33b32022-12-16 21:22:27 +000039 * TODO(b/262721340): Replace all direct SharedPreference refs with LauncherPrefs / Item methods.
Stefan Andonian956b88e2023-03-20 20:38:46 +000040 * TODO(b/274501660): Fix ReorderWidgets#simpleReorder test before enabling
41 * isBootAwareStartupDataEnabled
Stefan Andoniand1b33b32022-12-16 21:22:27 +000042 */
Stefan Andonian5bd9a222023-02-23 00:58:33 +000043class LauncherPrefs(private val encryptedContext: Context) {
44 private val deviceProtectedStorageContext =
45 encryptedContext.createDeviceProtectedStorageContext()
46
47 private val bootAwarePrefs
48 get() =
49 deviceProtectedStorageContext.getSharedPreferences(BOOT_AWARE_PREFS_KEY, MODE_PRIVATE)
50
51 private val Item.encryptedPrefs
52 get() = encryptedContext.getSharedPreferences(sharedPrefFile, MODE_PRIVATE)
53
54 // This call to `SharedPreferences` needs to be explicit rather than using `get` since doing so
55 // would result in a circular dependency between `isStartupDataMigrated` and `choosePreferences`
56 val isStartupDataMigrated: Boolean
57 get() =
58 bootAwarePrefs.getBoolean(
59 IS_STARTUP_DATA_MIGRATED.sharedPrefKey,
60 IS_STARTUP_DATA_MIGRATED.defaultValue
61 )
62
63 private fun chooseSharedPreferences(item: Item): SharedPreferences =
Stefan Andonian939d5f82023-09-25 18:07:43 +000064 if (
Stefan Andonian54495f32023-09-29 23:41:26 +000065 (moveStartupDataToDeviceProtectedStorageIsEnabled &&
66 item.encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
67 isStartupDataMigrated) || item.encryptionType == EncryptionType.DEVICE_PROTECTED
Stefan Andonian939d5f82023-09-25 18:07:43 +000068 )
Stefan Andonian5bd9a222023-02-23 00:58:33 +000069 bootAwarePrefs
70 else item.encryptedPrefs
Stefan Andonian146701c2022-11-10 23:07:40 +000071
Stefan Andonian1d7f7032023-01-23 21:55:04 +000072 /** Wrapper around `getInner` for a `ContextualItem` */
Stefan Andonian5bd9a222023-02-23 00:58:33 +000073 fun <T> get(item: ContextualItem<T>): T =
74 getInner(item, item.defaultValueFromContext(encryptedContext))
Stefan Andonian1d7f7032023-01-23 21:55:04 +000075
76 /** Wrapper around `getInner` for an `Item` */
Stefan Andonian4c9612b2023-02-22 00:00:03 +000077 fun <T> get(item: ConstantItem<T>): T = getInner(item, item.defaultValue)
Stefan Andonian1d7f7032023-01-23 21:55:04 +000078
Stefan Andoniand1b33b32022-12-16 21:22:27 +000079 /**
80 * Retrieves the value for an [Item] from [SharedPreferences]. It handles method typing via the
81 * default value type, and will throw an error if the type of the item provided is not a
82 * `String`, `Boolean`, `Float`, `Int`, `Long`, or `Set<String>`.
83 */
84 @Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
Stefan Andonian4c9612b2023-02-22 00:00:03 +000085 private fun <T> getInner(item: Item, default: T): T {
Stefan Andonian5bd9a222023-02-23 00:58:33 +000086 val sp = chooseSharedPreferences(item)
Stefan Andoniand1b33b32022-12-16 21:22:27 +000087
Stefan Andonian4c9612b2023-02-22 00:00:03 +000088 return when (item.type) {
89 String::class.java -> sp.getString(item.sharedPrefKey, default as? String)
Stefan Andoniand1b33b32022-12-16 21:22:27 +000090 Boolean::class.java,
Stefan Andonian1d7f7032023-01-23 21:55:04 +000091 java.lang.Boolean::class.java -> sp.getBoolean(item.sharedPrefKey, default as Boolean)
Stefan Andoniand1b33b32022-12-16 21:22:27 +000092 Int::class.java,
Stefan Andonian1d7f7032023-01-23 21:55:04 +000093 java.lang.Integer::class.java -> sp.getInt(item.sharedPrefKey, default as Int)
Stefan Andoniand1b33b32022-12-16 21:22:27 +000094 Float::class.java,
Stefan Andonian1d7f7032023-01-23 21:55:04 +000095 java.lang.Float::class.java -> sp.getFloat(item.sharedPrefKey, default as Float)
Stefan Andoniand1b33b32022-12-16 21:22:27 +000096 Long::class.java,
Stefan Andonian1d7f7032023-01-23 21:55:04 +000097 java.lang.Long::class.java -> sp.getLong(item.sharedPrefKey, default as Long)
Stefan Andonian4c9612b2023-02-22 00:00:03 +000098 Set::class.java -> sp.getStringSet(item.sharedPrefKey, default as? Set<String>)
Stefan Andoniand1b33b32022-12-16 21:22:27 +000099 else ->
100 throw IllegalArgumentException(
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000101 "item type: ${item.type}" + " is not compatible with sharedPref methods"
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000102 )
103 }
104 as T
Stefan Andonian146701c2022-11-10 23:07:40 +0000105 }
106
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000107 /**
108 * Stores each of the values provided in `SharedPreferences` according to the configuration
109 * contained within the associated items provided. Internally, it uses apply, so the caller
110 * cannot assume that the values that have been put are immediately available for use.
111 *
112 * The forEach loop is necessary here since there is 1 `SharedPreference.Editor` returned from
113 * prepareToPutValue(itemsToValues) for every distinct `SharedPreferences` file present in the
114 * provided item configurations.
115 */
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000116 fun put(vararg itemsToValues: Pair<Item, Any>): Unit =
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000117 prepareToPutValues(itemsToValues).forEach { it.apply() }
118
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000119 /** See referenced `put` method above. */
120 fun <T : Any> put(item: Item, value: T): Unit = put(item.to(value))
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000121
122 /**
123 * Synchronously stores all the values provided according to their associated Item
124 * configuration.
125 */
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000126 fun putSync(vararg itemsToValues: Pair<Item, Any>): Unit =
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000127 prepareToPutValues(itemsToValues).forEach { it.commit() }
128
129 /**
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000130 * Updates the values stored in `SharedPreferences` for each corresponding Item-value pair. If
131 * the item is boot aware, this method updates both the boot aware and the encrypted files. This
132 * is done because: 1) It allows for easy roll-back if the data is already in encrypted prefs
133 * and we need to turn off the boot aware data feature & 2) It simplifies Backup/Restore, which
134 * already points to encrypted storage.
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000135 *
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000136 * Returns a list of editors with all transactions added so that the caller can determine to use
137 * .apply() or .commit()
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000138 */
139 private fun prepareToPutValues(
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000140 updates: Array<out Pair<Item, Any>>
141 ): List<SharedPreferences.Editor> {
Stefan Andonian54495f32023-09-29 23:41:26 +0000142 val updatesPerPrefFile =
143 updates
144 .filter { it.first.encryptionType != EncryptionType.DEVICE_PROTECTED }
145 .groupBy { it.first.encryptedPrefs }
146 .toMutableMap()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000147
Stefan Andonian54495f32023-09-29 23:41:26 +0000148 val bootAwareUpdates =
149 updates.filter {
150 (it.first.encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
151 moveStartupDataToDeviceProtectedStorageIsEnabled) ||
152 it.first.encryptionType == EncryptionType.DEVICE_PROTECTED
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000153 }
Stefan Andonian54495f32023-09-29 23:41:26 +0000154 if (bootAwareUpdates.isNotEmpty()) {
155 updatesPerPrefFile[bootAwarePrefs] = bootAwareUpdates
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000156 }
157
158 return updatesPerPrefFile.map { prefToItemValueList ->
159 prefToItemValueList.key.edit().apply {
160 prefToItemValueList.value.forEach { itemToValue: Pair<Item, Any> ->
161 putValue(itemToValue.first, itemToValue.second)
162 }
163 }
164 }
165 }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000166
167 /**
168 * Handles adding values to `SharedPreferences` regardless of type. This method is especially
169 * helpful for updating `SharedPreferences` values for `List<<Item>Any>` that have multiple
170 * types of Item values.
171 */
172 @Suppress("UNCHECKED_CAST")
173 private fun SharedPreferences.Editor.putValue(
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000174 item: Item,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000175 value: Any?
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000176 ): SharedPreferences.Editor =
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000177 when (item.type) {
178 String::class.java -> putString(item.sharedPrefKey, value as? String)
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000179 Boolean::class.java,
180 java.lang.Boolean::class.java -> putBoolean(item.sharedPrefKey, value as Boolean)
181 Int::class.java,
182 java.lang.Integer::class.java -> putInt(item.sharedPrefKey, value as Int)
183 Float::class.java,
184 java.lang.Float::class.java -> putFloat(item.sharedPrefKey, value as Float)
185 Long::class.java,
186 java.lang.Long::class.java -> putLong(item.sharedPrefKey, value as Long)
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000187 Set::class.java -> putStringSet(item.sharedPrefKey, value as? Set<String>)
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000188 else ->
189 throw IllegalArgumentException(
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000190 "item type: ${item.type} is not compatible with sharedPref methods"
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000191 )
192 }
193
194 /**
195 * After calling this method, the listener will be notified of any future updates to the
196 * `SharedPreferences` files associated with the provided list of items. The listener will need
197 * to filter update notifications so they don't activate for non-relevant updates.
198 */
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000199 fun addListener(listener: OnSharedPreferenceChangeListener, vararg items: Item) {
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000200 items
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000201 .map { chooseSharedPreferences(it) }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000202 .distinct()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000203 .forEach { it.registerOnSharedPreferenceChangeListener(listener) }
Thales Lima03ac3772023-01-06 15:16:41 +0000204 }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000205
206 /**
207 * Stops the listener from getting notified of any more updates to any of the
208 * `SharedPreferences` files associated with any of the provided list of [Item].
209 */
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000210 fun removeListener(listener: OnSharedPreferenceChangeListener, vararg items: Item) {
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000211 // If a listener is not registered to a SharedPreference, unregistering it does nothing
212 items
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000213 .map { chooseSharedPreferences(it) }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000214 .distinct()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000215 .forEach { it.unregisterOnSharedPreferenceChangeListener(listener) }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000216 }
217
218 /**
219 * Checks if all the provided [Item] have values stored in their corresponding
220 * `SharedPreferences` files.
221 */
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000222 fun has(vararg items: Item): Boolean {
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000223 items
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000224 .groupBy { chooseSharedPreferences(it) }
225 .forEach { (prefs, itemsSublist) ->
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000226 if (!itemsSublist.none { !prefs.contains(it.sharedPrefKey) }) return false
227 }
228 return true
229 }
230
231 /**
232 * Asynchronously removes the [Item]'s value from its corresponding `SharedPreferences` file.
233 */
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000234 fun remove(vararg items: Item) = prepareToRemove(items).forEach { it.apply() }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000235
236 /** Synchronously removes the [Item]'s value from its corresponding `SharedPreferences` file. */
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000237 fun removeSync(vararg items: Item) = prepareToRemove(items).forEach { it.commit() }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000238
239 /**
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000240 * Removes the key value pairs stored in `SharedPreferences` for each corresponding Item. If the
241 * item is boot aware, this method removes the data from both the boot aware and encrypted
242 * files.
243 *
244 * @return a list of editors with all transactions added so that the caller can determine to use
Stefan Andonian956b88e2023-03-20 20:38:46 +0000245 * .apply() or .commit()
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000246 */
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000247 private fun prepareToRemove(items: Array<out Item>): List<SharedPreferences.Editor> {
Stefan Andonian54495f32023-09-29 23:41:26 +0000248 val itemsPerFile =
249 items
250 .filter { it.encryptionType != EncryptionType.DEVICE_PROTECTED }
251 .groupBy { it.encryptedPrefs }
252 .toMutableMap()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000253
Stefan Andonian54495f32023-09-29 23:41:26 +0000254 val bootAwareUpdates =
255 items.filter {
256 (it.encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
257 moveStartupDataToDeviceProtectedStorageIsEnabled) ||
258 it.encryptionType == EncryptionType.DEVICE_PROTECTED
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000259 }
Stefan Andonian54495f32023-09-29 23:41:26 +0000260 if (bootAwareUpdates.isNotEmpty()) {
261 itemsPerFile[bootAwarePrefs] = bootAwareUpdates
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000262 }
263
264 return itemsPerFile.map { (prefs, items) ->
265 prefs.edit().also { editor ->
266 items.forEach { item -> editor.remove(item.sharedPrefKey) }
267 }
268 }
269 }
270
271 fun migrateStartupDataToDeviceProtectedStorage() {
Stefan Andonian54495f32023-09-29 23:41:26 +0000272 if (!moveStartupDataToDeviceProtectedStorageIsEnabled) return
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000273
274 Log.d(
275 TAG,
276 "Migrating data to unencrypted shared preferences to enable preloading " +
277 "while the user is locked the next time the device reboots."
278 )
279
280 with(bootAwarePrefs.edit()) {
Stefan Andonian54495f32023-09-29 23:41:26 +0000281 ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE.forEach { putValue(it, get(it)) }
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000282 putBoolean(IS_STARTUP_DATA_MIGRATED.sharedPrefKey, true)
283 apply()
284 }
285 }
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000286
287 companion object {
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000288 private const val TAG = "LauncherPrefs"
289 @VisibleForTesting const val BOOT_AWARE_PREFS_KEY = "boot_aware_prefs"
290
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000291 @JvmField var INSTANCE = MainThreadInitializedObject { LauncherPrefs(it) }
292
293 @JvmStatic fun get(context: Context): LauncherPrefs = INSTANCE.get(context)
294
Jagrut Desaic6d625b2023-04-03 16:57:32 -0700295 const val TASKBAR_PINNING_KEY = "TASKBAR_PINNING_KEY"
fbarone58aaf12023-09-25 11:34:56 -0700296 const val SHOULD_SHOW_SMARTSPACE_KEY = "SHOULD_SHOW_SMARTSPACE_KEY"
Stefan Andonian54495f32023-09-29 23:41:26 +0000297 @JvmField
298 val ICON_STATE =
299 nonRestorableItem(
300 LauncherAppState.KEY_ICON_STATE,
301 "",
302 EncryptionType.MOVE_TO_DEVICE_PROTECTED
303 )
Andy Wickhamaf5bb302023-05-17 14:13:44 -0700304 @JvmField
305 val ALL_APPS_OVERVIEW_THRESHOLD =
Stefan Andonian54495f32023-09-29 23:41:26 +0000306 nonRestorableItem(
307 LauncherAppState.KEY_ALL_APPS_OVERVIEW_THRESHOLD,
308 180,
309 EncryptionType.MOVE_TO_DEVICE_PROTECTED
310 )
311 @JvmField
Andy Wickham9fbc52f2023-09-22 17:44:23 -0700312 val LONG_PRESS_NAV_HANDLE_SLOP_PERCENTAGE =
313 nonRestorableItem(
314 LauncherAppState.KEY_LONG_PRESS_NAV_HANDLE_SLOP_PERCENTAGE,
315 100,
316 EncryptionType.MOVE_TO_DEVICE_PROTECTED
317 )
318 @JvmField
319 val LONG_PRESS_NAV_HANDLE_TIMEOUT_MS =
320 nonRestorableItem(
321 LauncherAppState.KEY_LONG_PRESS_NAV_HANDLE_TIMEOUT_MS,
322 ViewConfiguration.getLongPressTimeout(),
323 EncryptionType.MOVE_TO_DEVICE_PROTECTED
324 )
325 @JvmField
Stefan Andonian54495f32023-09-29 23:41:26 +0000326 val THEMED_ICONS =
327 backedUpItem(Themes.KEY_THEMED_ICONS, false, EncryptionType.MOVE_TO_DEVICE_PROTECTED)
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000328 @JvmField val PROMISE_ICON_IDS = backedUpItem(InstallSessionHelper.PROMISE_ICON_IDS, "")
Sunny Goyal576f7a52023-10-06 11:28:25 -0700329 @JvmField val WORK_EDU_STEP = backedUpItem("showed_work_profile_edu", 0)
Stefan Andonian54495f32023-09-29 23:41:26 +0000330 @JvmField
331 val WORKSPACE_SIZE =
332 backedUpItem(
333 DeviceGridState.KEY_WORKSPACE_SIZE,
334 "",
335 EncryptionType.MOVE_TO_DEVICE_PROTECTED
336 )
337 @JvmField
338 val HOTSEAT_COUNT =
339 backedUpItem(
340 DeviceGridState.KEY_HOTSEAT_COUNT,
341 -1,
342 EncryptionType.MOVE_TO_DEVICE_PROTECTED
343 )
344 @JvmField
345 val TASKBAR_PINNING =
346 backedUpItem(TASKBAR_PINNING_KEY, false, EncryptionType.DEVICE_PROTECTED)
Jagrut Desaic6d625b2023-04-03 16:57:32 -0700347
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000348 @JvmField
349 val DEVICE_TYPE =
Stefan Andonian54495f32023-09-29 23:41:26 +0000350 backedUpItem(
351 DeviceGridState.KEY_DEVICE_TYPE,
352 InvariantDeviceProfile.TYPE_PHONE,
353 EncryptionType.MOVE_TO_DEVICE_PROTECTED
354 )
355 @JvmField
356 val DB_FILE =
357 backedUpItem(DeviceGridState.KEY_DB_FILE, "", EncryptionType.MOVE_TO_DEVICE_PROTECTED)
fbarone58aaf12023-09-25 11:34:56 -0700358 @JvmField
359 val SHOULD_SHOW_SMARTSPACE =
Stefan Andonian54495f32023-09-29 23:41:26 +0000360 backedUpItem(
361 SHOULD_SHOW_SMARTSPACE_KEY,
362 WIDGET_ON_FIRST_SCREEN,
363 EncryptionType.DEVICE_PROTECTED
364 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000365 @JvmField
366 val RESTORE_DEVICE =
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000367 backedUpItem(
368 RestoreDbTask.RESTORED_DEVICE_TYPE,
369 InvariantDeviceProfile.TYPE_PHONE,
Stefan Andonian54495f32023-09-29 23:41:26 +0000370 EncryptionType.MOVE_TO_DEVICE_PROTECTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000371 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000372 @JvmField val APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_IDS, "")
373 @JvmField val OLD_APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_OLD_IDS, "")
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000374 @JvmField
375 val GRID_NAME =
376 ConstantItem(
377 "idp_grid_name",
378 isBackedUp = true,
379 defaultValue = null,
Stefan Andonian54495f32023-09-29 23:41:26 +0000380 encryptionType = EncryptionType.MOVE_TO_DEVICE_PROTECTED,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000381 type = String::class.java
382 )
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000383 @JvmField
384 val ALLOW_ROTATION =
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000385 backedUpItem(RotationHelper.ALLOW_ROTATION_PREFERENCE_KEY, Boolean::class.java) {
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000386 RotationHelper.getAllowRotationDefaultValue(DisplayController.INSTANCE.get(it).info)
387 }
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000388 @JvmField
389 val IS_STARTUP_DATA_MIGRATED =
390 ConstantItem(
391 "is_startup_data_boot_aware",
392 isBackedUp = false,
393 defaultValue = false,
Stefan Andonian54495f32023-09-29 23:41:26 +0000394 encryptionType = EncryptionType.DEVICE_PROTECTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000395 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000396
Sunny Goyal576f7a52023-10-06 11:28:25 -0700397 // Preferences for widget configurations
398 @JvmField
399 val RECONFIGURABLE_WIDGET_EDUCATION_TIP_SEEN =
400 backedUpItem("launcher.reconfigurable_widget_education_tip_seen", false)
401 @JvmField
402 val WIDGETS_EDUCATION_DIALOG_SEEN =
403 backedUpItem("launcher.widgets_education_dialog_seen", false)
404 @JvmField
405 val WIDGETS_EDUCATION_TIP_SEEN = backedUpItem("launcher.widgets_education_tip_seen", false)
406
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000407 @JvmStatic
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000408 fun <T> backedUpItem(
409 sharedPrefKey: String,
410 defaultValue: T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000411 encryptionType: EncryptionType = EncryptionType.ENCRYPTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000412 ): ConstantItem<T> =
Stefan Andonian54495f32023-09-29 23:41:26 +0000413 ConstantItem(sharedPrefKey, isBackedUp = true, defaultValue, encryptionType)
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000414
415 @JvmStatic
416 fun <T> backedUpItem(
417 sharedPrefKey: String,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000418 type: Class<out T>,
Stefan Andonian54495f32023-09-29 23:41:26 +0000419 encryptionType: EncryptionType = EncryptionType.ENCRYPTED,
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000420 defaultValueFromContext: (c: Context) -> T
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000421 ): ContextualItem<T> =
422 ContextualItem(
423 sharedPrefKey,
424 isBackedUp = true,
425 defaultValueFromContext,
Stefan Andonian54495f32023-09-29 23:41:26 +0000426 encryptionType,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000427 type
428 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000429
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000430 @JvmStatic
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000431 fun <T> nonRestorableItem(
432 sharedPrefKey: String,
433 defaultValue: T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000434 encryptionType: EncryptionType = EncryptionType.ENCRYPTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000435 ): ConstantItem<T> =
Stefan Andonian54495f32023-09-29 23:41:26 +0000436 ConstantItem(sharedPrefKey, isBackedUp = false, defaultValue, encryptionType)
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000437
438 @Deprecated("Don't use shared preferences directly. Use other LauncherPref methods.")
439 @JvmStatic
440 fun getPrefs(context: Context): SharedPreferences {
441 // Use application context for shared preferences, so we use single cached instance
442 return context.applicationContext.getSharedPreferences(
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000443 SHARED_PREFERENCES_KEY,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000444 MODE_PRIVATE
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000445 )
446 }
447
448 @Deprecated("Don't use shared preferences directly. Use other LauncherPref methods.")
449 @JvmStatic
450 fun getDevicePrefs(context: Context): SharedPreferences {
451 // Use application context for shared preferences, so we use a single cached instance
452 return context.applicationContext.getSharedPreferences(
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000453 DEVICE_PREFERENCES_KEY,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000454 MODE_PRIVATE
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000455 )
456 }
457 }
458}
459
Stefan Andoniandbc8ec52023-09-21 19:05:07 +0000460// It is a var because the unit tests are setting this to true so they can run.
Stefan Andonian54495f32023-09-29 23:41:26 +0000461var moveStartupDataToDeviceProtectedStorageIsEnabled: Boolean =
462 com.android.launcher3.config.FeatureFlags.MOVE_STARTUP_DATA_TO_DEVICE_PROTECTED_STORAGE.get()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000463
Stefan Andonian54495f32023-09-29 23:41:26 +0000464private val ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE: MutableSet<ConstantItem<*>> = mutableSetOf()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000465
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000466abstract class Item {
467 abstract val sharedPrefKey: String
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000468 abstract val isBackedUp: Boolean
469 abstract val type: Class<*>
Stefan Andonian54495f32023-09-29 23:41:26 +0000470 abstract val encryptionType: EncryptionType
Stefan Andonian246eeaa2023-02-21 22:14:47 +0000471 val sharedPrefFile: String
472 get() = if (isBackedUp) SHARED_PREFERENCES_KEY else DEVICE_PREFERENCES_KEY
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000473
474 fun <T> to(value: T): Pair<Item, T> = Pair(this, value)
475}
476
477data class ConstantItem<T>(
478 override val sharedPrefKey: String,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000479 override val isBackedUp: Boolean,
480 val defaultValue: T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000481 override val encryptionType: EncryptionType,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000482 // The default value can be null. If so, the type needs to be explicitly stated, or else NPE
483 override val type: Class<out T> = defaultValue!!::class.java
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000484) : Item() {
485 init {
Stefan Andonian54495f32023-09-29 23:41:26 +0000486 if (
487 encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
488 moveStartupDataToDeviceProtectedStorageIsEnabled
489 ) {
490 ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE.add(this)
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000491 }
492 }
Sunny Goyalcd447402023-10-06 13:47:50 -0700493
494 fun get(c: Context): T = LauncherPrefs.get(c).get(this)
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000495}
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000496
497data class ContextualItem<T>(
498 override val sharedPrefKey: String,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000499 override val isBackedUp: Boolean,
500 private val defaultSupplier: (c: Context) -> T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000501 override val encryptionType: EncryptionType,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000502 override val type: Class<out T>
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000503) : Item() {
504 private var default: T? = null
505
506 fun defaultValueFromContext(context: Context): T {
507 if (default == null) {
508 default = defaultSupplier(context)
509 }
510 return default!!
511 }
Sunny Goyalcd447402023-10-06 13:47:50 -0700512
513 fun get(c: Context): T = LauncherPrefs.get(c).get(this)
Thales Lima03ac3772023-01-06 15:16:41 +0000514}
Stefan Andonian54495f32023-09-29 23:41:26 +0000515
516enum class EncryptionType {
517 ENCRYPTED,
518 DEVICE_PROTECTED,
519 MOVE_TO_DEVICE_PROTECTED
520}