Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 27f13de1 authored by Gustav Sennton's avatar Gustav Sennton
Browse files

Fix WebView loading logic to allow changing provider if current disabled

Add an XML tag declaring whether a package can be used without
explicitly being chosen by the user (it is available-by-default).

Change the WebView loading logic to either
1. load a user-chosen and enabled package or
2. load an enabled and available-by-default package or
3. any package that is valid

Bug: 26400585
Change-Id: I8de253c1687e3cc7961184c2d770d4e385d6187a
parent 56a99538
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -131,6 +131,8 @@ public final class WebViewFactory {
    private static String TAG_WEBVIEW_PROVIDER = "webviewprovider";
    private static String TAG_PACKAGE_NAME = "packageName";
    private static String TAG_DESCRIPTION = "description";
    // Whether or not the provider must be explicitly chosen by the user to be used.
    private static String TAG_AVAILABILITY = "availableByDefault";
    private static String TAG_SIGNATURE = "signature";

    /**
@@ -180,8 +182,12 @@ public final class WebViewFactory {
                        throw new MissingWebViewPackageException(
                                "WebView provider in framework resources missing description");
                    }
                    String availableByDefault = parser.getAttributeValue(null, TAG_AVAILABILITY);
                    if (availableByDefault == null) {
                        availableByDefault = "false";
                    }
                    webViewProviders.add(
                            new WebViewProviderInfo(packageName, description,
                            new WebViewProviderInfo(packageName, description, availableByDefault,
                                readSignatures(parser)));
                }
                else {
+37 −1
Original line number Diff line number Diff line
@@ -40,9 +40,11 @@ public class WebViewProviderInfo implements Parcelable {
        public WebViewPackageNotFoundException(Exception e) { super(e); }
    }

    public WebViewProviderInfo(String packageName, String description, String[] signatures) {
    public WebViewProviderInfo(String packageName, String description, String availableByDefault,
            String[] signatures) {
        this.packageName = packageName;
        this.description = description;
        this.availableByDefault = availableByDefault.equals("true");
        this.signatures = signatures;
    }

@@ -89,6 +91,39 @@ public class WebViewProviderInfo implements Parcelable {
        return false;
    }

    /**
     * Returns whether this package is enabled.
     * This state can be changed by the user from Settings->Apps
     */
    public boolean isEnabled() {
        try {
            PackageManager pm = AppGlobals.getInitialApplication().getPackageManager();
            int enabled_state = pm.getApplicationEnabledSetting(packageName);
            switch (enabled_state) {
                case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
                    return true;
                case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
                    ApplicationInfo applicationInfo = getPackageInfo().applicationInfo;
                    return applicationInfo.enabled;
                default:
                    return false;
            }
        } catch (WebViewPackageNotFoundException e) {
            return false;
        } catch (IllegalArgumentException e) {
            // Thrown by PackageManager.getApplicationEnabledSetting if the package does not exist
            return false;
        }
    }

    /**
     * Returns whether the provider is always available as long as it is valid.
     * If this returns false, the provider will only be used if the user chose this provider.
     */
    public boolean isAvailableByDefault() {
        return availableByDefault;
    }

    public PackageInfo getPackageInfo() {
        if (packageInfo == null) {
            try {
@@ -135,6 +170,7 @@ public class WebViewProviderInfo implements Parcelable {
    // fields read from framework resource
    public String packageName;
    public String description;
    private boolean availableByDefault;

    private String[] signatures;

+1 −1
Original line number Diff line number Diff line
@@ -16,6 +16,6 @@

<webviewproviders>
    <!-- The default WebView implementation -->
    <webviewprovider description="Android WebView" packageName="com.android.webview">
    <webviewprovider description="Android WebView" packageName="com.android.webview" availableByDefault="true">
    </webviewprovider>
</webviewproviders>
+11 −2
Original line number Diff line number Diff line
@@ -291,15 +291,24 @@ public class WebViewUpdateService extends SystemService {

        // If the user has chosen provider, use that
        for (WebViewProviderInfo provider : providers) {
            if (provider.packageName.equals(userChosenProvider)) {
            if (provider.packageName.equals(userChosenProvider) && provider.isEnabled()) {
                return provider.getPackageInfo();
            }
        }

        // User did not choose, or the choice failed, use the most stable provider available
        // User did not choose, or the choice failed; use the most stable provider that is
        // enabled and available by default (not through user choice).
        for (WebViewProviderInfo provider : providers) {
            if (provider.isAvailableByDefault() && provider.isEnabled()) {
                return provider.getPackageInfo();
            }
        }

        // Could not find any enabled package either, use the most stable provider.
        for (WebViewProviderInfo provider : providers) {
            return provider.getPackageInfo();
        }

        mAnyWebViewInstalled = false;
        throw new WebViewFactory.MissingWebViewPackageException(
                "Could not find a loadable WebView package");