mobile/android/base/sync/SharedPreferencesNodeAssignmentCallback.java

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:13d6e4657c7d
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 import java.net.URI;
7 import java.net.URISyntaxException;
8
9 import org.mozilla.gecko.sync.delegates.NodeAssignmentCallback;
10
11 import android.content.SharedPreferences;
12 import android.content.SharedPreferences.Editor;
13
14 public class SharedPreferencesNodeAssignmentCallback implements NodeAssignmentCallback {
15 protected final SharedPreferences sharedPreferences;
16 protected final String nodeWeaveURL;
17
18 public SharedPreferencesNodeAssignmentCallback(SharedPreferences sharedPreferences, String nodeWeaveURL)
19 throws SyncConfigurationException {
20 this.sharedPreferences = sharedPreferences;
21 if (nodeWeaveURL == null) {
22 throw new IllegalArgumentException("nodeWeaveURL must not be null");
23 }
24 try {
25 new URI(nodeWeaveURL);
26 } catch (URISyntaxException e) {
27 throw new SyncConfigurationException();
28 }
29 this.nodeWeaveURL = nodeWeaveURL;
30 }
31
32 public synchronized boolean getClusterURLIsStale() {
33 return sharedPreferences.getBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, false);
34 }
35
36 public synchronized void setClusterURLIsStale(boolean clusterURLIsStale) {
37 Editor edit = sharedPreferences.edit();
38 edit.putBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, clusterURLIsStale);
39 edit.commit();
40 }
41
42 @Override
43 public boolean wantNodeAssignment() {
44 return getClusterURLIsStale();
45 }
46
47 @Override
48 public void informNodeAuthenticationFailed(GlobalSession session, URI failedClusterURL) {
49 // TODO: communicate to the user interface that we need a new user password!
50 // TODO: only freshen the cluster URL (better yet, forget the cluster URL) after the user has provided new credentials.
51 setClusterURLIsStale(false);
52 }
53
54 @Override
55 public void informNodeAssigned(GlobalSession session, URI oldClusterURL, URI newClusterURL) {
56 setClusterURLIsStale(false);
57 }
58
59 @Override
60 public String nodeWeaveURL() {
61 return this.nodeWeaveURL;
62 }
63 }

mercurial