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

Commit b03de69d authored by Jacky Wang's avatar Jacky Wang Committed by Cherrypicker Worker
Browse files

Sort applications to avoid backup data change

Bug: 328698829
Fix: 328698829
Test: Manual test
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:ea8ed47d070be8b74297f1ca330dfdf9b196ec29)
Merged-In: I0e5bf1145d972db136bbd85571f015b4ab6fb171
Change-Id: I0e5bf1145d972db136bbd85571f015b4ab6fb171
parent 85e130ad
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/** An implementation to backup and restore battery configurations. */
@@ -321,8 +322,8 @@ public final class BatterySettingsStorage extends ObservableBackupRestoreStorage
                @NonNull BackupContext backupContext, @NonNull OutputStream outputStream)
                throws IOException {
            final long timestamp = System.currentTimeMillis();
            final ArraySet<ApplicationInfo> applications = getInstalledApplications();
            if (applications == null || applications.isEmpty()) {
            final ApplicationInfo[] applications = getInstalledApplications();
            if (applications.length == 0) {
                Log.w(TAG, "no data found in the getInstalledApplications()");
                return EntityBackupResult.DELETE;
            }
@@ -360,15 +361,24 @@ public final class BatterySettingsStorage extends ObservableBackupRestoreStorage
                    TAG,
                    String.format(
                            "backup getInstalledApplications():%d count=%d in %d/ms",
                            applications.size(),
                            applications.length,
                            backupCount,
                            (System.currentTimeMillis() - timestamp)));
            return EntityBackupResult.UPDATE;
        }

        private @Nullable ArraySet<ApplicationInfo> getInstalledApplications() {
            return BatteryOptimizeUtils.getInstalledApplications(
        private ApplicationInfo[] getInstalledApplications() {
            ArraySet<ApplicationInfo> installedApplications =
                    BatteryOptimizeUtils.getInstalledApplications(
                            mApplication, AppGlobals.getPackageManager());
            ApplicationInfo[] applicationInfos = new ApplicationInfo[0];
            if (installedApplications == null || installedApplications.isEmpty()) {
                return applicationInfos;
            }
            applicationInfos = installedApplications.toArray(applicationInfos);
            // sort the list to ensure backup data is stable
            Arrays.sort(applicationInfos, Comparator.comparing(info -> info.packageName));
            return applicationInfos;
        }

        static @NonNull SharedPreferences getSharedPreferences(Context context) {