Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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.sync.synchronizer; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 8 | import org.mozilla.gecko.sync.SynchronizerConfiguration; |
michael@0 | 9 | import org.mozilla.gecko.sync.repositories.Repository; |
michael@0 | 10 | import org.mozilla.gecko.sync.repositories.RepositorySessionBundle; |
michael@0 | 11 | |
michael@0 | 12 | import android.content.Context; |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * I perform a sync. |
michael@0 | 16 | * |
michael@0 | 17 | * Initialize me by calling `load` with a SynchronizerConfiguration. |
michael@0 | 18 | * |
michael@0 | 19 | * Start synchronizing by calling `synchronize` with a SynchronizerDelegate. I |
michael@0 | 20 | * provide coarse-grained feedback by calling my delegate's callback methods. |
michael@0 | 21 | * |
michael@0 | 22 | * I always call exactly one of my delegate's `onSynchronized` or |
michael@0 | 23 | * `onSynchronizeFailed` callback methods. In addition, I call |
michael@0 | 24 | * `onSynchronizeAborted` before `onSynchronizeFailed` when I encounter a fetch, |
michael@0 | 25 | * store, or session error while synchronizing. |
michael@0 | 26 | * |
michael@0 | 27 | * After synchronizing, call `save` to get back a SynchronizerConfiguration with |
michael@0 | 28 | * updated bundle information. |
michael@0 | 29 | */ |
michael@0 | 30 | public class Synchronizer implements SynchronizerSessionDelegate { |
michael@0 | 31 | public static final String LOG_TAG = "SyncDelSDelegate"; |
michael@0 | 32 | |
michael@0 | 33 | protected String configSyncID; // Used to pass syncID from load() back into save(). |
michael@0 | 34 | |
michael@0 | 35 | protected SynchronizerDelegate synchronizerDelegate; |
michael@0 | 36 | |
michael@0 | 37 | protected SynchronizerSession session = null; |
michael@0 | 38 | |
michael@0 | 39 | public SynchronizerSession getSynchronizerSession() { |
michael@0 | 40 | return session; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | @Override |
michael@0 | 44 | public void onInitialized(SynchronizerSession session) { |
michael@0 | 45 | session.synchronize(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | @Override |
michael@0 | 49 | public void onSynchronized(SynchronizerSession synchronizerSession) { |
michael@0 | 50 | Logger.debug(LOG_TAG, "Got onSynchronized."); |
michael@0 | 51 | Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate."); |
michael@0 | 52 | this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer()); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | @Override |
michael@0 | 56 | public void onSynchronizeSkipped(SynchronizerSession synchronizerSession) { |
michael@0 | 57 | Logger.debug(LOG_TAG, "Got onSynchronizeSkipped."); |
michael@0 | 58 | Logger.debug(LOG_TAG, "Notifying SynchronizerDelegate as if on success."); |
michael@0 | 59 | this.synchronizerDelegate.onSynchronized(synchronizerSession.getSynchronizer()); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | @Override |
michael@0 | 63 | public void onSynchronizeFailed(SynchronizerSession session, |
michael@0 | 64 | Exception lastException, String reason) { |
michael@0 | 65 | this.synchronizerDelegate.onSynchronizeFailed(session.getSynchronizer(), lastException, reason); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | public Repository repositoryA; |
michael@0 | 69 | public Repository repositoryB; |
michael@0 | 70 | public RepositorySessionBundle bundleA; |
michael@0 | 71 | public RepositorySessionBundle bundleB; |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Fetch a synchronizer session appropriate for this <code>Synchronizer</code> |
michael@0 | 75 | */ |
michael@0 | 76 | protected SynchronizerSession newSynchronizerSession() { |
michael@0 | 77 | return new SynchronizerSession(this, this); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | /** |
michael@0 | 81 | * Start synchronizing, calling delegate's callback methods. |
michael@0 | 82 | */ |
michael@0 | 83 | public void synchronize(Context context, SynchronizerDelegate delegate) { |
michael@0 | 84 | this.synchronizerDelegate = delegate; |
michael@0 | 85 | this.session = newSynchronizerSession(); |
michael@0 | 86 | this.session.init(context, bundleA, bundleB); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | public SynchronizerConfiguration save() { |
michael@0 | 90 | return new SynchronizerConfiguration(configSyncID, bundleA, bundleB); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Set my repository session bundles from a SynchronizerConfiguration. |
michael@0 | 95 | * |
michael@0 | 96 | * This method is not thread-safe. |
michael@0 | 97 | * |
michael@0 | 98 | * @param config |
michael@0 | 99 | */ |
michael@0 | 100 | public void load(SynchronizerConfiguration config) { |
michael@0 | 101 | bundleA = config.remoteBundle; |
michael@0 | 102 | bundleB = config.localBundle; |
michael@0 | 103 | configSyncID = config.syncID; |
michael@0 | 104 | } |
michael@0 | 105 | } |