I'm developing android library that logs all touch inputs of an app. I already managed to intercept all touch inputs for activities (and fragments), but not popups.
how to detect touch inputs for new windows that pop up in the activity?
The following code shows how I intercept all touch inputs for a activity window.
Inside of Application.onCreate()
I call Application.registerActivityLifecycleCallbacks()
, and register following callback:
override fun onActivityCreated(activity: Activity, savedState: Bundle?) {
val localCallback = activity.window.callback
activity.window.callback = object : ActivityWindowCallbackWrapper(activity, localCallback) {
override fun dispatchTouchEvent(motionEvent: MotionEvent): Boolean {
//over here I can log all touch input
return super.dispatchTouchEvent(motionEvent)
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
//here I can detect that some popup has popped up
//if hasFocus == false it means that some other window has focus -> probably popup
super.onWindowFocusChanged(hasFocus)
}
}
}
This code intercepts all touch events for an activity window and it recognizes when a different window is focused (Another Window pops up), but I don't know how to register the callback for the newly focused window.
here is a great example on how to detect all root views of an application, but I can not register any callbacks on them.