mobile/android/base/sync/SynchronizerConfiguration.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/SynchronizerConfiguration.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,69 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync;
     1.9 +
    1.10 +import java.io.IOException;
    1.11 +
    1.12 +import org.json.simple.parser.ParseException;
    1.13 +import org.mozilla.gecko.background.common.log.Logger;
    1.14 +import org.mozilla.gecko.sync.SyncConfiguration.ConfigurationBranch;
    1.15 +import org.mozilla.gecko.sync.repositories.RepositorySessionBundle;
    1.16 +
    1.17 +import android.content.SharedPreferences.Editor;
    1.18 +
    1.19 +public class SynchronizerConfiguration {
    1.20 +  private static final String LOG_TAG = "SynczrConfiguration";
    1.21 +
    1.22 +  public String syncID;
    1.23 +  public RepositorySessionBundle remoteBundle;
    1.24 +  public RepositorySessionBundle localBundle;
    1.25 +
    1.26 +  public SynchronizerConfiguration(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException {
    1.27 +    this.load(config);
    1.28 +  }
    1.29 +
    1.30 +  public SynchronizerConfiguration(String syncID, RepositorySessionBundle remoteBundle, RepositorySessionBundle localBundle) {
    1.31 +    this.syncID       = syncID;
    1.32 +    this.remoteBundle = remoteBundle;
    1.33 +    this.localBundle  = localBundle;
    1.34 +  }
    1.35 +
    1.36 +  // This should get partly shuffled back into SyncConfiguration, I think.
    1.37 +  public void load(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException {
    1.38 +    if (config == null) {
    1.39 +      throw new IllegalArgumentException("config cannot be null.");
    1.40 +    }
    1.41 +    String remoteJSON = config.getString("remote", null);
    1.42 +    String localJSON  = config.getString("local",  null);
    1.43 +    RepositorySessionBundle rB = new RepositorySessionBundle(remoteJSON);
    1.44 +    RepositorySessionBundle lB = new RepositorySessionBundle(localJSON);
    1.45 +    if (remoteJSON == null) {
    1.46 +      rB.setTimestamp(0);
    1.47 +    }
    1.48 +    if (localJSON == null) {
    1.49 +      lB.setTimestamp(0);
    1.50 +    }
    1.51 +    syncID = config.getString("syncID", null);
    1.52 +    remoteBundle = rB;
    1.53 +    localBundle  = lB;
    1.54 +    Logger.debug(LOG_TAG, "Loaded SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle);
    1.55 +  }
    1.56 +
    1.57 +  public void persist(ConfigurationBranch config) {
    1.58 +    if (config == null) {
    1.59 +      throw new IllegalArgumentException("config cannot be null.");
    1.60 +    }
    1.61 +    String jsonRemote = remoteBundle.toJSONString();
    1.62 +    String jsonLocal  = localBundle.toJSONString();
    1.63 +    Editor editor = config.edit();
    1.64 +    editor.putString("remote", jsonRemote);
    1.65 +    editor.putString("local",  jsonLocal);
    1.66 +    editor.putString("syncID", syncID);
    1.67 +
    1.68 +    // Synchronous.
    1.69 +    editor.commit();
    1.70 +    Logger.debug(LOG_TAG, "Persisted SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle);
    1.71 +  }
    1.72 +}

mercurial