mobile/android/base/sync/SharedPreferencesNodeAssignmentCallback.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 package org.mozilla.gecko.sync;
     6 import java.net.URI;
     7 import java.net.URISyntaxException;
     9 import org.mozilla.gecko.sync.delegates.NodeAssignmentCallback;
    11 import android.content.SharedPreferences;
    12 import android.content.SharedPreferences.Editor;
    14 public class SharedPreferencesNodeAssignmentCallback implements NodeAssignmentCallback {
    15   protected final SharedPreferences sharedPreferences;
    16   protected final String nodeWeaveURL;
    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   }
    32   public synchronized boolean getClusterURLIsStale() {
    33     return sharedPreferences.getBoolean(SyncConfiguration.PREF_CLUSTER_URL_IS_STALE, false);
    34   }
    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   }
    42   @Override
    43   public boolean wantNodeAssignment() {
    44     return getClusterURLIsStale();
    45   }
    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   }
    54   @Override
    55   public void informNodeAssigned(GlobalSession session, URI oldClusterURL, URI newClusterURL) {
    56     setClusterURLIsStale(false);
    57   }
    59   @Override
    60   public String nodeWeaveURL() {
    61     return this.nodeWeaveURL;
    62   }
    63 }

mercurial