Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get WebView implementation programmatically

It is of course possible for a user to choose which WebView implementation to use, as explained here. Is it possible to determine programatically which implementation they have chosen? I know that it's possible to get the User-Agent string programatically, but that's not quite the same information.

enter image description here

enter image description here

In the above example, I want to know that "Chrome Stable" has been selected as the WebView implementation. On some user devices, there is apparently also an option to choose "Android WebView" from the list, even though Chrome is also an option, and even though the Android WebView is disabled in the system apps listing.

This is troubling because presumably the Android System WebView will be receiving no further updates (it will be stuck in the past), while Chrome will be receiving updates and is the more appropriate choice as the WebView implementation. It is even more troubling because the selection of WebView implementation is completely hidden from the ordinary user.

So I want to know particularly if "Android WebView" is selected, even for Android 7.0+ where it would seem more appropriate that Chrome is selected instead.

EDIT... the following is the selection screen for one of my users, on Android 7.0, showing that both "Android WebView" and "Chrome" are options... and the user is 99% sure that "Android WebView" was the default (which was powered by an ancient Chrome 51.0.2704.91)... would have been stuck on this indefinitely had we not tracked this down:

enter image description here

like image 791
drmrbrewer Avatar asked Jan 29 '23 02:01

drmrbrewer


2 Answers

From SDK 26 a new API has been introduced - WebView#getCurrentWebViewPackage():

If WebView has already been loaded into the current process this method will return the package that was used to load it. Otherwise, the package that would be used if the WebView was loaded right now will be returned; this does not cause WebView to be loaded, so this information may become outdated at any time. The WebView package changes either when the current WebView package is updated, disabled, or uninstalled. It can also be changed through a Developer Setting. If the WebView package changes, any app process that has loaded WebView will be killed. The next time the app starts and loads WebView it will use the new WebView package instead.

Starting from API 25 it's not possible to select "Android System WebView":

API 25

API 26

As to your question:

So I want to know particularly if "Android System WebView" is selected, even for Android 7.0+

I cannot see how "Android System WebView" can be selected for Android 7.0+. For earlier versions, obviously, you cannot use getCurrentWebViewPackage() API, which is available for API 26 upward. I've looked through implementation of the method and came up to this chunk of code, which would give the desired output:


    Class webViewFactory = Class.forName("android.webkit.WebViewFactory");
    Method method = webViewFactory.getMethod("getLoadedPackageInfo");
    PackageInfo packageInfo = (PackageInfo) method.invoke(null, null);

    if ("com.android.webview".equals(packageInfo.packageName)) {
        // "Android System WebView" is selected
    } else {
        // something else selected
        // in case of chrome it would be "com.android.chrome"
    }

like image 185
azizbekian Avatar answered Jan 31 '23 18:01

azizbekian


Try "adb shell settings list global" You will get something like "webview_provider=com.chrome.beta" And then, You can use "dumpsys package com.chrome.beta|grep versionName" to get the chrome version!(depends on the packageName of your webview_provider)

If you can't get webview_provider=...., maybe that means you can't set webview implementation in settings.

like image 29
Kai Long Avatar answered Jan 31 '23 17:01

Kai Long