gecko/mobile/android/base/sync/synchronizer/Synchronizer.java
Nick Alexander 80b57618b7 Bug 844347 - Factor logging code that is not Sync-specific out of org.mozilla.gecko.sync. r=rnewman
--HG--
rename : mobile/android/base/sync/GlobalConstants.java.in => mobile/android/base/background/common/GlobalConstants.java.in
rename : mobile/android/base/sync/Logger.java => mobile/android/base/background/common/log/Logger.java
rename : mobile/android/base/sync/log/writers/AndroidLevelCachingLogWriter.java => mobile/android/base/background/common/log/writers/AndroidLevelCachingLogWriter.java
rename : mobile/android/base/sync/log/writers/AndroidLogWriter.java => mobile/android/base/background/common/log/writers/AndroidLogWriter.java
rename : mobile/android/base/sync/log/writers/LevelFilteringLogWriter.java => mobile/android/base/background/common/log/writers/LevelFilteringLogWriter.java
rename : mobile/android/base/sync/log/writers/LogWriter.java => mobile/android/base/background/common/log/writers/LogWriter.java
rename : mobile/android/base/sync/log/writers/PrintLogWriter.java => mobile/android/base/background/common/log/writers/PrintLogWriter.java
rename : mobile/android/base/sync/log/writers/SimpleTagLogWriter.java => mobile/android/base/background/common/log/writers/SimpleTagLogWriter.java
rename : mobile/android/base/sync/log/writers/StringLogWriter.java => mobile/android/base/background/common/log/writers/StringLogWriter.java
rename : mobile/android/base/sync/log/writers/TagLogWriter.java => mobile/android/base/background/common/log/writers/TagLogWriter.java
rename : mobile/android/base/sync/log/writers/ThreadLocalTagLogWriter.java => mobile/android/base/background/common/log/writers/ThreadLocalTagLogWriter.java
2013-02-27 15:44:21 -08:00

106 lines
3.5 KiB
Java

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