mobile/android/base/fxa/sync/FxAccountSyncStatusHelper.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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.sync;
michael@0 6
michael@0 7 import java.util.Map;
michael@0 8 import java.util.Map.Entry;
michael@0 9 import java.util.WeakHashMap;
michael@0 10
michael@0 11 import org.mozilla.gecko.fxa.FirefoxAccounts;
michael@0 12 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
michael@0 13
michael@0 14 import android.content.ContentResolver;
michael@0 15 import android.content.SyncStatusObserver;
michael@0 16
michael@0 17 /**
michael@0 18 * Abstract away some details of Android's SyncStatusObserver.
michael@0 19 * <p>
michael@0 20 * Provides a simplified sync started/sync finished delegate.
michael@0 21 */
michael@0 22 public class FxAccountSyncStatusHelper implements SyncStatusObserver {
michael@0 23 @SuppressWarnings("unused")
michael@0 24 private static final String LOG_TAG = FxAccountSyncStatusHelper.class.getSimpleName();
michael@0 25
michael@0 26 protected static FxAccountSyncStatusHelper sInstance = null;
michael@0 27
michael@0 28 public synchronized static FxAccountSyncStatusHelper getInstance() {
michael@0 29 if (sInstance == null) {
michael@0 30 sInstance = new FxAccountSyncStatusHelper();
michael@0 31 }
michael@0 32 return sInstance;
michael@0 33 }
michael@0 34
michael@0 35 // Used to unregister this as a listener.
michael@0 36 protected Object handle = null;
michael@0 37
michael@0 38 // Maps delegates to whether their underlying Android account was syncing the
michael@0 39 // last time we observed a status change.
michael@0 40 protected Map<FirefoxAccounts.SyncStatusListener, Boolean> delegates = new WeakHashMap<FirefoxAccounts.SyncStatusListener, Boolean>();
michael@0 41
michael@0 42 @Override
michael@0 43 public synchronized void onStatusChanged(int which) {
michael@0 44 for (Entry<FirefoxAccounts.SyncStatusListener, Boolean> entry : delegates.entrySet()) {
michael@0 45 final FirefoxAccounts.SyncStatusListener delegate = entry.getKey();
michael@0 46 final AndroidFxAccount fxAccount = new AndroidFxAccount(delegate.getContext(), delegate.getAccount());
michael@0 47 final boolean active = fxAccount.isCurrentlySyncing();
michael@0 48 // Remember for later.
michael@0 49 boolean wasActiveLastTime = entry.getValue();
michael@0 50 // It's okay to update the value of an entry while iterating the entrySet.
michael@0 51 entry.setValue(active);
michael@0 52
michael@0 53 if (active && !wasActiveLastTime) {
michael@0 54 // We've started a sync.
michael@0 55 delegate.onSyncStarted();
michael@0 56 }
michael@0 57 if (!active && wasActiveLastTime) {
michael@0 58 // We've finished a sync.
michael@0 59 delegate.onSyncFinished();
michael@0 60 }
michael@0 61 }
michael@0 62 }
michael@0 63
michael@0 64 protected void addListener() {
michael@0 65 final int mask = ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
michael@0 66 if (this.handle != null) {
michael@0 67 throw new IllegalStateException("Already registered this as an observer?");
michael@0 68 }
michael@0 69 this.handle = ContentResolver.addStatusChangeListener(mask, this);
michael@0 70 }
michael@0 71
michael@0 72 protected void removeListener() {
michael@0 73 Object handle = this.handle;
michael@0 74 this.handle = null;
michael@0 75 if (handle != null) {
michael@0 76 ContentResolver.removeStatusChangeListener(handle);
michael@0 77 }
michael@0 78 }
michael@0 79
michael@0 80 public synchronized void startObserving(FirefoxAccounts.SyncStatusListener delegate) {
michael@0 81 if (delegate == null) {
michael@0 82 throw new IllegalArgumentException("delegate must not be null");
michael@0 83 }
michael@0 84 if (delegates.containsKey(delegate)) {
michael@0 85 return;
michael@0 86 }
michael@0 87 // If we are the first delegate to the party, start listening.
michael@0 88 if (delegates.isEmpty()) {
michael@0 89 addListener();
michael@0 90 }
michael@0 91 delegates.put(delegate, Boolean.FALSE);
michael@0 92 }
michael@0 93
michael@0 94 public synchronized void stopObserving(FirefoxAccounts.SyncStatusListener delegate) {
michael@0 95 delegates.remove(delegate);
michael@0 96 // If we are the last delegate leaving the party, stop listening.
michael@0 97 if (delegates.isEmpty()) {
michael@0 98 removeListener();
michael@0 99 }
michael@0 100 }
michael@0 101 }

mercurial