Skip to content

Commit 924e052

Browse files
Feat: Added custom gestures.
Added Gestures: - DoubleTap. - Swipe: Up, Down, Left, Right. Signed-off-by: HeCodes2Much <wayne6324@gmail.com>
1 parent c290024 commit 924e052

File tree

9 files changed

+373
-161
lines changed

9 files changed

+373
-161
lines changed

app/src/main/java/com/github/droidworksstudio/launcher/accessibility/ActionService.kt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import android.accessibilityservice.AccessibilityServiceInfo
55
import android.content.Context
66
import android.content.Intent
77
import android.os.Build
8+
import android.os.Handler
9+
import android.os.Looper
810
import android.provider.Settings
911
import android.view.accessibility.AccessibilityEvent
1012
import androidx.annotation.RequiresApi
@@ -61,6 +63,7 @@ class ActionService : AccessibilityService() {
6163
return performGlobalAction(GLOBAL_ACTION_QUICK_SETTINGS)
6264
}
6365

66+
@RequiresApi(Build.VERSION_CODES.P)
6467
fun openPowerDialog(): Boolean {
6568
return performGlobalAction(GLOBAL_ACTION_POWER_DIALOG)
6669
}
@@ -78,22 +81,23 @@ class ActionService : AccessibilityService() {
7881
companion object {
7982
fun runAccessibilityMode(context: Context) {
8083
if (instance() == null) {
81-
val state: String = context.getString(R.string.accessibility_settings_enable)
82-
83-
val builder = MaterialAlertDialogBuilder(context)
84-
85-
builder.setTitle(R.string.accessibility_settings_title)
86-
builder.setMessage(R.string.accessibility_service_desc)
87-
builder.setPositiveButton(state) { _, _ ->
88-
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
89-
context.startActivity(intent)
90-
}
91-
builder.setNegativeButton(android.R.string.cancel) { dialog, _ ->
92-
dialog.dismiss()
84+
// Create a Handler that posts to the main thread
85+
Handler(Looper.getMainLooper()).post {
86+
val state: String = context.getString(R.string.accessibility_settings_enable)
87+
88+
val builder = MaterialAlertDialogBuilder(context)
89+
builder.setTitle(R.string.accessibility_settings_title)
90+
builder.setMessage(R.string.accessibility_service_desc)
91+
builder.setPositiveButton(state) { _, _ ->
92+
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
93+
context.startActivity(intent)
94+
}
95+
builder.setNegativeButton(android.R.string.cancel) { dialog, _ ->
96+
dialog.dismiss()
97+
}
98+
builder.show()
9399
}
94-
builder.show()
95100
}
96-
return
97101
}
98102

99103
private var mInstance: WeakReference<ActionService> = WeakReference(null)

app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AppHelper @Inject constructor() {
4444
}
4545

4646
@SuppressLint("WrongConstant", "PrivateApi")
47-
private fun expandQuickSettings(context: Context) {
47+
fun expandQuickSettings(context: Context) {
4848
try {
4949
Class.forName(Constants.QUICKSETTINGS_MANAGER)
5050
.getMethod(Constants.QUICKSETTINGS_METHOD)
@@ -192,33 +192,4 @@ class AppHelper @Inject constructor() {
192192
fun restoreSharedPreferences(context: Context) {
193193
context.restoreSharedPreferences(context.getString(R.string.settings_backups_file))
194194
}
195-
196-
fun enableAppAsAccessibilityService(context: Context, accessibilityState: Boolean) {
197-
val state: String = if (accessibilityState) {
198-
context.getString(R.string.accessibility_settings_disable)
199-
} else {
200-
context.getString(R.string.accessibility_settings_enable)
201-
}
202-
203-
when (state) {
204-
"Enable" -> {
205-
val builder = MaterialAlertDialogBuilder(context)
206-
207-
builder.setTitle(R.string.accessibility_settings_title)
208-
builder.setMessage(R.string.accessibility_service_desc)
209-
builder.setPositiveButton(state) { _, _ ->
210-
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
211-
context.startActivity(intent)
212-
}
213-
builder.setNegativeButton(android.R.string.cancel) { dialog, _ ->
214-
dialog.dismiss()
215-
}
216-
builder.show()
217-
}
218-
219-
else -> {
220-
return
221-
}
222-
}
223-
}
224195
}

app/src/main/java/com/github/droidworksstudio/launcher/helper/PreferenceHelper.kt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,10 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
106106
get() = prefs.getFloat(Constants.DAILY_WORD_TEXT_SIZE, 18f)
107107
set(value) = prefs.edit().putFloat(Constants.DAILY_WORD_TEXT_SIZE, value).apply()
108108

109-
var tapLockScreen: Boolean
110-
get() = prefs.getBoolean(Constants.DOUBLE_TAP_LOCK, false)
111-
set(value) = prefs.edit().putBoolean(Constants.DOUBLE_TAP_LOCK, value).apply()
112-
113109
var settingsLock: Boolean
114110
get() = prefs.getBoolean(Constants.TOGGLE_SETTING_LOCK, false)
115111
set(value) = prefs.edit().putBoolean(Constants.TOGGLE_SETTING_LOCK, value).apply()
116112

117-
var swipeNotification: Boolean
118-
get() = prefs.getBoolean(Constants.SWIPE_NOTIFICATION, true)
119-
set(value) = prefs.edit().putBoolean(Constants.SWIPE_NOTIFICATION, value).apply()
120-
121-
var swipeSearch: Boolean
122-
get() = prefs.getBoolean(Constants.SWIPE_SEARCH, false)
123-
set(value) = prefs.edit().putBoolean(Constants.SWIPE_SEARCH, value).apply()
124-
125113
var searchEngines: Constants.SearchEngines
126114
get() {
127115
return try {
@@ -136,4 +124,36 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
136124
}
137125
}
138126
set(value) = prefs.edit().putString(Constants.SEARCH_ENGINE, value.name).apply()
127+
128+
var swipeUpAction: Constants.Action
129+
get() = loadAction(Constants.SWIPE_UP_ACTION, Constants.Action.ShowRecents)
130+
set(value) = storeAction(Constants.SWIPE_UP_ACTION, value)
131+
132+
var swipeDownAction: Constants.Action
133+
get() = loadAction(Constants.SWIPE_DOWN_ACTION, Constants.Action.ShowNotification)
134+
set(value) = storeAction(Constants.SWIPE_DOWN_ACTION, value)
135+
136+
var swipeLeftAction: Constants.Action
137+
get() = loadAction(Constants.SWIPE_LEFT_ACTION, Constants.Action.ShowAppList)
138+
set(value) = storeAction(Constants.SWIPE_LEFT_ACTION, value)
139+
140+
var swipeRightAction: Constants.Action
141+
get() = loadAction(Constants.SWIPE_RIGHT_ACTION, Constants.Action.ShowFavoriteList)
142+
set(value) = storeAction(Constants.SWIPE_RIGHT_ACTION, value)
143+
144+
var doubleTapAction: Constants.Action
145+
get() = loadAction(Constants.DOUBLE_TAP_ACTION, Constants.Action.LockScreen)
146+
set(value) = storeAction(Constants.DOUBLE_TAP_ACTION, value)
147+
148+
private fun loadAction(prefString: String, default: Constants.Action): Constants.Action {
149+
val string = prefs.getString(
150+
prefString,
151+
default.toString()
152+
).toString()
153+
return Constants.Action.valueOf(string)
154+
}
155+
156+
private fun storeAction(prefString: String, value: Constants.Action) {
157+
prefs.edit().putString(prefString, value.name).apply()
158+
}
139159
}

app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import android.content.IntentFilter
88
import android.os.BatteryManager
99
import android.os.Build
1010
import android.os.Bundle
11+
import android.os.Handler
12+
import android.os.Looper
1113
import android.text.format.DateFormat
1214
import android.util.Log
1315
import android.view.*
@@ -25,7 +27,6 @@ import com.github.droidworksstudio.common.launchApp
2527
import com.github.droidworksstudio.common.launchCalendar
2628
import com.github.droidworksstudio.common.launchClock
2729
import com.github.droidworksstudio.common.openBatteryManager
28-
import com.github.droidworksstudio.common.searchView
2930
import com.github.droidworksstudio.common.showLongToast
3031
import com.github.droidworksstudio.launcher.R
3132
import com.github.droidworksstudio.launcher.accessibility.ActionService
@@ -38,6 +39,7 @@ import com.github.droidworksstudio.launcher.listener.OnItemClickedListener
3839
import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener
3940
import com.github.droidworksstudio.launcher.listener.ScrollEventListener
4041
import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.AppInfoBottomSheetFragment
42+
import com.github.droidworksstudio.launcher.utils.Constants
4143
import com.github.droidworksstudio.launcher.viewmodel.AppViewModel
4244
import com.github.droidworksstudio.launcher.viewmodel.PreferenceViewModel
4345
import dagger.hilt.android.AndroidEntryPoint
@@ -295,33 +297,81 @@ class HomeFragment : Fragment(),
295297
@RequiresApi(Build.VERSION_CODES.P)
296298
override fun onDoubleClick() {
297299
super.onDoubleClick()
298-
if (preferenceHelper.tapLockScreen) {
299-
ActionService.runAccessibilityMode(context)
300-
ActionService.instance()?.lockScreen()
301-
} else {
302-
return
303-
}
300+
handleOtherAction(preferenceHelper.doubleTapAction)
301+
}
302+
303+
@RequiresApi(Build.VERSION_CODES.P)
304+
override fun onSwipeUp() {
305+
super.onSwipeUp()
306+
handleOtherAction(preferenceHelper.swipeUpAction)
307+
}
308+
309+
@RequiresApi(Build.VERSION_CODES.P)
310+
override fun onSwipeDown() {
311+
super.onSwipeDown()
312+
handleOtherAction(preferenceHelper.swipeDownAction)
304313
}
305314

315+
@RequiresApi(Build.VERSION_CODES.P)
306316
override fun onSwipeLeft() {
307317
super.onSwipeLeft()
308-
findNavController().navigate(R.id.action_HomeFragment_to_DrawFragment)
318+
handleOtherAction(preferenceHelper.swipeLeftAction)
309319
}
310320

321+
@RequiresApi(Build.VERSION_CODES.P)
311322
override fun onSwipeRight() {
312323
super.onSwipeRight()
313-
findNavController().navigate(R.id.action_HomeFragment_to_FavoriteFragment)
324+
handleOtherAction(preferenceHelper.swipeRightAction)
314325
}
326+
}
327+
}
315328

316-
override fun onSwipeDown() {
317-
super.onSwipeDown()
318-
if (preferenceHelper.swipeNotification) appHelper.expandNotificationDrawer(context)
329+
@RequiresApi(Build.VERSION_CODES.P)
330+
private fun handleOtherAction(action: Constants.Action) {
331+
when (action) {
332+
// Constants.Action.OpenApp -> {}
333+
334+
Constants.Action.LockScreen -> {
335+
ActionService.runAccessibilityMode(context)
336+
ActionService.instance()?.lockScreen()
319337
}
320338

321-
override fun onSwipeUp() {
322-
super.onSwipeUp()
323-
if (preferenceHelper.swipeSearch) context.searchView()
339+
Constants.Action.ShowNotification -> {
340+
appHelper.expandNotificationDrawer(context)
341+
}
342+
343+
Constants.Action.ShowAppList -> {
344+
Handler(Looper.getMainLooper()).post {
345+
findNavController().navigate(R.id.action_HomeFragment_to_DrawFragment)
346+
}
324347
}
348+
349+
Constants.Action.ShowFavoriteList -> {
350+
Handler(Looper.getMainLooper()).post {
351+
findNavController().navigate(R.id.action_HomeFragment_to_FavoriteFragment)
352+
}
353+
}
354+
355+
Constants.Action.OpenQuickSettings -> {
356+
appHelper.expandQuickSettings(context)
357+
}
358+
359+
Constants.Action.ShowRecents -> {
360+
ActionService.runAccessibilityMode(context)
361+
ActionService.instance()?.showRecents()
362+
}
363+
364+
Constants.Action.OpenPowerDialog -> {
365+
ActionService.runAccessibilityMode(context)
366+
ActionService.instance()?.openPowerDialog()
367+
}
368+
369+
Constants.Action.TakeScreenShot -> {
370+
ActionService.runAccessibilityMode(context)
371+
ActionService.instance()?.takeScreenShot()
372+
}
373+
374+
Constants.Action.Disabled -> {}
325375
}
326376
}
327377

0 commit comments

Comments
 (0)