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; michael@0: michael@0: import java.io.IOException; michael@0: michael@0: import org.json.simple.parser.ParseException; michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.SyncConfiguration.ConfigurationBranch; michael@0: import org.mozilla.gecko.sync.repositories.RepositorySessionBundle; michael@0: michael@0: import android.content.SharedPreferences.Editor; michael@0: michael@0: public class SynchronizerConfiguration { michael@0: private static final String LOG_TAG = "SynczrConfiguration"; michael@0: michael@0: public String syncID; michael@0: public RepositorySessionBundle remoteBundle; michael@0: public RepositorySessionBundle localBundle; michael@0: michael@0: public SynchronizerConfiguration(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException { michael@0: this.load(config); michael@0: } michael@0: michael@0: public SynchronizerConfiguration(String syncID, RepositorySessionBundle remoteBundle, RepositorySessionBundle localBundle) { michael@0: this.syncID = syncID; michael@0: this.remoteBundle = remoteBundle; michael@0: this.localBundle = localBundle; michael@0: } michael@0: michael@0: // This should get partly shuffled back into SyncConfiguration, I think. michael@0: public void load(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException { michael@0: if (config == null) { michael@0: throw new IllegalArgumentException("config cannot be null."); michael@0: } michael@0: String remoteJSON = config.getString("remote", null); michael@0: String localJSON = config.getString("local", null); michael@0: RepositorySessionBundle rB = new RepositorySessionBundle(remoteJSON); michael@0: RepositorySessionBundle lB = new RepositorySessionBundle(localJSON); michael@0: if (remoteJSON == null) { michael@0: rB.setTimestamp(0); michael@0: } michael@0: if (localJSON == null) { michael@0: lB.setTimestamp(0); michael@0: } michael@0: syncID = config.getString("syncID", null); michael@0: remoteBundle = rB; michael@0: localBundle = lB; michael@0: Logger.debug(LOG_TAG, "Loaded SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle); michael@0: } michael@0: michael@0: public void persist(ConfigurationBranch config) { michael@0: if (config == null) { michael@0: throw new IllegalArgumentException("config cannot be null."); michael@0: } michael@0: String jsonRemote = remoteBundle.toJSONString(); michael@0: String jsonLocal = localBundle.toJSONString(); michael@0: Editor editor = config.edit(); michael@0: editor.putString("remote", jsonRemote); michael@0: editor.putString("local", jsonLocal); michael@0: editor.putString("syncID", syncID); michael@0: michael@0: // Synchronous. michael@0: editor.commit(); michael@0: Logger.debug(LOG_TAG, "Persisted SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle); michael@0: } michael@0: }