Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Android WebView is consuming touch events

TL;DR

How can I detect whether Android WebView consumed a touch event? onTouchEvent always returns true and WebViewClient's onUnhandledInputEvent is never triggered.

Detailed description

I have multiple WebViews inside a TwoDScrollView. As its name suggests, the TwoDScrollView can be scrolled both vertically and horizontally. The contents of TwoDScrollView can be zoomed in / out. When the user drags his finger or uses pinch-to-zoom, I want to dispatch the touch event to:

  • WebView if its content is scrollable / zoomable (i.e. only the inside of the WebView will scroll / zoom)
  • TwoDScrollView if the above condition is false (all contents of the TwoDScrollView will scroll / zoom)

I have partially achieved this by using the canScrollHorizontally and canScrollVertically methods. But these methods only work for "native scrolling". However, in some cases, some JavaScript inside the WebView consumes the touch event, for example Google Maps. In this case, the methods return false. Is there any way to find out whether the WebView's contents consumes the touch events, i.e. is scrollable / zoomable? I cannot change the contents of the WebView, therefore my question is different from this one.

I have considered checking touch handlers by executing some JavaScript inside the Webview by the evaluateJavaScript method, but according to this answer there is no easy way to achieve this and also the page can have some other nested iframes. Any help will be appreciated.

What I've already tried

  1. I overrode WebView's onTouchEvent and read super.onTouchEvent() which always returns true, no matter what.
  2. canScrollHorizontally and canScrollVertically only partially solve this problem, as mentioned above
  3. onScrollChanged isn't useful either
  4. WebViewClient.onUnhandledInputEvent is never triggered
  5. I considered using JavaScript via evaluateJavaScript, but it is a very complicated and ugly solution
  6. I tried to trace the MotionEvent by Debug.startMethodTracing. I found out it is propagated as follows:
    • android.webkit.WebView.onTouchEvent
    • com.android.webview.chromium.WebViewChromium.onTouchEvent
    • com.android.org.chromium.android_webview.AwContents.onTouchEvent
    • com.android.org.chromium.android_webview.AwContents$AwViewMethodsImpl.onTouchEvent
    • com.android.org.chromium.content.browser.ContentViewCore.onTouchEventImpl
    • According to ContentViewCore's source code the touch event ends up in a native method nativeOnTouchEvent and I don't know what further happens with it. Anyway, onTouchEvent always returns true and even if it was possible to find out somewhere whether the event was consumed or not, it would require using private methods which is also quite ugly.

Note

I don't need to know how to intercept touch events sent to WebView, but whether the WebView is consuming them, i.e. is using them for doing anything, such as scrolling, dragging etc.

like image 908
Miloš Černilovský Avatar asked Apr 23 '15 11:04

Miloš Černilovský


People also ask

How do I turn off Touch listener on Android?

Use btn. setEnabled(false) to temporarily disable it and then btn. setEnabled(true) to enable it again.

How are Android touch events delivered?

The touch event is passed in as a MotionEvent , which contains the x,y coordinates, time, type of event, and other information. The touch event is sent to the Window's superDispatchTouchEvent() . Window is an abstract class. The actual implementation is PhoneWindow .

Which method you should override to control your touch action?

To make sure that each view correctly receives the touch events intended for it, override the onInterceptTouchEvent() method.


1 Answers

According to this issue report, not possible. If the web code is under your control, you can implement some JavaScriptInterface to workaround this. If not, I am afraid there is no solution here.

like image 76
hqzxzwb Avatar answered Sep 22 '22 22:09

hqzxzwb