This source file includes following definitions.
- edit
- commit
- getSavedSyncedTypes
- setSyncTypes
- getSavedObjectIds
- setObjectIds
- getSavedSyncedAccount
- setAccount
- getInternalNotificationClientState
- setInternalNotificationClientState
- getObjectIdString
- getObjectId
package org.chromium.sync.notifier;
import android.accounts.Account;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Base64;
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;
import java.util.Set;
import javax.annotation.Nullable;
public class InvalidationPreferences {
public class EditContext {
private final SharedPreferences.Editor editor;
EditContext() {
this.editor = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
}
}
@VisibleForTesting
public static class PrefKeys {
@VisibleForTesting
public static final String SYNC_TANGO_TYPES = "sync_tango_types";
@VisibleForTesting
public static final String TANGO_OBJECT_IDS = "tango_object_ids";
@VisibleForTesting
public static final String SYNC_ACCT_NAME = "sync_acct_name";
static final String SYNC_ACCT_TYPE = "sync_acct_type";
static final String SYNC_TANGO_INTERNAL_STATE = "sync_tango_internal_state";
}
private static final String TAG = "InvalidationPreferences";
private final Context mContext;
public InvalidationPreferences(Context context) {
this.mContext = Preconditions.checkNotNull(context.getApplicationContext());
}
public EditContext edit() {
return new EditContext();
}
public boolean commit(EditContext editContext) {
if (!editContext.editor.commit()) {
Log.w(TAG, "Failed to commit invalidation preferences");
return false;
}
return true;
}
@Nullable public Set<String> getSavedSyncedTypes() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
return preferences.getStringSet(PrefKeys.SYNC_TANGO_TYPES, null);
}
public void setSyncTypes(EditContext editContext, Collection<String> syncTypes) {
Preconditions.checkNotNull(syncTypes);
Set<String> selectedTypesSet = new HashSet<String>(syncTypes);
editContext.editor.putStringSet(PrefKeys.SYNC_TANGO_TYPES, selectedTypesSet);
}
@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;
}
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);
}
@Nullable public Account getSavedSyncedAccount() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
String accountName = preferences.getString(PrefKeys.SYNC_ACCT_NAME, null);
String accountType = preferences.getString(PrefKeys.SYNC_ACCT_TYPE, null);
if (accountName == null || accountType == null) {
return null;
}
return new Account(accountName, accountType);
}
public void setAccount(EditContext editContext, Account account) {
editContext.editor.putString(PrefKeys.SYNC_ACCT_NAME, account.name);
editContext.editor.putString(PrefKeys.SYNC_ACCT_TYPE, account.type);
}
@Nullable public byte[] getInternalNotificationClientState() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
String base64State = preferences.getString(PrefKeys.SYNC_TANGO_INTERNAL_STATE, null);
if (base64State == null) {
return null;
}
return Base64.decode(base64State, Base64.DEFAULT);
}
public void setInternalNotificationClientState(EditContext editContext, byte[] state) {
editContext.editor.putString(PrefKeys.SYNC_TANGO_INTERNAL_STATE,
Base64.encodeToString(state, Base64.DEFAULT));
}
private String getObjectIdString(ObjectId objectId) {
return objectId.getSource() + ":" + new String(objectId.getName());
}
private ObjectId getObjectId(String objectIdString) {
int separatorPos = objectIdString.indexOf(':');
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);
}
}