How to detect the touch inputs of an Android popup

I am developing an Android library that registers all the touch inputs of an application. I have already managed to intercept all the touch inputs for actions (and fragments), but not pop-ups.

How to detect touch inputs for new windows that appear in activity?

The following code shows how I intercept all touch inputs for an activity window.

Inside, Application.onCreate()I call Application.registerActivityLifecycleCallbacks()and register the 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 captures all touch events for the activity window, and it recognizes when another window is focused (another window appears), but I don’t know how to register a callback for a newly focused window.

- , , .

+4

Source: https://habr.com/ru/post/1689552/


All Articles