Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync; |
michael@0 | 6 | import java.net.URI; |
michael@0 | 7 | import java.net.URISyntaxException; |
michael@0 | 8 | |
michael@0 | 9 | import org.mozilla.gecko.sync.delegates.NodeAssignmentCallback; |
michael@0 | 10 | |
michael@0 | 11 | import android.content.SharedPreferences; |
michael@0 | 12 | import android.content.SharedPreferences.Editor; |
michael@0 | 13 | |
michael@0 | 14 | public class SharedPreferencesNodeAssignmentCallback implements NodeAssignmentCallback { |
michael@0 | 15 | protected final SharedPreferences sharedPreferences; |
michael@0 | 16 | protected final String nodeWeaveURL; |
michael@0 | 17 | |
michael@0 | 18 | public SharedPreferencesNodeAssignmentCallback(SharedPreferences sharedPreferences, String nodeWeaveURL) |
michael@0 | 19 | throws SyncConfigurationException { |
michael@0 | 20 | this.sharedPreferences = sharedPreferences; |
michael@0 | 21 | if (nodeWeaveURL == null) { |
michael@0 | 22 | throw new IllegalArgumentException("nodeWeaveURL must not be null"); |
michael@0 | 23 | } |
michael@0 | 24 | try { |
michael@0 | 25 | new URI(nodeWeaveURL); |
michael@0 | 26 | } catch (URISyntaxException e) { |
michael@0 | 27 | throw new SyncConfigurationException(); |
michael@0 | 28 | } |
michael@0 | 29 | this.nodeWeaveURL = nodeWeaveURL; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | public synchronized boolean getClusterURLIsStale() { |
michael@0 | 33 | return sharedPreferences.getBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, false); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | public synchronized void setClusterURLIsStale(boolean clusterURLIsStale) { |
michael@0 | 37 | Editor edit = sharedPreferences.edit(); |
michael@0 | 38 | edit.putBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, clusterURLIsStale); |
michael@0 | 39 | edit.commit(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | @Override |
michael@0 | 43 | public boolean wantNodeAssignment() { |
michael@0 | 44 | return getClusterURLIsStale(); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | @Override |
michael@0 | 48 | public void informNodeAuthenticationFailed(GlobalSession session, URI failedClusterURL) { |
michael@0 | 49 | // TODO: communicate to the user interface that we need a new user password! |
michael@0 | 50 | // TODO: only freshen the cluster URL (better yet, forget the cluster URL) after the user has provided new credentials. |
michael@0 | 51 | setClusterURLIsStale(false); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | @Override |
michael@0 | 55 | public void informNodeAssigned(GlobalSession session, URI oldClusterURL, URI newClusterURL) { |
michael@0 | 56 | setClusterURLIsStale(false); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | @Override |
michael@0 | 60 | public String nodeWeaveURL() { |
michael@0 | 61 | return this.nodeWeaveURL; |
michael@0 | 62 | } |
michael@0 | 63 | } |