Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/signin/ConfirmSyncDataStateMachine.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/signin/ConfirmSyncDataStateMachine.java b/chrome/android/java/src/org/chromium/chrome/browser/signin/ConfirmSyncDataStateMachine.java |
| index b545e8177948046899b54b1c531bbdddd8b97f3c..bf224d7dcd6f1cd2d473b7bb1183695b7dcbfe04 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/signin/ConfirmSyncDataStateMachine.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/signin/ConfirmSyncDataStateMachine.java |
| @@ -12,7 +12,7 @@ import android.support.annotation.IntDef; |
| import android.text.TextUtils; |
| import org.chromium.base.Callback; |
| -import org.chromium.base.Promise; |
| +import org.chromium.base.ThreadUtils; |
| import org.chromium.chrome.browser.signin.ConfirmImportSyncDataDialog.ImportSyncType; |
| import java.lang.annotation.Retention; |
| @@ -25,12 +25,13 @@ import java.lang.annotation.RetentionPolicy; |
| * |
| * This class progresses along the following state machine: |
| * |
| - * E----\ G--\ |
| - * ^ | ^ | |
| - * | | | v |
| - * A->B->C->D-+->F->H |
| - * | | |
| - * \-------/ |
| + * E-----\ G--\ |
| + * ^ | ^ | |
| + * | v | v |
| + * A->B->C->D->+->F->H |
| + * | ^ |
| + * v | |
| + * \--------/ |
| * |
| * Where: |
| * A - Start |
| @@ -48,29 +49,29 @@ import java.lang.annotation.RetentionPolicy; |
| */ |
| public class ConfirmSyncDataStateMachine |
| implements ConfirmImportSyncDataDialog.Listener, ConfirmManagedSyncDataDialog.Listener { |
| - |
| - @IntDef({ |
| - BEFORE_OLD_ACCOUNT_DIALOG, BEFORE_NEW_ACCOUNT_DIALOG, |
| - AFTER_NEW_ACCOUNT_DIALOG, DONE |
| - }) |
| @Retention(RetentionPolicy.SOURCE) |
| + @IntDef({BEFORE_OLD_ACCOUNT_DIALOG, BEFORE_NEW_ACCOUNT_DIALOG, AFTER_NEW_ACCOUNT_DIALOG, DONE}) |
| private @interface State {} |
| private static final int BEFORE_OLD_ACCOUNT_DIALOG = 0; // Start of state B. |
| private static final int BEFORE_NEW_ACCOUNT_DIALOG = 1; // Start of state F. |
| private static final int AFTER_NEW_ACCOUNT_DIALOG = 2; // Start of state H. |
| private static final int DONE = 4; |
| - private boolean mWipeData; |
| @State private int mState = BEFORE_OLD_ACCOUNT_DIALOG; |
| + private static final int ACCOUNT_CHECK_TIMEOUT_MS = 30000; |
| + |
| private final ConfirmImportSyncDataDialog.Listener mCallback; |
| private final String mOldAccountName; |
| private final String mNewAccountName; |
| private final boolean mCurrentlyManaged; |
| - private final Promise<Boolean> mNewAccountManaged = new Promise<>(); |
| private final FragmentManager mFragmentManager; |
| private final Context mContext; |
| private final ImportSyncType mImportSyncType; |
| + private final ConfirmSyncDataStateMachineDelegate mDelegate; |
| + |
| + private boolean mWipeData; |
| + private Boolean mNewAccountManaged; |
| /** |
| * Run this state machine, displaying the appropriate dialogs. |
| @@ -122,8 +123,10 @@ public class ConfirmSyncDataStateMachine |
| mCurrentlyManaged = SigninManager.get(context).getManagementDomain() != null; |
| + mDelegate = new ConfirmSyncDataStateMachineDelegate(mContext); |
| + |
| // This check isn't needed right now, but can take a few seconds, so we kick it off early. |
| - SigninManager.isUserManaged(mNewAccountName, mNewAccountManaged.fulfillmentCallback()); |
| + requestNewAccountManagementStatus(); |
| } |
| /** |
| @@ -162,23 +165,13 @@ public class ConfirmSyncDataStateMachine |
| break; |
| case BEFORE_NEW_ACCOUNT_DIALOG: |
| mState = AFTER_NEW_ACCOUNT_DIALOG; |
| - |
| - mNewAccountManaged.then(new Callback<Boolean>() { |
| - @Override |
| - public void onResult(Boolean newAccountManaged) { |
| - if (newAccountManaged) { |
| - // Show 'logging into managed account' dialog |
| - // This will call back into onConfirm on success. |
| - ConfirmManagedSyncDataDialog.showSignInToManagedAccountDialog( |
| - ConfirmSyncDataStateMachine.this, |
| - mFragmentManager, mContext.getResources(), |
| - SigninManager.extractDomainName(mNewAccountName)); |
| - } else { |
| - progress(); |
| - } |
| - } |
| - }); |
| - |
| + if (mNewAccountManaged != null) { |
| + // No need to show dialog if account management status is already known |
| + handleNewAccountManagementStatus(); |
| + } else { |
| + showProgressDialog(); |
| + scheduleTimeout(); |
| + } |
| break; |
| case AFTER_NEW_ACCOUNT_DIALOG: |
| mState = DONE; |
| @@ -189,6 +182,80 @@ public class ConfirmSyncDataStateMachine |
| } |
| } |
| + private void requestNewAccountManagementStatus() { |
| + SigninManager.isUserManaged(mNewAccountName, new Callback<Boolean>() { |
| + @Override |
| + public void onResult(Boolean result) { |
| + setIsNewAccountManaged(result); |
| + } |
| + }); |
| + } |
| + |
| + private void setIsNewAccountManaged(Boolean isManaged) { |
| + assert isManaged != null; |
| + mNewAccountManaged = isManaged; |
| + if (mState == AFTER_NEW_ACCOUNT_DIALOG) { |
| + handleNewAccountManagementStatus(); |
| + } |
| + } |
| + |
| + private void handleNewAccountManagementStatus() { |
| + assert mNewAccountManaged != null; |
| + assert mState == AFTER_NEW_ACCOUNT_DIALOG; |
| + |
| + mDelegate.dismissProgressDialog(); |
| + mDelegate.dismissTimeoutDialog(); |
| + |
| + if (mNewAccountManaged) { |
| + // Show 'logging into managed account' dialog |
| + // This will call back into onConfirm on success. |
| + ConfirmManagedSyncDataDialog.showSignInToManagedAccountDialog( |
| + ConfirmSyncDataStateMachine.this, mFragmentManager, mContext.getResources(), |
| + SigninManager.extractDomainName(mNewAccountName)); |
| + } else { |
| + progress(); |
| + } |
| + } |
| + |
| + private void showProgressDialog() { |
| + mDelegate.showProgressDialog( |
| + new ConfirmSyncDataStateMachineDelegate.ProgressDialogListener() { |
| + @Override |
| + public void onCancel() { |
| + ConfirmSyncDataStateMachine.this.onCancel(); |
| + } |
| + }); |
| + } |
| + |
| + private void scheduleTimeout() { |
| + ThreadUtils.postOnUiThreadDelayed(new Runnable() { |
|
msarda
2017/03/28 11:09:02
I do not know how this works, but it would be bett
bsazonov
2017/03/28 12:59:58
I've found only https://developer.android.com/refe
Bernhard Bauer
2017/03/28 14:19:21
If you keep a reference to the Runnable around, yo
bsazonov
2017/03/29 13:05:05
Thanks for the tip! Done.
|
| + @Override |
| + public void run() { |
| + checkTimeout(); |
| + } |
| + }, ACCOUNT_CHECK_TIMEOUT_MS); |
| + } |
| + |
| + private void checkTimeout() { |
| + if (mState != AFTER_NEW_ACCOUNT_DIALOG || mNewAccountManaged != null) { |
| + return; |
| + } |
| + |
| + mDelegate.showTimeoutDialog( |
| + new ConfirmSyncDataStateMachineDelegate.TimeoutDialogListener() { |
| + @Override |
| + public void onCancel() { |
| + ConfirmSyncDataStateMachine.this.onCancel(); |
| + } |
| + |
| + @Override |
| + public void onRetry() { |
| + requestNewAccountManagementStatus(); |
| + scheduleTimeout(); |
| + } |
| + }); |
| + } |
| + |
| // ConfirmImportSyncDataDialog.Listener implementation. |
| @Override |
| public void onConfirm(boolean wipeData) { |
| @@ -207,6 +274,8 @@ public class ConfirmSyncDataStateMachine |
| public void onCancel() { |
| mState = DONE; |
| mCallback.onCancel(); |
| + mDelegate.dismissProgressDialog(); |
| + mDelegate.dismissTimeoutDialog(); |
| } |
| } |