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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java

Issue 2230603003: [Offline pages] Downloads UI: Extening DownloadSharedPreferenceEntry to include offline page (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding OWNERS file to the tests Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java
index e323b4a511191f80adc6f901ead5cfbdb4cb9946..703f35fcba5e99ea55ca4736e1ebcced50d71e0b 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadSharedPreferenceEntry.java
@@ -17,22 +17,32 @@ import java.util.UUID;
*/
public class DownloadSharedPreferenceEntry {
private static final String TAG = "DownloadEntry";
+
// Current version of the DownloadSharedPreferenceEntry. When changing the SharedPreference,
// we need to change the version number too.
- @VisibleForTesting static final int VERSION = 2;
+ @VisibleForTesting
+ static final int VERSION = 3;
+ public static final int ITEM_TYPE_DOWNLOAD = 1;
+ public static final int ITEM_TYPE_OFFLINE_PAGE = 2;
+
public final int notificationId;
public final boolean isOffTheRecord; // Whether the download is public (non incognito).
public boolean canDownloadWhileMetered;
public final String fileName;
public final String downloadGuid;
+ public final int itemType;
+
+ static final DownloadSharedPreferenceEntry INVALID_ENTRY =
+ new DownloadSharedPreferenceEntry(-1, false, false, null, "", ITEM_TYPE_DOWNLOAD);
DownloadSharedPreferenceEntry(int notificationId, boolean isOffTheRecord,
- boolean canDownloadWhileMetered, String guid, String fileName) {
+ boolean canDownloadWhileMetered, String guid, String fileName, int itemType) {
this.notificationId = notificationId;
this.isOffTheRecord = isOffTheRecord;
this.canDownloadWhileMetered = canDownloadWhileMetered;
this.downloadGuid = guid;
this.fileName = fileName;
+ this.itemType = itemType;
}
/**
@@ -43,28 +53,51 @@ public class DownloadSharedPreferenceEntry {
* @return a DownloadSharedPreferenceEntry object.
*/
static DownloadSharedPreferenceEntry parseFromString(String sharedPrefString) {
- String[] values = sharedPrefString.split(",", 6);
- if (values.length == 6) {
- try {
- int version = Integer.parseInt(values[0]);
- // Ignore all SharedPreference entries that has an invalid version for now.
- if (version < 1) {
- return new DownloadSharedPreferenceEntry(-1, false, false, null, "");
- }
- int id = Integer.parseInt(values[1]);
- boolean isOffTheRecord =
- (version >= 2) ? "1".equals(values[2]) : "0".equals(values[2]);
- boolean canDownloadWhileMetered = "1".equals(values[3]);
- if (!isValidGUID(values[4])) {
- return new DownloadSharedPreferenceEntry(-1, false, false, null, "");
- }
- return new DownloadSharedPreferenceEntry(
- id, isOffTheRecord, canDownloadWhileMetered, values[4], values[5]);
- } catch (NumberFormatException nfe) {
- Log.w(TAG, "Exception while parsing pending download:" + sharedPrefString);
+ String versionString = sharedPrefString.substring(0, sharedPrefString.indexOf(","));
+ // Ignore all SharedPreference entries that has an invalid version for now.
+ int version = 0;
+ try {
+ version = Integer.parseInt(versionString);
+ } catch (NumberFormatException nfe) {
+ Log.w(TAG, "Exception while parsing pending download:" + sharedPrefString);
+ }
+ if (version <= 0 || version > 3) return INVALID_ENTRY;
+
+ // Expected number of items for version 1 and 2 is 6, version 3 is 7.
+ int expectedItemsNumber = (version == 3 ? 7 : 6);
+ String[] values = sharedPrefString.split(",", expectedItemsNumber);
+ if (values.length != expectedItemsNumber) return INVALID_ENTRY;
+
+ // Index == 0 is used for version, therefor we start from 1.
+ int currentIndex = 1;
+ int id = 0;
+ int itemType = ITEM_TYPE_DOWNLOAD;
+ try {
+ id = Integer.parseInt(values[currentIndex++]);
+ if (version > 2) {
+ itemType = Integer.parseInt(values[currentIndex++]);
}
+ } catch (NumberFormatException nfe) {
+ Log.w(TAG, "Exception while parsing pending download:" + sharedPrefString);
+ return INVALID_ENTRY;
+ }
+ if (itemType != ITEM_TYPE_DOWNLOAD && itemType != ITEM_TYPE_OFFLINE_PAGE) {
+ return INVALID_ENTRY;
}
- return new DownloadSharedPreferenceEntry(-1, false, false, null, "");
+
+ boolean isOffTheRecord = (version >= 2) ? "1".equals(values[currentIndex])
+ : "0".equals(values[currentIndex]);
+ ++currentIndex;
+
+ boolean canDownloadWhileMetered = "1".equals(values[currentIndex++]);
+
+ String guid = values[currentIndex++];
+ if (!isValidGUID(guid)) return INVALID_ENTRY;
+
+ String fileName = values[currentIndex++];
+
+ return new DownloadSharedPreferenceEntry(
+ id, isOffTheRecord, canDownloadWhileMetered, guid, fileName, itemType);
}
/**
@@ -72,8 +105,8 @@ public class DownloadSharedPreferenceEntry {
* SharedPrefs.
*/
String getSharedPreferenceString() {
- return VERSION + "," + notificationId + "," + (isOffTheRecord ? "1" : "0") + ","
- + (canDownloadWhileMetered ? "1" : "0") + "," + downloadGuid + "," + fileName;
+ return VERSION + "," + notificationId + "," + itemType + "," + (isOffTheRecord ? "1" : "0")
+ + "," + (canDownloadWhileMetered ? "1" : "0") + "," + downloadGuid + "," + fileName;
}
/**
@@ -116,6 +149,7 @@ public class DownloadSharedPreferenceEntry {
return TextUtils.equals(downloadGuid, other.downloadGuid)
&& TextUtils.equals(fileName, other.fileName)
&& notificationId == other.notificationId
+ && itemType == other.itemType
&& isOffTheRecord == other.isOffTheRecord
&& canDownloadWhileMetered == other.canDownloadWhileMetered;
}
@@ -126,6 +160,7 @@ public class DownloadSharedPreferenceEntry {
hash = 37 * hash + (isOffTheRecord ? 1 : 0);
hash = 37 * hash + (canDownloadWhileMetered ? 1 : 0);
hash = 37 * hash + notificationId;
+ hash = 37 * hash + itemType;
hash = 37 * hash + downloadGuid.hashCode();
hash = 37 * hash + fileName.hashCode();
return hash;

Powered by Google App Engine
This is Rietveld 408576698