Change OnTouch Methods to GestureHandler

This commit is contained in:
Finn M Glas 2020-05-15 14:10:24 +02:00
parent 44c31dd3a3
commit 2893437b14

View file

@ -1,18 +1,15 @@
package com.finnmglas.launcher
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences.Editor
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.view.View.OnLongClickListener
import android.view.View.OnTouchListener
import android.view.WindowManager
import android.view.*
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.MotionEventCompat
import androidx.core.view.GestureDetectorCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*
@ -27,7 +24,13 @@ var leftApp = "com.google.android.calendar"
var volumeUpApp = "com.whatsapp"
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
val displayMetrics = DisplayMetrics()
@ -51,91 +54,49 @@ class MainActivity : AppCompatActivity() {
applicationContext.startActivity(intent2)
overridePendingTransition(0, 0)
} 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 launchClock(v: View){ launchApp("com.sec.android.app.clockpackage") }
fun launchCalendar(v: View) {
launchApp("com.google.android.calendar", "com.samsung.android.calendar")
}
fun launchUpApp() { launchApp(upApp) }
fun launchDownApp() { launchApp(downApp) }
fun lauchLeftApp() { launchApp(leftApp) }
fun lauchRightApp() { launchApp(rightApp) }
fun launchClock(v: View) {
launchApp("com.sec.android.app.clockpackage")
}
fun lauchVolumeUpApp() { launchApp(volumeUpApp) }
fun lauchVolumeDownApp() { launchApp(volumeDownApp) }
fun launchUpApp() {
launchApp(upApp)
}
fun launchDownApp() {
launchApp(downApp)
}
fun lauchLeftApp() {
launchApp(leftApp)
}
fun lauchRightApp() {
launchApp(rightApp)
}
fun lauchVolumeUpApp() {
launchApp(volumeUpApp)
}
fun lauchVolumeDownApp() {
launchApp(volumeDownApp)
}
/* 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 {
if (keyCode == KeyEvent.KEYCODE_BACK) return true
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) lauchVolumeUpApp()
@ -147,7 +108,10 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
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)
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
@ -161,5 +125,86 @@ class MainActivity : AppCompatActivity() {
}
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
}
}