michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: package org.mozilla.gecko.sync;
michael@0:
michael@0: import org.mozilla.gecko.background.common.GlobalConstants;
michael@0: import org.mozilla.gecko.sync.delegates.ClientsDataDelegate;
michael@0:
michael@0: import android.content.SharedPreferences;
michael@0:
michael@0: /**
michael@0: * A ClientsDataDelegate
implementation that persists to a
michael@0: * SharedPreferences
instance.
michael@0: */
michael@0: public class SharedPreferencesClientsDataDelegate implements ClientsDataDelegate {
michael@0: protected final SharedPreferences sharedPreferences;
michael@0:
michael@0: public SharedPreferencesClientsDataDelegate(SharedPreferences sharedPreferences) {
michael@0: this.sharedPreferences = sharedPreferences;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public synchronized String getAccountGUID() {
michael@0: String accountGUID = sharedPreferences.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null);
michael@0: if (accountGUID == null) {
michael@0: accountGUID = Utils.generateGuid();
michael@0: sharedPreferences.edit().putString(SyncConfiguration.PREF_ACCOUNT_GUID, accountGUID).commit();
michael@0: }
michael@0: return accountGUID;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public synchronized String getClientName() {
michael@0: String clientName = sharedPreferences.getString(SyncConfiguration.PREF_CLIENT_NAME, null);
michael@0: if (clientName == null) {
michael@0: clientName = GlobalConstants.MOZ_APP_DISPLAYNAME + " on " + android.os.Build.MODEL;
michael@0: sharedPreferences.edit().putString(SyncConfiguration.PREF_CLIENT_NAME, clientName).commit();
michael@0: }
michael@0: return clientName;
michael@0: }
michael@0:
michael@0: @Override
michael@0: public synchronized void setClientsCount(int clientsCount) {
michael@0: sharedPreferences.edit().putLong(SyncConfiguration.PREF_NUM_CLIENTS, (long) clientsCount).commit();
michael@0: }
michael@0:
michael@0: @Override
michael@0: public boolean isLocalGUID(String guid) {
michael@0: return getAccountGUID().equals(guid);
michael@0: }
michael@0:
michael@0: @Override
michael@0: public synchronized int getClientsCount() {
michael@0: return (int) sharedPreferences.getLong(SyncConfiguration.PREF_NUM_CLIENTS, 0);
michael@0: }
michael@0: }