Last active
April 24, 2020 12:07
-
-
Save marcouberti/4c5b5cb793e8ca796bfef14aaea829cc to your computer and use it in GitHub Desktop.
Android Global Multitouch Events Interceptor. This multitouch events interceptor can be plugged into your app without directly coupling it with your activities.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApplication : Application(), SecretMenuTouchManagerListener { | |
override fun onCreate() { | |
super.onCreate() | |
val touchManager = SecretMenuTouchManager(this) | |
val globalTouchListener = SecretMenuGlobalTouchListener(touchManager) | |
} | |
fun onActivateSecretMenu() { | |
// do your stuff here | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.MotionEvent | |
import android.view.ViewGroup | |
import android.widget.FrameLayout | |
/** | |
* This is the invisible [ViewGroup] overlay that intercepts | |
* all the multitouch events. | |
*/ | |
class InvisibleOverlayView @JvmOverloads constructor( | |
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 | |
) : FrameLayout(context, attrs, defStyleAttr) { | |
var activity: Activity? = null | |
private var listener: TouchListener? = null | |
override fun dispatchTouchEvent(event: MotionEvent): Boolean { | |
// handle the motion event | |
listener?.onTouch(event) | |
// but dispatch it to other sibling view group | |
val parentViewGroup = this.parent as? ViewGroup | |
parentViewGroup?.let { parent -> | |
val childCount = parent.childCount | |
for(i in 0..childCount) { | |
val view = parent.getChildAt(i) | |
if(view != this) { | |
view?.dispatchTouchEvent(event) | |
} else { | |
// skip myself | |
} | |
} | |
} | |
// important to return true here, otherwise | |
// multi touch events will not be intercepted, but only ACTION_DOWN | |
return true | |
} | |
fun setTouchListener(listener: TouchListener) { | |
this.listener = listener | |
} | |
interface TouchListener { | |
fun onTouch(event: MotionEvent): Boolean | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment