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:
Update 2
I have similar issues on IOS on some devices, most problematic is:
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With