Merge "Keep SetupActivity alive to check re-launching from launcher"
diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
index acb0766..97cf7fc 100644
--- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
+++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
@@ -63,7 +63,8 @@
     private static final int STEP_1 = 1;
     private static final int STEP_2 = 2;
     private static final int STEP_3 = 3;
-    private boolean mWasLanguageAndInputSettingsInvoked;
+    private static final int STEP_LAUNCHING_IME_SETTINGS = 4;
+    private static final int STEP_BACK_FROM_IME_SETTINGS = 5;
 
     private final SettingsPoolingHandler mHandler = new SettingsPoolingHandler(this);
 
@@ -83,7 +84,7 @@
             }
             switch (msg.what) {
             case MSG_POLLING_IME_SETTINGS:
-                if (SetupActivity.isThisImeEnabled(setupActivity)) {
+                if (isThisImeEnabled(setupActivity)) {
                     setupActivity.invokeSetupWizardOfThisIme();
                     return;
                 }
@@ -112,17 +113,7 @@
         RichInputMethodManager.init(this);
 
         if (savedInstanceState == null) {
-            mStepNumber = determineSetupStepNumber();
-            if (mStepNumber == STEP_1 && !mWasLanguageAndInputSettingsInvoked) {
-                mStepNumber = STEP_WELCOME;
-            }
-            if (mStepNumber == STEP_3) {
-                // This IME already has been enabled and set as current IME.
-                // TODO: Implement tutorial.
-                invokeSettingsOfThisIme();
-                finish();
-                return;
-            }
+            mStepNumber = determineSetupStepNumberFromLauncher();
         } else {
             mStepNumber = savedInstanceState.getInt(STATE_STEP);
         }
@@ -265,7 +256,6 @@
         intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS);
         intent.addCategory(Intent.CATEGORY_DEFAULT);
         startActivity(intent);
-        mWasLanguageAndInputSettingsInvoked = true;
     }
 
     private void invokeSubtypeEnablerOfThisIme() {
@@ -313,6 +303,17 @@
         return myImi.getId().equals(currentImeId);
     }
 
+    private int determineSetupStepNumberFromLauncher() {
+        final int stepNumber = determineSetupStepNumber();
+        if (stepNumber == STEP_1) {
+            return STEP_WELCOME;
+        }
+        if (stepNumber == STEP_3) {
+            return STEP_LAUNCHING_IME_SETTINGS;
+        }
+        return stepNumber;
+    }
+
     private int determineSetupStepNumber() {
         mHandler.cancelPollingImeSettings();
         if (!isThisImeEnabled(this)) {
@@ -336,10 +337,14 @@
         mStepNumber = savedInstanceState.getInt(STATE_STEP);
     }
 
+    private static boolean isInSetupSteps(final int stepNumber) {
+        return stepNumber >= STEP_1 && stepNumber <= STEP_3;
+    }
+
     @Override
     protected void onRestart() {
         super.onRestart();
-        if (mStepNumber != STEP_WELCOME) {
+        if (isInSetupSteps(mStepNumber)) {
             mStepNumber = determineSetupStepNumber();
         }
     }
@@ -347,6 +352,15 @@
     @Override
     protected void onResume() {
         super.onResume();
+        if (mStepNumber == STEP_LAUNCHING_IME_SETTINGS) {
+            invokeSettingsOfThisIme();
+            mStepNumber = STEP_BACK_FROM_IME_SETTINGS;
+            return;
+        }
+        if (mStepNumber == STEP_BACK_FROM_IME_SETTINGS) {
+            finish();
+            return;
+        }
         updateSetupStepView();
     }
 
@@ -374,7 +388,7 @@
     @Override
     public void onWindowFocusChanged(final boolean hasFocus) {
         super.onWindowFocusChanged(hasFocus);
-        if (hasFocus && mStepNumber != STEP_WELCOME) {
+        if (hasFocus && isInSetupSteps(mStepNumber)) {
             mStepNumber = determineSetupStepNumber();
             updateSetupStepView();
         }
@@ -383,7 +397,7 @@
     private void updateSetupStepView() {
         final boolean welcomeScreen = (mStepNumber == STEP_WELCOME);
         mWelcomeScreen.setVisibility(welcomeScreen ? View.VISIBLE : View.GONE);
-        mSetupScreen.setVisibility(welcomeScreen ? View.GONE: View.VISIBLE);
+        mSetupScreen.setVisibility(welcomeScreen ? View.GONE : View.VISIBLE);
         if (welcomeScreen) {
             mWelcomeVideoView.setVisibility(View.VISIBLE);
             mWelcomeVideoView.setVideoURI(mWelcomeVideoUri);
@@ -410,7 +424,7 @@
 
         public SetupStep(final int stepNo, final String applicationName, final TextView bulletView,
                 final View stepView, final int title, final int instruction,
-                final int finishedInstruction,final int actionIcon, final int actionLabel) {
+                final int finishedInstruction, final int actionIcon, final int actionLabel) {
             mStepNo = stepNo;
             mStepView = stepView;
             mBulletView = bulletView;