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

Unified Diff: sync/android/java/src/org/chromium/sync/notifier/InvalidationPreferences.java

Issue 23643002: Enable invalidations for arbitrary objects on Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 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: sync/android/java/src/org/chromium/sync/notifier/InvalidationPreferences.java
diff --git a/sync/android/java/src/org/chromium/sync/notifier/InvalidationPreferences.java b/sync/android/java/src/org/chromium/sync/notifier/InvalidationPreferences.java
index 2b212066c32792727342dc23632dbca91b19f9ad..612a470a899bd161390a0568f4013ea7de26c687 100644
--- a/sync/android/java/src/org/chromium/sync/notifier/InvalidationPreferences.java
+++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationPreferences.java
@@ -13,6 +13,7 @@ import android.util.Log;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
+import com.google.ipc.invalidation.external.client.types.ObjectId;
import java.util.Collection;
import java.util.HashSet;
@@ -55,6 +56,13 @@ public class InvalidationPreferences {
@VisibleForTesting
public static final String SYNC_TANGO_TYPES = "sync_tango_types";
+ /**
+ * Shared preference key to store tango object ids for additional objects that we want to
+ * register for.
+ */
+ @VisibleForTesting
+ public static final String TANGO_OBJECT_IDS = "tango_object_ids";
+
/** Shared preference key to store the name of the account in use. */
@VisibleForTesting
public static final String SYNC_ACCT_NAME = "sync_acct_name";
@@ -106,6 +114,34 @@ public class InvalidationPreferences {
editContext.editor.putStringSet(PrefKeys.SYNC_TANGO_TYPES, selectedTypesSet);
}
+ /** Returns the saved non-sync object ids, or {@code null} if none exist. */
+ @Nullable
+ public Set<ObjectId> getSavedObjectIds() {
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
+ Set<String> objectIdStrings = preferences.getStringSet(PrefKeys.TANGO_OBJECT_IDS, null);
+ if (objectIdStrings == null) {
+ return null;
+ }
+ Set<ObjectId> objectIds = new HashSet<ObjectId>(objectIdStrings.size());
+ for (String objectIdString : objectIdStrings) {
+ ObjectId objectId = getObjectId(objectIdString);
+ if (objectId != null) {
+ objectIds.add(objectId);
+ }
+ }
+ return objectIds;
+ }
+
+ /** Sets the saved non-sync object ids */
+ public void setObjectIds(EditContext editContext, Collection<ObjectId> objectIds) {
+ Preconditions.checkNotNull(objectIds);
+ Set<String> objectIdStrings = new HashSet<String>(objectIds.size());
+ for (ObjectId objectId : objectIds) {
+ objectIdStrings.add(getObjectIdString(objectId));
+ }
+ editContext.editor.putStringSet(PrefKeys.TANGO_OBJECT_IDS, objectIdStrings);
+ }
+
/** Returns the saved account, or {@code null} if none exists. */
@Nullable public Account getSavedSyncedAccount() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
@@ -138,4 +174,29 @@ public class InvalidationPreferences {
editContext.editor.putString(PrefKeys.SYNC_TANGO_INTERNAL_STATE,
Base64.encodeToString(state, Base64.DEFAULT));
}
+
+ /** Converts the given object id to a string for storage in preferences. */
+ private String getObjectIdString(ObjectId objectId) {
+ return objectId.getSource() + ":" + new String(objectId.getName());
+ }
+
+ /**
+ * Converts the given object id string stored in preferences to an object id.
+ * Returns null if the string does not represent a valid object id.
+ */
+ private ObjectId getObjectId(String objectIdString) {
+ int separatorPos = objectIdString.indexOf(':');
+ // Ensure that the separator is surrounded by at least one character on each side.
+ if (separatorPos < 1 || separatorPos == objectIdString.length() - 1) {
+ return null;
+ }
+ int objectSource;
+ try {
+ objectSource = Integer.parseInt(objectIdString.substring(0, separatorPos));
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ byte[] objectName = objectIdString.substring(separatorPos + 1).getBytes();
+ return ObjectId.newInstance(objectSource, objectName);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698