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

Commit e3fffa96 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Fix "app doesn't support backup and restore" bug

Always make sure to scan all packages before back up. There was a case
where SPI.updateFromPackageInfo() wasn't called before backup.

Fix: 72638013
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest1 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest2 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest3 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest4 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest5 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest6 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest7 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest8 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest9 -w com.android.frameworks.servicestests
Test: adb shell am instrument -w -e class com.android.server.pm.ShortcutManagerTest10 -w com.android.frameworks.servicestests
Test: atest CtsShortcutHostTestCases
Change-Id: I7fd39a62924a2d3724dfbd759ed157933af3f357
parent b4edb988
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -230,7 +230,7 @@ class ShortcutLauncher extends ShortcutPackageItem {
        out.startTag(null, TAG_ROOT);
        ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName());
        ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId());
        getPackageInfo().saveToXml(out, forBackup);
        getPackageInfo().saveToXml(mShortcutUser.mService, out, forBackup);

        for (int i = 0; i < size; i++) {
            final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
+1 −1
Original line number Diff line number Diff line
@@ -1348,7 +1348,7 @@ class ShortcutPackage extends ShortcutPackageItem {
        ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
        ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
        ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
        getPackageInfo().saveToXml(out, forBackup);
        getPackageInfo().saveToXml(mShortcutUser.mService, out, forBackup);

        for (int j = 0; j < size; j++) {
            saveShortcut(out, mShortcuts.valueAt(j), forBackup,
+10 −1
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ class ShortcutPackageInfo {
    private static final String ATTR_LAST_UPDATE_TIME = "last_udpate_time";
    private static final String ATTR_BACKUP_SOURCE_VERSION = "bk_src_version";
    private static final String ATTR_BACKUP_ALLOWED = "allow-backup";
    private static final String ATTR_BACKUP_ALLOWED_INITIALIZED = "allow-backup-initialized";
    private static final String ATTR_BACKUP_SOURCE_BACKUP_ALLOWED = "bk_src_backup-allowed";
    private static final String ATTR_SHADOW = "shadow";

@@ -187,7 +188,11 @@ class ShortcutPackageInfo {
        mSigHashes = BackupUtils.hashSignatureArray(pi.signatures);
    }

    public void saveToXml(XmlSerializer out, boolean forBackup) throws IOException {
    public void saveToXml(ShortcutService s, XmlSerializer out, boolean forBackup)
            throws IOException {
        if (forBackup && !mBackupAllowedInitialized) {
            s.wtf("Backup happened before mBackupAllowed is initialized.");
        }

        out.startTag(null, TAG_ROOT);

@@ -196,6 +201,10 @@ class ShortcutPackageInfo {
        ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow);
        ShortcutService.writeAttr(out, ATTR_BACKUP_ALLOWED, mBackupAllowed);

        // We don't need to save this field (we don't even read it back), but it'll show up
        // in the dumpsys in the backup / restore payload.
        ShortcutService.writeAttr(out, ATTR_BACKUP_ALLOWED_INITIALIZED, mBackupAllowedInitialized);

        ShortcutService.writeAttr(out, ATTR_BACKUP_SOURCE_VERSION, mBackupSourceVersionCode);
        ShortcutService.writeAttr(out,
                ATTR_BACKUP_SOURCE_BACKUP_ALLOWED, mBackupSourceBackupAllowed);
+9 −4
Original line number Diff line number Diff line
@@ -3545,9 +3545,11 @@ public class ShortcutService extends IShortcutService.Stub {
            // Update the signatures for all packages.
            user.forAllPackageItems(spi -> spi.refreshPackageSignatureAndSave());

            // Rescan all apps; this will also update the version codes and "allow-backup".
            user.forAllPackages(pkg -> pkg.rescanPackageIfNeeded(
                    /*isNewApp=*/ false, /*forceRescan=*/ true));

            // Set the version code for the launchers.
            // We shouldn't do this for publisher packages, because we don't want to update the
            // version code without rescanning the manifest.
            user.forAllLaunchers(launcher -> launcher.ensurePackageInfo());

            // Save to the filesystem.
@@ -3566,7 +3568,9 @@ public class ShortcutService extends IShortcutService.Stub {
                Slog.w(TAG, "Backup failed.", e);
                return null;
            }
            return os.toByteArray();
            byte[] payload = os.toByteArray();
            mShortcutDumpFiles.save("backup-1-payload.txt", payload);
            return payload;
        }
    }

@@ -3846,6 +3850,8 @@ public class ShortcutService extends IShortcutService.Stub {
                pw.print(next);
                pw.print("] ");
                pw.print(formatTime(next));
                pw.println();
                pw.println();

                pw.print("  Config:");
                pw.print("    Max icon dim: ");
@@ -4241,7 +4247,6 @@ public class ShortcutService extends IShortcutService.Stub {
    }

    // Injection point.
    @VisibleForTesting
    String injectBuildFingerprint() {
        return Build.FINGERPRINT;
    }
+22 −5
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import android.content.pm.ShortcutManager;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
import android.util.Slog;

@@ -62,6 +61,7 @@ class ShortcutUser {
    // Suffix "2" was added to force rescan all packages after the next OTA.
    private static final String ATTR_LAST_APP_SCAN_TIME = "last-app-scan-time2";
    private static final String ATTR_LAST_APP_SCAN_OS_FINGERPRINT = "last-app-scan-fp";
    private static final String ATTR_RESTORE_SOURCE_FINGERPRINT = "restore-from-fp";
    private static final String KEY_USER_ID = "userId";
    private static final String KEY_LAUNCHERS = "launchers";
    private static final String KEY_PACKAGES = "packages";
@@ -128,6 +128,7 @@ class ShortcutUser {
    private long mLastAppScanTime;

    private String mLastAppScanOsFingerprint;
    private String mRestoreFromOsFingerprint;

    public ShortcutUser(ShortcutService service, int userId) {
        mService = service;
@@ -340,8 +341,13 @@ class ShortcutUser {
                    mLastAppScanTime);
            ShortcutService.writeAttr(out, ATTR_LAST_APP_SCAN_OS_FINGERPRINT,
                    mLastAppScanOsFingerprint);
            ShortcutService.writeAttr(out, ATTR_RESTORE_SOURCE_FINGERPRINT,
                    mRestoreFromOsFingerprint);

            ShortcutService.writeTagValue(out, TAG_LAUNCHER, mLastKnownLauncher);
        } else {
            ShortcutService.writeAttr(out, ATTR_RESTORE_SOURCE_FINGERPRINT,
                    mService.injectBuildFingerprint());
        }

        // Can't use forEachPackageItem due to the checked exceptions.
@@ -387,6 +393,8 @@ class ShortcutUser {
            ret.mLastAppScanTime = lastAppScanTime < currentTime ? lastAppScanTime : 0;
            ret.mLastAppScanOsFingerprint = ShortcutService.parseStringAttribute(parser,
                    ATTR_LAST_APP_SCAN_OS_FINGERPRINT);
            ret.mRestoreFromOsFingerprint = ShortcutService.parseStringAttribute(parser,
                    ATTR_RESTORE_SOURCE_FINGERPRINT);
            final int outerDepth = parser.getDepth();
            int type;
            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
@@ -524,6 +532,8 @@ class ShortcutUser {
        restored.mLaunchers.clear();
        restored.mPackages.clear();

        mRestoreFromOsFingerprint = restored.mRestoreFromOsFingerprint;

        Slog.i(TAG, "Restored: L=" + restoredLaunchers[0]
                + " P=" + restoredPackages[0]
                + " S=" + restoredShortcuts[0]);
@@ -539,12 +549,19 @@ class ShortcutUser {
            pw.print("  Last app scan: [");
            pw.print(mLastAppScanTime);
            pw.print("] ");
            pw.print(ShortcutService.formatTime(mLastAppScanTime));
            pw.println(ShortcutService.formatTime(mLastAppScanTime));

            prefix += prefix + "  ";

            pw.print(prefix);
            pw.print("Last app scan FP: ");
            pw.print(mLastAppScanOsFingerprint);
            pw.println(mLastAppScanOsFingerprint);

            pw.print(prefix);
            pw.print("Restore from FP: ");
            pw.print(mRestoreFromOsFingerprint);
            pw.println();

            prefix += prefix + "  ";

            pw.print(prefix);
            pw.print("Cached launcher: ");