Adapt grid dimensions to device profiles

This commit is contained in:
Suphon Thanakornpakapong
2022-05-19 11:43:18 +07:00
parent 7aed96fc75
commit e8ac3e23ff
7 changed files with 99 additions and 53 deletions
+2
View File
@@ -22,6 +22,8 @@
<string name="launcher_activity_logic_class" translatable="false">app.lawnchair.LauncherActivityCachingLogic</string>
<string name="main_process_initializer_class" translatable="false">app.lawnchair.LawnchairProcessInitializer</string>
<string name="task_overlay_factory_class" translatable="false">app.lawnchair.overview.TaskOverlayFactoryImpl</string>
<string name="wallpaper_picker_package" translatable="false">com.android.wallpaper</string>
<string name="wallpaper_picker_package_alt" translatable="false">com.android.wallpaper</string>
<bool name="config_header_protection_supported">true</bool>
@@ -15,6 +15,38 @@ class DeviceProfileOverrides(context: Context) {
private val prefs = PreferenceManager.getInstance(context)
private val preferenceManager2 = PreferenceManager2.getInstance(context)
private val predefinedGrids = InvariantDeviceProfile.parseAllGridOptions(context)
.map { option ->
val gridInfo = DBGridInfo(
numHotseatColumns = option.numHotseatIcons,
numRows = option.numRows,
numColumns = option.numColumns
)
gridInfo to option.name
}
fun getGridInfo() = DBGridInfo(prefs)
fun getGridInfo(gridName: String) = predefinedGrids
.first { it.second == gridName }
.first
fun getGridName(gridInfo: DBGridInfo): String {
val match = predefinedGrids
.firstOrNull { it.first.numRows >= gridInfo.numRows && it.first.numColumns >= gridInfo.numColumns }
?: predefinedGrids.last()
return match.second
}
fun getCurrentGridName() = getGridName(getGridInfo())
fun setCurrentGrid(gridName: String) {
val gridInfo = getGridInfo(gridName)
prefs.workspaceRows.set(gridInfo.numRows)
prefs.workspaceColumns.set(gridInfo.numColumns)
prefs.hotseatColumns.set(gridInfo.numHotseatColumns)
}
fun getOverrides(defaultGrid: InvariantDeviceProfile.GridOption) =
Options(
prefs = prefs,
@@ -22,10 +54,21 @@ class DeviceProfileOverrides(context: Context) {
defaultGrid = defaultGrid,
)
data class Options(
data class DBGridInfo(
var numHotseatColumns: Int,
var numRows: Int,
var numColumns: Int,
) {
val dbFile get() = "launcher_${numRows}_${numColumns}_${numHotseatColumns}.db"
constructor(prefs: PreferenceManager) : this(
numHotseatColumns = prefs.hotseatColumns.get(),
numRows = prefs.workspaceRows.get(),
numColumns = prefs.workspaceColumns.get(),
)
}
data class Options(
var numAllAppsColumns: Int,
var numFolderRows: Int,
var numFolderColumns: Int,
@@ -40,16 +83,11 @@ class DeviceProfileOverrides(context: Context) {
var enableTaskbarOnPhone: Boolean,
) {
val dbFile get() = "launcher_${numRows}_${numColumns}_${numHotseatColumns}.db"
constructor(
prefs: PreferenceManager,
prefs2: PreferenceManager2,
defaultGrid: InvariantDeviceProfile.GridOption,
) : this(
numHotseatColumns = prefs.hotseatColumns.get(defaultGrid),
numRows = prefs.workspaceRows.get(defaultGrid),
numColumns = prefs.workspaceColumns.get(defaultGrid),
numAllAppsColumns = prefs2.drawerColumns.firstBlocking(gridOption = defaultGrid),
numFolderRows = prefs.folderRows.get(defaultGrid),
numFolderColumns = prefs2.folderColumns.firstBlocking(gridOption = defaultGrid),
@@ -58,11 +58,11 @@ abstract class BasePreferenceManager(private val context: Context) : SharedPrefe
inBatchMode = value > 0
}
init {
if (!sp.contains("version")) {
sp.edit {
putInt("version", 1)
}
fun migratePrefs(currentVersion: Int, block: (oldVersion: Int) -> Unit) {
val oldVersion = sp.getInt("version", 9999)
block(oldVersion)
sp.edit {
putInt("version", currentVersion)
}
}
@@ -40,9 +40,9 @@ class PreferenceManager private constructor(private val context: Context) : Base
val allowRotation = BoolPref("pref_allowRotation", false)
val wrapAdaptiveIcons = BoolPref("prefs_wrapAdaptive", false, reloadIcons)
val addIconToHome = BoolPref("pref_add_icon_to_home", true)
val hotseatColumns = IdpIntPref("pref_hotseatColumns", { numHotseatIcons }, reloadGrid)
val workspaceColumns = IdpIntPref("pref_workspaceColumns", { numColumns })
val workspaceRows = IdpIntPref("pref_workspaceRows", { numRows })
val hotseatColumns = IntPref("pref_hotseatColumns", 4, reloadGrid)
val workspaceColumns = IntPref("pref_workspaceColumns", 4)
val workspaceRows = IntPref("pref_workspaceRows", 5)
val folderRows = IdpIntPref("pref_folderRows", { numFolderRows }, reloadGrid)
val drawerOpacity = FloatPref("pref_drawerOpacity", 1F, recreate)
@@ -86,9 +86,21 @@ class PreferenceManager private constructor(private val context: Context) : Base
init {
sp.registerOnSharedPreferenceChangeListener(this)
migratePrefs(CURRENT_VERSION) { oldVersion ->
if (oldVersion < 2) {
listOf(hotseatColumns, workspaceColumns, workspaceRows)
.forEach {
if (it.get() == -1) {
it.set(it.defaultValue)
}
}
}
}
}
companion object {
private const val CURRENT_VERSION = 2
@JvmField
val INSTANCE = MainThreadInitializedObject(::PreferenceManager)
@@ -9,13 +9,12 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import app.lawnchair.DeviceProfileOverrides
import app.lawnchair.preferences.preferenceManager
import app.lawnchair.preferences2.preferenceManager2
import com.android.launcher3.InvariantDeviceProfile
@Composable
fun GridOverridesPreview(
modifier: Modifier = Modifier,
updateGridOptions: DeviceProfileOverrides.Options.() -> Unit
updateGridOptions: DeviceProfileOverrides.DBGridInfo.() -> Unit
) {
DummyLauncherBox(modifier = modifier) {
WallpaperPreview(modifier = Modifier.fillMaxSize())
@@ -27,15 +26,13 @@ fun GridOverridesPreview(
}
@Composable
fun createPreviewIdp(updateGridOptions: DeviceProfileOverrides.Options.() -> Unit): InvariantDeviceProfile {
fun createPreviewIdp(updateGridOptions: DeviceProfileOverrides.DBGridInfo.() -> Unit): InvariantDeviceProfile {
val context = LocalContext.current
val prefs = preferenceManager()
val preferenceManager2 = preferenceManager2()
val defaultGrid = invariantDeviceProfile().closestProfile
val newIdp by remember {
derivedStateOf {
val options = DeviceProfileOverrides.Options(prefs, preferenceManager2, defaultGrid)
val options = DeviceProfileOverrides.DBGridInfo(prefs)
updateGridOptions(options)
InvariantDeviceProfile(context, options)
}
@@ -202,23 +202,12 @@ public class InvariantDeviceProfile {
* This constructor should NOT have any monitors by design.
*/
public InvariantDeviceProfile(Context context, String gridName) {
String newName = initGrid(context, gridName);
if (newName == null || !newName.equals(gridName)) {
throw new IllegalArgumentException("Unknown grid name");
}
this(context, DeviceProfileOverrides.INSTANCE.get(context).getGridInfo(gridName));
}
public InvariantDeviceProfile(Context context, DeviceProfileOverrides.Options overrideOptions) {
// Get the display info based on default display and interpolate it to existing display
Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
@DeviceType int defaultDeviceType = getDeviceType(defaultInfo);
String gridName = getCurrentGridName(context);
ArrayList<DisplayOption> allOptions =
getPredefinedDeviceProfiles(context, gridName, defaultDeviceType, false);
DisplayOption displayOption =
invDistWeightedInterpolate(defaultInfo, allOptions, defaultDeviceType);
initGrid(context, defaultInfo, displayOption, defaultDeviceType, overrideOptions);
public InvariantDeviceProfile(Context context, DeviceProfileOverrides.DBGridInfo dbGridInfo) {
String gridName = DeviceProfileOverrides.INSTANCE.get(context).getGridName(dbGridInfo);
initGrid(context, gridName, dbGridInfo);
}
/**
@@ -299,11 +288,16 @@ public class InvariantDeviceProfile {
}
public static String getCurrentGridName(Context context) {
return Utilities.isGridOptionsEnabled(context)
? Utilities.getPrefs(context).getString(KEY_IDP_GRID_NAME, null) : null;
return DeviceProfileOverrides.INSTANCE.get(context).getCurrentGridName();
}
private String initGrid(Context context, String gridName) {
DeviceProfileOverrides.DBGridInfo dbGridInfo = DeviceProfileOverrides.INSTANCE.get(context)
.getGridInfo();
return initGrid(context, gridName, dbGridInfo);
}
private String initGrid(Context context, String gridName, DeviceProfileOverrides.DBGridInfo dbGridInfo) {
Info displayInfo = DisplayController.INSTANCE.get(context).getInfo();
@DeviceType int deviceType = getDeviceType(displayInfo);
@@ -312,25 +306,26 @@ public class InvariantDeviceProfile {
RestoreDbTask.isPending(context));
DisplayOption displayOption =
invDistWeightedInterpolate(displayInfo, allOptions, deviceType);
initGrid(context, displayInfo, displayOption, deviceType);
initGrid(context, displayInfo, displayOption, deviceType, dbGridInfo);
return displayOption.grid.name;
}
private void initGrid(
Context context, Info displayInfo, DisplayOption displayOption,
private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
@DeviceType int deviceType) {
DeviceProfileOverrides.Options overrideOptions = DeviceProfileOverrides.INSTANCE.get(context)
.getOverrides(displayOption.grid);
initGrid(context, displayInfo, displayOption, deviceType, overrideOptions);
DeviceProfileOverrides.DBGridInfo dbGridInfo = DeviceProfileOverrides.INSTANCE.get(context)
.getGridInfo();
initGrid(context, displayInfo, displayOption, deviceType, dbGridInfo);
}
private void initGrid(Context context, Info displayInfo, DisplayOption displayOption,
@DeviceType int deviceType, DeviceProfileOverrides.Options overrideOptions) {
@DeviceType int deviceType, DeviceProfileOverrides.DBGridInfo dbGridInfo) {
DeviceProfileOverrides.Options overrideOptions = DeviceProfileOverrides.INSTANCE.get(context)
.getOverrides(displayOption.grid);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
closestProfile = displayOption.grid;
numRows = overrideOptions.getNumRows();
numColumns = overrideOptions.getNumColumns();
dbFile = overrideOptions.getDbFile();
numRows = dbGridInfo.getNumRows();
numColumns = dbGridInfo.getNumColumns();
dbFile = dbGridInfo.getDbFile();
defaultLayoutId = closestProfile.defaultLayoutId;
demoModeLayoutId = closestProfile.demoModeLayoutId;
numFolderRows = closestProfile.numFolderRows;
@@ -351,8 +346,8 @@ public class InvariantDeviceProfile {
horizontalMargin = displayOption.horizontalMargin;
numShownHotseatIcons = overrideOptions.getNumHotseatColumns();
numDatabaseHotseatIcons = overrideOptions.getNumHotseatColumns();
numShownHotseatIcons = dbGridInfo.getNumHotseatColumns();
numDatabaseHotseatIcons = dbGridInfo.getNumHotseatColumns();
numAllAppsColumns = closestProfile.numAllAppsColumns;
numDatabaseAllAppsColumns = deviceType == TYPE_MULTI_DISPLAY
@@ -414,8 +409,8 @@ public class InvariantDeviceProfile {
public void setCurrentGrid(Context context, String gridName) {
DeviceProfileOverrides.INSTANCE.get(context).setCurrentGrid(gridName);
Context appContext = context.getApplicationContext();
Utilities.getPrefs(appContext).edit().putString(KEY_IDP_GRID_NAME, gridName).apply();
MAIN_EXECUTOR.execute(() -> onConfigChanged(appContext));
}
@@ -501,7 +496,9 @@ public class InvariantDeviceProfile {
/**
* @return all the grid options that can be shown on the device
*/
public List<GridOption> parseAllGridOptions(Context context) {
public static List<GridOption> parseAllGridOptions(Context context) {
Info defaultInfo = DisplayController.INSTANCE.get(context).getInfo();
@DeviceType int deviceType = getDeviceType(defaultInfo);
List<GridOption> result = new ArrayList<>();
try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
@@ -83,7 +83,7 @@ public class GridCustomizationsProvider extends ContentProvider {
MatrixCursor cursor = new MatrixCursor(new String[] {
KEY_NAME, KEY_ROWS, KEY_COLS, KEY_PREVIEW_COUNT, KEY_IS_DEFAULT});
InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(getContext());
for (GridOption gridOption : idp.parseAllGridOptions(getContext())) {
for (GridOption gridOption : InvariantDeviceProfile.parseAllGridOptions(getContext())) {
cursor.newRow()
.add(KEY_NAME, gridOption.name)
.add(KEY_ROWS, gridOption.numRows)
@@ -128,7 +128,7 @@ public class GridCustomizationsProvider extends ContentProvider {
InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(getContext());
// Verify that this is a valid grid option
GridOption match = null;
for (GridOption option : idp.parseAllGridOptions(getContext())) {
for (GridOption option : InvariantDeviceProfile.parseAllGridOptions(getContext())) {
if (option.name.equals(gridName)) {
match = option;
break;