Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocked JS deviceorientation devicemotion on Android WebView

I've written a JS SDK for Android WebView that collects device orientation and motion. The SDK listens to deviceorientation and devicemotion events on the window like so:

window.addEventListener('devicemotion', (event) => {...})
window.addEventListener('deviceorientation', (event) => {...})

On some devices/integrations, I get no sensors data. I've tried to mimic a "bad" integration, attempting to block the WebView sensors access by adding the following to the app manifest but with no luck. The JS events are still triggered:

<activity
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden|orientation|screenSize"

What are other possible ways to block the WebView from triggering the events, besides disabling JS all together?

Update: Some insights:
the most problematic devices are:

  • Samsung Galaxy Tab a 10.1 SM-T580
  • Samsung Galaxy J5 Prime SM-G570M

Update 2
I have similar issues on IOS on some devices, most problematic is:

  • Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148
like image 781
Shlomi Schwartz Avatar asked Jul 14 '19 11:07

Shlomi Schwartz


1 Answers

Might be, it's not possible to stop only triggering JS events through android, if you want it then it'll disable whole JS.

//disabled
  wView.getSettings().setJavaScriptEnabled(false);
//enabled
  wView.getSettings().setJavaScriptEnabled(true);

As, you told that on some devices you aren't getting sensors data, for this you need to check your webview decleration code. Sample code is here:

  wView= new WebView(this);
  wView.getSettings().setLoadsImagesAutomatically(true);
  wView.getSettings().setJavaScriptEnabled(true);
  wView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

  wView.setWebViewClient(new WebViewClient() {
        @SuppressWarnings("deprecation")
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
        }

        @TargetApi(android.os.Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
            // Redirect to deprecated method, so you can use it in all SDK versions
            onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
        }
    });

  wView.loadUrl(HOST_URL);

In Manifest

<activity
  android:name=".MainActivity"
  android:configChanges="orientation|screenSize" />
like image 95
Ali Azaz Alam Avatar answered Sep 22 '22 08:09

Ali Azaz Alam