Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9acb884c authored by Stefan Andonian's avatar Stefan Andonian Committed by Android (Google) Code Review
Browse files

Merge "Permanently provide functionality for LauncherPref items that don't...

Merge "Permanently provide functionality for LauncherPref items that don't need to be migrated, but are boot aware." into main
parents 57fd0048 54495f30
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ import android.content.Context
import android.util.Log
import com.android.launcher3.LauncherAppState
import com.android.launcher3.LauncherPrefs
import com.android.launcher3.isBootAwareStartupDataEnabled
import com.android.launcher3.moveStartupDataToDeviceProtectedStorageIsEnabled
import com.android.launcher3.util.LockedUserState

/**
@@ -33,7 +33,8 @@ object BootAwarePreloader {
    fun start(context: Context) {
        val lp = LauncherPrefs.get(context)
        when {
            LockedUserState.get(context).isUserUnlocked || !isBootAwareStartupDataEnabled -> {
            LockedUserState.get(context).isUserUnlocked ||
                !moveStartupDataToDeviceProtectedStorageIsEnabled -> {
                /* No-Op */
            }
            lp.isStartupDataMigrated -> {
+102 −44
Original line number Diff line number Diff line
@@ -60,11 +60,11 @@ class LauncherPrefs(private val encryptedContext: Context) {
                IS_STARTUP_DATA_MIGRATED.defaultValue
            )

    // TODO: Remove `item == TASKBAR_PINNING` once isBootAwareStartupDataEnabled is always true
    private fun chooseSharedPreferences(item: Item): SharedPreferences =
        if (
            (isBootAwareStartupDataEnabled && item.isBootAware && isStartupDataMigrated) ||
                item == TASKBAR_PINNING
            (moveStartupDataToDeviceProtectedStorageIsEnabled &&
                item.encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
                isStartupDataMigrated) || item.encryptionType == EncryptionType.DEVICE_PROTECTED
        )
            bootAwarePrefs
        else item.encryptedPrefs
@@ -139,14 +139,21 @@ class LauncherPrefs(private val encryptedContext: Context) {
    private fun prepareToPutValues(
        updates: Array<out Pair<Item, Any>>
    ): List<SharedPreferences.Editor> {
        val updatesPerPrefFile = updates.groupBy { it.first.encryptedPrefs }.toMutableMap()

        if (isBootAwareStartupDataEnabled) {
            val bootAwareUpdates = updates.filter { it.first.isBootAware }
        val updatesPerPrefFile =
            updates
                .filter { it.first.encryptionType != EncryptionType.DEVICE_PROTECTED }
                .groupBy { it.first.encryptedPrefs }
                .toMutableMap()

        val bootAwareUpdates =
            updates.filter {
                (it.first.encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
                    moveStartupDataToDeviceProtectedStorageIsEnabled) ||
                    it.first.encryptionType == EncryptionType.DEVICE_PROTECTED
            }
        if (bootAwareUpdates.isNotEmpty()) {
            updatesPerPrefFile[bootAwarePrefs] = bootAwareUpdates
        }
        }

        return updatesPerPrefFile.map { prefToItemValueList ->
            prefToItemValueList.key.edit().apply {
@@ -238,14 +245,21 @@ class LauncherPrefs(private val encryptedContext: Context) {
     *   .apply() or .commit()
     */
    private fun prepareToRemove(items: Array<out Item>): List<SharedPreferences.Editor> {
        val itemsPerFile = items.groupBy { it.encryptedPrefs }.toMutableMap()
        val itemsPerFile =
            items
                .filter { it.encryptionType != EncryptionType.DEVICE_PROTECTED }
                .groupBy { it.encryptedPrefs }
                .toMutableMap()

        if (isBootAwareStartupDataEnabled) {
            val bootAwareUpdates = items.filter { it.isBootAware }
        val bootAwareUpdates =
            items.filter {
                (it.encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
                    moveStartupDataToDeviceProtectedStorageIsEnabled) ||
                    it.encryptionType == EncryptionType.DEVICE_PROTECTED
            }
        if (bootAwareUpdates.isNotEmpty()) {
            itemsPerFile[bootAwarePrefs] = bootAwareUpdates
        }
        }

        return itemsPerFile.map { (prefs, items) ->
            prefs.edit().also { editor ->
@@ -255,7 +269,7 @@ class LauncherPrefs(private val encryptedContext: Context) {
    }

    fun migrateStartupDataToDeviceProtectedStorage() {
        if (!isBootAwareStartupDataEnabled) return
        if (!moveStartupDataToDeviceProtectedStorageIsEnabled) return

        Log.d(
            TAG,
@@ -264,7 +278,7 @@ class LauncherPrefs(private val encryptedContext: Context) {
        )

        with(bootAwarePrefs.edit()) {
            BOOT_AWARE_ITEMS.forEach { putValue(it, get(it)) }
            ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE.forEach { putValue(it, get(it)) }
            putBoolean(IS_STARTUP_DATA_MIGRATED.sharedPrefKey, true)
            apply()
        }
@@ -280,30 +294,66 @@ class LauncherPrefs(private val encryptedContext: Context) {

        const val TASKBAR_PINNING_KEY = "TASKBAR_PINNING_KEY"
        const val SHOULD_SHOW_SMARTSPACE_KEY = "SHOULD_SHOW_SMARTSPACE_KEY"
        @JvmField val ICON_STATE = nonRestorableItem(LauncherAppState.KEY_ICON_STATE, "", true)
        @JvmField
        val ICON_STATE =
            nonRestorableItem(
                LauncherAppState.KEY_ICON_STATE,
                "",
                EncryptionType.MOVE_TO_DEVICE_PROTECTED
            )
        @JvmField
        val ALL_APPS_OVERVIEW_THRESHOLD =
            nonRestorableItem(LauncherAppState.KEY_ALL_APPS_OVERVIEW_THRESHOLD, 180, true)
        @JvmField val THEMED_ICONS = backedUpItem(Themes.KEY_THEMED_ICONS, false, true)
            nonRestorableItem(
                LauncherAppState.KEY_ALL_APPS_OVERVIEW_THRESHOLD,
                180,
                EncryptionType.MOVE_TO_DEVICE_PROTECTED
            )
        @JvmField
        val THEMED_ICONS =
            backedUpItem(Themes.KEY_THEMED_ICONS, false, EncryptionType.MOVE_TO_DEVICE_PROTECTED)
        @JvmField val PROMISE_ICON_IDS = backedUpItem(InstallSessionHelper.PROMISE_ICON_IDS, "")
        @JvmField val WORK_EDU_STEP = backedUpItem(WorkProfileManager.KEY_WORK_EDU_STEP, 0)
        @JvmField val WORKSPACE_SIZE = backedUpItem(DeviceGridState.KEY_WORKSPACE_SIZE, "", true)
        @JvmField val HOTSEAT_COUNT = backedUpItem(DeviceGridState.KEY_HOTSEAT_COUNT, -1, true)
        @JvmField val TASKBAR_PINNING = backedUpItem(TASKBAR_PINNING_KEY, false, true)
        @JvmField
        val WORKSPACE_SIZE =
            backedUpItem(
                DeviceGridState.KEY_WORKSPACE_SIZE,
                "",
                EncryptionType.MOVE_TO_DEVICE_PROTECTED
            )
        @JvmField
        val HOTSEAT_COUNT =
            backedUpItem(
                DeviceGridState.KEY_HOTSEAT_COUNT,
                -1,
                EncryptionType.MOVE_TO_DEVICE_PROTECTED
            )
        @JvmField
        val TASKBAR_PINNING =
            backedUpItem(TASKBAR_PINNING_KEY, false, EncryptionType.DEVICE_PROTECTED)

        @JvmField
        val DEVICE_TYPE =
            backedUpItem(DeviceGridState.KEY_DEVICE_TYPE, InvariantDeviceProfile.TYPE_PHONE, true)
            backedUpItem(
                DeviceGridState.KEY_DEVICE_TYPE,
                InvariantDeviceProfile.TYPE_PHONE,
                EncryptionType.MOVE_TO_DEVICE_PROTECTED
            )
        @JvmField
        val DB_FILE =
            backedUpItem(DeviceGridState.KEY_DB_FILE, "", EncryptionType.MOVE_TO_DEVICE_PROTECTED)
        @JvmField
        val SHOULD_SHOW_SMARTSPACE =
            backedUpItem("SHOULD_SHOW_SMARTSPACE_KEY", WIDGET_ON_FIRST_SCREEN, true)
        @JvmField val DB_FILE = backedUpItem(DeviceGridState.KEY_DB_FILE, "", true)
            backedUpItem(
                SHOULD_SHOW_SMARTSPACE_KEY,
                WIDGET_ON_FIRST_SCREEN,
                EncryptionType.DEVICE_PROTECTED
            )
        @JvmField
        val RESTORE_DEVICE =
            backedUpItem(
                RestoreDbTask.RESTORED_DEVICE_TYPE,
                InvariantDeviceProfile.TYPE_PHONE,
                true
                EncryptionType.MOVE_TO_DEVICE_PROTECTED
            )
        @JvmField val APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_IDS, "")
        @JvmField val OLD_APP_WIDGET_IDS = backedUpItem(RestoreDbTask.APPWIDGET_OLD_IDS, "")
@@ -313,7 +363,7 @@ class LauncherPrefs(private val encryptedContext: Context) {
                "idp_grid_name",
                isBackedUp = true,
                defaultValue = null,
                isBootAware = true,
                encryptionType = EncryptionType.MOVE_TO_DEVICE_PROTECTED,
                type = String::class.java
            )
        @JvmField
@@ -327,7 +377,7 @@ class LauncherPrefs(private val encryptedContext: Context) {
                "is_startup_data_boot_aware",
                isBackedUp = false,
                defaultValue = false,
                isBootAware = true
                encryptionType = EncryptionType.DEVICE_PROTECTED
            )

        @VisibleForTesting
@@ -335,22 +385,22 @@ class LauncherPrefs(private val encryptedContext: Context) {
        fun <T> backedUpItem(
            sharedPrefKey: String,
            defaultValue: T,
            isBootAware: Boolean = false
            encryptionType: EncryptionType = EncryptionType.ENCRYPTED
        ): ConstantItem<T> =
            ConstantItem(sharedPrefKey, isBackedUp = true, defaultValue, isBootAware)
            ConstantItem(sharedPrefKey, isBackedUp = true, defaultValue, encryptionType)

        @JvmStatic
        fun <T> backedUpItem(
            sharedPrefKey: String,
            type: Class<out T>,
            isBootAware: Boolean = false,
            encryptionType: EncryptionType = EncryptionType.ENCRYPTED,
            defaultValueFromContext: (c: Context) -> T
        ): ContextualItem<T> =
            ContextualItem(
                sharedPrefKey,
                isBackedUp = true,
                defaultValueFromContext,
                isBootAware,
                encryptionType,
                type
            )

@@ -359,9 +409,9 @@ class LauncherPrefs(private val encryptedContext: Context) {
        fun <T> nonRestorableItem(
            sharedPrefKey: String,
            defaultValue: T,
            isBootAware: Boolean = false
            encryptionType: EncryptionType = EncryptionType.ENCRYPTED
        ): ConstantItem<T> =
            ConstantItem(sharedPrefKey, isBackedUp = false, defaultValue, isBootAware)
            ConstantItem(sharedPrefKey, isBackedUp = false, defaultValue, encryptionType)

        @Deprecated("Don't use shared preferences directly. Use other LauncherPref methods.")
        @JvmStatic
@@ -386,17 +436,16 @@ class LauncherPrefs(private val encryptedContext: Context) {
}

// It is a var because the unit tests are setting this to true so they can run.
@VisibleForTesting
var isBootAwareStartupDataEnabled: Boolean =
    com.android.launcher3.config.FeatureFlags.ENABLE_BOOT_AWARE_STARTUP_DATA.get()
var moveStartupDataToDeviceProtectedStorageIsEnabled: Boolean =
    com.android.launcher3.config.FeatureFlags.MOVE_STARTUP_DATA_TO_DEVICE_PROTECTED_STORAGE.get()

private val BOOT_AWARE_ITEMS: MutableSet<ConstantItem<*>> = mutableSetOf()
private val ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE: MutableSet<ConstantItem<*>> = mutableSetOf()

abstract class Item {
    abstract val sharedPrefKey: String
    abstract val isBackedUp: Boolean
    abstract val type: Class<*>
    abstract val isBootAware: Boolean
    abstract val encryptionType: EncryptionType
    val sharedPrefFile: String
        get() = if (isBackedUp) SHARED_PREFERENCES_KEY else DEVICE_PREFERENCES_KEY

@@ -407,13 +456,16 @@ data class ConstantItem<T>(
    override val sharedPrefKey: String,
    override val isBackedUp: Boolean,
    val defaultValue: T,
    override val isBootAware: Boolean,
    override val encryptionType: EncryptionType,
    // The default value can be null. If so, the type needs to be explicitly stated, or else NPE
    override val type: Class<out T> = defaultValue!!::class.java
) : Item() {
    init {
        if (isBootAware && isBootAwareStartupDataEnabled) {
            BOOT_AWARE_ITEMS.add(this)
        if (
            encryptionType == EncryptionType.MOVE_TO_DEVICE_PROTECTED &&
                moveStartupDataToDeviceProtectedStorageIsEnabled
        ) {
            ITEMS_TO_MOVE_TO_DEVICE_PROTECTED_STORAGE.add(this)
        }
    }
}
@@ -422,7 +474,7 @@ data class ContextualItem<T>(
    override val sharedPrefKey: String,
    override val isBackedUp: Boolean,
    private val defaultSupplier: (c: Context) -> T,
    override val isBootAware: Boolean,
    override val encryptionType: EncryptionType,
    override val type: Class<out T>
) : Item() {
    private var default: T? = null
@@ -434,3 +486,9 @@ data class ContextualItem<T>(
        return default!!
    }
}

enum class EncryptionType {
    ENCRYPTED,
    DEVICE_PROTECTED,
    MOVE_TO_DEVICE_PROTECTED
}
+5 −4
Original line number Diff line number Diff line
@@ -260,10 +260,11 @@ public final class FeatureFlags {
            "Enables taskbar pinning to allow user to switch between transient and persistent "
                    + "taskbar flavors");

    public static final BooleanFlag ENABLE_BOOT_AWARE_STARTUP_DATA = getDebugFlag(251502424,
            "ENABLE_BOOT_AWARE_STARTUP_DATA", DISABLED, "Marks LauncherPref data as (and allows it "
                    + "to) available while the device is locked. Enabling this causes a 1-time "
                    + "migration of certain SharedPreferences data. Improves startup latency.");
    public static final BooleanFlag MOVE_STARTUP_DATA_TO_DEVICE_PROTECTED_STORAGE = getDebugFlag(
            251502424, "ENABLE_BOOT_AWARE_STARTUP_DATA", DISABLED,
            "Marks LauncherPref data as (and allows it to) available while the device is"
                    + " locked. Enabling this causes a 1-time movement of certain SharedPreferences"
                    + " data. Improves startup latency.");

    // TODO(Block 18): Clean up flags
    public static final BooleanFlag ENABLE_APP_PAIRS = getDebugFlag(274189428,
+6 −16
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.launcher3.model;

import static com.android.launcher3.BuildConfig.WIDGET_ON_FIRST_SCREEN;
import static com.android.launcher3.LauncherPrefs.SHOULD_SHOW_SMARTSPACE;
import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
import static com.android.launcher3.config.FeatureFlags.SMARTSPACE_AS_A_WIDGET;
import static com.android.launcher3.model.BgDataModel.Callbacks.FLAG_HAS_SHORTCUT_PERMISSION;
@@ -299,28 +300,17 @@ public class LoaderTask implements Runnable {
            logASplit("bindWidgets");
            verifyNotStopped();

            if (SMARTSPACE_AS_A_WIDGET.get() && LauncherPrefs.get(mApp.getContext())
                    .get(LauncherPrefs.SHOULD_SHOW_SMARTSPACE)) {
            LauncherPrefs prefs = LauncherPrefs.get(mApp.getContext());
            if (SMARTSPACE_AS_A_WIDGET.get() && prefs.get(SHOULD_SHOW_SMARTSPACE)) {
                mLauncherBinder.bindSmartspaceWidget();
                // Turn off pref.
                LauncherPrefs.get(mApp.getContext()).putSync(
                        LauncherPrefs.backedUpItem(
                                        LauncherPrefs.SHOULD_SHOW_SMARTSPACE_KEY,
                                        WIDGET_ON_FIRST_SCREEN,
                                        true)
                                .to(false));
                prefs.putSync(SHOULD_SHOW_SMARTSPACE.to(false));
                logASplit("bindSmartspaceWidget");
                verifyNotStopped();
            } else if (!SMARTSPACE_AS_A_WIDGET.get() && WIDGET_ON_FIRST_SCREEN
                    && !LauncherPrefs.get(mApp.getContext())
                    .get(LauncherPrefs.SHOULD_SHOW_SMARTSPACE)) {
                    && !prefs.get(LauncherPrefs.SHOULD_SHOW_SMARTSPACE)) {
                // Turn on pref.
                LauncherPrefs.get(mApp.getContext()).putSync(
                        LauncherPrefs.backedUpItem(
                                        LauncherPrefs.SHOULD_SHOW_SMARTSPACE_KEY,
                                        WIDGET_ON_FIRST_SCREEN,
                                        true)
                                .to(true));
                prefs.putSync(SHOULD_SHOW_SMARTSPACE.to(true));
            }

            if (FeatureFlags.CHANGE_MODEL_DELEGATE_LOADING_ORDER.get()) {
+3 −2
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import com.android.launcher3.AutoInstallsLayout;
import com.android.launcher3.AutoInstallsLayout.SourceResources;
import com.android.launcher3.ConstantItem;
import com.android.launcher3.DefaultLayoutParser;
import com.android.launcher3.EncryptionType;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherFiles;
@@ -499,11 +500,11 @@ public class ModelDbController {
    private ConstantItem<Boolean> getEmptyDbCreatedKey(String dbName) {
        if (mContext instanceof SandboxContext) {
            return LauncherPrefs.nonRestorableItem(EMPTY_DATABASE_CREATED,
                    false /* default value */, false /* boot aware */);
                    false /* default value */, EncryptionType.ENCRYPTED);
        }
        String key = TextUtils.equals(dbName, LauncherFiles.LAUNCHER_DB)
                ? EMPTY_DATABASE_CREATED : EMPTY_DATABASE_CREATED + "@" + dbName;
        return LauncherPrefs.backedUpItem(key, false /* default value */, false /* boot aware */);
        return LauncherPrefs.backedUpItem(key, false /* default value */, EncryptionType.ENCRYPTED);
    }

    /**
Loading