diff --git a/lawnchair/src/app/lawnchair/DeviceProfileOverrides.kt b/lawnchair/src/app/lawnchair/DeviceProfileOverrides.kt index d93a3855bc..38e0c166a0 100644 --- a/lawnchair/src/app/lawnchair/DeviceProfileOverrides.kt +++ b/lawnchair/src/app/lawnchair/DeviceProfileOverrides.kt @@ -1,7 +1,6 @@ package app.lawnchair import android.content.Context -import android.util.DisplayMetrics import app.lawnchair.preferences.PreferenceManager import com.android.launcher3.InvariantDeviceProfile import com.android.launcher3.util.MainThreadInitializedObject @@ -9,13 +8,15 @@ import com.android.launcher3.util.MainThreadInitializedObject class DeviceProfileOverrides(context: Context) { private val prefs = PreferenceManager.getInstance(context) - fun apply(idp: InvariantDeviceProfile, dm: DisplayMetrics) { - idp.numHotseatIcons = prefs.hotseatColumns.get(idp) - idp.numRows = prefs.workspaceRows.get(idp) - idp.numColumns = prefs.workspaceColumns.get(idp) - idp.numAllAppsColumns = prefs.allAppsColumns.get(idp) - idp.numFolderRows = prefs.folderRows.get(idp) - idp.numFolderColumns = prefs.folderColumns.get(idp) + fun apply(idp: InvariantDeviceProfile, defaultGrid: InvariantDeviceProfile.GridOption) { + val dbNameSuffix = if (prefs.currentDbSlot.get() == "a") "" else "_b" + idp.dbFile = idp.dbFile + dbNameSuffix + idp.numHotseatIcons = prefs.hotseatColumns.get(defaultGrid) + idp.numRows = prefs.workspaceRows.get(defaultGrid) + idp.numColumns = prefs.workspaceColumns.get(defaultGrid) + idp.numAllAppsColumns = prefs.allAppsColumns.get(defaultGrid) + idp.numFolderRows = prefs.folderRows.get(defaultGrid) + idp.numFolderColumns = prefs.folderColumns.get(defaultGrid) } companion object { diff --git a/lawnchair/src/app/lawnchair/preferences/BasePreferenceManager.kt b/lawnchair/src/app/lawnchair/preferences/BasePreferenceManager.kt index 975f94259e..38fecbc554 100644 --- a/lawnchair/src/app/lawnchair/preferences/BasePreferenceManager.kt +++ b/lawnchair/src/app/lawnchair/preferences/BasePreferenceManager.kt @@ -140,7 +140,7 @@ abstract class BasePreferenceManager(context: Context) : SharedPreferences.OnSha inner class IdpIntPref( key: String, - private val selectDefaultValue: InvariantDeviceProfile.() -> Int, + private val selectDefaultValue: InvariantDeviceProfile.GridOption.() -> Int, primaryListener: ChangeListener? = null ) : IntPref(key, -1, primaryListener) { override val defaultValue: Int @@ -154,21 +154,21 @@ abstract class BasePreferenceManager(context: Context) : SharedPreferences.OnSha error("unsupported") } - fun defaultValue(idp: InvariantDeviceProfile): Int { - return selectDefaultValue(idp) + fun defaultValue(defaultGrid: InvariantDeviceProfile.GridOption): Int { + return selectDefaultValue(defaultGrid) } - fun get(idp: InvariantDeviceProfile): Int { + fun get(defaultGrid: InvariantDeviceProfile.GridOption): Int { val value = super.get() return if (value == -1) { - selectDefaultValue(idp) + selectDefaultValue(defaultGrid) } else { value } } - fun set(newValue: Int, idp: InvariantDeviceProfile) { - if (newValue == selectDefaultValue(idp)) { + fun set(newValue: Int, defaultGrid: InvariantDeviceProfile.GridOption) { + if (newValue == selectDefaultValue(defaultGrid)) { super.set(-1) } else { super.set(newValue) diff --git a/lawnchair/src/app/lawnchair/preferences/PreferenceAdapter.kt b/lawnchair/src/app/lawnchair/preferences/PreferenceAdapter.kt index 56e44268b5..b0defe4dd4 100644 --- a/lawnchair/src/app/lawnchair/preferences/PreferenceAdapter.kt +++ b/lawnchair/src/app/lawnchair/preferences/PreferenceAdapter.kt @@ -18,6 +18,7 @@ package app.lawnchair.preferences import androidx.compose.runtime.* import androidx.compose.ui.platform.LocalContext +import com.android.launcher3.InvariantDeviceProfile import com.android.launcher3.LauncherAppState import kotlin.reflect.KProperty @@ -46,8 +47,13 @@ class PreferenceAdapter( @Composable fun BasePreferenceManager.IdpIntPref.getAdapter(): PreferenceAdapter { val context = LocalContext.current - val idp = remember { LauncherAppState.getIDP(context) } - return getAdapter(this, { get(idp).toFloat() }, { newValue -> set(newValue.toInt(), idp) }) + val idp = remember { InvariantDeviceProfile.INSTANCE.get(context) } + val defaultGrid = idp.closestProfile + return getAdapter( + this, + { get(defaultGrid).toFloat() }, + { newValue -> set(newValue.toInt(), defaultGrid) } + ) } @Composable diff --git a/lawnchair/src/app/lawnchair/preferences/PreferenceManager.kt b/lawnchair/src/app/lawnchair/preferences/PreferenceManager.kt index ee19fe67cc..7a5a0b93de 100644 --- a/lawnchair/src/app/lawnchair/preferences/PreferenceManager.kt +++ b/lawnchair/src/app/lawnchair/preferences/PreferenceManager.kt @@ -20,23 +20,31 @@ import android.content.Context import androidx.compose.runtime.Composable import androidx.compose.ui.platform.LocalContext import app.lawnchair.LawnchairLauncher +import com.android.launcher3.InvariantDeviceProfile import com.android.launcher3.LauncherAppState +import com.android.launcher3.model.GridSizeMigrationTaskV2 import com.android.launcher3.states.RotationHelper import com.android.launcher3.util.MainThreadInitializedObject -class PreferenceManager private constructor(context: Context) : BasePreferenceManager(context) { - private val reloadIcons = { - val model = LauncherAppState.getInstance(context).model - model.clearIconCache() - model.forceReload() +class PreferenceManager private constructor(private val context: Context) : BasePreferenceManager(context) { + private val idp get() = InvariantDeviceProfile.INSTANCE.get(context) + private val reloadIcons = { idp.onPreferencesChanged(context, InvariantDeviceProfile.CHANGE_FLAG_ICON_PARAMS) } + private val reloadGrid: () -> Unit = { + val defaultGrid = idp.closestProfile + val numColumns = workspaceColumns.get(defaultGrid) + val numRows = workspaceRows.get(defaultGrid) + val numHotseatIcons = hotseatColumns.get(defaultGrid) + if (GridSizeMigrationTaskV2.needsToMigrate(context, numColumns, numRows, numHotseatIcons)) { + toggleCurrentDbSlot() + } + idp.onPreferencesChanged(context, InvariantDeviceProfile.CHANGE_FLAG_GRID) } + private val scheduleRestart = { LawnchairLauncher.instance?.scheduleRestart() Unit } - private val reloadGrid = scheduleRestart - val hiddenAppSet = StringSetPref("hidden-app-set", setOf()) val iconPackPackage = StringPref("pref_iconPackPackage", "", reloadIcons) val allowRotation = BoolPref("pref_allowRotation", RotationHelper.getAllowRotationDefaultValue()) @@ -71,6 +79,15 @@ class PreferenceManager private constructor(context: Context) : BasePreferenceMa sp.registerOnSharedPreferenceChangeListener(this) } + val currentDbSlot = StringPref("pref_currentDbSlot", "a") + private fun toggleCurrentDbSlot() { + if (currentDbSlot.get() == "a") { + currentDbSlot.set("b") + } else { + currentDbSlot.set("a") + } + } + companion object { val INSTANCE = MainThreadInitializedObject(::PreferenceManager) diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java index 79a97f9ce8..76fe4e2d94 100644 --- a/src/com/android/launcher3/InvariantDeviceProfile.java +++ b/src/com/android/launcher3/InvariantDeviceProfile.java @@ -93,6 +93,11 @@ public class InvariantDeviceProfile { private static final int CONFIG_ICON_MASK_RES_ID = Resources.getSystem().getIdentifier( "config_icon_mask", "string", "android"); + /** + * Original profile before preference overrides + */ + public GridOption closestProfile; + /** * Number of icons per row and column in the workspace. */ @@ -243,7 +248,7 @@ public class InvariantDeviceProfile { private void initGrid( Context context, DefaultDisplay.Info displayInfo, DisplayOption displayOption) { - GridOption closestProfile = displayOption.grid; + closestProfile = displayOption.grid; numRows = closestProfile.numRows; numColumns = closestProfile.numColumns; numHotseatIcons = closestProfile.numHotseatIcons; @@ -274,7 +279,7 @@ public class InvariantDeviceProfile { // If the partner customization apk contains any grid overrides, apply them // Supported overrides: numRows, numColumns, iconSize applyPartnerDeviceProfileOverrides(context, displayInfo.metrics); - DeviceProfileOverrides.getINSTANCE().get(context).apply(this, displayInfo.metrics); + DeviceProfileOverrides.getINSTANCE().get(context).apply(this, closestProfile); Point realSize = new Point(displayInfo.realSize); // The real size never changes. smallSide and largeSide will remain the @@ -340,7 +345,16 @@ public class InvariantDeviceProfile { MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext)); } + public void onPreferencesChanged(Context context, int changeFlags) { + Context appContext = context.getApplicationContext(); + MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext, changeFlags)); + } + private void onConfigChanged(Context context) { + onConfigChanged(context, 0); + } + + private void onConfigChanged(Context context, int changeFlags) { // Config changes, what shall we do? InvariantDeviceProfile oldProfile = new InvariantDeviceProfile(this); @@ -348,7 +362,6 @@ public class InvariantDeviceProfile { String gridName = getCurrentGridName(context); initGrid(context, gridName); - int changeFlags = 0; if (numRows != oldProfile.numRows || numColumns != oldProfile.numColumns || numFolderColumns != oldProfile.numFolderColumns || @@ -578,13 +591,13 @@ public class InvariantDeviceProfile { public final int numRows; public final int numColumns; - private final int numFolderRows; - private final int numFolderColumns; + public final int numFolderRows; + public final int numFolderColumns; - private final int numHotseatIcons; + public final int numHotseatIcons; private final String dbFile; - private final int numAllAppsColumns; + public final int numAllAppsColumns; private final int defaultLayoutId; private final int demoModeLayoutId; diff --git a/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java b/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java index 79467d36f6..fc7ce2aba7 100644 --- a/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java +++ b/src/com/android/launcher3/model/GridSizeMigrationTaskV2.java @@ -106,11 +106,15 @@ public class GridSizeMigrationTaskV2 { * Check given a new IDP, if migration is necessary. */ public static boolean needsToMigrate(Context context, InvariantDeviceProfile idp) { + return needsToMigrate(context, idp.numColumns, idp.numRows, idp.numHotseatIcons); + } + + public static boolean needsToMigrate(Context context, int numColumns, int numRows, int numHotseatIcons) { SharedPreferences prefs = Utilities.getPrefs(context); - String gridSizeString = getPointString(idp.numColumns, idp.numRows); + String gridSizeString = getPointString(numColumns, numRows); return !gridSizeString.equals(prefs.getString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, "")) - || idp.numHotseatIcons != prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, -1); + || numHotseatIcons != prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, -1); } /** See {@link #migrateGridIfNeeded(Context, InvariantDeviceProfile)} */