diff --git a/lawnchair/res/drawable/ic_home_pin.xml b/lawnchair/res/drawable/ic_home_pin.xml new file mode 100644 index 0000000000..73d522ac5f --- /dev/null +++ b/lawnchair/res/drawable/ic_home_pin.xml @@ -0,0 +1,10 @@ + + + diff --git a/lawnchair/res/values/strings.xml b/lawnchair/res/values/strings.xml index f79c692d6f..6f012682dd 100644 --- a/lawnchair/res/values/strings.xml +++ b/lawnchair/res/values/strings.xml @@ -656,6 +656,9 @@ Home screen is locked Prevent changes to the home screen layout + Set as default page + Default home page updated + Pop-up menu Pop-up menu items Edit pop-up menu items diff --git a/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt b/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt index 3e485a7cb1..6e1578ebc0 100644 --- a/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt +++ b/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt @@ -59,6 +59,7 @@ import com.android.launcher3.LauncherAppState import com.android.launcher3.LauncherPrefs import com.android.launcher3.LauncherPrefs.Companion.ENABLE_TWOLINE_ALLAPPS_TOGGLE import com.android.launcher3.R +import com.android.launcher3.Workspace import com.android.launcher3.dagger.ApplicationContext import com.android.launcher3.dagger.LauncherAppComponent import com.android.launcher3.dagger.LauncherAppSingleton @@ -345,6 +346,11 @@ class PreferenceManager2 @Inject constructor( defaultValue = context.resources.getBoolean(R.bool.config_default_lock_home_screen), ) + val defaultHomePage = preference( + key = intPreferencesKey(name = "default_home_page"), + defaultValue = Workspace.DEFAULT_PAGE, + ) + val legacyPopupOptionsMigrated = preference( key = booleanPreferencesKey(name = "legacy_popup_options_migrated"), defaultValue = false, diff --git a/lawnchair/src/app/lawnchair/ui/popup/LauncherOptionsPopup.kt b/lawnchair/src/app/lawnchair/ui/popup/LauncherOptionsPopup.kt index 3af2ea37d0..8f0b173e4c 100644 --- a/lawnchair/src/app/lawnchair/ui/popup/LauncherOptionsPopup.kt +++ b/lawnchair/src/app/lawnchair/ui/popup/LauncherOptionsPopup.kt @@ -1,6 +1,7 @@ package app.lawnchair.ui.popup import android.view.View +import android.widget.Toast import androidx.annotation.DrawableRes import androidx.annotation.StringRes import app.lawnchair.preferences2.PreferenceManager2.Companion.getInstance @@ -23,6 +24,7 @@ object LauncherOptionsPopup { LauncherOptionPopupItem("all_apps", true), LauncherOptionPopupItem("home_settings", true), LauncherOptionPopupItem("sys_settings", false), + LauncherOptionPopupItem("default_page", true), ) fun restoreMissingPopupOptions( @@ -39,7 +41,7 @@ object LauncherOptionsPopup { } prefs2.launcherPopupOrder.setBlocking( - (missingItems + currentOptions).toOptionOrderString(), + (currentOptions + missingItems).toOptionOrderString(), ) } @@ -123,6 +125,13 @@ object LauncherOptionsPopup { LauncherEvent.LAUNCHER_SETTINGS_BUTTON_TAP_OR_LONGPRESS, onStartHomeSettings, ), + "default_page" to OptionItem( + launcher, + R.string.set_default_home_page, + R.drawable.ic_home_pin, + LauncherEvent.IGNORE, + ::setAsDefaultHomePage, + ), ) val options = ArrayList() @@ -137,12 +146,21 @@ object LauncherOptionsPopup { true } } + .filter { it.identifier != "default_page" || !launcher.workspace.isCurrentPageDefault } .mapNotNull { optionsList[it.identifier] } .forEach { options.add(it) } return options } + private fun setAsDefaultHomePage(v: View): Boolean { + val launcher = Launcher.getLauncher(v.context) + val currentPage = launcher.workspace.getNextPage() + launcher.workspace.setDefaultPage(currentPage) + Toast.makeText(launcher, R.string.default_home_page_set, Toast.LENGTH_SHORT).show() + return true + } + fun getMetadataForOption(identifier: String): LauncherOptionMetadata { return when (identifier) { "carousel" -> LauncherOptionMetadata( @@ -186,6 +204,11 @@ object LauncherOptionsPopup { icon = R.drawable.ic_home_screen, ) + "default_page" -> LauncherOptionMetadata( + label = R.string.set_default_home_page, + icon = R.drawable.ic_home_pin, + ) + else -> throw IllegalArgumentException("invalid popup option") } } diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 772729cb4a..300e26e390 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -1596,7 +1596,7 @@ public class Launcher extends StatefulActivity } if (shouldMoveToDefaultScreen && !mWorkspace.isHandlingTouch()) { - if (mWorkspace.getNextPage() != Workspace.DEFAULT_PAGE) { + if (mWorkspace.getNextPage() != mWorkspace.getDefaultPage()) { mWorkspace.post(mWorkspace::moveToDefaultScreen); } else { handleHomeTap(); diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java index 1a433b558f..1a56520e0e 100644 --- a/src/com/android/launcher3/Workspace.java +++ b/src/com/android/launcher3/Workspace.java @@ -598,7 +598,7 @@ public class Workspace extends PagedView * Initializes various states for this workspace. */ protected void initWorkspace() { - mCurrentPage = DEFAULT_PAGE; + mCurrentPage = getDefaultPage(); setClipToPadding(false); setupLayoutTransition(); @@ -1152,6 +1152,12 @@ public class Workspace extends PagedView // Now that we have removed some pages, ensure state description is up to date. updateAccessibilityViewPageDescription(); + + // Reset default home page if it's now out of range after page removal + int storedDefault = PreferenceExtensionsKt.firstBlocking(mPreferenceManager2.getDefaultHomePage()); + if (storedDefault >= getChildCount()) { + setDefaultPage(DEFAULT_PAGE); + } } /** @@ -3536,7 +3542,7 @@ public class Workspace extends PagedView * Calls {@link #snapToPage(int)} on the {@link #DEFAULT_PAGE}, then requests focus on it. */ public void moveToDefaultScreen() { - int page = DEFAULT_PAGE; + int page = getDefaultPage(); if (!workspaceInModalState() && getNextPage() != page) { snapToPage(page); } @@ -3546,6 +3552,33 @@ public class Workspace extends PagedView } } + /** + * Returns the validated default home page index from user preferences. + * Falls back to {@link #DEFAULT_PAGE} if the stored page is out of range. + */ + public int getDefaultPage() { + int storedPage = PreferenceExtensionsKt.firstBlocking(mPreferenceManager2.getDefaultHomePage()); + int pageCount = getChildCount(); + if (storedPage >= 0 && storedPage < pageCount) { + return storedPage; + } + return DEFAULT_PAGE; + } + + /** + * Sets the given page index as the default home page. + */ + public void setDefaultPage(int pageIndex) { + PreferenceExtensionsKt.setBlocking(mPreferenceManager2.getDefaultHomePage(), pageIndex); + } + + /** + * Returns true if the current page is the default home page. + */ + public boolean isCurrentPageDefault() { + return getNextPage() == getDefaultPage(); + } + /** * Set the given view's pivot point to match the workspace's, so that it scales together. Since * both this view and workspace can move, transform the point manually instead of using