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.synchronizer; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.SynchronizerConfiguration; michael@0: import org.mozilla.gecko.sync.repositories.Repository; michael@0: import org.mozilla.gecko.sync.repositories.RepositorySessionBundle; michael@0: michael@0: import android.content.Context; michael@0: michael@0: /** michael@0: * I perform a sync. michael@0: * michael@0: * Initialize me by calling `load` with a SynchronizerConfiguration. michael@0: * michael@0: * Start synchronizing by calling `synchronize` with a SynchronizerDelegate. I michael@0: * provide coarse-grained feedback by calling my delegate's callback methods. michael@0: * michael@0: * I always call exactly one of my delegate's `onSynchronized` or michael@0: * `onSynchronizeFailed` callback methods. In addition, I call michael@0: * `onSynchronizeAborted` before `onSynchronizeFailed` when I encounter a fetch, michael@0: * store, or session error while synchronizing. michael@0: * michael@0: * After synchronizing, call `save` to get back a SynchronizerConfiguration with michael@0: * updated bundle information. michael@0: */ michael@0: public class Synchronizer implements SynchronizerSessionDelegate { michael@0: public static final String LOG_TAG = "SyncDelSDelegate"; michael@0: michael@0: protected String configSyncID; // Used to pass syncID from load() back into save(). michael@0: michael@0: protected SynchronizerDelegate synchronizerDelegate; michael@0: michael@0: protected SynchronizerSession session = null; michael@0: michael@0: public SynchronizerSession getSynchronizerSession() { michael@0: return session; michael@0: } michael@0: michael@0: @Override michael@0: public void onInitialized(SynchronizerSession session) { michael@0: session.synchronize(); michael@0: } michael@0: michael@0: @Override michael@0: public void onSynchronized(SynchronizerSession synchronizerSession) { michael@0: Logger.debug(LOG_TAG, "Got onSynchronized."); michael@0: Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate."); michael@0: this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer()); michael@0: } michael@0: michael@0: @Override michael@0: public void onSynchronizeSkipped(SynchronizerSession synchronizerSession) { michael@0: Logger.debug(LOG_TAG, "Got onSynchronizeSkipped."); michael@0: Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate as if on success."); michael@0: this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer()); michael@0: } michael@0: michael@0: @Override michael@0: public void onSynchronizeFailed(SynchronizerSession session, michael@0: Exception lastException, String reason) { michael@0: this.synchronizerDelegate.onSynchronizeFailed(session.getSynchronizer(), lastException, reason); michael@0: } michael@0: michael@0: public Repository repositoryA; michael@0: public Repository repositoryB; michael@0: public RepositorySessionBundle bundleA; michael@0: public RepositorySessionBundle bundleB; michael@0: michael@0: /** michael@0: * Fetch a synchronizer session appropriate for this Synchronizer michael@0: */ michael@0: protected SynchronizerSession newSynchronizerSession() { michael@0: return new SynchronizerSession(this, this); michael@0: } michael@0: michael@0: /** michael@0: * Start synchronizing, calling delegate's callback methods. michael@0: */ michael@0: public void synchronize(Context context, SynchronizerDelegate delegate) { michael@0: this.synchronizerDelegate = delegate; michael@0: this.session = newSynchronizerSession(); michael@0: this.session.init(context, bundleA, bundleB); michael@0: } michael@0: michael@0: public SynchronizerConfiguration save() { michael@0: return new SynchronizerConfiguration(configSyncID, bundleA, bundleB); michael@0: } michael@0: michael@0: /** michael@0: * Set my repository session bundles from a SynchronizerConfiguration. michael@0: * michael@0: * This method is not thread-safe. michael@0: * michael@0: * @param config michael@0: */ michael@0: public void load(SynchronizerConfiguration config) { michael@0: bundleA = config.remoteBundle; michael@0: bundleB = config.localBundle; michael@0: configSyncID = config.syncID; michael@0: } michael@0: }