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

Commit 8658bbe3 authored by Hai Zhang's avatar Hai Zhang
Browse files

Fix backup and restore for default browser app.

This was broken by ag/6216880 back in Q because it didn't take into
account that writeDefaultAppsLPr() is also used by
getDefaultAppsBackup(), so that it didn't modify
getDefaultAppsBackup() to read the browser role instead when
effectively removing pm/Settings.mDefaultBrowserApp.

This CL fixes the regression in a minimal and backwards-compatible
way. It extracts the XML related code into a static method, and makes
getDefaultAppsBackup() use that static method by passing in the
current default browser obtained from the browser role.

Bug: 271337188
Test: manually perform a restore via cable and verify that the source
      device provided its current default browser to backup helper via
      locally added log.
Change-Id: I1fde685049e9f3371bbef6956418f9a7eb5ff850
Merged-In: I1fde685049e9f3371bbef6956418f9a7eb5ff850
(cherry picked from commit 6963b38a)
parent 5909b534
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -7569,6 +7569,11 @@ public class PackageManagerService implements PackageSender, TestUtilityService
                callback);
    }

    @Nullable
    String getDefaultBrowser(@UserIdInt int userId) {
        return mDefaultAppProvider.getDefaultBrowser(userId);
    }

    void setDefaultBrowser(@Nullable String packageName, boolean async, @UserIdInt int userId) {
        mDefaultAppProvider.setDefaultBrowser(packageName, async, userId);
    }
+3 −9
Original line number Diff line number Diff line
@@ -557,9 +557,8 @@ final class PreferredActivityHelper {
            serializer.startDocument(null, true);
            serializer.startTag(null, TAG_DEFAULT_APPS);

            synchronized (mPm.mLock) {
                mPm.mSettings.writeDefaultAppsLPr(serializer, userId);
            }
            final String defaultBrowser = mPm.getDefaultBrowser(userId);
            Settings.writeDefaultApps(serializer, defaultBrowser);

            serializer.endTag(null, TAG_DEFAULT_APPS);
            serializer.endDocument();
@@ -584,12 +583,7 @@ final class PreferredActivityHelper {
            parser.setInput(new ByteArrayInputStream(backup), StandardCharsets.UTF_8.name());
            restoreFromXml(parser, userId, TAG_DEFAULT_APPS,
                    (parser1, userId1) -> {
                        final String defaultBrowser;
                        synchronized (mPm.mLock) {
                            mPm.mSettings.readDefaultAppsLPw(parser1, userId1);
                            defaultBrowser = mPm.mSettings.removeDefaultBrowserPackageNameLPw(
                                    userId1);
                        }
                        final String defaultBrowser = Settings.readDefaultApps(parser1);
                        if (defaultBrowser != null) {
                            mPm.setDefaultBrowser(defaultBrowser, false, userId1);
                        }
+21 −3
Original line number Diff line number Diff line
@@ -1688,6 +1688,19 @@ public final class Settings implements Watchable, Snappable, ResilientAtomicFile

    void readDefaultAppsLPw(XmlPullParser parser, int userId)
            throws XmlPullParserException, IOException {
        String defaultBrowser = readDefaultApps(parser);
        if (defaultBrowser != null) {
            mDefaultBrowserApp.put(userId, defaultBrowser);
        }
    }

    /**
     * @return the package name for the default browser app, or {@code null} if none.
     */
    @Nullable
    static String readDefaultApps(@NonNull XmlPullParser parser)
            throws XmlPullParserException, IOException {
        String defaultBrowser = null;
        int outerDepth = parser.getDepth();
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
@@ -1697,8 +1710,7 @@ public final class Settings implements Watchable, Snappable, ResilientAtomicFile
            }
            String tagName = parser.getName();
            if (tagName.equals(TAG_DEFAULT_BROWSER)) {
                String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
                mDefaultBrowserApp.put(userId, packageName);
                defaultBrowser = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
            } else if (tagName.equals(TAG_DEFAULT_DIALER)) {
                // Ignored.
            } else {
@@ -1708,6 +1720,7 @@ public final class Settings implements Watchable, Snappable, ResilientAtomicFile
                XmlUtils.skipCurrentTag(parser);
            }
        }
        return defaultBrowser;
    }

    void readBlockUninstallPackagesLPw(TypedXmlPullParser parser, int userId)
@@ -2087,8 +2100,13 @@ public final class Settings implements Watchable, Snappable, ResilientAtomicFile

    void writeDefaultAppsLPr(XmlSerializer serializer, int userId)
            throws IllegalArgumentException, IllegalStateException, IOException {
        serializer.startTag(null, TAG_DEFAULT_APPS);
        String defaultBrowser = mDefaultBrowserApp.get(userId);
        writeDefaultApps(serializer, defaultBrowser);
    }

    static void writeDefaultApps(@NonNull XmlSerializer serializer, @Nullable String defaultBrowser)
            throws IllegalArgumentException, IllegalStateException, IOException {
        serializer.startTag(null, TAG_DEFAULT_APPS);
        if (!TextUtils.isEmpty(defaultBrowser)) {
            serializer.startTag(null, TAG_DEFAULT_BROWSER);
            serializer.attribute(null, ATTR_PACKAGE_NAME, defaultBrowser);