Bug 1346061 part 2 - Register device on FxA on Engaged state. r?nalexander, grisha draft
authorEdouard Oger <eoger@fastmail.com>
Wed, 22 Mar 2017 13:11:34 -0400
changeset 552539 d49ddc1568ccd4f5d9079ecdd532c80cf6d190a6
parent 552538 314023e343c0be3aba2d30bff204665965484638
child 552540 a843bc051ed89bdcdee19702b466ffeea5e981c0
push id51379
push userbmo:eoger@fastmail.com
push dateTue, 28 Mar 2017 16:39:50 +0000
reviewersnalexander, grisha
bugs1346061
milestone55.0a1
Bug 1346061 part 2 - Register device on FxA on Engaged state. r?nalexander, grisha MozReview-Commit-ID: 3HrXePgBxg6
mobile/android/services/src/main/java/org/mozilla/gecko/fxa/sync/FxAccountSyncAdapter.java
--- a/mobile/android/services/src/main/java/org/mozilla/gecko/fxa/sync/FxAccountSyncAdapter.java
+++ b/mobile/android/services/src/main/java/org/mozilla/gecko/fxa/sync/FxAccountSyncAdapter.java
@@ -399,16 +399,32 @@ public class FxAccountSyncAdapter extend
         return System.currentTimeMillis() + delay;
       }
     };
 
     TokenServerClient tokenServerclient = new TokenServerClient(tokenServerEndpointURI, executor);
     tokenServerclient.getTokenFromBrowserIDAssertion(assertion, true, clientState, delegate);
   }
 
+  public void maybeRegisterDevice(Context context, AndroidFxAccount fxAccount) {
+    // Register the device if necessary (asynchronous, in another thread).
+    // As part of device registration, we obtain a PushSubscription, register our push endpoint
+    // with FxA, and update account data with fxaDeviceId, which is part of our synced
+    // clients record.
+    if (fxAccount.getDeviceRegistrationVersion() != FxAccountDeviceRegistrator.DEVICE_REGISTRATION_VERSION
+            || TextUtils.isEmpty(fxAccount.getDeviceId())) {
+      FxAccountDeviceRegistrator.register(context);
+      // We might need to re-register periodically to ensure our FxA push subscription is valid.
+      // This involves unsubscribing, subscribing and updating remote FxA device record with
+      // new push subscription information.
+    } else if (FxAccountDeviceRegistrator.needToRenewRegistration(fxAccount.getDeviceRegistrationTimestamp())) {
+      FxAccountDeviceRegistrator.renewRegistration(context);
+    }
+  }
+
   /**
    * A trivial Sync implementation that does not cache client keys,
    * certificates, or tokens.
    *
    * This should be replaced with a full {@link FxAccountAuthenticator}-based
    * token implementation.
    */
   @Override
@@ -525,16 +541,19 @@ public class FxAccountSyncAdapter extend
 
       final FxAccountLoginStateMachine stateMachine = new FxAccountLoginStateMachine();
       stateMachine.advance(state, StateLabel.Married, new FxADefaultLoginStateMachineDelegate(context, fxAccount) {
         @Override
         public void handleNotMarried(State notMarried) {
           Logger.info(LOG_TAG, "handleNotMarried: in " + notMarried.getStateLabel());
           schedulePolicy.onHandleFinal(notMarried.getNeededAction());
           syncDelegate.handleCannotSync(notMarried);
+          if (notMarried.getStateLabel() == StateLabel.Engaged) {
+            maybeRegisterDevice(context, fxAccount);
+          }
         }
 
         private boolean shouldRequestToken(final BackoffHandler tokenBackoffHandler, final Bundle extras) {
           return shouldPerformSync(tokenBackoffHandler, "token", extras);
         }
 
         @Override
         public void handleMarried(Married married) {
@@ -573,29 +592,17 @@ public class FxAccountSyncAdapter extend
 
             final SessionCallback sessionCallback = new SessionCallback(syncDelegate, schedulePolicy);
             final KeyBundle syncKeyBundle = married.getSyncKeyBundle();
             final String clientState = married.getClientState();
             syncWithAssertion(
                     assertion, tokenServerEndpointURI, tokenBackoffHandler, sharedPrefs,
                     syncKeyBundle, clientState, sessionCallback, extras, fxAccount, syncDeadline);
 
-            // Register the device if necessary (asynchronous, in another thread).
-            // As part of device registration, we obtain a PushSubscription, register our push endpoint
-            // with FxA, and update account data with fxaDeviceId, which is part of our synced
-            // clients record.
-            if (fxAccount.getDeviceRegistrationVersion() != FxAccountDeviceRegistrator.DEVICE_REGISTRATION_VERSION
-              || TextUtils.isEmpty(fxAccount.getDeviceId())) {
-              FxAccountDeviceRegistrator.register(context);
-            // We might need to re-register periodically to ensure our FxA push subscription is valid.
-            // This involves unsubscribing, subscribing and updating remote FxA device record with
-            // new push subscription information.
-            } else if (FxAccountDeviceRegistrator.needToRenewRegistration(fxAccount.getDeviceRegistrationTimestamp())) {
-              FxAccountDeviceRegistrator.renewRegistration(context);
-            }
+            maybeRegisterDevice(context, fxAccount);
 
             // Force fetch the profile avatar information. (asynchronous, in another thread)
             Logger.info(LOG_TAG, "Fetching profile avatar information.");
             fxAccount.fetchProfileJSON();
           } catch (Exception e) {
             syncDelegate.handleError(e);
             return;
           }