Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceHelper.java

Issue 2625493004: Move DownloadSharedPreferenceEntry handling into another class (Closed)
Patch Set: fixing thread check in tests Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.download;
6
7 import android.content.SharedPreferences;
8
9 import org.chromium.base.ContextUtils;
10 import org.chromium.base.ThreadUtils;
11 import org.chromium.base.VisibleForTesting;
12
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Set;
18
19 /**
20 * Class for maintaining all entries of DownloadSharedPreferenceEntry.
21 */
22 public class DownloadSharedPreferenceHelper {
23 @VisibleForTesting
24 static final String KEY_PENDING_DOWNLOAD_NOTIFICATIONS = "PendingDownloadNot ifications";
25 private final List<DownloadSharedPreferenceEntry> mDownloadSharedPreferenceE ntries =
26 new ArrayList<DownloadSharedPreferenceEntry>();
27 private SharedPreferences mSharedPrefs;
28
29 // "Initialization on demand holder idiom"
30 private static class LazyHolder {
31 private static final DownloadSharedPreferenceHelper INSTANCE =
32 new DownloadSharedPreferenceHelper();
33 }
34
35 /**
36 * Creates DownloadSharedPreferenceHelper.
37 */
38 public static DownloadSharedPreferenceHelper getInstance() {
39 ThreadUtils.assertOnUiThread();
40 return LazyHolder.INSTANCE;
41 }
42
43 private DownloadSharedPreferenceHelper() {
44 mSharedPrefs = ContextUtils.getAppSharedPreferences();
45 parseDownloadSharedPrefs();
46 }
47
48 /**
49 * Adds a DownloadSharedPreferenceEntry to SharedPrefs. If an entry with the GUID already exists
50 * in SharedPrefs, update it if it has changed.
51 * @param pendingEntry A DownloadSharedPreferenceEntry to be added.
52 */
53 public void addOrReplaceSharedPreferenceEntry(DownloadSharedPreferenceEntry pendingEntry) {
54 ThreadUtils.assertOnUiThread();
55 Iterator<DownloadSharedPreferenceEntry> iterator =
56 mDownloadSharedPreferenceEntries.iterator();
57 while (iterator.hasNext()) {
58 DownloadSharedPreferenceEntry entry = iterator.next();
59 if (entry.downloadGuid.equals(pendingEntry.downloadGuid)) {
60 if (entry.equals(pendingEntry)) return;
61 iterator.remove();
62 break;
63 }
64 }
65 mDownloadSharedPreferenceEntries.add(pendingEntry);
66 storeDownloadSharedPreferenceEntries();
67 }
68
69 /**
70 * Removes a DownloadSharedPreferenceEntry from SharedPrefs given by the GUI D.
71 * @param guid Download GUID to be removed.
72 */
73 public void removeSharedPreferenceEntry(String guid) {
74 ThreadUtils.assertOnUiThread();
75 Iterator<DownloadSharedPreferenceEntry> iterator =
76 mDownloadSharedPreferenceEntries.iterator();
77 boolean found = false;
78 while (iterator.hasNext()) {
79 DownloadSharedPreferenceEntry entry = iterator.next();
80 if (entry.downloadGuid.equals(guid)) {
81 iterator.remove();
82 found = true;
83 break;
84 }
85 }
86 if (found) {
87 storeDownloadSharedPreferenceEntries();
88 }
89 }
90
91 /**
92 * Gets a list of stored SharedPreference entries.
93 * @param a list of DownloadSharedPreferenceEntry stored in SharedPrefs.
94 */
95 public List<DownloadSharedPreferenceEntry> getEntries() {
96 ThreadUtils.assertOnUiThread();
97 return mDownloadSharedPreferenceEntries;
98 }
99
100 /**
101 * Parse a list of the DownloadSharedPreferenceEntry from |mSharedPrefs|.
102 */
103 private void parseDownloadSharedPrefs() {
104 if (!mSharedPrefs.contains(KEY_PENDING_DOWNLOAD_NOTIFICATIONS)) return;
105 Set<String> entries = DownloadManagerService.getStoredDownloadInfo(
106 mSharedPrefs, KEY_PENDING_DOWNLOAD_NOTIFICATIONS);
107 for (String entryString : entries) {
108 DownloadSharedPreferenceEntry entry =
109 DownloadSharedPreferenceEntry.parseFromString(entryString);
110 if (entry.notificationId > 0) {
111 mDownloadSharedPreferenceEntries.add(
112 DownloadSharedPreferenceEntry.parseFromString(entryStrin g));
113 }
114 }
115 }
116
117 /**
118 * Gets a DownloadSharedPreferenceEntry that has the given GUID.
119 * @param guid GUID to query.
120 * @return a DownloadSharedPreferenceEntry that has the specified GUID.
121 */
122 public DownloadSharedPreferenceEntry getDownloadSharedPreferenceEntry(String guid) {
123 ThreadUtils.assertOnUiThread();
124 for (int i = 0; i < mDownloadSharedPreferenceEntries.size(); ++i) {
125 if (mDownloadSharedPreferenceEntries.get(i).downloadGuid.equals(guid )) {
126 return mDownloadSharedPreferenceEntries.get(i);
127 }
128 }
129 return null;
130 }
131
132 /**
133 * Helper method to store all the SharedPreferences entries.
134 */
135 private void storeDownloadSharedPreferenceEntries() {
136 Set<String> entries = new HashSet<String>();
137 for (int i = 0; i < mDownloadSharedPreferenceEntries.size(); ++i) {
138 entries.add(mDownloadSharedPreferenceEntries.get(i).getSharedPrefere nceString());
139 }
140 DownloadManagerService.storeDownloadInfo(
141 mSharedPrefs, KEY_PENDING_DOWNLOAD_NOTIFICATIONS, entries);
142 }
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698