Commit d3c8f507 authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Migrate some SharedPrefs keys from download to ChromePreferenceManager

Use SharedPreferencesManager consistently instead of SharedPreferences
directly.

Bug: 1022108
Change-Id: I92c343d9ca09cc429fb4b6fa1723534c43beb317
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1992691Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#729812}
parent 03fbbe91
...@@ -44,8 +44,10 @@ import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl; ...@@ -44,8 +44,10 @@ import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl;
import org.chromium.chrome.browser.feature_engagement.TrackerFactory; import org.chromium.chrome.browser.feature_engagement.TrackerFactory;
import org.chromium.chrome.browser.flags.FeatureUtilities; import org.chromium.chrome.browser.flags.FeatureUtilities;
import org.chromium.chrome.browser.media.MediaViewerUtils; import org.chromium.chrome.browser.media.MediaViewerUtils;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.Pref; import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.preferences.PrefServiceBridge; import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.profiles.ProfileManager; import org.chromium.chrome.browser.profiles.ProfileManager;
import org.chromium.chrome.browser.util.ConversionUtils; import org.chromium.chrome.browser.util.ConversionUtils;
...@@ -88,7 +90,6 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi ...@@ -88,7 +90,6 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
DownloadServiceDelegate, ProfileManager.Observer { DownloadServiceDelegate, ProfileManager.Observer {
private static final String TAG = "DownloadService"; private static final String TAG = "DownloadService";
private static final String DOWNLOAD_DIRECTORY = "Download"; private static final String DOWNLOAD_DIRECTORY = "Download";
private static final String DOWNLOAD_UMA_ENTRY = "DownloadUmaEntry";
private static final String DOWNLOAD_RETRY_COUNT_FILE_NAME = "DownloadRetryCount"; private static final String DOWNLOAD_RETRY_COUNT_FILE_NAME = "DownloadRetryCount";
private static final String DOWNLOAD_MANUAL_RETRY_SUFFIX = ".Manual"; private static final String DOWNLOAD_MANUAL_RETRY_SUFFIX = ".Manual";
private static final String DOWNLOAD_TOTAL_RETRY_SUFFIX = ".Total"; private static final String DOWNLOAD_TOTAL_RETRY_SUFFIX = ".Total";
...@@ -97,8 +98,6 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi ...@@ -97,8 +98,6 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
private static final long RESUME_DELAY_MILLIS = 10000; private static final long RESUME_DELAY_MILLIS = 10000;
private static final int UNKNOWN_DOWNLOAD_STATUS = -1; private static final int UNKNOWN_DOWNLOAD_STATUS = -1;
public static final long UNKNOWN_BYTES_RECEIVED = -1; public static final long UNKNOWN_BYTES_RECEIVED = -1;
private static final String PREF_IS_DOWNLOAD_HOME_ENABLED =
"org.chromium.chrome.browser.download.IS_DOWNLOAD_HOME_ENABLED";
private static final Set<String> sFirstSeenDownloadIds = new HashSet<String>(); private static final Set<String> sFirstSeenDownloadIds = new HashSet<String>();
private static final Set<String> sBackgroundDownloadIds = new HashSet<String>(); private static final Set<String> sBackgroundDownloadIds = new HashSet<String>();
...@@ -107,7 +106,7 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi ...@@ -107,7 +106,7 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
private static boolean sIsNetworkListenerDisabled; private static boolean sIsNetworkListenerDisabled;
private static boolean sIsNetworkMetered; private static boolean sIsNetworkMetered;
private final SharedPreferences mSharedPrefs; private final SharedPreferencesManager mSharedPrefs;
private final HashMap<String, DownloadProgress> mDownloadProgressMap = private final HashMap<String, DownloadProgress> mDownloadProgressMap =
new HashMap<String, DownloadProgress>(4, 0.75f); new HashMap<String, DownloadProgress>(4, 0.75f);
...@@ -281,9 +280,9 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi ...@@ -281,9 +280,9 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
protected DownloadManagerService( protected DownloadManagerService(
DownloadNotifier downloadNotifier, Handler handler, long updateDelayInMillis) { DownloadNotifier downloadNotifier, Handler handler, long updateDelayInMillis) {
Context applicationContext = ContextUtils.getApplicationContext(); Context applicationContext = ContextUtils.getApplicationContext();
mSharedPrefs = ContextUtils.getAppSharedPreferences(); mSharedPrefs = SharedPreferencesManager.getInstance();
// Clean up unused shared prefs. TODO(qinmin): remove this after M61. // Clean up unused shared prefs. TODO(qinmin): remove this after M61.
mSharedPrefs.edit().remove(PREF_IS_DOWNLOAD_HOME_ENABLED).apply(); mSharedPrefs.removeKey(ChromePreferenceKeys.DOWNLOAD_IS_DOWNLOAD_HOME_ENABLED);
mDownloadNotifier = downloadNotifier; mDownloadNotifier = downloadNotifier;
mUpdateDelayInMillis = updateDelayInMillis; mUpdateDelayInMillis = updateDelayInMillis;
mHandler = handler; mHandler = handler;
...@@ -469,41 +468,38 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi ...@@ -469,41 +468,38 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
/** /**
* Gets download information from SharedPreferences. * Gets download information from SharedPreferences.
* @param sharedPrefs The SharedPreferences object to parse. * @param sharedPrefs The SharedPreferencesManager to read from.
* @param type Type of the information to retrieve. * @param type Type of the information to retrieve.
* @return download information saved to the SharedPrefs for the given type. * @return download information saved to the SharedPrefs for the given type.
*/ */
@VisibleForTesting @VisibleForTesting
protected static Set<String> getStoredDownloadInfo(SharedPreferences sharedPrefs, String type) { protected static Set<String> getStoredDownloadInfo(
return new HashSet<String>(sharedPrefs.getStringSet(type, new HashSet<String>())); SharedPreferencesManager sharedPrefs, String type) {
return new HashSet<>(sharedPrefs.readStringSet(type));
} }
/** /**
* Stores download information to shared preferences. The information can be * Stores download information to shared preferences. The information can be
* either pending download IDs, or pending OMA downloads. * either pending download IDs, or pending OMA downloads.
* *
* @param sharedPrefs SharedPreferences to update. * @param sharedPrefs SharedPreferencesManager to write to.
* @param type Type of the information. * @param type Type of the information.
* @param downloadInfo Information to be saved. * @param downloadInfo Information to be saved.
* @param forceCommit Whether to synchronously update shared preferences. * @param forceCommit Whether to synchronously update shared preferences.
*/ */
@SuppressLint({"ApplySharedPref", "CommitPrefEdits"}) @SuppressLint({"ApplySharedPref", "CommitPrefEdits"})
static void storeDownloadInfo(SharedPreferences sharedPrefs, String type, static void storeDownloadInfo(SharedPreferencesManager sharedPrefs, String type,
Set<String> downloadInfo, boolean forceCommit) { Set<String> downloadInfo, boolean forceCommit) {
SharedPreferences.Editor editor = sharedPrefs.edit(); boolean success;
if (downloadInfo.isEmpty()) { if (downloadInfo.isEmpty()) {
editor.remove(type); success = sharedPrefs.removeKey(type, forceCommit);
} else { } else {
editor.putStringSet(type, downloadInfo); success = sharedPrefs.writeStringSet(type, downloadInfo, forceCommit);
} }
if (forceCommit) { if (!success) {
// Write synchronously because it might be used on restart and needs to stay up-to-date. // Write synchronously because it might be used on restart and needs to stay up-to-date.
if (!editor.commit()) { Log.e(TAG, "Failed to write DownloadInfo " + type);
Log.e(TAG, "Failed to write DownloadInfo " + type);
}
} else {
editor.apply();
} }
} }
...@@ -1541,7 +1537,8 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi ...@@ -1541,7 +1537,8 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
for (int i = 0; i < mUmaEntries.size(); ++i) { for (int i = 0; i < mUmaEntries.size(); ++i) {
entries.add(mUmaEntries.get(i).getSharedPreferenceString()); entries.add(mUmaEntries.get(i).getSharedPreferenceString());
} }
storeDownloadInfo(mSharedPrefs, DOWNLOAD_UMA_ENTRY, entries, false /* forceCommit */); storeDownloadInfo(mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_UMA_ENTRY, entries,
false /* forceCommit */);
} }
/** /**
...@@ -1563,9 +1560,9 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi ...@@ -1563,9 +1560,9 @@ public class DownloadManagerService implements DownloadController.DownloadNotifi
* Parse the DownloadUmaStatsEntry from the shared preference. * Parse the DownloadUmaStatsEntry from the shared preference.
*/ */
private void parseUMAStatsEntriesFromSharedPrefs() { private void parseUMAStatsEntriesFromSharedPrefs() {
if (mSharedPrefs.contains(DOWNLOAD_UMA_ENTRY)) { if (mSharedPrefs.contains(ChromePreferenceKeys.DOWNLOAD_UMA_ENTRY)) {
Set<String> entries = Set<String> entries = DownloadManagerService.getStoredDownloadInfo(
DownloadManagerService.getStoredDownloadInfo(mSharedPrefs, DOWNLOAD_UMA_ENTRY); mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_UMA_ENTRY);
for (String entryString : entries) { for (String entryString : entries) {
DownloadUmaStatsEntry entry = DownloadUmaStatsEntry.parseFromString(entryString); DownloadUmaStatsEntry entry = DownloadUmaStatsEntry.parseFromString(entryString);
if (entry != null) mUmaEntries.add(entry); if (entry != null) mUmaEntries.add(entry);
......
...@@ -4,12 +4,9 @@ ...@@ -4,12 +4,9 @@
package org.chromium.chrome.browser.download; package org.chromium.chrome.browser.download;
import android.content.SharedPreferences;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ContextUtils;
import org.chromium.base.ObserverList; import org.chromium.base.ObserverList;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.components.offline_items_collection.ContentId; import org.chromium.components.offline_items_collection.ContentId;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -28,13 +25,11 @@ public class DownloadSharedPreferenceHelper { ...@@ -28,13 +25,11 @@ public class DownloadSharedPreferenceHelper {
void onAddOrReplaceDownloadSharedPreferenceEntry(ContentId id); void onAddOrReplaceDownloadSharedPreferenceEntry(ContentId id);
} }
@VisibleForTesting
static final String KEY_PENDING_DOWNLOAD_NOTIFICATIONS = "PendingDownloadNotifications";
private final List<DownloadSharedPreferenceEntry> mDownloadSharedPreferenceEntries = private final List<DownloadSharedPreferenceEntry> mDownloadSharedPreferenceEntries =
new ArrayList<DownloadSharedPreferenceEntry>(); new ArrayList<DownloadSharedPreferenceEntry>();
private final ObserverList<Observer> mObservers = new ObserverList<>(); private final ObserverList<Observer> mObservers = new ObserverList<>();
private SharedPreferences mSharedPrefs; private SharedPreferencesManager mSharedPrefs;
// "Initialization on demand holder idiom" // "Initialization on demand holder idiom"
private static class LazyHolder { private static class LazyHolder {
...@@ -50,7 +45,7 @@ public class DownloadSharedPreferenceHelper { ...@@ -50,7 +45,7 @@ public class DownloadSharedPreferenceHelper {
} }
private DownloadSharedPreferenceHelper() { private DownloadSharedPreferenceHelper() {
mSharedPrefs = ContextUtils.getAppSharedPreferences(); mSharedPrefs = SharedPreferencesManager.getInstance();
parseDownloadSharedPrefs(); parseDownloadSharedPrefs();
} }
...@@ -133,9 +128,11 @@ public class DownloadSharedPreferenceHelper { ...@@ -133,9 +128,11 @@ public class DownloadSharedPreferenceHelper {
* Parse a list of the DownloadSharedPreferenceEntry from |mSharedPrefs|. * Parse a list of the DownloadSharedPreferenceEntry from |mSharedPrefs|.
*/ */
private void parseDownloadSharedPrefs() { private void parseDownloadSharedPrefs() {
if (!mSharedPrefs.contains(KEY_PENDING_DOWNLOAD_NOTIFICATIONS)) return; if (!mSharedPrefs.contains(ChromePreferenceKeys.DOWNLOAD_PENDING_DOWNLOAD_NOTIFICATIONS)) {
return;
}
Set<String> entries = DownloadManagerService.getStoredDownloadInfo( Set<String> entries = DownloadManagerService.getStoredDownloadInfo(
mSharedPrefs, KEY_PENDING_DOWNLOAD_NOTIFICATIONS); mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_PENDING_DOWNLOAD_NOTIFICATIONS);
for (String entryString : entries) { for (String entryString : entries) {
DownloadSharedPreferenceEntry entry = DownloadSharedPreferenceEntry entry =
DownloadSharedPreferenceEntry.parseFromString(entryString); DownloadSharedPreferenceEntry.parseFromString(entryString);
...@@ -185,7 +182,7 @@ public class DownloadSharedPreferenceHelper { ...@@ -185,7 +182,7 @@ public class DownloadSharedPreferenceHelper {
for (int i = 0; i < mDownloadSharedPreferenceEntries.size(); ++i) { for (int i = 0; i < mDownloadSharedPreferenceEntries.size(); ++i) {
entries.add(mDownloadSharedPreferenceEntries.get(i).getSharedPreferenceString()); entries.add(mDownloadSharedPreferenceEntries.get(i).getSharedPreferenceString());
} }
DownloadManagerService.storeDownloadInfo( DownloadManagerService.storeDownloadInfo(mSharedPrefs,
mSharedPrefs, KEY_PENDING_DOWNLOAD_NOTIFICATIONS, entries, forceCommit); ChromePreferenceKeys.DOWNLOAD_PENDING_DOWNLOAD_NOTIFICATIONS, entries, forceCommit);
} }
} }
...@@ -11,7 +11,6 @@ import android.content.Context; ...@@ -11,7 +11,6 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
...@@ -34,7 +33,6 @@ import org.xmlpull.v1.XmlPullParserFactory; ...@@ -34,7 +33,6 @@ import org.xmlpull.v1.XmlPullParserFactory;
import org.chromium.base.ApplicationStatus; import org.chromium.base.ApplicationStatus;
import org.chromium.base.BuildInfo; import org.chromium.base.BuildInfo;
import org.chromium.base.ContentUriUtils; import org.chromium.base.ContentUriUtils;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.ObserverList; import org.chromium.base.ObserverList;
import org.chromium.base.PackageManagerUtils; import org.chromium.base.PackageManagerUtils;
...@@ -44,6 +42,8 @@ import org.chromium.chrome.browser.content.ContentUtils; ...@@ -44,6 +42,8 @@ import org.chromium.chrome.browser.content.ContentUtils;
import org.chromium.chrome.browser.download.DownloadManagerBridge.DownloadEnqueueRequest; import org.chromium.chrome.browser.download.DownloadManagerBridge.DownloadEnqueueRequest;
import org.chromium.chrome.browser.download.DownloadManagerBridge.DownloadEnqueueResponse; import org.chromium.chrome.browser.download.DownloadManagerBridge.DownloadEnqueueResponse;
import org.chromium.chrome.browser.download.items.OfflineContentAggregatorFactory; import org.chromium.chrome.browser.download.items.OfflineContentAggregatorFactory;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.download.R; import org.chromium.chrome.download.R;
import org.chromium.components.download.DownloadCollectionBridge; import org.chromium.components.download.DownloadCollectionBridge;
import org.chromium.components.offline_items_collection.LegacyHelpers; import org.chromium.components.offline_items_collection.LegacyHelpers;
...@@ -90,7 +90,6 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -90,7 +90,6 @@ public class OMADownloadHandler extends BroadcastReceiver {
public interface TestObserver { void onDownloadEnqueued(long downloadId); } public interface TestObserver { void onDownloadEnqueued(long downloadId); }
private static final String TAG = "OMADownloadHandler"; private static final String TAG = "OMADownloadHandler";
private static final String PENDING_OMA_DOWNLOADS = "PendingOMADownloads";
// Valid download descriptor attributes. // Valid download descriptor attributes.
protected static final String OMA_TYPE = "type"; protected static final String OMA_TYPE = "type";
...@@ -121,7 +120,7 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -121,7 +120,7 @@ public class OMADownloadHandler extends BroadcastReceiver {
private static final String DOWNLOAD_STATUS_LOADER_ERROR = "954 Loader Error \n\r"; private static final String DOWNLOAD_STATUS_LOADER_ERROR = "954 Loader Error \n\r";
private final Context mContext; private final Context mContext;
private final SharedPreferences mSharedPrefs; private final SharedPreferencesManager mSharedPrefs;
private final LongSparseArray<DownloadItem> mSystemDownloadIdMap = private final LongSparseArray<DownloadItem> mSystemDownloadIdMap =
new LongSparseArray<DownloadItem>(); new LongSparseArray<DownloadItem>();
private final LongSparseArray<OMAInfo> mPendingOMADownloads = private final LongSparseArray<OMAInfo> mPendingOMADownloads =
...@@ -254,7 +253,7 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -254,7 +253,7 @@ public class OMADownloadHandler extends BroadcastReceiver {
/** Constructor. */ /** Constructor. */
public OMADownloadHandler(Context context) { public OMADownloadHandler(Context context) {
mContext = context; mContext = context;
mSharedPrefs = ContextUtils.getAppSharedPreferences(); mSharedPrefs = SharedPreferencesManager.getInstance();
} }
/** /**
...@@ -883,8 +882,9 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -883,8 +882,9 @@ public class OMADownloadHandler extends BroadcastReceiver {
* Clear any pending OMA downloads by reading them from shared prefs. * Clear any pending OMA downloads by reading them from shared prefs.
*/ */
void clearPendingOMADownloads() { void clearPendingOMADownloads() {
if (mSharedPrefs.contains(PENDING_OMA_DOWNLOADS)) { if (mSharedPrefs.contains(ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS)) {
Set<String> omaDownloads = getStoredDownloadInfo(mSharedPrefs, PENDING_OMA_DOWNLOADS); Set<String> omaDownloads = getStoredDownloadInfo(
mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS);
for (String omaDownload : omaDownloads) { for (String omaDownload : omaDownloads) {
OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload); OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload);
clearPendingOMADownload(entry.mDownloadId, entry.mInstallNotifyURI); clearPendingOMADownload(entry.mDownloadId, entry.mInstallNotifyURI);
...@@ -898,7 +898,8 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -898,7 +898,8 @@ public class OMADownloadHandler extends BroadcastReceiver {
* @param type Type of the information to retrieve. * @param type Type of the information to retrieve.
* @return download information saved to the SharedPrefs for the given type. * @return download information saved to the SharedPrefs for the given type.
*/ */
private static Set<String> getStoredDownloadInfo(SharedPreferences sharedPrefs, String type) { private static Set<String> getStoredDownloadInfo(
SharedPreferencesManager sharedPrefs, String type) {
return DownloadManagerService.getStoredDownloadInfo(sharedPrefs, type); return DownloadManagerService.getStoredDownloadInfo(sharedPrefs, type);
} }
...@@ -911,7 +912,7 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -911,7 +912,7 @@ public class OMADownloadHandler extends BroadcastReceiver {
* @param downloadInfo Information to be saved. * @param downloadInfo Information to be saved.
*/ */
static void storeDownloadInfo( static void storeDownloadInfo(
SharedPreferences sharedPrefs, String type, Set<String> downloadInfo) { SharedPreferencesManager sharedPrefs, String type, Set<String> downloadInfo) {
DownloadManagerService.storeDownloadInfo( DownloadManagerService.storeDownloadInfo(
sharedPrefs, type, downloadInfo, false /* forceCommit */); sharedPrefs, type, downloadInfo, false /* forceCommit */);
} }
...@@ -1058,9 +1059,11 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -1058,9 +1059,11 @@ public class OMADownloadHandler extends BroadcastReceiver {
* @param omaInfo OMA download information to save. * @param omaInfo OMA download information to save.
*/ */
private void addOMADownloadToSharedPrefs(String omaInfo) { private void addOMADownloadToSharedPrefs(String omaInfo) {
Set<String> omaDownloads = getStoredDownloadInfo(mSharedPrefs, PENDING_OMA_DOWNLOADS); Set<String> omaDownloads = getStoredDownloadInfo(
mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS);
omaDownloads.add(omaInfo); omaDownloads.add(omaInfo);
storeDownloadInfo(mSharedPrefs, PENDING_OMA_DOWNLOADS, omaDownloads); storeDownloadInfo(
mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS, omaDownloads);
} }
/** /**
...@@ -1068,12 +1071,14 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -1068,12 +1071,14 @@ public class OMADownloadHandler extends BroadcastReceiver {
* @param downloadId ID to be removed. * @param downloadId ID to be removed.
*/ */
private void removeOMADownloadFromSharedPrefs(long downloadId) { private void removeOMADownloadFromSharedPrefs(long downloadId) {
Set<String> omaDownloads = getStoredDownloadInfo(mSharedPrefs, PENDING_OMA_DOWNLOADS); Set<String> omaDownloads = getStoredDownloadInfo(
mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS);
for (String omaDownload : omaDownloads) { for (String omaDownload : omaDownloads) {
OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload); OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload);
if (entry.mDownloadId == downloadId) { if (entry.mDownloadId == downloadId) {
omaDownloads.remove(omaDownload); omaDownloads.remove(omaDownload);
storeDownloadInfo(mSharedPrefs, PENDING_OMA_DOWNLOADS, omaDownloads); storeDownloadInfo(mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS,
omaDownloads);
return; return;
} }
} }
...@@ -1082,10 +1087,11 @@ public class OMADownloadHandler extends BroadcastReceiver { ...@@ -1082,10 +1087,11 @@ public class OMADownloadHandler extends BroadcastReceiver {
/** /**
* Check if a download ID is in OMA SharedPrefs. * Check if a download ID is in OMA SharedPrefs.
* @param downloadId Download identifier to check. * @param downloadId Download identifier to check.
* @param true if it is in the SharedPrefs, or false otherwise. * @return true if it is in the SharedPrefs, or false otherwise.
*/ */
private boolean isDownloadIdInOMASharedPrefs(long downloadId) { private boolean isDownloadIdInOMASharedPrefs(long downloadId) {
Set<String> omaDownloads = getStoredDownloadInfo(mSharedPrefs, PENDING_OMA_DOWNLOADS); Set<String> omaDownloads = getStoredDownloadInfo(
mSharedPrefs, ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS);
for (String omaDownload : omaDownloads) { for (String omaDownload : omaDownloads) {
OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload); OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload);
if (entry.mDownloadId == downloadId) return true; if (entry.mDownloadId == downloadId) return true;
......
...@@ -28,6 +28,7 @@ import org.chromium.base.test.params.ParameterizedRunner; ...@@ -28,6 +28,7 @@ import org.chromium.base.test.params.ParameterizedRunner;
import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.ChromeFeatureList; import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate; import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.util.browser.Features; import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.offline_items_collection.ContentId; import org.chromium.components.offline_items_collection.ContentId;
...@@ -116,7 +117,7 @@ public class DownloadNotificationServiceTest { ...@@ -116,7 +117,7 @@ public class DownloadNotificationServiceTest {
DownloadNotificationService.clearResumptionAttemptLeft(); DownloadNotificationService.clearResumptionAttemptLeft();
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
SharedPreferences.Editor editor = sharedPrefs.edit(); SharedPreferences.Editor editor = sharedPrefs.edit();
editor.remove(DownloadSharedPreferenceHelper.KEY_PENDING_DOWNLOAD_NOTIFICATIONS); editor.remove(ChromePreferenceKeys.DOWNLOAD_PENDING_DOWNLOAD_NOTIFICATIONS);
editor.apply(); editor.apply();
} }
......
...@@ -20,13 +20,14 @@ import org.junit.runner.RunWith; ...@@ -20,13 +20,14 @@ import org.junit.runner.RunWith;
import org.chromium.base.ApiCompatibilityUtils; import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.AdvancedMockContext; import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
import org.chromium.chrome.browser.download.DownloadManagerBridge.DownloadQueryResult; import org.chromium.chrome.browser.download.DownloadManagerBridge.DownloadQueryResult;
import org.chromium.chrome.browser.download.OMADownloadHandler.OMAInfo; import org.chromium.chrome.browser.download.OMADownloadHandler.OMAInfo;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.test.ChromeBrowserTestRule; import org.chromium.chrome.test.ChromeBrowserTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.components.offline_items_collection.OfflineItem; import org.chromium.components.offline_items_collection.OfflineItem;
...@@ -50,7 +51,6 @@ public class OMADownloadHandlerTest { ...@@ -50,7 +51,6 @@ public class OMADownloadHandlerTest {
@Rule @Rule
public final ChromeBrowserTestRule mBrowserTestRule = new ChromeBrowserTestRule(); public final ChromeBrowserTestRule mBrowserTestRule = new ChromeBrowserTestRule();
private static final String PENDING_OMA_DOWNLOADS = "PendingOMADownloads";
private static final String INSTALL_NOTIFY_URI = "http://test/test"; private static final String INSTALL_NOTIFY_URI = "http://test/test";
private TestInfoBarController mTestInfoBarController; private TestInfoBarController mTestInfoBarController;
...@@ -322,11 +322,13 @@ public class OMADownloadHandlerTest { ...@@ -322,11 +322,13 @@ public class OMADownloadHandlerTest {
// Write a few pending downloads into shared preferences. // Write a few pending downloads into shared preferences.
Set<String> pendingOmaDownloads = new HashSet<>(); Set<String> pendingOmaDownloads = new HashSet<>();
pendingOmaDownloads.add(String.valueOf(downloadId1) + "," + INSTALL_NOTIFY_URI); pendingOmaDownloads.add(String.valueOf(downloadId1) + "," + INSTALL_NOTIFY_URI);
DownloadManagerService.storeDownloadInfo(ContextUtils.getAppSharedPreferences(), DownloadManagerService.storeDownloadInfo(SharedPreferencesManager.getInstance(),
PENDING_OMA_DOWNLOADS, pendingOmaDownloads, false /* forceCommit */); ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS, pendingOmaDownloads,
false /* forceCommit */);
pendingOmaDownloads = DownloadManagerService.getStoredDownloadInfo( pendingOmaDownloads =
ContextUtils.getAppSharedPreferences(), PENDING_OMA_DOWNLOADS); DownloadManagerService.getStoredDownloadInfo(SharedPreferencesManager.getInstance(),
ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS);
Assert.assertEquals(1, pendingOmaDownloads.size()); Assert.assertEquals(1, pendingOmaDownloads.size());
omaHandler.clearPendingOMADownloads(); omaHandler.clearPendingOMADownloads();
...@@ -341,8 +343,9 @@ public class OMADownloadHandlerTest { ...@@ -341,8 +343,9 @@ public class OMADownloadHandlerTest {
}); });
// The pending downloads set in the shared prefs should be empty now. // The pending downloads set in the shared prefs should be empty now.
pendingOmaDownloads = DownloadManagerService.getStoredDownloadInfo( pendingOmaDownloads =
ContextUtils.getAppSharedPreferences(), PENDING_OMA_DOWNLOADS); DownloadManagerService.getStoredDownloadInfo(SharedPreferencesManager.getInstance(),
ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS);
Assert.assertEquals(0, pendingOmaDownloads.size()); Assert.assertEquals(0, pendingOmaDownloads.size());
Assert.assertEquals(omaHandler.mNofityURI, INSTALL_NOTIFY_URI); Assert.assertEquals(omaHandler.mNofityURI, INSTALL_NOTIFY_URI);
...@@ -387,7 +390,8 @@ public class OMADownloadHandlerTest { ...@@ -387,7 +390,8 @@ public class OMADownloadHandlerTest {
}); });
Set<String> downloads = DownloadManagerService.getStoredDownloadInfo( Set<String> downloads = DownloadManagerService.getStoredDownloadInfo(
ContextUtils.getAppSharedPreferences(), PENDING_OMA_DOWNLOADS); SharedPreferencesManager.getInstance(),
ChromePreferenceKeys.DOWNLOAD_PENDING_OMA_DOWNLOADS);
Assert.assertEquals(1, downloads.size()); Assert.assertEquals(1, downloads.size());
OMADownloadHandler.OMAEntry entry = OMADownloadHandler.OMAEntry entry =
OMADownloadHandler.OMAEntry.parseOMAEntry((String) (downloads.toArray()[0])); OMADownloadHandler.OMAEntry.parseOMAEntry((String) (downloads.toArray()[0]));
......
...@@ -237,6 +237,13 @@ public final class ChromePreferenceKeys { ...@@ -237,6 +237,13 @@ public final class ChromePreferenceKeys {
public static final String DATA_REDUCTION_SITE_BREAKDOWN_ALLOWED_DATE = public static final String DATA_REDUCTION_SITE_BREAKDOWN_ALLOWED_DATE =
"data_reduction_site_breakdown_allowed_date"; "data_reduction_site_breakdown_allowed_date";
public static final String DOWNLOAD_IS_DOWNLOAD_HOME_ENABLED =
"org.chromium.chrome.browser.download.IS_DOWNLOAD_HOME_ENABLED";
public static final String DOWNLOAD_PENDING_DOWNLOAD_NOTIFICATIONS =
"PendingDownloadNotifications";
public static final String DOWNLOAD_PENDING_OMA_DOWNLOADS = "PendingOMADownloads";
public static final String DOWNLOAD_UMA_ENTRY = "DownloadUmaEntry";
/** /**
* Indicates whether or not there are prefetched content in chrome that can be viewed offline. * Indicates whether or not there are prefetched content in chrome that can be viewed offline.
*/ */
...@@ -624,6 +631,10 @@ public final class ChromePreferenceKeys { ...@@ -624,6 +631,10 @@ public final class ChromePreferenceKeys {
DATA_REDUCTION_FIRST_ENABLED_TIME, DATA_REDUCTION_FIRST_ENABLED_TIME,
DATA_REDUCTION_FRE_PROMO_OPT_OUT, DATA_REDUCTION_FRE_PROMO_OPT_OUT,
DATA_REDUCTION_SITE_BREAKDOWN_ALLOWED_DATE, DATA_REDUCTION_SITE_BREAKDOWN_ALLOWED_DATE,
DOWNLOAD_IS_DOWNLOAD_HOME_ENABLED,
DOWNLOAD_PENDING_DOWNLOAD_NOTIFICATIONS,
DOWNLOAD_PENDING_OMA_DOWNLOADS,
DOWNLOAD_UMA_ENTRY,
FIRST_RUN_CACHED_TOS_ACCEPTED, FIRST_RUN_CACHED_TOS_ACCEPTED,
FIRST_RUN_FLOW_COMPLETE, FIRST_RUN_FLOW_COMPLETE,
FIRST_RUN_FLOW_SIGNIN_ACCOUNT_NAME, FIRST_RUN_FLOW_SIGNIN_ACCOUNT_NAME,
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.chromium.chrome.browser.preferences; package org.chromium.chrome.browser.preferences;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -124,7 +125,7 @@ public class SharedPreferencesManager { ...@@ -124,7 +125,7 @@ public class SharedPreferencesManager {
Set<String> values = new HashSet<>( Set<String> values = new HashSet<>(
ContextUtils.getAppSharedPreferences().getStringSet(key, Collections.emptySet())); ContextUtils.getAppSharedPreferences().getStringSet(key, Collections.emptySet()));
values.add(value); values.add(value);
writeStringSetUnchecked(key, values); writeStringSetUnchecked(key, values, false);
} }
/** /**
...@@ -135,7 +136,7 @@ public class SharedPreferencesManager { ...@@ -135,7 +136,7 @@ public class SharedPreferencesManager {
Set<String> values = new HashSet<>( Set<String> values = new HashSet<>(
ContextUtils.getAppSharedPreferences().getStringSet(key, Collections.emptySet())); ContextUtils.getAppSharedPreferences().getStringSet(key, Collections.emptySet()));
if (values.remove(value)) { if (values.remove(value)) {
writeStringSetUnchecked(key, values); writeStringSetUnchecked(key, values, false);
} }
} }
...@@ -144,11 +145,28 @@ public class SharedPreferencesManager { ...@@ -144,11 +145,28 @@ public class SharedPreferencesManager {
*/ */
public void writeStringSet(String key, Set<String> values) { public void writeStringSet(String key, Set<String> values) {
mKeyChecker.checkIsKeyInUse(key); mKeyChecker.checkIsKeyInUse(key);
writeStringSetUnchecked(key, values); writeStringSetUnchecked(key, values, /*sync=*/false);
} }
private void writeStringSetUnchecked(String key, Set<String> values) { /**
ContextUtils.getAppSharedPreferences().edit().putStringSet(key, values).apply(); * Writes string set to shared preferences.
*/
public boolean writeStringSet(String key, Set<String> values, boolean sync) {
mKeyChecker.checkIsKeyInUse(key);
return writeStringSetUnchecked(key, values, sync);
}
/**
* Writes string set to shared preferences.
*/
private boolean writeStringSetUnchecked(String key, Set<String> values, boolean sync) {
Editor editor = ContextUtils.getAppSharedPreferences().edit().putStringSet(key, values);
if (sync) {
return editor.commit();
} else {
editor.apply();
return true;
}
} }
/** /**
...@@ -305,6 +323,18 @@ public class SharedPreferencesManager { ...@@ -305,6 +323,18 @@ public class SharedPreferencesManager {
ed.apply(); ed.apply();
} }
public boolean removeKey(String key, boolean sync) {
mKeyChecker.checkIsKeyInUse(key);
SharedPreferences.Editor ed = ContextUtils.getAppSharedPreferences().edit();
ed.remove(key);
if (sync) {
return ed.commit();
} else {
ed.apply();
return true;
}
}
/** /**
* Checks if any value was written associated to a key in shared preferences. * Checks if any value was written associated to a key in shared preferences.
* *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment