1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/SharedPreferencesNodeAssignmentCallback.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,63 @@ 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 +import java.net.URI; 1.10 +import java.net.URISyntaxException; 1.11 + 1.12 +import org.mozilla.gecko.sync.delegates.NodeAssignmentCallback; 1.13 + 1.14 +import android.content.SharedPreferences; 1.15 +import android.content.SharedPreferences.Editor; 1.16 + 1.17 +public class SharedPreferencesNodeAssignmentCallback implements NodeAssignmentCallback { 1.18 + protected final SharedPreferences sharedPreferences; 1.19 + protected final String nodeWeaveURL; 1.20 + 1.21 + public SharedPreferencesNodeAssignmentCallback(SharedPreferences sharedPreferences, String nodeWeaveURL) 1.22 + throws SyncConfigurationException { 1.23 + this.sharedPreferences = sharedPreferences; 1.24 + if (nodeWeaveURL == null) { 1.25 + throw new IllegalArgumentException("nodeWeaveURL must not be null"); 1.26 + } 1.27 + try { 1.28 + new URI(nodeWeaveURL); 1.29 + } catch (URISyntaxException e) { 1.30 + throw new SyncConfigurationException(); 1.31 + } 1.32 + this.nodeWeaveURL = nodeWeaveURL; 1.33 + } 1.34 + 1.35 + public synchronized boolean getClusterURLIsStale() { 1.36 + return sharedPreferences.getBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, false); 1.37 + } 1.38 + 1.39 + public synchronized void setClusterURLIsStale(boolean clusterURLIsStale) { 1.40 + Editor edit = sharedPreferences.edit(); 1.41 + edit.putBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, clusterURLIsStale); 1.42 + edit.commit(); 1.43 + } 1.44 + 1.45 + @Override 1.46 + public boolean wantNodeAssignment() { 1.47 + return getClusterURLIsStale(); 1.48 + } 1.49 + 1.50 + @Override 1.51 + public void informNodeAuthenticationFailed(GlobalSession session, URI failedClusterURL) { 1.52 + // TODO: communicate to the user interface that we need a new user password! 1.53 + // TODO: only freshen the cluster URL (better yet, forget the cluster URL) after the user has provided new credentials. 1.54 + setClusterURLIsStale(false); 1.55 + } 1.56 + 1.57 + @Override 1.58 + public void informNodeAssigned(GlobalSession session, URI oldClusterURL, URI newClusterURL) { 1.59 + setClusterURLIsStale(false); 1.60 + } 1.61 + 1.62 + @Override 1.63 + public String nodeWeaveURL() { 1.64 + return this.nodeWeaveURL; 1.65 + } 1.66 +}