1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/SharedPreferencesClientsDataDelegate.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,57 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync; 1.9 + 1.10 +import org.mozilla.gecko.background.common.GlobalConstants; 1.11 +import org.mozilla.gecko.sync.delegates.ClientsDataDelegate; 1.12 + 1.13 +import android.content.SharedPreferences; 1.14 + 1.15 +/** 1.16 + * A <code>ClientsDataDelegate</code> implementation that persists to a 1.17 + * <code>SharedPreferences</code> instance. 1.18 + */ 1.19 +public class SharedPreferencesClientsDataDelegate implements ClientsDataDelegate { 1.20 + protected final SharedPreferences sharedPreferences; 1.21 + 1.22 + public SharedPreferencesClientsDataDelegate(SharedPreferences sharedPreferences) { 1.23 + this.sharedPreferences = sharedPreferences; 1.24 + } 1.25 + 1.26 + @Override 1.27 + public synchronized String getAccountGUID() { 1.28 + String accountGUID = sharedPreferences.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null); 1.29 + if (accountGUID == null) { 1.30 + accountGUID = Utils.generateGuid(); 1.31 + sharedPreferences.edit().putString(SyncConfiguration.PREF_ACCOUNT_GUID, accountGUID).commit(); 1.32 + } 1.33 + return accountGUID; 1.34 + } 1.35 + 1.36 + @Override 1.37 + public synchronized String getClientName() { 1.38 + String clientName = sharedPreferences.getString(SyncConfiguration.PREF_CLIENT_NAME, null); 1.39 + if (clientName == null) { 1.40 + clientName = GlobalConstants.MOZ_APP_DISPLAYNAME + " on " + android.os.Build.MODEL; 1.41 + sharedPreferences.edit().putString(SyncConfiguration.PREF_CLIENT_NAME, clientName).commit(); 1.42 + } 1.43 + return clientName; 1.44 + } 1.45 + 1.46 + @Override 1.47 + public synchronized void setClientsCount(int clientsCount) { 1.48 + sharedPreferences.edit().putLong(SyncConfiguration.PREF_NUM_CLIENTS, (long) clientsCount).commit(); 1.49 + } 1.50 + 1.51 + @Override 1.52 + public boolean isLocalGUID(String guid) { 1.53 + return getAccountGUID().equals(guid); 1.54 + } 1.55 + 1.56 + @Override 1.57 + public synchronized int getClientsCount() { 1.58 + return (int) sharedPreferences.getLong(SyncConfiguration.PREF_NUM_CLIENTS, 0); 1.59 + } 1.60 +}