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

Commit c19f9562 authored by Felipe Leme's avatar Felipe Leme Committed by Steve McKay
Browse files

Minor improvements on LocalPreferences.

BUG: 30900628
Change-Id: I09ee88152fbcefd0a301e52599e1c71e2f96557d
(cherry picked from commit da1fc0a9)
parent c02b4b0a
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.annotation.IntDef;
import android.annotation.Nullable;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.UserHandle;
import android.preference.PreferenceManager;

@@ -84,6 +85,11 @@ public class LocalPreferences {
    @Retention(RetentionPolicy.SOURCE)
    public @interface PermissionStatus {}

    /**
     * Clears all preferences associated with a given package.
     *
     * <p>Typically called when a package is removed or when user asked to clear its data.
     */
    static void clearPackagePreferences(Context context, String packageName) {
        clearScopedAccessPreferences(context, packageName);
    }
@@ -115,10 +121,17 @@ public class LocalPreferences {
    private static void clearScopedAccessPreferences(Context context, String packageName) {
        final String keySubstring = "|" + packageName + "|";
        final SharedPreferences prefs = getPrefs(context);
        Editor editor = null;
        for (final String key : prefs.getAll().keySet()) {
            if (key.contains(keySubstring)) {
                prefs.edit().remove(key).apply();
                if (editor == null) {
                    editor = prefs.edit();
                }
                editor.remove(key);
            }
        }
        if (editor != null) {
            editor.apply();
        }
    }