2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.sync;
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
import org.mozilla.gecko.sync.SyncConfiguration.ConfigurationBranch;
|
2011-12-21 08:44:08 -08:00
|
|
|
import org.mozilla.gecko.sync.repositories.RepositorySessionBundle;
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
public class SynchronizerConfiguration {
|
2012-02-15 22:05:52 -08:00
|
|
|
private static final String LOG_TAG = "SynczrConfiguration";
|
2011-12-21 08:44:08 -08:00
|
|
|
|
|
|
|
public String syncID;
|
|
|
|
public RepositorySessionBundle remoteBundle;
|
|
|
|
public RepositorySessionBundle localBundle;
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
public SynchronizerConfiguration(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException {
|
|
|
|
this.load(config);
|
|
|
|
}
|
|
|
|
|
2011-12-21 08:44:08 -08:00
|
|
|
public SynchronizerConfiguration(String syncID, RepositorySessionBundle remoteBundle, RepositorySessionBundle localBundle) {
|
|
|
|
this.syncID = syncID;
|
|
|
|
this.remoteBundle = remoteBundle;
|
|
|
|
this.localBundle = localBundle;
|
|
|
|
}
|
|
|
|
|
2012-01-14 09:20:31 -08:00
|
|
|
// This should get partly shuffled back into SyncConfiguration, I think.
|
|
|
|
public void load(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException {
|
|
|
|
if (config == null) {
|
|
|
|
throw new IllegalArgumentException("config cannot be null.");
|
|
|
|
}
|
|
|
|
String remoteJSON = config.getString("remote", null);
|
|
|
|
String localJSON = config.getString("local", null);
|
|
|
|
RepositorySessionBundle rB = new RepositorySessionBundle(remoteJSON);
|
|
|
|
RepositorySessionBundle lB = new RepositorySessionBundle(localJSON);
|
|
|
|
if (remoteJSON == null) {
|
|
|
|
rB.setTimestamp(0);
|
|
|
|
}
|
|
|
|
if (localJSON == null) {
|
|
|
|
lB.setTimestamp(0);
|
|
|
|
}
|
|
|
|
syncID = config.getString("syncID", null);
|
|
|
|
remoteBundle = rB;
|
|
|
|
localBundle = lB;
|
2012-05-17 13:20:49 -07:00
|
|
|
Logger.debug(LOG_TAG, "Loaded SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle);
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void persist(ConfigurationBranch config) {
|
|
|
|
if (config == null) {
|
|
|
|
throw new IllegalArgumentException("config cannot be null.");
|
|
|
|
}
|
|
|
|
String jsonRemote = remoteBundle.toJSONString();
|
|
|
|
String jsonLocal = localBundle.toJSONString();
|
|
|
|
Editor editor = config.edit();
|
|
|
|
editor.putString("remote", jsonRemote);
|
|
|
|
editor.putString("local", jsonLocal);
|
|
|
|
editor.putString("syncID", syncID);
|
|
|
|
|
|
|
|
// Synchronous.
|
|
|
|
editor.commit();
|
2012-05-17 13:20:49 -07:00
|
|
|
Logger.debug(LOG_TAG, "Persisted SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle);
|
2012-01-14 09:20:31 -08:00
|
|
|
}
|
2011-12-21 08:44:08 -08:00
|
|
|
}
|