1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 package org.mozilla.gecko.sync; 6 7 import org.mozilla.gecko.background.fxa.FxAccountUtils; 8 import org.mozilla.gecko.fxa.FirefoxAccounts; 9 import org.mozilla.gecko.fxa.FxAccountDeviceRegistrator; 10 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; 11 import org.mozilla.gecko.sync.delegates.ClientsDataDelegate; 12 import org.mozilla.gecko.util.HardwareUtils; 13 14 import android.accounts.Account; 15 import android.content.Context; 16 import android.content.SharedPreferences; 17 18 /** 19 * A <code>ClientsDataDelegate</code> implementation that persists to a 20 * <code>SharedPreferences</code> instance. 21 */ 22 public class SharedPreferencesClientsDataDelegate implements ClientsDataDelegate { 23 protected final SharedPreferences sharedPreferences; 24 protected final Context context; 25 SharedPreferencesClientsDataDelegate(SharedPreferences sharedPreferences, Context context)26 public SharedPreferencesClientsDataDelegate(SharedPreferences sharedPreferences, Context context) { 27 this.sharedPreferences = sharedPreferences; 28 this.context = context; 29 30 // It's safe to init this multiple times. 31 HardwareUtils.init(context); 32 } 33 34 @Override getAccountGUID()35 public synchronized String getAccountGUID() { 36 String accountGUID = sharedPreferences.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null); 37 if (accountGUID == null) { 38 accountGUID = Utils.generateGuid(); 39 sharedPreferences.edit().putString(SyncConfiguration.PREF_ACCOUNT_GUID, accountGUID).commit(); 40 } 41 return accountGUID; 42 } 43 saveClientNameToSharedPreferences(String clientName, long now)44 private synchronized void saveClientNameToSharedPreferences(String clientName, long now) { 45 sharedPreferences 46 .edit() 47 .putString(SyncConfiguration.PREF_CLIENT_NAME, clientName) 48 .putLong(SyncConfiguration.PREF_CLIENT_DATA_TIMESTAMP, now) 49 .apply(); 50 } 51 52 /** 53 * Set client name. 54 * 55 * @param clientName to change to. 56 */ 57 @Override setClientName(String clientName, long now)58 public synchronized void setClientName(String clientName, long now) { 59 saveClientNameToSharedPreferences(clientName, now); 60 61 // Update the FxA device registration 62 final Account account = FirefoxAccounts.getFirefoxAccount(context); 63 if (account != null) { 64 final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account); 65 fxAccount.resetDeviceRegistrationVersion(); 66 } 67 } 68 69 @Override getDefaultClientName()70 public String getDefaultClientName() { 71 return FxAccountUtils.defaultClientName(context); 72 } 73 74 @Override getClientName()75 public synchronized String getClientName() { 76 String clientName = sharedPreferences.getString(SyncConfiguration.PREF_CLIENT_NAME, null); 77 if (clientName == null) { 78 clientName = getDefaultClientName(); 79 long now = System.currentTimeMillis(); 80 saveClientNameToSharedPreferences(clientName, now); // Save locally only to avoid a recursion loop 81 } 82 return clientName; 83 } 84 85 @Override setClientsCount(int clientsCount)86 public synchronized void setClientsCount(int clientsCount) { 87 sharedPreferences.edit().putLong(SyncConfiguration.PREF_NUM_CLIENTS, clientsCount).commit(); 88 } 89 90 @Override isLocalGUID(String guid)91 public boolean isLocalGUID(String guid) { 92 return getAccountGUID().equals(guid); 93 } 94 95 @Override getClientsCount()96 public synchronized int getClientsCount() { 97 return (int) sharedPreferences.getLong(SyncConfiguration.PREF_NUM_CLIENTS, 0); 98 } 99 100 @Override getLastModifiedTimestamp()101 public long getLastModifiedTimestamp() { 102 return sharedPreferences.getLong(SyncConfiguration.PREF_CLIENT_DATA_TIMESTAMP, 0); 103 } 104 105 @Override getFormFactor()106 public String getFormFactor() { 107 if (HardwareUtils.isLargeTablet()) { 108 return "largetablet"; 109 } 110 111 if (HardwareUtils.isSmallTablet()) { 112 return "smalltablet"; 113 } 114 115 if (HardwareUtils.isTelevision()) { 116 return "tv"; 117 } 118 119 return "phone"; 120 } 121 } 122