mirror of
https://github.com/jrpie/Launcher.git
synced 2025-02-23 06:21:31 +01:00
Change OnTouch Methods to GestureHandler
This commit is contained in:
parent
44c31dd3a3
commit
2893437b14
1 changed files with 136 additions and 91 deletions
|
@ -1,18 +1,15 @@
|
||||||
package com.finnmglas.launcher
|
package com.finnmglas.launcher
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.SharedPreferences.Editor
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.DisplayMetrics
|
import android.util.DisplayMetrics
|
||||||
import android.view.KeyEvent
|
import android.view.*
|
||||||
import android.view.MotionEvent
|
|
||||||
import android.view.View
|
|
||||||
import android.view.View.OnLongClickListener
|
|
||||||
import android.view.View.OnTouchListener
|
|
||||||
import android.view.WindowManager
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.MotionEventCompat
|
import androidx.core.view.GestureDetectorCompat
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -27,13 +24,19 @@ var leftApp = "com.google.android.calendar"
|
||||||
var volumeUpApp = "com.whatsapp"
|
var volumeUpApp = "com.whatsapp"
|
||||||
var volumeDownApp = "com.sec.android.app.popupcalculator"
|
var volumeDownApp = "com.sec.android.app.popupcalculator"
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity(),
|
||||||
|
GestureDetector.OnGestureListener,
|
||||||
|
GestureDetector.OnDoubleTapListener {
|
||||||
|
|
||||||
|
|
||||||
|
private lateinit var mDetector: GestureDetectorCompat
|
||||||
|
|
||||||
// get device dimensions
|
// get device dimensions
|
||||||
val displayMetrics = DisplayMetrics()
|
val displayMetrics = DisplayMetrics()
|
||||||
|
|
||||||
private fun getIntent(packageName: String) : Intent? {
|
private fun getIntent(packageName: String): Intent? {
|
||||||
val pm = applicationContext.packageManager
|
val pm = applicationContext.packageManager
|
||||||
val intent:Intent? = pm.getLaunchIntentForPackage(packageName)
|
val intent: Intent? = pm.getLaunchIntentForPackage(packageName)
|
||||||
intent?.addCategory(Intent.CATEGORY_LAUNCHER)
|
intent?.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
|
@ -41,101 +44,59 @@ class MainActivity : AppCompatActivity() {
|
||||||
private fun launchApp(packageName: String, fallback: String = "") {
|
private fun launchApp(packageName: String, fallback: String = "") {
|
||||||
val intent1 = getIntent(packageName)
|
val intent1 = getIntent(packageName)
|
||||||
|
|
||||||
if(intent1!=null){
|
if (intent1 != null) {
|
||||||
applicationContext.startActivity(intent1)
|
applicationContext.startActivity(intent1)
|
||||||
overridePendingTransition(0,0)
|
overridePendingTransition(0, 0)
|
||||||
} else {
|
} else {
|
||||||
val intent2 = getIntent(fallback)
|
val intent2 = getIntent(fallback)
|
||||||
|
|
||||||
if(intent2!=null){
|
if (intent2 != null) {
|
||||||
applicationContext.startActivity(intent2)
|
applicationContext.startActivity(intent2)
|
||||||
overridePendingTransition(0,0)
|
overridePendingTransition(0, 0)
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, "Package '$packageName' not found. Change your Settings.", Toast.LENGTH_SHORT).show()
|
Toast.makeText(
|
||||||
|
this,
|
||||||
|
"Package '$packageName' not found. Change your Settings.",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun launchCalendar(v: View){ launchApp("com.google.android.calendar", "com.samsung.android.calendar") }
|
fun launchCalendar(v: View) {
|
||||||
fun launchClock(v: View){ launchApp("com.sec.android.app.clockpackage") }
|
launchApp("com.google.android.calendar", "com.samsung.android.calendar")
|
||||||
|
}
|
||||||
|
|
||||||
fun launchUpApp() { launchApp(upApp) }
|
fun launchClock(v: View) {
|
||||||
fun launchDownApp() { launchApp(downApp) }
|
launchApp("com.sec.android.app.clockpackage")
|
||||||
fun lauchLeftApp() { launchApp(leftApp) }
|
}
|
||||||
fun lauchRightApp() { launchApp(rightApp) }
|
|
||||||
|
|
||||||
fun lauchVolumeUpApp() { launchApp(volumeUpApp) }
|
fun launchUpApp() {
|
||||||
fun lauchVolumeDownApp() { launchApp(volumeDownApp) }
|
launchApp(upApp)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun launchDownApp() {
|
||||||
|
launchApp(downApp)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun lauchLeftApp() {
|
||||||
|
launchApp(leftApp)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun lauchRightApp() {
|
||||||
|
launchApp(rightApp)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun lauchVolumeUpApp() {
|
||||||
|
launchApp(volumeUpApp)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun lauchVolumeDownApp() {
|
||||||
|
launchApp(volumeDownApp)
|
||||||
|
}
|
||||||
|
|
||||||
/* Overrides */
|
/* Overrides */
|
||||||
|
|
||||||
var touchX : Float = 0F
|
|
||||||
var touchY : Float = 0F
|
|
||||||
var touchT : Long = 0L
|
|
||||||
|
|
||||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
|
||||||
|
|
||||||
return when (MotionEventCompat.getActionMasked(event)) {
|
|
||||||
MotionEvent.ACTION_DOWN -> {
|
|
||||||
touchX = event.x
|
|
||||||
touchY = event.y
|
|
||||||
touchT = System.currentTimeMillis()
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
MotionEvent.ACTION_MOVE -> {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
MotionEvent.ACTION_UP -> {
|
|
||||||
windowManager.defaultDisplay.getMetrics(displayMetrics)
|
|
||||||
|
|
||||||
val width = displayMetrics.widthPixels
|
|
||||||
val height = displayMetrics.heightPixels
|
|
||||||
|
|
||||||
val diffX = touchX - event.x
|
|
||||||
val diffY = touchY - event.y
|
|
||||||
val diffT = System.currentTimeMillis() - touchT
|
|
||||||
|
|
||||||
val strictness = 4 // of direction
|
|
||||||
|
|
||||||
/* Decide for an action */
|
|
||||||
|
|
||||||
if (diffY > height/8
|
|
||||||
&& abs(diffY) > strictness * abs(diffX))
|
|
||||||
launchUpApp()
|
|
||||||
|
|
||||||
// Only open if the swipe was not from the phone edge
|
|
||||||
else if (diffY < -height/8
|
|
||||||
&& abs(diffY) > strictness * abs(diffX)
|
|
||||||
&& touchY > 100)
|
|
||||||
launchDownApp()
|
|
||||||
|
|
||||||
else if (diffX > width/4
|
|
||||||
&& abs(diffX) > strictness * abs(diffY))
|
|
||||||
lauchLeftApp()
|
|
||||||
|
|
||||||
else if (diffX < -width/4
|
|
||||||
&& abs(diffX) > strictness * abs(diffY))
|
|
||||||
lauchRightApp()
|
|
||||||
|
|
||||||
// Open Settings on LongPress
|
|
||||||
else if (abs(diffX) < 10
|
|
||||||
&& abs(diffX) < 10 && diffT > 750){
|
|
||||||
startActivity(Intent(this, SettingsActivity::class.java))
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
MotionEvent.ACTION_CANCEL -> {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
MotionEvent.ACTION_OUTSIDE -> {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
else -> super.onTouchEvent(event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
||||||
if (keyCode == KeyEvent.KEYCODE_BACK) return true
|
if (keyCode == KeyEvent.KEYCODE_BACK) return true
|
||||||
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) lauchVolumeUpApp()
|
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) lauchVolumeUpApp()
|
||||||
|
@ -147,7 +108,10 @@ class MainActivity : AppCompatActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
window.setFlags(
|
||||||
|
WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||||
|
WindowManager.LayoutParams.FLAG_FULLSCREEN
|
||||||
|
)
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
|
|
||||||
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
|
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
|
||||||
|
@ -161,5 +125,86 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
|
mDetector = GestureDetectorCompat(this, this)
|
||||||
|
mDetector.setOnDoubleTapListener(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||||
|
return if (mDetector.onTouchEvent(event)) {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
super.onTouchEvent(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDown(event: MotionEvent): Boolean {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFling(
|
||||||
|
e1: MotionEvent,
|
||||||
|
e2: MotionEvent,
|
||||||
|
differenceX: Float,
|
||||||
|
differenceY: Float
|
||||||
|
): Boolean {
|
||||||
|
|
||||||
|
windowManager.defaultDisplay.getMetrics(displayMetrics)
|
||||||
|
val width = displayMetrics.widthPixels
|
||||||
|
val height = displayMetrics.heightPixels
|
||||||
|
|
||||||
|
val diffX = e1.x - e2.x
|
||||||
|
val diffY = e1.y - e2.y
|
||||||
|
|
||||||
|
val strictness = 4 // of direction
|
||||||
|
|
||||||
|
/* Decide for an action */
|
||||||
|
|
||||||
|
if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX)) launchUpApp()
|
||||||
|
// Only open if the swipe was not from the phone edge
|
||||||
|
else if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100) launchDownApp()
|
||||||
|
else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY)) lauchLeftApp()
|
||||||
|
else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY)) lauchRightApp()
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open Settings
|
||||||
|
override fun onLongPress(event: MotionEvent) {
|
||||||
|
startActivity(Intent(this, SettingsActivity::class.java))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onScroll(
|
||||||
|
e1: MotionEvent,
|
||||||
|
e2: MotionEvent,
|
||||||
|
diffX: Float,
|
||||||
|
diffY: Float
|
||||||
|
): Boolean {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onShowPress(event: MotionEvent) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSingleTapUp(event: MotionEvent): Boolean {
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDoubleTap(event: MotionEvent): Boolean {
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDoubleTapEvent(event: MotionEvent): Boolean {
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue