Chromium Code Reviews| 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..dc6bcb7c24ef2221c3595af033433075bc304bc5 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,33 @@ 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() { |
|
nyquist
2013/09/05 22:50:08
Nit: \n before public
Steve Condie
2013/09/06 20:05:22
Done.
|
| + 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 +173,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 a object id. |
|
nyquist
2013/09/05 22:50:08
Nit: an object id
Steve Condie
2013/09/06 20:05:22
Done.
|
| + * 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); |
| + } |
| } |