Merge "DO NOT MERGE ANYWHERE - Clear preferences when package is removed." into nyc-mr2-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
589c217580
@@ -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,15 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Methods below are used to keep track of denied user requests on scoped directory access so
|
||||
* the dialog is not offered when user checked the 'Do not ask again' box
|
||||
@@ -108,6 +118,23 @@ public class LocalPreferences {
|
||||
getPrefs(context).edit().putInt(key, status).apply();
|
||||
}
|
||||
|
||||
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)) {
|
||||
if (editor == null) {
|
||||
editor = prefs.edit();
|
||||
}
|
||||
editor.remove(key);
|
||||
}
|
||||
}
|
||||
if (editor != null) {
|
||||
editor.apply();
|
||||
}
|
||||
}
|
||||
|
||||
private static String getScopedAccessDenialsKey(String packageName, String uuid,
|
||||
String directory) {
|
||||
final int userId = UserHandle.myUserId();
|
||||
|
||||
@@ -23,7 +23,7 @@ import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
|
||||
/**
|
||||
* Clean up {@link RecentsProvider} when packages are removed.
|
||||
* Cleans up {@link RecentsProvider} and {@link LocalPreferences} when packages are removed.
|
||||
*/
|
||||
public class PackageReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
@@ -31,15 +31,19 @@ public class PackageReceiver extends BroadcastReceiver {
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
|
||||
final String action = intent.getAction();
|
||||
final Uri data = intent.getData();
|
||||
final String packageName = data == null ? null : data.getSchemeSpecificPart();
|
||||
|
||||
if (Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(action)) {
|
||||
resolver.call(RecentsProvider.buildRecent(), RecentsProvider.METHOD_PURGE, null, null);
|
||||
|
||||
if (packageName != null) {
|
||||
LocalPreferences.clearPackagePreferences(context, packageName);
|
||||
}
|
||||
} else if (Intent.ACTION_PACKAGE_DATA_CLEARED.equals(action)) {
|
||||
final Uri data = intent.getData();
|
||||
if (data != null) {
|
||||
final String packageName = data.getSchemeSpecificPart();
|
||||
if (packageName != null) {
|
||||
resolver.call(RecentsProvider.buildRecent(), RecentsProvider.METHOD_PURGE_PACKAGE,
|
||||
packageName, null);
|
||||
LocalPreferences.clearPackagePreferences(context, packageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user