implement #93 - treat back button as a gesture
Some checks failed
Android CI / build (push) Has been cancelled

This commit is contained in:
Josia Pietsch 2025-02-06 22:27:58 +01:00
parent fa2f1c4127
commit 944eb89fef
Signed by: jrpie
GPG key ID: E70B571D66986A2D
12 changed files with 108 additions and 13 deletions

View file

@ -21,6 +21,9 @@ sealed interface Action {
fun getIcon(context: Context): Drawable? fun getIcon(context: Context): Drawable?
fun isAvailable(context: Context): Boolean fun isAvailable(context: Context): Boolean
// Can the action be used to reach µLauncher settings?
fun canReachSettings(): Boolean
fun bindToGesture(prefEditor: Editor, id: String) { fun bindToGesture(prefEditor: Editor, id: String) {
prefEditor.putString(id, Json.encodeToString(this)) prefEditor.putString(id, Json.encodeToString(this))
@ -50,7 +53,10 @@ sealed interface Action {
.map { Pair(it, Json.decodeFromString<Action>(it)) } .map { Pair(it, Json.decodeFromString<Action>(it)) }
.firstOrNull { it.second.isAvailable(context) } .firstOrNull { it.second.isAvailable(context) }
?.apply { ?.apply {
boundActions.add(first) // allow to bind CHOOSE to multiple gestures
if (second != LauncherAction.CHOOSE) {
boundActions.add(first)
}
second.bindToGesture(editor, gesture.id) second.bindToGesture(editor, gesture.id)
} }
} }

View file

@ -74,4 +74,8 @@ class AppAction(val app: AppInfo) : Action {
// check if app is installed // check if app is installed
return DetailedAppInfo.fromAppInfo(app, context) != null return DetailedAppInfo.fromAppInfo(app, context) != null
} }
override fun canReachSettings(): Boolean {
return false
}
} }

View file

