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: import java.net.URI; michael@0: import java.net.URISyntaxException; michael@0: michael@0: import org.mozilla.gecko.sync.delegates.NodeAssignmentCallback; michael@0: michael@0: import android.content.SharedPreferences; michael@0: import android.content.SharedPreferences.Editor; michael@0: michael@0: public class SharedPreferencesNodeAssignmentCallback implements NodeAssignmentCallback { michael@0: protected final SharedPreferences sharedPreferences; michael@0: protected final String nodeWeaveURL; michael@0: michael@0: public SharedPreferencesNodeAssignmentCallback(SharedPreferences sharedPreferences, String nodeWeaveURL) michael@0: throws SyncConfigurationException { michael@0: this.sharedPreferences = sharedPreferences; michael@0: if (nodeWeaveURL == null) { michael@0: throw new IllegalArgumentException("nodeWeaveURL must not be null"); michael@0: } michael@0: try { michael@0: new URI(nodeWeaveURL); michael@0: } catch (URISyntaxException e) { michael@0: throw new SyncConfigurationException(); michael@0: } michael@0: this.nodeWeaveURL = nodeWeaveURL; michael@0: } michael@0: michael@0: public synchronized boolean getClusterURLIsStale() { michael@0: return sharedPreferences.getBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, false); michael@0: } michael@0: michael@0: public synchronized void setClusterURLIsStale(boolean clusterURLIsStale) { michael@0: Editor edit = sharedPreferences.edit(); michael@0: edit.putBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, clusterURLIsStale); michael@0: edit.commit(); michael@0: } michael@0: michael@0: @Override michael@0: public boolean wantNodeAssignment() { michael@0: return getClusterURLIsStale(); michael@0: } michael@0: michael@0: @Override michael@0: public void informNodeAuthenticationFailed(GlobalSession session, URI failedClusterURL) { michael@0: // TODO: communicate to the user interface that we need a new user password! michael@0: // TODO: only freshen the cluster URL (better yet, forget the cluster URL) after the user has provided new credentials. michael@0: setClusterURLIsStale(false); michael@0: } michael@0: michael@0: @Override michael@0: public void informNodeAssigned(GlobalSession session, URI oldClusterURL, URI newClusterURL) { michael@0: setClusterURLIsStale(false); michael@0: } michael@0: michael@0: @Override michael@0: public String nodeWeaveURL() { michael@0: return this.nodeWeaveURL; michael@0: } michael@0: }