Restrict remote debugging on Android WebView - android

I am having a webApp that loads in a webView. I don't want remote debugging to be enabled on the webview.
I have read several blogs and unable to find any related to this query. Can someone provide any pointer on this.
Appreciate your help!!!

allow only for build debug then
if (BuildConfig.DEBUG) {
WebView.setWebContentsDebuggingEnabled(true);
}
or
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
( The default is false.)
source :
Documentation
Java Documentation

Related

How to check if "Multiple users" is enabled

Is there a system setting table or API I can check to see if the "Multiple users" setting is turned on in Settings -> System -> Advanced -> Multiple users?
Thanks!
After a few hours of searching, I found the answer by browsing /data/system/users/0 and looking through settings_system.xml, settings_secure.xml, and settings_global.xml. It turns out what I'm looking for is "user_switcher_enabled" in settings_global.xml.
Here's my code:
public static boolean isMultipleUsersEnabled(Context context) {
try {
int pref = Settings.Global.getInt(context.getContentResolver(), "user_switcher_enabled");
return pref == 1 && (Build.VERSION.SDK_INT < Build.VERSION_CODES.N || UserManager.supportsMultipleUsers());
} catch (Settings.SettingNotFoundException e) {
Utils.logError(TAG, "user_switcher_enabled setting not found: " + e.toString());
return false;
}
}
The check for UserManager.supportsMultipleUsers() is probably not needed, but just in case the preference is somehow bogus on devices that don't actually support multiple users.
If there's anything you want to add to this answer, please comment below.
For API level 24 and above, you can use the method UserManager.supportsMultipleUsers(), which returns whether the device supports multiple users in boolean.
Before API level 24, there are no methods to check this without system permissions. There can be a workaround, like getting the count of users using the method getUserCount(). Again this also needs android.permission.MANAGE_USERS permission.

Why does Google recommend this code for WebView debugging?

To enable WebView debugging based on the debuggable flag, Google recommends the following code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
WebView.setWebContentsDebuggingEnabled(true);
}
}
Doesn't this have side effects, since the &= operator reassigns the flag field? I am assuming the application flags are reduced to FLAG_DEBUGGABLE after this call. Why would you want the &= operator here instead of &?
Yep, it's a bug in documentation. The code is broken. It does indeed strip out any flags from ApplicationInfo.flags other than FLAG_DEBUGGABLE. The correct check is getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE. This check does not modify the flags field.
No, &= means getApplicationInfo().flags = getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE which just sets that flag from 0 to 1.
It is just like += or |= which shortens the expression a bit.
All existing flags will be kept.

Android WebChromeClient supply different method on all Android version, how to use them?

WebChromeClient supply "onShowFileChooser" on Android 5.0, also supply "openFileChooser" method below Android 4.4 method,How to compatible with them? Has anyone managed to do this?
Maybe you can use "Build.VERSION.SDK_INIT" to check the current sdk version on the phone used your app. And decide which to use.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// do something with onShowFileChooser()
} else {
//do something with openFileChooser()
}

Offline Scorm Player for Android

I need to display and track the scorm content in a android application. I have search some post related to scorm player for mobile devices and it was closed too. I have already display the content using imsmanifest.xml
I am stuck with the tracking of LMS in the scorm file. After a long search, I found that I need to execute a JavaScript file for interacting with the scorm file and the application as Scorm_1.2 and Scorm_1.3 for old and newer version of scorm. I have executed the JavaScript too but its not communicating with the scorm file.
It always returning null value for the API object call from the JavaScript from scorm
if (win.API != null) {
return win.API; //It always null value
}
else {
if (win.frames.length > 0) {
for (var i = 0; i < win.frames.length; i++) {
if (win.frames[i] && win.frames[i].API != null) //Here too it comes null value
alert(win.frames[i].API);
return win.frames[i].API;
}
}
if (typeof(win.opener) != "undefined" && win.opener != null) {
return findAPI(win.opener);
}
if (win.parent != window && win.parent != win) {
return findAPI(win.parent);
}
return null;
}
Due to null value from the JavaScript its not communicating with the JavaScript in Scorm_1.2
SCORM is designed to be run from a web browser, not an app. The LMS must launch the course in a popup window or frameset, and the window/frameset must provide the SCORM API in the form of a JavaScript object.
Offline and mobile use cases (like yours) are one of the driving forces behind the Experience API (aka Tin Can).
As mentioned in other StackOverflow posts, there are commercial solutions that provide offline support, but technically they skirt the rules of the SCORM specification.

Android WebView performance

In my app, I am loading a list of external url's in webview and allow user to flip through them. Webviews are loaded on to a view flipper. I find the performance is really bad in webview load url. I have tried everything from using the frame layout to limiting the number of webviews to load. Still the performance is not satisfactory.
How do I optimize the performance of webview? This should be a common usage. Am I missing something obvious.
My Webview settings are -
webView.setInitialScale(WEBVIEW_SCALE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.setWebViewClient(new MyWebViewClient());
webView.setOnTouchListener( new OnTouchListener());
Try this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
I think the following works best:
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Android 19 has Chromium engine for WebView. I guess it works better with hardware acceleration.
For more info Android 4.4 KitKat, the browser and the Chrome WebView
This has already been discussed here: Enhance webView performance (should be the same performance as native Web Browser)
I ran into a similar issue, and after some heavy debugging noticed the
native browser and WebView browser seem to be using different caches.
This code can be used to disable the WebView cache, and made WebView
much faster for me (though at the expense of not caching). Note that
it uses private APIs, so by using it you're risking the code will
break in future releases:
try
{
Method m = CacheManager.class.getDeclaredMethod("setCacheDisabled", boolean.class);
m.setAccessible(true);
m.invoke(null, true);
}
catch (Throwable e)
{
Log.i("myapp","Reflection failed", e);
}
Improvise Answer:
The above Solution mentioned CacheManager.class is not supported.
try {
val m: Method = ServiceWorkerWebSettingsCompat.CacheMode::class.java.getDeclaredMethod(
"setCacheDisabled",
Boolean::class.javaPrimitiveType
)
m.isAccessible = true
m.invoke(null, true)
} catch (e: Throwable) {
Log.i("myapp", "Reflection failed", e)
}

Categories

Resources