|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync; |
|
6 |
|
7 import java.io.IOException; |
|
8 |
|
9 import org.json.simple.parser.ParseException; |
|
10 import org.mozilla.gecko.background.common.log.Logger; |
|
11 import org.mozilla.gecko.sync.SyncConfiguration.ConfigurationBranch; |
|
12 import org.mozilla.gecko.sync.repositories.RepositorySessionBundle; |
|
13 |
|
14 import android.content.SharedPreferences.Editor; |
|
15 |
|
16 public class SynchronizerConfiguration { |
|
17 private static final String LOG_TAG = "SynczrConfiguration"; |
|
18 |
|
19 public String syncID; |
|
20 public RepositorySessionBundle remoteBundle; |
|
21 public RepositorySessionBundle localBundle; |
|
22 |
|
23 public SynchronizerConfiguration(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException { |
|
24 this.load(config); |
|
25 } |
|
26 |
|
27 public SynchronizerConfiguration(String syncID, RepositorySessionBundle remoteBundle, RepositorySessionBundle localBundle) { |
|
28 this.syncID = syncID; |
|
29 this.remoteBundle = remoteBundle; |
|
30 this.localBundle = localBundle; |
|
31 } |
|
32 |
|
33 // This should get partly shuffled back into SyncConfiguration, I think. |
|
34 public void load(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException { |
|
35 if (config == null) { |
|
36 throw new IllegalArgumentException("config cannot be null."); |
|
37 } |
|
38 String remoteJSON = config.getString("remote", null); |
|
39 String localJSON = config.getString("local", null); |
|
40 RepositorySessionBundle rB = new RepositorySessionBundle(remoteJSON); |
|
41 RepositorySessionBundle lB = new RepositorySessionBundle(localJSON); |
|
42 if (remoteJSON == null) { |
|
43 rB.setTimestamp(0); |
|
44 } |
|
45 if (localJSON == null) { |
|
46 lB.setTimestamp(0); |
|
47 } |
|
48 syncID = config.getString("syncID", null); |
|
49 remoteBundle = rB; |
|
50 localBundle = lB; |
|
51 Logger.debug(LOG_TAG, "Loaded SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle); |
|
52 } |
|
53 |
|
54 public void persist(ConfigurationBranch config) { |
|
55 if (config == null) { |
|
56 throw new IllegalArgumentException("config cannot be null."); |
|
57 } |
|
58 String jsonRemote = remoteBundle.toJSONString(); |
|
59 String jsonLocal = localBundle.toJSONString(); |
|
60 Editor editor = config.edit(); |
|
61 editor.putString("remote", jsonRemote); |
|
62 editor.putString("local", jsonLocal); |
|
63 editor.putString("syncID", syncID); |
|
64 |
|
65 // Synchronous. |
|
66 editor.commit(); |
|
67 Logger.debug(LOG_TAG, "Persisted SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle); |
|
68 } |
|
69 } |