@ -168,6 +168,12 @@ enum class Gesture(
R.string.settings_gesture_description_double_right, R.string.settings_gesture_description_double_right,
R.array.default_double_right, R.array.default_double_right,
R.anim.left_right R.anim.left_right
),
BACK(
"action.back",
R.string.settings_gesture_back,
R.string.settings_gesture_description_back,
R.array.default_up
); );
enum class Edge { enum class Edge {

View file

@ -13,6 +13,7 @@ import android.os.UserManager
import android.provider.Settings import android.provider.Settings
import android.view.KeyEvent import android.view.KeyEvent
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.widget.AppCompatDrawableManager
import de.jrpie.android.launcher.Application import de.jrpie.android.launcher.Application
import de.jrpie.android.launcher.R import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.apps.AppFilter import de.jrpie.android.launcher.apps.AppFilter
@ -40,25 +41,29 @@ enum class LauncherAction(
val label: Int, val label: Int,
val icon: Int, val icon: Int,
val launch: (Context) -> Unit, val launch: (Context) -> Unit,
val available: (Context) -> Boolean = { true } private val canReachSettings: Boolean = false,
val available: (Context) -> Boolean = { true },
) : Action { ) : Action {
SETTINGS( SETTINGS(
"settings", "settings",
R.string.list_other_settings, R.string.list_other_settings,
R.drawable.baseline_settings_24, R.drawable.baseline_settings_24,
::openSettings ::openSettings,
true
), ),
CHOOSE( CHOOSE(
"choose", "choose",
R.string.list_other_list, R.string.list_other_list,
R.drawable.baseline_menu_24, R.drawable.baseline_menu_24,
::openAppsList ::openAppsList,
true
), ),
CHOOSE_FROM_FAVORITES( CHOOSE_FROM_FAVORITES(
"choose_from_favorites", "choose_from_favorites",
R.string.list_other_list_favorites, R.string.list_other_list_favorites,
R.drawable.baseline_favorite_24, R.drawable.baseline_favorite_24,
{ context -> openAppsList(context, true) } { context -> openAppsList(context, true) },
true
), ),
TOGGLE_PRIVATE_SPACE_LOCK( TOGGLE_PRIVATE_SPACE_LOCK(
"toggle_private_space_lock", "toggle_private_space_lock",
@ -127,7 +132,11 @@ enum class LauncherAction(
} }
override fun isAvailable(context: Context): Boolean { override fun isAvailable(context: Context): Boolean {
return true return this.available(context)
}
override fun canReachSettings(): Boolean {
return this.canReachSettings
} }
companion object { companion object {

View file

@ -7,6 +7,7 @@ import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.apps.AppInfo import de.jrpie.android.launcher.apps.AppInfo
import de.jrpie.android.launcher.apps.DetailedAppInfo import de.jrpie.android.launcher.apps.DetailedAppInfo
import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersion1 import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersion1
import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersion2
import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersionUnknown import de.jrpie.android.launcher.preferences.legacy.migratePreferencesFromVersionUnknown
import de.jrpie.android.launcher.ui.HomeActivity import de.jrpie.android.launcher.ui.HomeActivity
@ -14,7 +15,7 @@ import de.jrpie.android.launcher.ui.HomeActivity
* Increase when breaking changes are introduced and write an appropriate case in * Increase when breaking changes are introduced and write an appropriate case in
* `migratePreferencesToNewVersion` * `migratePreferencesToNewVersion`
*/ */
const val PREFERENCE_VERSION = 2 const val PREFERENCE_VERSION = 3
const val UNKNOWN_PREFERENCE_VERSION = -1 const val UNKNOWN_PREFERENCE_VERSION = -1
private const val TAG = "Launcher - Preferences" private const val TAG = "Launcher - Preferences"
@ -32,13 +33,16 @@ fun migratePreferencesToNewVersion(context: Context) {
UNKNOWN_PREFERENCE_VERSION -> { /* still using the old preferences file */ UNKNOWN_PREFERENCE_VERSION -> { /* still using the old preferences file */
migratePreferencesFromVersionUnknown(context) migratePreferencesFromVersionUnknown(context)
Log.i(TAG, "migration of preferences complete (${UNKNOWN_PREFERENCE_VERSION} -> ${PREFERENCE_VERSION}).")
Log.i(TAG, "migration of preferences complete.")
} }
1 -> { 1 -> {
migratePreferencesFromVersion1() migratePreferencesFromVersion1()
Log.i(TAG, "migration of preferences complete.") Log.i(TAG, "migration of preferences complete (1 -> ${PREFERENCE_VERSION}).")
}
2 -> {
migratePreferencesFromVersion2()
Log.i(TAG, "migration of preferences complete (2 -> ${PREFERENCE_VERSION}).")
} }
else -> { else -> {

View file

@ -117,11 +117,12 @@ private fun migrateAction(key: String) {
* (see [PREFERENCE_VERSION]) * (see [PREFERENCE_VERSION])
*/ */
fun migratePreferencesFromVersion1() { fun migratePreferencesFromVersion1() {
assert(PREFERENCE_VERSION == 2)
assert(LauncherPreferences.internal().versionCode() == 1) assert(LauncherPreferences.internal().versionCode() == 1)
Gesture.entries.forEach { g -> migrateAction(g.id) } Gesture.entries.forEach { g -> migrateAction(g.id) }
migrateAppInfoSet(LauncherPreferences.apps().keys().hidden()) migrateAppInfoSet(LauncherPreferences.apps().keys().hidden())
migrateAppInfoSet(LauncherPreferences.apps().keys().favorites()) migrateAppInfoSet(LauncherPreferences.apps().keys().favorites())
migrateAppInfoStringMap(LauncherPreferences.apps().keys().customNames()) migrateAppInfoStringMap(LauncherPreferences.apps().keys().customNames())
LauncherPreferences.internal().versionCode(2) LauncherPreferences.internal().versionCode(2)
migratePreferencesFromVersion2()
} }

View file

@ -0,0 +1,20 @@
package de.jrpie.android.launcher.preferences.legacy
import de.jrpie.android.launcher.actions.Action
import de.jrpie.android.launcher.actions.Gesture
import de.jrpie.android.launcher.actions.LauncherAction
import de.jrpie.android.launcher.preferences.LauncherPreferences
import de.jrpie.android.launcher.preferences.PREFERENCE_VERSION
/**
* Migrate preferences from version 2 (used until version 0.0.21) to the current format
* (see [PREFERENCE_VERSION])
*/
fun migratePreferencesFromVersion2() {
assert(PREFERENCE_VERSION == 3)
assert(LauncherPreferences.internal().versionCode() == 2)
// previously there was no setting for this
Action.setActionForGesture(Gesture.BACK, LauncherAction.CHOOSE)
LauncherPreferences.internal().versionCode(3)
}

View file

@ -50,7 +50,6 @@ private const val TAG = "Preferences ? -> 1"
* and a different file was used. * and a different file was used.
*/ */
fun migratePreferencesFromVersionUnknown(context: Context) { fun migratePreferencesFromVersionUnknown(context: Context) {
assert(PREFERENCE_VERSION == 2)
Log.i( Log.i(
TAG, TAG,

View file

@ -9,6 +9,7 @@ import android.util.DisplayMetrics
import android.view.GestureDetector import android.view.GestureDetector
import android.view.KeyEvent import android.view.KeyEvent
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration import android.view.ViewConfiguration
import android.window.OnBackInvokedDispatcher import android.window.OnBackInvokedDispatcher
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -54,6 +55,10 @@ class HomeActivity : UIObject, AppCompatActivity(),
) { ) {
recreate() recreate()
} }
if (prefKey?.startsWith("action.") == true) {
updateSettingsFallbackButtonVisibility()
}
} }
private var edgeWidth = 0.15f private var edgeWidth = 0.15f
@ -80,6 +85,10 @@ class HomeActivity : UIObject, AppCompatActivity(),
handleBack() handleBack()
} }
} }
binding.buttonFallbackSettings.setOnClickListener {
LauncherAction.SETTINGS.invoke(this)
}
} }
@ -96,6 +105,20 @@ class HomeActivity : UIObject, AppCompatActivity(),
} }
private fun updateSettingsFallbackButtonVisibility() {
// If µLauncher settings can not be reached from any action bound to an enabled gesture,
// show the fallback button.
binding.buttonFallbackSettings.visibility = if (
!Gesture.entries.any { g ->
g.isEnabled() && Action.forGesture(g)?.canReachSettings() == true
}
) {
View.VISIBLE
} else {
View.GONE
}
}
private fun initClock() { private fun initClock() {
val locale = Locale.getDefault() val locale = Locale.getDefault()
val dateVisible = LauncherPreferences.clock().dateVisible() val dateVisible = LauncherPreferences.clock().dateVisible()
@ -152,6 +175,7 @@ class HomeActivity : UIObject, AppCompatActivity(),
edgeWidth = LauncherPreferences.enabled_gestures().edgeSwipeEdgeWidth() / 100f edgeWidth = LauncherPreferences.enabled_gestures().edgeSwipeEdgeWidth() / 100f
initClock() initClock()
updateSettingsFallbackButtonVisibility()
} }
override fun onDestroy() { override fun onDestroy() {
@ -299,7 +323,7 @@ class HomeActivity : UIObject, AppCompatActivity(),
private fun handleBack() { private fun handleBack() {
LauncherAction.CHOOSE.launch(this) Gesture.BACK(this)
} }
override fun isHomeScreen(): Boolean { override fun isHomeScreen(): Boolean {

View file

@ -34,4 +34,18 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />
<!-- only shown when µLauncher settings can't be reached by a gesture -->
<ImageView
android:id="@+id/button_fallback_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/settings"
android:visibility="gone"
tools:visibility="visible"
android:padding="20dp"
android:alpha="0.4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/baseline_settings_24"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -3,6 +3,12 @@
<resources> <resources>
<!-- Default Apps for different actions (button-press, swipes ...) --> <!-- Default Apps for different actions (button-press, swipes ...) -->
<!-- Back - Apps list -->
<string-array name="default_back">
<item>{\"type\": \"action:launcher\", \"value\": \"choose\"}</item> <!-- All Apps -->
</string-array>
<!-- Swipe up - Apps list --> <!-- Swipe up - Apps list -->
<string-array name="default_up"> <string-array name="default_up">
<item>{\"type\": \"action:launcher\", \"value\": \"choose\"}</item> <!-- All Apps --> <item>{\"type\": \"action:launcher\", \"value\": \"choose\"}</item> <!-- All Apps -->

View file

@ -25,6 +25,8 @@
- Settings : Apps - Settings : Apps
- -
--> -->
<string name="settings_gesture_back">Back</string>
<string name="settings_gesture_description_back">Back button / back gesture</string>
<string name="settings_gesture_up">Up</string> <string name="settings_gesture_up">Up</string>
<string name="settings_gesture_description_up">Swipe up</string> <string name="settings_gesture_description_up">Swipe up</string>
<string name="settings_gesture_double_up">Double Up</string> <string name="settings_gesture_double_up">Double Up</string>