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

Commit 9da55a7a authored by Matt Pietal's avatar Matt Pietal
Browse files

Controls UI - Correctly persist seeding state

It is incorrect to attempt to modify a Set that is returned from
SharedPreferences.getStringSet(). Won't work. Create a new Set.

Also handle the case on device restore, where we need to set the
seeded flag when controls exist.

Also fix the uninstall logic to look at the actual service list and
not the favorites list

Fixes: 158678748
Test: seed, remove all controls, and restart phone
Change-Id: I3dbc1c7cbac999b06b572b8348cf41617a73adde
parent a4614b0f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -200,9 +200,9 @@ class ControlsControllerImpl @Inject constructor (
                    GlobalActionsDialog.PREFS_CONTROLS_FILE, Context.MODE_PRIVATE)
                val completedSeedingPackageSet = prefs.getStringSet(
                    GlobalActionsDialog.PREFS_CONTROLS_SEEDING_COMPLETED, mutableSetOf<String>())
                val favoritePackageSet = favoriteComponentSet.map { it.packageName }
                val servicePackageSet = serviceInfoSet.map { it.packageName }
                prefs.edit().putStringSet(GlobalActionsDialog.PREFS_CONTROLS_SEEDING_COMPLETED,
                    completedSeedingPackageSet.intersect(favoritePackageSet)).apply()
                    completedSeedingPackageSet.intersect(servicePackageSet)).apply()

                var changed = false
                favoriteComponentSet.subtract(serviceInfoSet).forEach {
+23 −23
Original line number Diff line number Diff line
@@ -247,7 +247,6 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
    private final Executor mBackgroundExecutor;
    private List<ControlsServiceInfo> mControlsServiceInfos = new ArrayList<>();
    private Optional<ControlsController> mControlsControllerOptional;
    private SharedPreferences mControlsPreferences;
    private final RingerModeTracker mRingerModeTracker;
    private int mDialogPressDelay = DIALOG_PRESS_DELAY; // ms
    private Handler mMainHandler;
@@ -405,12 +404,6 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
                    });
        }

        // Need to be user-specific with the context to make sure we read the correct prefs
        Context userContext = context.createContextAsUser(
                new UserHandle(mUserManager.getUserHandle()), 0);
        mControlsPreferences = userContext.getSharedPreferences(PREFS_CONTROLS_FILE,
            Context.MODE_PRIVATE);

        // Listen for changes to show controls on the power menu while locked
        onPowerMenuLockScreenSettingsChanged();
        mContext.getContentResolver().registerContentObserver(
@@ -444,21 +437,24 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
                Collections.emptySet());

        List<ComponentName> componentsToSeed = new ArrayList<>();
        for (int i = 0; i < Math.min(SEEDING_MAX, preferredControlsPackages.length); i++) {
            String pkg = preferredControlsPackages[i];
            for (ControlsServiceInfo info : mControlsServiceInfos) {
            String pkg = info.componentName.getPackageName();
            if (seededPackages.contains(pkg)
                    || mControlsControllerOptional.get().countFavoritesForComponent(
                            info.componentName) > 0) {
                continue;
                if (!pkg.equals(info.componentName.getPackageName())) continue;
                if (seededPackages.contains(pkg)) {
                    break;
                } else if (mControlsControllerOptional.get()
                        .countFavoritesForComponent(info.componentName) > 0) {
                    // When there are existing controls but no saved preference, assume it
                    // is out of sync, perhaps through a device restore, and update the
                    // preference
                    addPackageToSeededSet(prefs, pkg);
                    break;
                }

            for (int i = 0; i < Math.min(SEEDING_MAX, preferredControlsPackages.length); i++) {
                if (pkg.equals(preferredControlsPackages[i])) {
                componentsToSeed.add(info.componentName);
                break;
            }
        }
        }

        if (componentsToSeed.isEmpty()) return;

@@ -466,16 +462,20 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
                componentsToSeed,
                (response) -> {
                    Log.d(TAG, "Controls seeded: " + response);
                    Set<String> completedPkgs = prefs.getStringSet(PREFS_CONTROLS_SEEDING_COMPLETED,
                                                                   new HashSet<String>());
                    if (response.getAccepted()) {
                        completedPkgs.add(response.getPackageName());
                        prefs.edit().putStringSet(PREFS_CONTROLS_SEEDING_COMPLETED,
                                                  completedPkgs).apply();
                        addPackageToSeededSet(prefs, response.getPackageName());
                    }
                });
    }

    private void addPackageToSeededSet(SharedPreferences prefs, String pkg) {
        Set<String> seededPackages = prefs.getStringSet(PREFS_CONTROLS_SEEDING_COMPLETED,
                Collections.emptySet());
        Set<String> updatedPkgs = new HashSet<>(seededPackages);
        updatedPkgs.add(pkg);
        prefs.edit().putStringSet(PREFS_CONTROLS_SEEDING_COMPLETED, updatedPkgs).apply();
    }

    /**
     * Show the global actions dialog (creating if necessary)
     *