Skip to content

Commit

Permalink
[Feature] - Added Long-Press Swipe Gestures (#374)
Browse files Browse the repository at this point in the history
* Added 4 New Gestures
  • Loading branch information
CreativeCodeCat authored Mar 21, 2024
1 parent 4422228 commit ce09db5
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
prefs.setHomeAppModel(n, appModel)
}

AppDrawerFlag.SetSwipeLeft -> prefs.appSwipeLeft = appModel
AppDrawerFlag.SetSwipeRight -> prefs.appSwipeRight = appModel
AppDrawerFlag.SetSwipeUp -> prefs.appSwipeUp = appModel
AppDrawerFlag.SetSwipeDown -> prefs.appSwipeDown = appModel
AppDrawerFlag.SetSwipeLeft -> prefs.appSwipeLeft = appModel
AppDrawerFlag.SetSwipeRight -> prefs.appSwipeRight = appModel
AppDrawerFlag.SetLongPressSwipeUp -> prefs.appLongPressSwipeUp = appModel
AppDrawerFlag.SetLongPressSwipeDown -> prefs.appLongPressSwipeDown = appModel
AppDrawerFlag.SetLongPressSwipeLeft -> prefs.appLongPressSwipeLeft = appModel
AppDrawerFlag.SetLongPressSwipeRight -> prefs.appLongPressSwipeRight = appModel
AppDrawerFlag.SetClickClock -> prefs.appClickClock = appModel
AppDrawerFlag.SetClickDate -> prefs.appClickDate = appModel
AppDrawerFlag.SetDoubleTap -> prefs.appDoubleTap = appModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ object Constants {
LaunchApp,
HiddenApps,
SetHomeApp,
SetSwipeLeft,
SetSwipeRight,
SetSwipeUp,
SetSwipeDown,
SetSwipeLeft,
SetSwipeRight,
SetLongPressSwipeUp,
SetLongPressSwipeDown,
SetLongPressSwipeLeft,
SetLongPressSwipeRight,
SetClickClock,
SetClickDate,
SetDoubleTap,
Expand Down Expand Up @@ -247,15 +251,15 @@ object Constants {
}

enum class Action : EnumOption {
Disabled,
OpenApp,
LockScreen,
ShowNotification,
ShowAppList,
OpenQuickSettings,
ShowRecents,
OpenPowerDialog,
TakeScreenShot,
ShowNotification;
Disabled;

@Composable
override fun string(): String {
Expand Down
73 changes: 53 additions & 20 deletions app/src/main/java/com/github/hecodes2much/mlauncher/data/Prefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ private const val SWIPE_UP_ACTION = "SWIPE_UP_ACTION"
private const val SWIPE_DOWN_ACTION = "SWIPE_DOWN_ACTION"
private const val SWIPE_RIGHT_ACTION = "SWIPE_RIGHT_ACTION"
private const val SWIPE_LEFT_ACTION = "SWIPE_LEFT_ACTION"
private const val LONG_PRESS_SWIPE_UP_ACTION = "LONG_PRESS_SWIPE_UP_ACTION"
private const val LONG_PRESS_SWIPE_DOWN_ACTION = "LONG_PRESS_SWIPE_DOWN_ACTION"
private const val LONG_PRESS_SWIPE_RIGHT_ACTION = "LONG_PRESS_SWIPE_RIGHT_ACTION"
private const val LONG_PRESS_SWIPE_LEFT_ACTION = "LONG_PRESS_SWIPE_LEFT_ACTION"
private const val CLICK_CLOCK_ACTION = "CLICK_CLOCK_ACTION"
private const val CLICK_DATE_ACTION = "CLICK_DATE_ACTION"
private const val DOUBLE_TAP_ACTION = "DOUBLE_TAP_ACTION"
Expand All @@ -52,10 +56,14 @@ private const val APP_ACTIVITY = "APP_ACTIVITY"
private const val APP_OPACITY = "APP_OPACITY"
private const val APP_THEME = "APP_THEME"

private const val SWIPE_RIGHT = "SWIPE_RIGHT"
private const val SWIPE_LEFT = "SWIPE_LEFT"
private const val SWIPE_DOWN = "SWIPE_DOWN"
private const val SWIPE_UP = "SWIPE_UP"
private const val SWIPE_DOWN = "SWIPE_DOWN"
private const val SWIPE_LEFT = "SWIPE_LEFT"
private const val SWIPE_RIGHT = "SWIPE_RIGHT"
private const val LONG_PRESS_SWIPE_UP = "LONG_PRESS_SWIPE_UP"
private const val LONG_PRESS_SWIPE_DOWN = "LONG_PRESS_SWIPE_DOWN"
private const val LONG_PRESS_SWIPE_LEFT = "LONG_PRESS_SWIPE_LEFT"
private const val LONG_PRESS_SWIPE_RIGHT = "LONG_PRESS_SWIPE_RIGHT"
private const val CLICK_CLOCK = "CLICK_CLOCK"
private const val CLICK_DATE = "CLICK_DATE"
private const val DOUBLE_TAP = "DOUBLE_TAP"
Expand Down Expand Up @@ -216,6 +224,13 @@ class Prefs(val context: Context) {
var followAccentColors: Boolean
get() = prefs.getBoolean(HOME_FOLLOW_ACCENT, false)
set(value) = prefs.edit().putBoolean(HOME_FOLLOW_ACCENT, value).apply()
var swipeUpAction: Constants.Action
get() = loadAction(SWIPE_UP_ACTION, Constants.Action.ShowAppList)
set(value) = storeAction(SWIPE_UP_ACTION, value)

var swipeDownAction: Constants.Action
get() = loadAction(SWIPE_DOWN_ACTION, Constants.Action.ShowNotification)
set(value) = storeAction(SWIPE_DOWN_ACTION, value)

var swipeLeftAction: Constants.Action
get() = loadAction(SWIPE_LEFT_ACTION, Constants.Action.OpenApp)
Expand All @@ -225,13 +240,21 @@ class Prefs(val context: Context) {
get() = loadAction(SWIPE_RIGHT_ACTION, Constants.Action.OpenApp)
set(value) = storeAction(SWIPE_RIGHT_ACTION, value)

var swipeDownAction: Constants.Action
get() = loadAction(SWIPE_DOWN_ACTION, Constants.Action.ShowNotification)
set(value) = storeAction(SWIPE_DOWN_ACTION, value)
var longPressSwipeUpAction: Constants.Action
get() = loadAction(LONG_PRESS_SWIPE_UP_ACTION, Constants.Action.ShowAppList)
set(value) = storeAction(LONG_PRESS_SWIPE_UP_ACTION, value)

var swipeUpAction: Constants.Action
get() = loadAction(SWIPE_UP_ACTION, Constants.Action.ShowAppList)
set(value) = storeAction(SWIPE_UP_ACTION, value)
var longPressSwipeDownAction: Constants.Action
get() = loadAction(LONG_PRESS_SWIPE_DOWN_ACTION, Constants.Action.ShowNotification)
set(value) = storeAction(LONG_PRESS_SWIPE_DOWN_ACTION, value)

var longPressSwipeLeftAction: Constants.Action
get() = loadAction(LONG_PRESS_SWIPE_LEFT_ACTION, Constants.Action.Disabled)
set(value) = storeAction(LONG_PRESS_SWIPE_LEFT_ACTION, value)

var longPressSwipeRightAction: Constants.Action
get() = loadAction(LONG_PRESS_SWIPE_RIGHT_ACTION, Constants.Action.Disabled)
set(value) = storeAction(LONG_PRESS_SWIPE_RIGHT_ACTION, value)

var clickClockAction: Constants.Action
get() = loadAction(CLICK_CLOCK_ACTION, Constants.Action.OpenApp)
Expand Down Expand Up @@ -305,21 +328,31 @@ class Prefs(val context: Context) {
prefs.edit().putString(nameId, name).apply()
}

var appSwipeRight: AppModel
get() = loadApp(SWIPE_RIGHT)
set(appModel) = storeApp(SWIPE_RIGHT, appModel)

var appSwipeLeft: AppModel
get() = loadApp(SWIPE_LEFT)
set(appModel) = storeApp(SWIPE_LEFT, appModel)

var appSwipeUp: AppModel
get() = loadApp(SWIPE_UP)
set(appModel) = storeApp(SWIPE_UP, appModel)
var appSwipeDown: AppModel
get() = loadApp(SWIPE_DOWN)
set(appModel) = storeApp(SWIPE_DOWN, appModel)
var appSwipeLeft: AppModel
get() = loadApp(SWIPE_LEFT)
set(appModel) = storeApp(SWIPE_LEFT, appModel)
var appSwipeRight: AppModel
get() = loadApp(SWIPE_RIGHT)
set(appModel) = storeApp(SWIPE_RIGHT, appModel)

var appSwipeUp: AppModel
get() = loadApp(SWIPE_UP)
set(appModel) = storeApp(SWIPE_UP, appModel)
var appLongPressSwipeUp: AppModel
get() = loadApp(LONG_PRESS_SWIPE_UP)
set(appModel) = storeApp(LONG_PRESS_SWIPE_UP, appModel)
var appLongPressSwipeDown: AppModel
get() = loadApp(LONG_PRESS_SWIPE_DOWN)
set(appModel) = storeApp(LONG_PRESS_SWIPE_DOWN, appModel)
var appLongPressSwipeLeft: AppModel
get() = loadApp(LONG_PRESS_SWIPE_LEFT)
set(appModel) = storeApp(LONG_PRESS_SWIPE_LEFT, appModel)
var appLongPressSwipeRight: AppModel
get() = loadApp(LONG_PRESS_SWIPE_RIGHT)
set(appModel) = storeApp(LONG_PRESS_SWIPE_RIGHT, appModel)

var appClickClock: AppModel
get() = loadApp(CLICK_CLOCK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.text.format.DateFormat
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
Expand Down Expand Up @@ -56,6 +57,8 @@ import com.github.hecodes2much.mlauncher.listener.OnSwipeTouchListener
import com.github.hecodes2much.mlauncher.listener.ViewSwipeTouchListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.math.abs
import kotlin.math.sqrt

class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener {

Expand Down Expand Up @@ -301,6 +304,17 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
}
}

private fun openSwipeUpApp() {
if (prefs.appSwipeUp.appPackage.isNotEmpty())
launchApp(prefs.appSwipeUp)
else openDialerApp(requireContext())
}
private fun openSwipeDownApp() {
if (prefs.appSwipeDown.appPackage.isNotEmpty())
launchApp(prefs.appSwipeDown)
else openDialerApp(requireContext())
}

private fun openSwipeLeftApp() {
if (prefs.appSwipeLeft.appPackage.isNotEmpty())
launchApp(prefs.appSwipeLeft)
Expand All @@ -313,15 +327,26 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
else openDialerApp(requireContext())
}

private fun openSwipeDownApp() {
if (prefs.appSwipeDown.appPackage.isNotEmpty())
launchApp(prefs.appSwipeDown)
private fun openLongPressSwipeUpApp() {
if (prefs.appLongPressSwipeUp.appPackage.isNotEmpty())
launchApp(prefs.appLongPressSwipeUp)
else openDialerApp(requireContext())
}
private fun openLongPressSwipeDownApp() {
if (prefs.appLongPressSwipeDown.appPackage.isNotEmpty())
launchApp(prefs.appLongPressSwipeDown)
else openDialerApp(requireContext())
}

private fun openSwipeUpApp() {
if (prefs.appSwipeUp.appPackage.isNotEmpty())
launchApp(prefs.appSwipeUp)
private fun openLongPressSwipeLeftApp() {
if (prefs.appLongPressSwipeLeft.appPackage.isNotEmpty())
launchApp(prefs.appLongPressSwipeLeft)
else openCameraApp(requireContext())
}

private fun openLongPressSwipeRightApp() {
if (prefs.appLongPressSwipeRight.appPackage.isNotEmpty())
launchApp(prefs.appLongPressSwipeRight)
else openDialerApp(requireContext())
}

Expand Down Expand Up @@ -395,6 +420,72 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener

private fun getHomeScreenGestureListener(context: Context): View.OnTouchListener {
return object : OnSwipeTouchListener(context) {
private var startX = 0f
private var startY = 0f
private var startTime: Long = 0

@SuppressLint("ClickableViewAccessibility")
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
startX = motionEvent.x
startY = motionEvent.y
startTime = System.currentTimeMillis()
}
MotionEvent.ACTION_UP -> {
val endX = motionEvent.x
val endY = motionEvent.y
val endTime = System.currentTimeMillis()
val duration = endTime - startTime
val deltaX = endX - startX
val deltaY = endY - startY
val distance = sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()

// Check if it's a hold swipe gesture
val holdDurationThreshold = 1000L // Adjust as needed
val swipeDistanceThreshold = 200f // Adjust as needed

if (duration <= holdDurationThreshold && distance >= swipeDistanceThreshold) {
Log.d("deltaX","deltaX: $deltaX, deltaY: $deltaY, distance: $distance, duration: $duration")
onLongPressSwipe(deltaX, deltaY)
}
}
}
return super.onTouch(view, motionEvent)
}

private fun onLongPressSwipe(deltaX: Float, deltaY: Float) {
val direction: String = if (abs(deltaX) < abs(deltaY)) {
if (deltaY < 0) "up" else "down"
} else {
if (deltaX < 0) "left" else "right"
}

when (direction) {
"up" -> when (val action = prefs.longPressSwipeUpAction) {
Action.OpenApp -> openLongPressSwipeUpApp()
else -> handleOtherAction(action)
}
"down" -> when (val action = prefs.longPressSwipeDownAction) {
Action.OpenApp -> openLongPressSwipeDownApp()
else -> handleOtherAction(action)
}
"left" -> when (val action = prefs.longPressSwipeLeftAction) {
Action.OpenApp -> openLongPressSwipeLeftApp()
else -> handleOtherAction(action)
}
"right" -> when (val action = prefs.longPressSwipeRightAction) {
Action.OpenApp -> openLongPressSwipeRightApp()
else -> handleOtherAction(action)
}
else -> showToastLong(
requireContext(),
getString(R.string.text_authentication_cancel)
)
}
Log.d("deltaX", direction)
}

override fun onSwipeLeft() {
super.onSwipeLeft()
when (val action = prefs.swipeLeftAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,50 @@ class SettingsFragment : Fragment() {
onSelect = { j -> setGesture(AppDrawerFlag.SetDoubleTap, j) },
appLabel = prefs.appDoubleTap.appLabel
)
},
{ open, onChange ->
SettingsGestureItem(
title = stringResource(R.string.long_press_swipe_up_app),
fontSize = iconFs,
open = open,
onChange = onChange,
currentAction = prefs.longPressSwipeUpAction,
onSelect = { j -> setGesture(AppDrawerFlag.SetLongPressSwipeUp, j) },
appLabel = prefs.appLongPressSwipeUp.appLabel,
)
},
{ open, onChange ->
SettingsGestureItem(
title = stringResource(R.string.long_press_swipe_down_app),
fontSize = iconFs,
open = open,
onChange = onChange,
currentAction = prefs.longPressSwipeDownAction,
onSelect = { j -> setGesture(AppDrawerFlag.SetLongPressSwipeDown, j) },
appLabel = prefs.appLongPressSwipeDown.appLabel,
)
},
{ open, onChange ->
SettingsGestureItem(
title = stringResource(R.string.long_press_swipe_left_app),
fontSize = iconFs,
open = open,
onChange = onChange,
currentAction = prefs.longPressSwipeLeftAction,
onSelect = { j -> setGesture(AppDrawerFlag.SetLongPressSwipeLeft, j) },
appLabel = prefs.appLongPressSwipeLeft.appLabel,
)
},
{ open, onChange ->
SettingsGestureItem(
title = stringResource(R.string.long_press_swipe_right_app),
fontSize = iconFs,
open = open,
onChange = onChange,
currentAction = prefs.longPressSwipeRightAction,
onSelect = { j -> setGesture(AppDrawerFlag.SetLongPressSwipeRight, j) },
appLabel = prefs.appLongPressSwipeRight.appLabel,
)
}
)
)
Expand Down Expand Up @@ -751,13 +795,17 @@ class SettingsFragment : Fragment() {

private fun setGesture(flag: AppDrawerFlag, action: Action) {
when (flag) {
AppDrawerFlag.SetSwipeLeft -> prefs.swipeLeftAction = action
AppDrawerFlag.SetSwipeRight -> prefs.swipeRightAction = action
AppDrawerFlag.SetSwipeUp -> prefs.swipeUpAction = action
AppDrawerFlag.SetSwipeDown -> prefs.swipeDownAction = action
AppDrawerFlag.SetSwipeLeft -> prefs.swipeLeftAction = action
AppDrawerFlag.SetSwipeRight -> prefs.swipeRightAction = action
AppDrawerFlag.SetClickClock -> prefs.clickClockAction = action
AppDrawerFlag.SetClickDate -> prefs.clickDateAction = action
AppDrawerFlag.SetDoubleTap -> prefs.doubleTapAction = action
AppDrawerFlag.SetLongPressSwipeUp -> prefs.longPressSwipeUpAction = action
AppDrawerFlag.SetLongPressSwipeDown -> prefs.longPressSwipeDownAction = action
AppDrawerFlag.SetLongPressSwipeLeft -> prefs.longPressSwipeLeftAction = action
AppDrawerFlag.SetLongPressSwipeRight -> prefs.longPressSwipeRightAction = action
AppDrawerFlag.SetHomeApp,
AppDrawerFlag.HiddenApps,
AppDrawerFlag.LaunchApp -> {
Expand Down
Loading

0 comments on commit ce09db5

Please sign in to comment.