Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.fxa.authenticator; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.UnsupportedEncodingException; |
michael@0 | 8 | import java.net.URISyntaxException; |
michael@0 | 9 | import java.security.GeneralSecurityException; |
michael@0 | 10 | import java.util.ArrayList; |
michael@0 | 11 | import java.util.Arrays; |
michael@0 | 12 | import java.util.Collections; |
michael@0 | 13 | import java.util.EnumSet; |
michael@0 | 14 | import java.util.List; |
michael@0 | 15 | |
michael@0 | 16 | import org.mozilla.gecko.background.common.GlobalConstants; |
michael@0 | 17 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 18 | import org.mozilla.gecko.background.fxa.FxAccountUtils; |
michael@0 | 19 | import org.mozilla.gecko.db.BrowserContract; |
michael@0 | 20 | import org.mozilla.gecko.fxa.FirefoxAccounts; |
michael@0 | 21 | import org.mozilla.gecko.fxa.FxAccountConstants; |
michael@0 | 22 | import org.mozilla.gecko.fxa.login.State; |
michael@0 | 23 | import org.mozilla.gecko.fxa.login.State.StateLabel; |
michael@0 | 24 | import org.mozilla.gecko.fxa.login.StateFactory; |
michael@0 | 25 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 26 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 27 | |
michael@0 | 28 | import android.accounts.Account; |
michael@0 | 29 | import android.accounts.AccountManager; |
michael@0 | 30 | import android.content.ContentResolver; |
michael@0 | 31 | import android.content.Context; |
michael@0 | 32 | import android.content.Intent; |
michael@0 | 33 | import android.content.SharedPreferences; |
michael@0 | 34 | import android.os.Bundle; |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * A Firefox Account that stores its details and state as user data attached to |
michael@0 | 38 | * an Android Account instance. |
michael@0 | 39 | * <p> |
michael@0 | 40 | * Account user data is accessible only to the Android App(s) that own the |
michael@0 | 41 | * Account type. Account user data is not removed when the App's private data is |
michael@0 | 42 | * cleared. |
michael@0 | 43 | */ |
michael@0 | 44 | public class AndroidFxAccount { |
michael@0 | 45 | protected static final String LOG_TAG = AndroidFxAccount.class.getSimpleName(); |
michael@0 | 46 | |
michael@0 | 47 | public static final int CURRENT_PREFS_VERSION = 1; |
michael@0 | 48 | |
michael@0 | 49 | // When updating the account, do not forget to update AccountPickler. |
michael@0 | 50 | public static final int CURRENT_ACCOUNT_VERSION = 3; |
michael@0 | 51 | public static final String ACCOUNT_KEY_ACCOUNT_VERSION = "version"; |
michael@0 | 52 | public static final String ACCOUNT_KEY_PROFILE = "profile"; |
michael@0 | 53 | public static final String ACCOUNT_KEY_IDP_SERVER = "idpServerURI"; |
michael@0 | 54 | |
michael@0 | 55 | // The audience should always be a prefix of the token server URI. |
michael@0 | 56 | public static final String ACCOUNT_KEY_AUDIENCE = "audience"; // Sync-specific. |
michael@0 | 57 | public static final String ACCOUNT_KEY_TOKEN_SERVER = "tokenServerURI"; // Sync-specific. |
michael@0 | 58 | public static final String ACCOUNT_KEY_DESCRIPTOR = "descriptor"; |
michael@0 | 59 | |
michael@0 | 60 | public static final int CURRENT_BUNDLE_VERSION = 2; |
michael@0 | 61 | public static final String BUNDLE_KEY_BUNDLE_VERSION = "version"; |
michael@0 | 62 | public static final String BUNDLE_KEY_STATE_LABEL = "stateLabel"; |
michael@0 | 63 | public static final String BUNDLE_KEY_STATE = "state"; |
michael@0 | 64 | |
michael@0 | 65 | protected static final List<String> ANDROID_AUTHORITIES = Collections.unmodifiableList(Arrays.asList( |
michael@0 | 66 | new String[] { BrowserContract.AUTHORITY })); |
michael@0 | 67 | |
michael@0 | 68 | protected final Context context; |
michael@0 | 69 | protected final AccountManager accountManager; |
michael@0 | 70 | protected final Account account; |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * Create an Android Firefox Account instance backed by an Android Account |
michael@0 | 74 | * instance. |
michael@0 | 75 | * <p> |
michael@0 | 76 | * We expect a long-lived application context to avoid life-cycle issues that |
michael@0 | 77 | * might arise if the internally cached AccountManager instance surfaces UI. |
michael@0 | 78 | * <p> |
michael@0 | 79 | * We take care to not install any listeners or observers that might outlive |
michael@0 | 80 | * the AccountManager; and Android ensures the AccountManager doesn't outlive |
michael@0 | 81 | * the associated context. |
michael@0 | 82 | * |
michael@0 | 83 | * @param applicationContext |
michael@0 | 84 | * to use as long-lived ambient Android context. |
michael@0 | 85 | * @param account |
michael@0 | 86 | * Android account to use for storage. |
michael@0 | 87 | */ |
michael@0 | 88 | public AndroidFxAccount(Context applicationContext, Account account) { |
michael@0 | 89 | this.context = applicationContext; |
michael@0 | 90 | this.account = account; |
michael@0 | 91 | this.accountManager = AccountManager.get(this.context); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /** |
michael@0 | 95 | * Persist the Firefox account to disk as a JSON object. Note that this is a wrapper around |
michael@0 | 96 | * {@link AccountPickler#pickle}, and is identical to calling it directly. |
michael@0 | 97 | * <p> |
michael@0 | 98 | * Note that pickling is different from bundling, which involves operations on a |
michael@0 | 99 | * {@link android.os.Bundle Bundle} object of miscellaenous data associated with the account. |
michael@0 | 100 | * See {@link #persistBundle} and {@link #unbundle} for more. |
michael@0 | 101 | */ |
michael@0 | 102 | public void pickle(final String filename) { |
michael@0 | 103 | AccountPickler.pickle(this, filename); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | public Account getAndroidAccount() { |
michael@0 | 107 | return this.account; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | protected int getAccountVersion() { |
michael@0 | 111 | String v = accountManager.getUserData(account, ACCOUNT_KEY_ACCOUNT_VERSION); |
michael@0 | 112 | if (v == null) { |
michael@0 | 113 | return 0; // Implicit. |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | try { |
michael@0 | 117 | return Integer.parseInt(v, 10); |
michael@0 | 118 | } catch (NumberFormatException ex) { |
michael@0 | 119 | return 0; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | /** |
michael@0 | 124 | * Saves the given data as the internal bundle associated with this account. |
michael@0 | 125 | * @param bundle to write to account. |
michael@0 | 126 | */ |
michael@0 | 127 | protected void persistBundle(ExtendedJSONObject bundle) { |
michael@0 | 128 | accountManager.setUserData(account, ACCOUNT_KEY_DESCRIPTOR, bundle.toJSONString()); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * Retrieve the internal bundle associated with this account. |
michael@0 | 133 | * @return bundle associated with account. |
michael@0 | 134 | */ |
michael@0 | 135 | protected ExtendedJSONObject unbundle() { |
michael@0 | 136 | final int version = getAccountVersion(); |
michael@0 | 137 | if (version < CURRENT_ACCOUNT_VERSION) { |
michael@0 | 138 | // Needs upgrade. For now, do nothing. We'd like to just put your account |
michael@0 | 139 | // into the Separated state here and have you update your credentials. |
michael@0 | 140 | return null; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | if (version > CURRENT_ACCOUNT_VERSION) { |
michael@0 | 144 | // Oh dear. |
michael@0 | 145 | return null; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | String bundle = accountManager.getUserData(account, ACCOUNT_KEY_DESCRIPTOR); |
michael@0 | 149 | if (bundle == null) { |
michael@0 | 150 | return null; |
michael@0 | 151 | } |
michael@0 | 152 | return unbundleAccountV2(bundle); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | protected String getBundleData(String key) { |
michael@0 | 156 | ExtendedJSONObject o = unbundle(); |
michael@0 | 157 | if (o == null) { |
michael@0 | 158 | return null; |
michael@0 | 159 | } |
michael@0 | 160 | return o.getString(key); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | protected boolean getBundleDataBoolean(String key, boolean def) { |
michael@0 | 164 | ExtendedJSONObject o = unbundle(); |
michael@0 | 165 | if (o == null) { |
michael@0 | 166 | return def; |
michael@0 | 167 | } |
michael@0 | 168 | Boolean b = o.getBoolean(key); |
michael@0 | 169 | if (b == null) { |
michael@0 | 170 | return def; |
michael@0 | 171 | } |
michael@0 | 172 | return b.booleanValue(); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | protected byte[] getBundleDataBytes(String key) { |
michael@0 | 176 | ExtendedJSONObject o = unbundle(); |
michael@0 | 177 | if (o == null) { |
michael@0 | 178 | return null; |
michael@0 | 179 | } |
michael@0 | 180 | return o.getByteArrayHex(key); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | protected void updateBundleDataBytes(String key, byte[] value) { |
michael@0 | 184 | updateBundleValue(key, value == null ? null : Utils.byte2Hex(value)); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | protected void updateBundleValue(String key, boolean value) { |
michael@0 | 188 | ExtendedJSONObject descriptor = unbundle(); |
michael@0 | 189 | if (descriptor == null) { |
michael@0 | 190 | return; |
michael@0 | 191 | } |
michael@0 | 192 | descriptor.put(key, value); |
michael@0 | 193 | persistBundle(descriptor); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | protected void updateBundleValue(String key, String value) { |
michael@0 | 197 | ExtendedJSONObject descriptor = unbundle(); |
michael@0 | 198 | if (descriptor == null) { |
michael@0 | 199 | return; |
michael@0 | 200 | } |
michael@0 | 201 | descriptor.put(key, value); |
michael@0 | 202 | persistBundle(descriptor); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | private ExtendedJSONObject unbundleAccountV1(String bundle) { |
michael@0 | 206 | ExtendedJSONObject o; |
michael@0 | 207 | try { |
michael@0 | 208 | o = new ExtendedJSONObject(bundle); |
michael@0 | 209 | } catch (Exception e) { |
michael@0 | 210 | return null; |
michael@0 | 211 | } |
michael@0 | 212 | if (CURRENT_BUNDLE_VERSION == o.getIntegerSafely(BUNDLE_KEY_BUNDLE_VERSION)) { |
michael@0 | 213 | return o; |
michael@0 | 214 | } |
michael@0 | 215 | return null; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | private ExtendedJSONObject unbundleAccountV2(String bundle) { |
michael@0 | 219 | return unbundleAccountV1(bundle); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | /** |
michael@0 | 223 | * Note that if the user clears data, an account will be left pointing to a |
michael@0 | 224 | * deleted profile. Such is life. |
michael@0 | 225 | */ |
michael@0 | 226 | public String getProfile() { |
michael@0 | 227 | return accountManager.getUserData(account, ACCOUNT_KEY_PROFILE); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | public String getAccountServerURI() { |
michael@0 | 231 | return accountManager.getUserData(account, ACCOUNT_KEY_IDP_SERVER); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | public String getAudience() { |
michael@0 | 235 | return accountManager.getUserData(account, ACCOUNT_KEY_AUDIENCE); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | public String getTokenServerURI() { |
michael@0 | 239 | return accountManager.getUserData(account, ACCOUNT_KEY_TOKEN_SERVER); |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | /** |
michael@0 | 243 | * This needs to return a string because of the tortured prefs access in GlobalSession. |
michael@0 | 244 | */ |
michael@0 | 245 | public String getSyncPrefsPath() throws GeneralSecurityException, UnsupportedEncodingException { |
michael@0 | 246 | String profile = getProfile(); |
michael@0 | 247 | String username = account.name; |
michael@0 | 248 | |
michael@0 | 249 | if (profile == null) { |
michael@0 | 250 | throw new IllegalStateException("Missing profile. Cannot fetch prefs."); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | if (username == null) { |
michael@0 | 254 | throw new IllegalStateException("Missing username. Cannot fetch prefs."); |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | final String tokenServerURI = getTokenServerURI(); |
michael@0 | 258 | if (tokenServerURI == null) { |
michael@0 | 259 | throw new IllegalStateException("No token server URI. Cannot fetch prefs."); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | final String fxaServerURI = getAccountServerURI(); |
michael@0 | 263 | if (fxaServerURI == null) { |
michael@0 | 264 | throw new IllegalStateException("No account server URI. Cannot fetch prefs."); |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | final String product = GlobalConstants.BROWSER_INTENT_PACKAGE + ".fxa"; |
michael@0 | 268 | final long version = CURRENT_PREFS_VERSION; |
michael@0 | 269 | |
michael@0 | 270 | // This is unique for each syncing 'view' of the account. |
michael@0 | 271 | final String serverURLThing = fxaServerURI + "!" + tokenServerURI; |
michael@0 | 272 | return Utils.getPrefsPath(product, username, serverURLThing, profile, version); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | public SharedPreferences getSyncPrefs() throws UnsupportedEncodingException, GeneralSecurityException { |
michael@0 | 276 | return context.getSharedPreferences(getSyncPrefsPath(), Utils.SHARED_PREFERENCES_MODE); |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | /** |
michael@0 | 280 | * Extract a JSON dictionary of the string values associated to this account. |
michael@0 | 281 | * <p> |
michael@0 | 282 | * <b>For debugging use only!</b> The contents of this JSON object completely |
michael@0 | 283 | * determine the user's Firefox Account status and yield access to whatever |
michael@0 | 284 | * user data the device has access to. |
michael@0 | 285 | * |
michael@0 | 286 | * @return JSON-object of Strings. |
michael@0 | 287 | */ |
michael@0 | 288 | public ExtendedJSONObject toJSONObject() { |
michael@0 | 289 | ExtendedJSONObject o = unbundle(); |
michael@0 | 290 | o.put("email", account.name); |
michael@0 | 291 | try { |
michael@0 | 292 | o.put("emailUTF8", Utils.byte2Hex(account.name.getBytes("UTF-8"))); |
michael@0 | 293 | } catch (UnsupportedEncodingException e) { |
michael@0 | 294 | // Ignore. |
michael@0 | 295 | } |
michael@0 | 296 | return o; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | public static AndroidFxAccount addAndroidAccount( |
michael@0 | 300 | Context context, |
michael@0 | 301 | String email, |
michael@0 | 302 | String profile, |
michael@0 | 303 | String idpServerURI, |
michael@0 | 304 | String tokenServerURI, |
michael@0 | 305 | State state) |
michael@0 | 306 | throws UnsupportedEncodingException, GeneralSecurityException, URISyntaxException { |
michael@0 | 307 | return addAndroidAccount(context, email, profile, idpServerURI, tokenServerURI, state, |
michael@0 | 308 | CURRENT_ACCOUNT_VERSION, true, false, null); |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | public static AndroidFxAccount addAndroidAccount( |
michael@0 | 312 | Context context, |
michael@0 | 313 | String email, |
michael@0 | 314 | String profile, |
michael@0 | 315 | String idpServerURI, |
michael@0 | 316 | String tokenServerURI, |
michael@0 | 317 | State state, |
michael@0 | 318 | final int accountVersion, |
michael@0 | 319 | final boolean syncEnabled, |
michael@0 | 320 | final boolean fromPickle, |
michael@0 | 321 | ExtendedJSONObject bundle) |
michael@0 | 322 | throws UnsupportedEncodingException, GeneralSecurityException, URISyntaxException { |
michael@0 | 323 | if (email == null) { |
michael@0 | 324 | throw new IllegalArgumentException("email must not be null"); |
michael@0 | 325 | } |
michael@0 | 326 | if (idpServerURI == null) { |
michael@0 | 327 | throw new IllegalArgumentException("idpServerURI must not be null"); |
michael@0 | 328 | } |
michael@0 | 329 | if (tokenServerURI == null) { |
michael@0 | 330 | throw new IllegalArgumentException("tokenServerURI must not be null"); |
michael@0 | 331 | } |
michael@0 | 332 | if (state == null) { |
michael@0 | 333 | throw new IllegalArgumentException("state must not be null"); |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | // TODO: Add migration code. |
michael@0 | 337 | if (accountVersion != CURRENT_ACCOUNT_VERSION) { |
michael@0 | 338 | throw new IllegalStateException("Could not create account of version " + accountVersion + |
michael@0 | 339 | ". Current version is " + CURRENT_ACCOUNT_VERSION + "."); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | // Android has internal restrictions that require all values in this |
michael@0 | 343 | // bundle to be strings. *sigh* |
michael@0 | 344 | Bundle userdata = new Bundle(); |
michael@0 | 345 | userdata.putString(ACCOUNT_KEY_ACCOUNT_VERSION, "" + CURRENT_ACCOUNT_VERSION); |
michael@0 | 346 | userdata.putString(ACCOUNT_KEY_IDP_SERVER, idpServerURI); |
michael@0 | 347 | userdata.putString(ACCOUNT_KEY_TOKEN_SERVER, tokenServerURI); |
michael@0 | 348 | userdata.putString(ACCOUNT_KEY_AUDIENCE, FxAccountUtils.getAudienceForURL(tokenServerURI)); |
michael@0 | 349 | userdata.putString(ACCOUNT_KEY_PROFILE, profile); |
michael@0 | 350 | |
michael@0 | 351 | if (bundle == null) { |
michael@0 | 352 | bundle = new ExtendedJSONObject(); |
michael@0 | 353 | // TODO: How to upgrade? |
michael@0 | 354 | bundle.put(BUNDLE_KEY_BUNDLE_VERSION, CURRENT_BUNDLE_VERSION); |
michael@0 | 355 | } |
michael@0 | 356 | bundle.put(BUNDLE_KEY_STATE_LABEL, state.getStateLabel().name()); |
michael@0 | 357 | bundle.put(BUNDLE_KEY_STATE, state.toJSONObject().toJSONString()); |
michael@0 | 358 | |
michael@0 | 359 | userdata.putString(ACCOUNT_KEY_DESCRIPTOR, bundle.toJSONString()); |
michael@0 | 360 | |
michael@0 | 361 | Account account = new Account(email, FxAccountConstants.ACCOUNT_TYPE); |
michael@0 | 362 | AccountManager accountManager = AccountManager.get(context); |
michael@0 | 363 | // We don't set an Android password, because we don't want to persist the |
michael@0 | 364 | // password (or anything else as powerful as the password). Instead, we |
michael@0 | 365 | // internally manage a sessionToken with a remotely owned lifecycle. |
michael@0 | 366 | boolean added = accountManager.addAccountExplicitly(account, null, userdata); |
michael@0 | 367 | if (!added) { |
michael@0 | 368 | return null; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | AndroidFxAccount fxAccount = new AndroidFxAccount(context, account); |
michael@0 | 372 | |
michael@0 | 373 | if (!fromPickle) { |
michael@0 | 374 | fxAccount.clearSyncPrefs(); |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | if (syncEnabled) { |
michael@0 | 378 | fxAccount.enableSyncing(); |
michael@0 | 379 | } else { |
michael@0 | 380 | fxAccount.disableSyncing(); |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | return fxAccount; |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | public void clearSyncPrefs() throws UnsupportedEncodingException, GeneralSecurityException { |
michael@0 | 387 | getSyncPrefs().edit().clear().commit(); |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | public static Iterable<String> getAndroidAuthorities() { |
michael@0 | 391 | return ANDROID_AUTHORITIES; |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | /** |
michael@0 | 395 | * Return true if the underlying Android account is currently set to sync automatically. |
michael@0 | 396 | * <p> |
michael@0 | 397 | * This is, confusingly, not the same thing as "being syncable": that refers |
michael@0 | 398 | * to whether this account can be synced, ever; this refers to whether Android |
michael@0 | 399 | * will try to sync the account at appropriate times. |
michael@0 | 400 | * |
michael@0 | 401 | * @return true if the account is set to sync automatically. |
michael@0 | 402 | */ |
michael@0 | 403 | public boolean isSyncing() { |
michael@0 | 404 | boolean isSyncEnabled = true; |
michael@0 | 405 | for (String authority : getAndroidAuthorities()) { |
michael@0 | 406 | isSyncEnabled &= ContentResolver.getSyncAutomatically(account, authority); |
michael@0 | 407 | } |
michael@0 | 408 | return isSyncEnabled; |
michael@0 | 409 | } |
michael@0 | 410 | |
michael@0 | 411 | public void enableSyncing() { |
michael@0 | 412 | Logger.info(LOG_TAG, "Enabling sync for account named like " + getObfuscatedEmail()); |
michael@0 | 413 | for (String authority : getAndroidAuthorities()) { |
michael@0 | 414 | ContentResolver.setSyncAutomatically(account, authority, true); |
michael@0 | 415 | ContentResolver.setIsSyncable(account, authority, 1); |
michael@0 | 416 | } |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | public void disableSyncing() { |
michael@0 | 420 | Logger.info(LOG_TAG, "Disabling sync for account named like " + getObfuscatedEmail()); |
michael@0 | 421 | for (String authority : getAndroidAuthorities()) { |
michael@0 | 422 | ContentResolver.setSyncAutomatically(account, authority, false); |
michael@0 | 423 | } |
michael@0 | 424 | } |
michael@0 | 425 | |
michael@0 | 426 | /** |
michael@0 | 427 | * Is a sync currently in progress? |
michael@0 | 428 | * |
michael@0 | 429 | * @return true if Android is currently syncing the underlying Android Account. |
michael@0 | 430 | */ |
michael@0 | 431 | public boolean isCurrentlySyncing() { |
michael@0 | 432 | boolean active = false; |
michael@0 | 433 | for (String authority : AndroidFxAccount.getAndroidAuthorities()) { |
michael@0 | 434 | active |= ContentResolver.isSyncActive(account, authority); |
michael@0 | 435 | } |
michael@0 | 436 | return active; |
michael@0 | 437 | } |
michael@0 | 438 | |
michael@0 | 439 | /** |
michael@0 | 440 | * Request a sync. See {@link FirefoxAccounts#requestSync(Account, EnumSet, String[], String[])}. |
michael@0 | 441 | */ |
michael@0 | 442 | public void requestSync() { |
michael@0 | 443 | requestSync(FirefoxAccounts.SOON, null, null); |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | /** |
michael@0 | 447 | * Request a sync. See {@link FirefoxAccounts#requestSync(Account, EnumSet, String[], String[])}. |
michael@0 | 448 | * |
michael@0 | 449 | * @param syncHints to pass to sync. |
michael@0 | 450 | */ |
michael@0 | 451 | public void requestSync(EnumSet<FirefoxAccounts.SyncHint> syncHints) { |
michael@0 | 452 | requestSync(syncHints, null, null); |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | /** |
michael@0 | 456 | * Request a sync. See {@link FirefoxAccounts#requestSync(Account, EnumSet, String[], String[])}. |
michael@0 | 457 | * |
michael@0 | 458 | * @param syncHints to pass to sync. |
michael@0 | 459 | * @param stagesToSync stage names to sync. |
michael@0 | 460 | * @param stagesToSkip stage names to skip. |
michael@0 | 461 | */ |
michael@0 | 462 | public void requestSync(EnumSet<FirefoxAccounts.SyncHint> syncHints, String[] stagesToSync, String[] stagesToSkip) { |
michael@0 | 463 | FirefoxAccounts.requestSync(getAndroidAccount(), syncHints, stagesToSync, stagesToSkip); |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | public synchronized void setState(State state) { |
michael@0 | 467 | if (state == null) { |
michael@0 | 468 | throw new IllegalArgumentException("state must not be null"); |
michael@0 | 469 | } |
michael@0 | 470 | Logger.info(LOG_TAG, "Moving account named like " + getObfuscatedEmail() + |
michael@0 | 471 | " to state " + state.getStateLabel().toString()); |
michael@0 | 472 | updateBundleValue(BUNDLE_KEY_STATE_LABEL, state.getStateLabel().name()); |
michael@0 | 473 | updateBundleValue(BUNDLE_KEY_STATE, state.toJSONObject().toJSONString()); |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | public synchronized State getState() { |
michael@0 | 477 | String stateLabelString = getBundleData(BUNDLE_KEY_STATE_LABEL); |
michael@0 | 478 | String stateString = getBundleData(BUNDLE_KEY_STATE); |
michael@0 | 479 | if (stateLabelString == null) { |
michael@0 | 480 | throw new IllegalStateException("stateLabelString must not be null"); |
michael@0 | 481 | } |
michael@0 | 482 | if (stateString == null) { |
michael@0 | 483 | throw new IllegalStateException("stateString must not be null"); |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | try { |
michael@0 | 487 | StateLabel stateLabel = StateLabel.valueOf(stateLabelString); |
michael@0 | 488 | return StateFactory.fromJSONObject(stateLabel, new ExtendedJSONObject(stateString)); |
michael@0 | 489 | } catch (Exception e) { |
michael@0 | 490 | throw new IllegalStateException("could not get state", e); |
michael@0 | 491 | } |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | /** |
michael@0 | 495 | * <b>For debugging only!</b> |
michael@0 | 496 | */ |
michael@0 | 497 | public void dump() { |
michael@0 | 498 | if (!FxAccountConstants.LOG_PERSONAL_INFORMATION) { |
michael@0 | 499 | return; |
michael@0 | 500 | } |
michael@0 | 501 | ExtendedJSONObject o = toJSONObject(); |
michael@0 | 502 | ArrayList<String> list = new ArrayList<String>(o.keySet()); |
michael@0 | 503 | Collections.sort(list); |
michael@0 | 504 | for (String key : list) { |
michael@0 | 505 | FxAccountConstants.pii(LOG_TAG, key + ": " + o.get(key)); |
michael@0 | 506 | } |
michael@0 | 507 | } |
michael@0 | 508 | |
michael@0 | 509 | /** |
michael@0 | 510 | * Return the Firefox Account's local email address. |
michael@0 | 511 | * <p> |
michael@0 | 512 | * It is important to note that this is the local email address, and not |
michael@0 | 513 | * necessarily the normalized remote email address that the server expects. |
michael@0 | 514 | * |
michael@0 | 515 | * @return local email address. |
michael@0 | 516 | */ |
michael@0 | 517 | public String getEmail() { |
michael@0 | 518 | return account.name; |
michael@0 | 519 | } |
michael@0 | 520 | |
michael@0 | 521 | /** |
michael@0 | 522 | * Return the Firefox Account's local email address, obfuscated. |
michael@0 | 523 | * <p> |
michael@0 | 524 | * Use this when logging. |
michael@0 | 525 | * |
michael@0 | 526 | * @return local email address, obfuscated. |
michael@0 | 527 | */ |
michael@0 | 528 | public String getObfuscatedEmail() { |
michael@0 | 529 | return Utils.obfuscateEmail(account.name); |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | /** |
michael@0 | 533 | * Create an intent announcing that a Firefox account will be deleted. |
michael@0 | 534 | * |
michael@0 | 535 | * @param context |
michael@0 | 536 | * Android context. |
michael@0 | 537 | * @param account |
michael@0 | 538 | * Android account being removed. |
michael@0 | 539 | * @return <code>Intent</code> to broadcast. |
michael@0 | 540 | */ |
michael@0 | 541 | public static Intent makeDeletedAccountIntent(final Context context, final Account account) { |
michael@0 | 542 | final Intent intent = new Intent(FxAccountConstants.ACCOUNT_DELETED_ACTION); |
michael@0 | 543 | |
michael@0 | 544 | intent.putExtra(FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION_KEY, |
michael@0 | 545 | Long.valueOf(FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION)); |
michael@0 | 546 | intent.putExtra(FxAccountConstants.ACCOUNT_DELETED_INTENT_ACCOUNT_KEY, account.name); |
michael@0 | 547 | return intent; |
michael@0 | 548 | } |
michael@0 | 549 | } |