Bug 1250782 - Update device registration when getting fxaDeviceId if necessary draft
authorEdouard Oger <eoger@fastmail.com>
Wed, 18 May 2016 13:43:53 -0700
changeset 369925 48342f51b611fa14c9b42fc85bf1543434a804b7
parent 369924 269dab1c0dbf7be3dfafecac68415a5add1194b4
child 521651 c998b731c815c35a9880ca36e0c3898511c87dd3
push id18951
push userbmo:edouard.oger@gmail.com
push dateTue, 24 May 2016 00:09:32 +0000
bugs1250782
milestone49.0a1
Bug 1250782 - Update device registration when getting fxaDeviceId if necessary MozReview-Commit-ID: A5HruZVE5WL
mobile/android/services/src/main/java/org/mozilla/gecko/fxa/authenticator/AndroidFxAccount.java
mobile/android/services/src/main/java/org/mozilla/gecko/sync/stage/SyncClientsEngineStage.java
--- a/mobile/android/services/src/main/java/org/mozilla/gecko/fxa/authenticator/AndroidFxAccount.java
+++ b/mobile/android/services/src/main/java/org/mozilla/gecko/fxa/authenticator/AndroidFxAccount.java
@@ -21,16 +21,18 @@ import android.text.TextUtils;
 import android.util.Log;
 
 import org.mozilla.gecko.background.common.GlobalConstants;
 import org.mozilla.gecko.background.common.log.Logger;
 import org.mozilla.gecko.background.fxa.FxAccountUtils;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.fxa.FirefoxAccounts;
 import org.mozilla.gecko.fxa.FxAccountConstants;
+import org.mozilla.gecko.fxa.FxAccountDeviceRegistrator;
+import org.mozilla.gecko.fxa.FxAccountDeviceRegistrator.RegisterDelegate;
 import org.mozilla.gecko.fxa.login.State;
 import org.mozilla.gecko.fxa.login.State.StateLabel;
 import org.mozilla.gecko.fxa.login.StateFactory;
 import org.mozilla.gecko.fxa.sync.FxAccountProfileService;
 import org.mozilla.gecko.sync.ExtendedJSONObject;
 import org.mozilla.gecko.sync.Utils;
 import org.mozilla.gecko.sync.setup.Constants;
 import org.mozilla.gecko.util.ThreadUtils;
@@ -777,16 +779,28 @@ public class AndroidFxAccount {
       }
     });
   }
 
   public String getDeviceId() {
     return accountManager.getUserData(account, ACCOUNT_KEY_DEVICE_ID);
   }
 
+  // This version of getDeviceId will update the device registration if too old or not registered
+  // and will return the deviceId in the delegate.
+  // Otherwise the delegate will not be called
+  public void maybeGetDeviceId(RegisterDelegate delegate) {
+    String deviceId = getDeviceId();
+    if (TextUtils.isEmpty(deviceId)
+            || getDeviceRegistrationVersion() < FxAccountDeviceRegistrator.DEVICE_REGISTRATION_VERSION) {
+      FxAccountDeviceRegistrator deviceRegistrator = new FxAccountDeviceRegistrator(this, context);
+      deviceRegistrator.register(delegate);
+    }
+  }
+
   @NonNull
   private Integer getDeviceRegistrationVersion() {
     String versionStr = accountManager.getUserData(account, ACCOUNT_KEY_DEVICE_REGISTRATION_VERSION);
     if (TextUtils.isEmpty(versionStr)) {
       return 0;
     } else {
       return Integer.parseInt(versionStr);
     }
--- a/mobile/android/services/src/main/java/org/mozilla/gecko/sync/stage/SyncClientsEngineStage.java
+++ b/mobile/android/services/src/main/java/org/mozilla/gecko/sync/stage/SyncClientsEngineStage.java
@@ -15,16 +15,17 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.background.common.log.Logger;
 import org.mozilla.gecko.fxa.FirefoxAccounts;
+import org.mozilla.gecko.fxa.FxAccountDeviceRegistrator.RegisterDelegate;
 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
 import org.mozilla.gecko.sync.CommandProcessor;
 import org.mozilla.gecko.sync.CommandProcessor.Command;
 import org.mozilla.gecko.sync.CryptoRecord;
 import org.mozilla.gecko.sync.ExtendedJSONObject;
 import org.mozilla.gecko.sync.HTTPFailureException;
 import org.mozilla.gecko.sync.NoCollectionKeysSetException;
 import org.mozilla.gecko.sync.Utils;
@@ -390,19 +391,31 @@ public class SyncClientsEngineStage exte
     r.appPackage = AppConstants.ANDROID_PACKAGE_NAME;
     r.device = android.os.Build.MODEL;
     r.formfactor = delegate.getFormFactor();
 
     Context context = session.getContext();
     final Account account = FirefoxAccounts.getFirefoxAccount(context);
     if (account != null) {
       final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);
-      final String deviceId = fxAccount.getDeviceId();
-      if (!TextUtils.isEmpty(deviceId)) {
-        r.fxaDeviceId = deviceId;
+
+      final String cachedDeviceId = fxAccount.getDeviceId();
+      fxAccount.maybeGetDeviceId(new RegisterDelegate() {
+        @Override
+        public void onComplete(String deviceId) {
+          try {
+            // In case the deviceId changes
+            if (!deviceId.equals(cachedDeviceId)) {
+              execute();
+            }
+          } catch (NoSuchStageException e) {}
+        }
+      });
+      if (!TextUtils.isEmpty(cachedDeviceId)) {
+        r.fxaDeviceId = cachedDeviceId;
       }
     }
 
 
     return r;
   }
 
   // TODO: Bug 726055 - More considered handling of when to sync.