2012-03-12 19:17:56 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-24 13:17:46 -07:00
|
|
|
* 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/. */
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.sync.synchronizer;
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
import org.mozilla.gecko.sync.Logger;
|
2012-01-14 09:20:31 -08:00
|
|
|
import org.mozilla.gecko.sync.SynchronizerConfiguration;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.repositories.Repository;
|
|
|
|
import org.mozilla.gecko.sync.repositories.RepositorySessionBundle;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
/**
|
2012-03-12 19:17:56 -07:00
|
|
|
* I perform a sync.
|
|
|
|
*
|
|
|
|
* Initialize me by calling `load` with a SynchronizerConfiguration.
|
2011-12-21 08:44:08 -08:00
|
|
|
*
|
2012-03-12 19:17:56 -07:00
|
|
|
* Start synchronizing by calling `synchronize` with a SynchronizerDelegate. I
|
|
|
|
* provide coarse-grained feedback by calling my delegate's callback methods.
|
2011-12-21 08:44:08 -08:00
|
|
|
*
|
2012-03-12 19:17:56 -07:00
|
|
|
* 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.
|
2011-12-21 08:44:08 -08:00
|
|
|
*
|
2012-03-12 19:17:56 -07:00
|
|
|
* After synchronizing, call `save` to get back a SynchronizerConfiguration with
|
|
|
|
* updated bundle information.
|
2011-12-21 08:44:08 -08:00
|
|
|
*/
|
2012-05-31 12:21:08 -07:00
|
|
|
public class Synchronizer implements SynchronizerSessionDelegate {
|
|
|
|
public static final String LOG_TAG = "SyncDelSDelegate";
|
2012-01-14 09:20:31 -08:00
|
|
|
|
2012-05-31 12:21:08 -07:00
|
|
|
protected String configSyncID; // Used to pass syncID from load() back into save().
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-05-31 12:21:08 -07:00
|
|
|
protected SynchronizerDelegate synchronizerDelegate;
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-07-11 14:34:22 -07:00
|
|
|
protected SynchronizerSession session = null;
|
|
|
|
|
|
|
|
public SynchronizerSession getSynchronizerSession() {
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2012-05-31 12:21:08 -07:00
|
|
|
@Override
|
|
|
|
public void onInitialized(SynchronizerSession session) {
|
|
|
|
session.synchronize();
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-05-31 12:21:08 -07:00
|
|
|
@Override
|
|
|
|
public void onSynchronized(SynchronizerSession synchronizerSession) {
|
2012-06-12 12:12:43 -07:00
|
|
|
Logger.debug(LOG_TAG, "Got onSynchronized.");
|
|
|
|
Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate.");
|
2012-05-31 12:21:08 -07:00
|
|
|
this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer());
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-05-31 12:21:08 -07:00
|
|
|
@Override
|
|
|
|
public void onSynchronizeSkipped(SynchronizerSession synchronizerSession) {
|
2012-06-12 12:12:43 -07:00
|
|
|
Logger.debug(LOG_TAG, "Got onSynchronizeSkipped.");
|
|
|
|
Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate as if on success.");
|
2012-05-31 12:21:08 -07:00
|
|
|
this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer());
|
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
|
2012-05-31 12:21:08 -07:00
|
|
|
@Override
|
|
|
|
public void onSynchronizeFailed(SynchronizerSession session,
|
|
|
|
Exception lastException, String reason) {
|
|
|
|
this.synchronizerDelegate.onSynchronizeFailed(session.getSynchronizer(), lastException, reason);
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Repository repositoryA;
|
|
|
|
public Repository repositoryB;
|
|
|
|
public RepositorySessionBundle bundleA;
|
|
|
|
public RepositorySessionBundle bundleB;
|
|
|
|
|
2012-05-31 12:21:08 -07:00
|
|
|
/**
|
|
|
|
* Fetch a synchronizer session appropriate for this <code>Synchronizer</code>
|
|
|
|
*/
|
2012-07-11 14:34:22 -07:00
|
|
|
protected SynchronizerSession newSynchronizerSession() {
|
2012-05-31 12:21:08 -07:00
|
|
|
return new SynchronizerSession(this, this);
|
|
|
|
}
|
|
|
|
|
2012-03-12 19:17:56 -07:00
|
|
|
/**
|
|
|
|
* Start synchronizing, calling delegate's callback methods.
|
|
|
|
*/
|
2011-12-21 08:44:08 -08:00
|
|
|
public void synchronize(Context context, SynchronizerDelegate delegate) {
|
2012-05-31 12:21:08 -07:00
|
|
|
this.synchronizerDelegate = delegate;
|
2012-07-11 14:34:22 -07:00
|
|
|
this.session = newSynchronizerSession();
|
|
|
|
this.session.init(context, bundleA, bundleB);
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|
2012-01-14 09:20:31 -08:00
|
|
|
|
|
|
|
public SynchronizerConfiguration save() {
|
2012-05-17 13:20:49 -07:00
|
|
|
return new SynchronizerConfiguration(configSyncID, bundleA, bundleB);
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
|
|
|
|
2012-03-12 19:17:56 -07:00
|
|
|
/**
|
|
|
|
* Set my repository session bundles from a SynchronizerConfiguration.
|
|
|
|
*
|
|
|
|
* This method is not thread-safe.
|
|
|
|
*
|
|
|
|
* @param config
|
|
|
|
*/
|
2012-01-14 09:20:31 -08:00
|
|
|
public void load(SynchronizerConfiguration config) {
|
|
|
|
bundleA = config.remoteBundle;
|
|
|
|
bundleB = config.localBundle;
|
2012-05-17 13:20:49 -07:00
|
|
|
configSyncID = config.syncID;
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|