blob: c6a9283a46b928d015b463ca97b9a3a7455e6622 [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(
Andreas Agvardd1674982023-10-16 15:49:56 +0200300 "pref_icon_shape_path",
Stefan Andonian54495f32023-09-29 23:41:26 +0000301 "",
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(
Andreas Agvardd1674982023-10-16 15:49:56 +0200307 "pref_all_apps_overview_threshold",
Stefan Andonian54495f32023-09-29 23:41:26 +0000308 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 =
Andreas Agvardd1674982023-10-16 15:49:56 +0200313 nonRestorableItem(
314 "pref_long_press_nav_handle_slop_multiplier",
315 100,
316 EncryptionType.MOVE_TO_DEVICE_PROTECTED
317 )
Andy Wickham9fbc52f2023-09-22 17:44:23 -0700318 @JvmField
319 val LONG_PRESS_NAV_HANDLE_TIMEOUT_MS =
Andreas Agvardd1674982023-10-16 15:49:56 +0200320 nonRestorableItem(
321 "pref_long_press_nav_handle_timeout_ms",
322 ViewConfiguration.getLongPressTimeout(),
323 EncryptionType.MOVE_TO_DEVICE_PROTECTED
324 )
325 @JvmField
326 val LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_START_SCALE_PERCENT =
327 nonRestorableItem(
328 "pref_long_press_nav_handle_haptic_hint_start_scale_percent",
329 0,
330 EncryptionType.MOVE_TO_DEVICE_PROTECTED
331 )
332 @JvmField
333 val LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_END_SCALE_PERCENT =
334 nonRestorableItem(
335 "pref_long_press_nav_handle_haptic_hint_end_scale_percent",
336 50,
337 EncryptionType.MOVE_TO_DEVICE_PROTECTED
338 )
339 @JvmField
340 val LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_SCALE_EXPONENT =
341 nonRestorableItem(
342 "pref_long_press_nav_handle_haptic_hint_scale_exponent",
343 2,
344 EncryptionType.MOVE_TO_DEVICE_PROTECTED
345 )
346 @JvmField
347 val LONG_PRESS_NAV_HANDLE_HAPTIC_HINT_ITERATIONS =
348 nonRestorableItem(
349 "pref_long_press_nav_handle_haptic_hint_iterations",
350 40,
351 EncryptionType.MOVE_TO_DEVICE_PROTECTED
352 )
Andy Wickham9fbc52f2023-09-22 17:44:23 -0700353 @JvmField
Stefan Andonian54495f32023-09-29 23:41:26 +0000354 val THEMED_ICONS =
355 backedUpItem(Themes.KEY_THEMED_ICONS, false, EncryptionType.MOVE_TO_DEVICE_PROTECTED)
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000356 @JvmField val PROMISE_ICON_IDS = backedUpItem(InstallSessionHelper.PROMISE_ICON_IDS, "")
Sunny Goyal576f7a52023-10-06 11:28:25 -0700357 @JvmField val WORK_EDU_STEP = backedUpItem("showed_work_profile_edu", 0)
Stefan Andonian54495f32023-09-29 23:41:26 +0000358 @JvmField
359 val WORKSPACE_SIZE =
360 backedUpItem(
361 DeviceGridState.KEY_WORKSPACE_SIZE,
362 "",
363 EncryptionType.MOVE_TO_DEVICE_PROTECTED
364 )
365 @JvmField
366 val HOTSEAT_COUNT =
367 backedUpItem(
368 DeviceGridState.KEY_HOTSEAT_COUNT,
369 -1,
370 EncryptionType.MOVE_TO_DEVICE_PROTECTED
371 )
372 @JvmField
373 val TASKBAR_PINNING =
374 backedUpItem(TASKBAR_PINNING_KEY, false, EncryptionType.DEVICE_PROTECTED)
Jagrut Desaic6d625b2023-04-03 16:57:32 -0700375
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000376 @JvmField
377 val DEVICE_TYPE =
Stefan Andonian54495f32023-09-29 23:41:26 +0000378 backedUpItem(
379 DeviceGridState.KEY_DEVICE_TYPE,
380 InvariantDeviceProfile.TYPE_PHONE,
381 EncryptionType.MOVE_TO_DEVICE_PROTECTED
382 )
383 @JvmField
384 val DB_FILE =
385 backedUpItem(DeviceGridState.KEY_DB_FILE, "", EncryptionType.MOVE_TO_DEVICE_PROTECTED)
fbarone58aaf12023-09-25 11:34:56 -0700386 @JvmField
387 val SHOULD_SHOW_SMARTSPACE =
Stefan Andonian54495f32023-09-29 23:41:26 +0000388 backedUpItem(
389 SHOULD_SHOW_SMARTSPACE_KEY,
390 WIDGET_ON_FIRST_SCREEN,
391 EncryptionType.DEVICE_PROTECTED
392 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000393 @JvmField
394 val RESTORE_DEVICE =
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000395 backedUpItem(
396 RestoreDbTask.RESTORED_DEVICE_TYPE,
397 InvariantDeviceProfile.TYPE_PHONE,
Stefan Andonian54495f32023-09-29 23:41:26 +0000398 EncryptionType.MOVE_TO_DEVICE_PROTECTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000399 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000400 @JvmField val APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_IDS, "")
401 @JvmField val OLD_APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_OLD_IDS, "")
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000402 @JvmField
403 val GRID_NAME =
404 ConstantItem(
405 "idp_grid_name",
406 isBackedUp = true,
407 defaultValue = null,
Stefan Andonian54495f32023-09-29 23:41:26 +0000408 encryptionType = EncryptionType.MOVE_TO_DEVICE_PROTECTED,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000409 type = String::class.java
410 )
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000411 @JvmField
412 val ALLOW_ROTATION =
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000413 backedUpItem(RotationHelper.ALLOW_ROTATION_PREFERENCE_KEY, Boolean::class.java) {
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000414 RotationHelper.getAllowRotationDefaultValue(DisplayController.INSTANCE.get(it).info)
415 }
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000416 @JvmField
417 val IS_STARTUP_DATA_MIGRATED =
418 ConstantItem(
419 "is_startup_data_boot_aware",
420 isBackedUp = false,
421 defaultValue = false,
Stefan Andonian54495f32023-09-29 23:41:26 +0000422 encryptionType = EncryptionType.DEVICE_PROTECTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000423 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000424
Sunny Goyal576f7a52023-10-06 11:28:25 -0700425 // Preferences for widget configurations
426 @JvmField
427 val RECONFIGURABLE_WIDGET_EDUCATION_TIP_SEEN =
428 backedUpItem("launcher.reconfigurable_widget_education_tip_seen", false)
429 @JvmField
430 val WIDGETS_EDUCATION_DIALOG_SEEN =
431 backedUpItem("launcher.widgets_education_dialog_seen", false)
432 @JvmField
433 val WIDGETS_EDUCATION_TIP_SEEN = backedUpItem("launcher.widgets_education_tip_seen", false)
434
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000435 @JvmStatic
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000436 fun <T> backedUpItem(
437 sharedPrefKey: String,
438 defaultValue: T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000439 encryptionType: EncryptionType = EncryptionType.ENCRYPTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000440 ): ConstantItem<T> =
Stefan Andonian54495f32023-09-29 23:41:26 +0000441 ConstantItem(sharedPrefKey, isBackedUp = true, defaultValue, encryptionType)
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000442
443 @JvmStatic
444 fun <T> backedUpItem(
445 sharedPrefKey: String,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000446 type: Class<out T>,
Stefan Andonian54495f32023-09-29 23:41:26 +0000447 encryptionType: EncryptionType = EncryptionType.ENCRYPTED,
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000448 defaultValueFromContext: (c: Context) -> T
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000449 ): ContextualItem<T> =
450 ContextualItem(
451 sharedPrefKey,
452 isBackedUp = true,
453 defaultValueFromContext,
Stefan Andonian54495f32023-09-29 23:41:26 +0000454 encryptionType,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000455 type
456 )
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000457
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000458 @JvmStatic
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000459 fun <T> nonRestorableItem(
460 sharedPrefKey: String,
461 defaultValue: T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000462 encryptionType: EncryptionType = EncryptionType.ENCRYPTED
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000463 ): ConstantItem<T> =
Stefan Andonian54495f32023-09-29 23:41:26 +0000464 ConstantItem(sharedPrefKey, isBackedUp = false, defaultValue, encryptionType)
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000465
466 @Deprecated("Don't use shared preferences directly. Use other LauncherPref methods.")
467 @JvmStatic
468 fun getPrefs(context: Context): SharedPreferences {
469 // Use application context for shared preferences, so we use single cached instance
470 return context.applicationContext.getSharedPreferences(
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000471 SHARED_PREFERENCES_KEY,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000472 MODE_PRIVATE
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000473 )
474 }
475
476 @Deprecated("Don't use shared preferences directly. Use other LauncherPref methods.")
477 @JvmStatic
478 fun getDevicePrefs(context: Context): SharedPreferences {
479 // Use application context for shared preferences, so we use a single cached instance
480 return context.applicationContext.getSharedPreferences(
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000481 DEVICE_PREFERENCES_KEY,
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000482 MODE_PRIVATE
Stefan Andoniand1b33b32022-12-16 21:22:27 +0000483 )
484 }
485 }
486}
487
Stefan Andoniandbc8ec52023-09-21 19:05:07 +0000488// It is a var because the unit tests are setting this to true so they can run.
Stefan Andonian54495f32023-09-29 23:41:26 +0000489var moveStartupDataToDeviceProtectedStorageIsEnabled: Boolean =
490 com.android.launcher3.config.FeatureFlags.MOVE_STARTUP_DATA_TO_DEVICE_PROTECTED_STORAGE.get()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000491
Stefan Andonian54495f32023-09-29 23:41:26 +0000492private val ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE: MutableSet<ConstantItem<*>> = mutableSetOf()
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000493
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000494abstract class Item {
495 abstract val sharedPrefKey: String
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000496 abstract val isBackedUp: Boolean
497 abstract val type: Class<*>
Stefan Andonian54495f32023-09-29 23:41:26 +0000498 abstract val encryptionType: EncryptionType
Stefan Andonian246eeaa2023-02-21 22:14:47 +0000499 val sharedPrefFile: String
500 get() = if (isBackedUp) SHARED_PREFERENCES_KEY else DEVICE_PREFERENCES_KEY
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000501
502 fun <T> to(value: T): Pair<Item, T> = Pair(this, value)
503}
504
505data class ConstantItem<T>(
506 override val sharedPrefKey: String,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000507 override val isBackedUp: Boolean,
508 val defaultValue: T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000509 override val encryptionType: EncryptionType,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000510 // The default value can be null. If so, the type needs to be explicitly stated, or else NPE
511 override val type: Class<out T> = defaultValue!!::class.java
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000512) : Item() {
513 init {
Stefan Andonian54495f32023-09-29 23:41:26 +0000514 if (
515 encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
516 moveStartupDataToDeviceProtectedStorageIsEnabled
517 ) {
518 ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE.add(this)
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000519 }
520 }
Sunny Goyalcd447402023-10-06 13:47:50 -0700521
522 fun get(c: Context): T = LauncherPrefs.get(c).get(this)
Stefan Andonian5bd9a222023-02-23 00:58:33 +0000523}
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000524
525data class ContextualItem<T>(
526 override val sharedPrefKey: String,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000527 override val isBackedUp: Boolean,
528 private val defaultSupplier: (c: Context) -> T,
Stefan Andonian54495f32023-09-29 23:41:26 +0000529 override val encryptionType: EncryptionType,
Stefan Andonian4c9612b2023-02-22 00:00:03 +0000530 override val type: Class<out T>
Stefan Andonian1d7f7032023-01-23 21:55:04 +0000531) : Item() {
532 private var default: T? = null
533
534 fun defaultValueFromContext(context: Context): T {
535 if (default == null) {
536 default = defaultSupplier(context)
537 }
538 return default!!
539 }
Sunny Goyalcd447402023-10-06 13:47:50 -0700540
541 fun get(c: Context): T = LauncherPrefs.get(c).get(this)
Thales Lima03ac3772023-01-06 15:16:41 +0000542}
Stefan Andonian54495f32023-09-29 23:41:26 +0000543
544enum class EncryptionType {
545 ENCRYPTED,
546 DEVICE_PROTECTED,
547 MOVE_TO_DEVICE_PROTECTED
548}