Merge "[PB2] Small bugfix"
diff --git a/java/res/layout/setup_steps_cards.xml b/java/res/layout/setup_steps_cards.xml
index 6c46461..2451731 100644
--- a/java/res/layout/setup_steps_cards.xml
+++ b/java/res/layout/setup_steps_cards.xml
@@ -56,6 +56,12 @@
             layout="@layout/setup_step" />
     </FrameLayout>
     <TextView
+        android:id="@+id/setup_next"
+        android:text="@string/setup_next_action"
+        android:gravity="end|center_vertical"
+        style="@style/setupStepActionLabelStyleCommon"
+        android:layout_marginTop="@dimen/setup_step_horizontal_line_height" />
+    <TextView
         android:id="@+id/setup_finish"
         android:text="@string/setup_finish_action"
         style="@style/setupStepActionLabelStyle"
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index d8a88a8..4a7c70b 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -461,6 +461,8 @@
     <string name="setup_step1_title">"Enable <xliff:g id="application_name">%s</xliff:g>"</string>
     <!-- Detailed instruction of the 1st step in the setup wizard. [CHAR LIMIT=120] -->
     <string name="setup_step1_instruction">"Please check \"<xliff:g id="application_name">%s</xliff:g>\" in your Language &amp; input settings. This will authorize it to run on your device."</string>
+    <!-- Detailed instruction of the already finished 1st step in the setup wizard. [CHAR LIMIT=120] -->
+    <string name="setup_step1_finished_instruction">"<xliff:g id="application_name">%s</xliff:g> is already enabled in your Language &amp; input settings, so this step is done. On to the next one!"</string>
     <!-- The label of the button that triggers the Language & input settings in order to enable the keyboard. [CHAR_LIMIT=64] -->
     <string name="setup_step1_action">Enable in Settings</string>
     <!-- Ordinal number of the 2nd step in the setup wizard. [CHAR LIMIT=5] -->
diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
index 66c0b5f..578787d 100644
--- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
+++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java
@@ -49,6 +49,8 @@
     private Uri mWelcomeVideoUri;
     private VideoView mWelcomeVideoView;
     private View mActionStart;
+    private View mActionNext;
+    private TextView mStep1Bullet;
     private TextView mActionFinish;
     private SetupStepGroup mSetupStepGroup;
     private static final String STATE_STEP = "step";
@@ -132,10 +134,13 @@
                 (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator);
         mSetupStepGroup = new SetupStepGroup(indicatorView);
 
+        mStep1Bullet = (TextView)findViewById(R.id.setup_step1_bullet);
+        mStep1Bullet.setOnClickListener(this);
         final SetupStep step1 = new SetupStep(STEP_1, applicationName,
-                (TextView)findViewById(R.id.setup_step1_bullet), findViewById(R.id.setup_step1),
+                mStep1Bullet, findViewById(R.id.setup_step1),
                 R.string.setup_step1_title, R.string.setup_step1_instruction,
-                R.drawable.ic_setup_step1, R.string.setup_step1_action);
+                R.string.setup_step1_finished_instruction, R.drawable.ic_setup_step1,
+                R.string.setup_step1_action);
         step1.setAction(new Runnable() {
             @Override
             public void run() {
@@ -148,7 +153,8 @@
         final SetupStep step2 = new SetupStep(STEP_2, applicationName,
                 (TextView)findViewById(R.id.setup_step2_bullet), findViewById(R.id.setup_step2),
                 R.string.setup_step2_title, R.string.setup_step2_instruction,
-                R.drawable.ic_setup_step2, R.string.setup_step2_action);
+                0 /* finishedInstruction */, R.drawable.ic_setup_step2,
+                R.string.setup_step2_action);
         step2.setAction(new Runnable() {
             @Override
             public void run() {
@@ -162,7 +168,8 @@
         final SetupStep step3 = new SetupStep(STEP_3, applicationName,
                 (TextView)findViewById(R.id.setup_step3_bullet), findViewById(R.id.setup_step3),
                 R.string.setup_step3_title, R.string.setup_step3_instruction,
-                R.drawable.ic_setup_step3, R.string.setup_step3_action);
+                0 /* finishedInstruction */, R.drawable.ic_setup_step3,
+                R.string.setup_step3_action);
         step3.setAction(new Runnable() {
             @Override
             public void run() {
@@ -186,6 +193,8 @@
 
         mActionStart = findViewById(R.id.setup_start_label);
         mActionStart.setOnClickListener(this);
+        mActionNext = findViewById(R.id.setup_next);
+        mActionNext.setOnClickListener(this);
         mActionFinish = (TextView)findViewById(R.id.setup_finish);
         TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds(mActionFinish,
                 getResources().getDrawable(R.drawable.ic_setup_finish), null, null, null);
@@ -194,15 +203,25 @@
 
     @Override
     public void onClick(final View v) {
-        if (v == mActionStart) {
-            mStepNumber = STEP_1;
-            updateSetupStepView();
-            return;
-        }
         if (v == mActionFinish) {
             finish();
             return;
         }
+        final int stepState = determineSetupState();
+        final int nextStep;
+        if (v == mActionStart) {
+            nextStep = STEP_1;
+        } else if (v == mActionNext) {
+            nextStep = mStepNumber + 1;
+        } else if (v == mStep1Bullet && stepState == STEP_2) {
+            nextStep = STEP_1;
+        } else {
+            nextStep = mStepNumber;
+        }
+        if (mStepNumber != nextStep) {
+            mStepNumber = nextStep;
+            updateSetupStepView();
+        }
     }
 
     private void invokeSetupWizardOfThisIme() {
@@ -360,7 +379,9 @@
             return;
         }
         mWelcomeVideoView.stopPlayback();
-        mSetupStepGroup.enableStep(mStepNumber);
+        final boolean isStepActionAlreadyDone = mStepNumber < determineSetupState();
+        mSetupStepGroup.enableStep(mStepNumber, isStepActionAlreadyDone);
+        mActionNext.setVisibility(isStepActionAlreadyDone ? View.VISIBLE : View.GONE);
         mActionFinish.setVisibility((mStepNumber == STEP_3) ? View.VISIBLE : View.GONE);
     }
 
@@ -370,12 +391,14 @@
         private final TextView mBulletView;
         private final int mActivatedColor;
         private final int mDeactivatedColor;
+        private final String mInstruction;
+        private final String mFinishedInstruction;
         private final TextView mActionLabel;
         private Runnable mAction;
 
         public SetupStep(final int stepNo, final String applicationName, final TextView bulletView,
-                final View stepView, final int title, final int instruction, final int actionIcon,
-                final int actionLabel) {
+                final View stepView, final int title, final int instruction,
+                final int finishedInstruction,final int actionIcon, final int actionLabel) {
             mStepNo = stepNo;
             mStepView = stepView;
             mBulletView = bulletView;
@@ -385,14 +408,10 @@
 
             final TextView titleView = (TextView)mStepView.findViewById(R.id.setup_step_title);
             titleView.setText(res.getString(title, applicationName));
-
-            final TextView instructionView = (TextView)mStepView.findViewById(
-                    R.id.setup_step_instruction);
-            if (instruction == 0) {
-                instructionView.setVisibility(View.GONE);
-            } else {
-                instructionView.setText(res.getString(instruction, applicationName));
-            }
+            mInstruction = (instruction == 0) ? null
+                    : res.getString(instruction, applicationName);
+            mFinishedInstruction = (finishedInstruction == 0) ? null
+                    : res.getString(finishedInstruction, applicationName);
 
             mActionLabel = (TextView)mStepView.findViewById(R.id.setup_step_action_label);
             mActionLabel.setText(res.getString(actionLabel));
@@ -405,9 +424,13 @@
             }
         }
 
-        public void setEnabled(final boolean enabled) {
+        public void setEnabled(final boolean enabled, final boolean isStepActionAlreadyDone) {
             mStepView.setVisibility(enabled ? View.VISIBLE : View.GONE);
             mBulletView.setTextColor(enabled ? mActivatedColor : mDeactivatedColor);
+            final TextView instructionView = (TextView)mStepView.findViewById(
+                    R.id.setup_step_instruction);
+            instructionView.setText(isStepActionAlreadyDone ? mFinishedInstruction : mInstruction);
+            mActionLabel.setVisibility(isStepActionAlreadyDone ? View.GONE : View.VISIBLE);
         }
 
         public void setAction(final Runnable action) {
@@ -436,9 +459,9 @@
             mGroup.add(step);
         }
 
-        public void enableStep(final int enableStepNo) {
+        public void enableStep(final int enableStepNo, final boolean isStepActionAlreadyDone) {
             for (final SetupStep step : mGroup) {
-                step.setEnabled(step.mStepNo == enableStepNo);
+                step.setEnabled(step.mStepNo == enableStepNo, isStepActionAlreadyDone);
             }
             mIndicatorView.setIndicatorPosition(enableStepNo - STEP_1, mGroup.size());
         }
diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h
index 6ef9f41..e6719c9 100644
--- a/native/jni/src/defines.h
+++ b/native/jni/src/defines.h
@@ -417,16 +417,25 @@
 } DoubleLetterLevel;
 
 typedef enum {
+    // Correction for MATCH_CHAR
     CT_MATCH,
+    // Correction for PROXIMITY_CHAR
     CT_PROXIMITY,
+    // Correction for ADDITIONAL_PROXIMITY_CHAR
     CT_ADDITIONAL_PROXIMITY,
+    // Correction for SUBSTITUTION_CHAR
     CT_SUBSTITUTION,
+    // Skip one omitted letter
     CT_OMISSION,
+    // Delete an unnecessarily inserted letter
     CT_INSERTION,
+    // Swap the order of next two touch points
     CT_TRANSPOSITION,
     CT_COMPLETION,
     CT_TERMINAL,
+    // Create new word with space omission
     CT_NEW_WORD_SPACE_OMITTION,
+    // Create new word with space substitution
     CT_NEW_WORD_SPACE_SUBSTITUTION,
 } CorrectionType;
 #endif // LATINIME_DEFINES_H
diff --git a/native/jni/src/suggest/core/policy/weighting.cpp b/native/jni/src/suggest/core/policy/weighting.cpp
index a6d30e4..6c08e76 100644
--- a/native/jni/src/suggest/core/policy/weighting.cpp
+++ b/native/jni/src/suggest/core/policy/weighting.cpp
@@ -163,11 +163,9 @@
         case CT_OMISSION:
             return true;
         case CT_ADDITIONAL_PROXIMITY:
-            // Should return true?
-            return false;
+            return true;
         case CT_SUBSTITUTION:
-            // Should return true?
-            return false;
+            return true;
         case CT_NEW_WORD_SPACE_OMITTION:
             return false;
         case CT_MATCH:
@@ -194,7 +192,7 @@
         case CT_OMISSION:
             return false;
         case CT_ADDITIONAL_PROXIMITY:
-            return false;
+            return true;
         case CT_SUBSTITUTION:
             return false;
         case CT_NEW_WORD_SPACE_OMITTION:
diff --git a/native/jni/src/suggest/policyimpl/typing/scoring_params.cpp b/native/jni/src/suggest/policyimpl/typing/scoring_params.cpp
index 9933586..299ca83 100644
--- a/native/jni/src/suggest/policyimpl/typing/scoring_params.cpp
+++ b/native/jni/src/suggest/policyimpl/typing/scoring_params.cpp
@@ -28,24 +28,24 @@
 const float ScoringParams::DISTANCE_WEIGHT_LENGTH = 0.132f;
 const float ScoringParams::PROXIMITY_COST = 0.086f;
 const float ScoringParams::FIRST_PROXIMITY_COST = 0.104f;
-const float ScoringParams::OMISSION_COST = 0.388f;
-const float ScoringParams::OMISSION_COST_SAME_CHAR = 0.431f;
-const float ScoringParams::OMISSION_COST_FIRST_CHAR = 0.532f;
-const float ScoringParams::INSERTION_COST = 0.670f;
-const float ScoringParams::INSERTION_COST_SAME_CHAR = 0.526f;
-const float ScoringParams::INSERTION_COST_FIRST_CHAR = 0.563f;
-const float ScoringParams::TRANSPOSITION_COST = 0.494f;
-const float ScoringParams::SPACE_SUBSTITUTION_COST = 0.289f;
+const float ScoringParams::OMISSION_COST = 0.458f;
+const float ScoringParams::OMISSION_COST_SAME_CHAR = 0.491f;
+const float ScoringParams::OMISSION_COST_FIRST_CHAR = 0.582f;
+const float ScoringParams::INSERTION_COST = 0.730f;
+const float ScoringParams::INSERTION_COST_SAME_CHAR = 0.586f;
+const float ScoringParams::INSERTION_COST_FIRST_CHAR = 0.623f;
+const float ScoringParams::TRANSPOSITION_COST = 0.516f;
+const float ScoringParams::SPACE_SUBSTITUTION_COST = 0.319f;
 const float ScoringParams::ADDITIONAL_PROXIMITY_COST = 0.380f;
-const float ScoringParams::SUBSTITUTION_COST = 0.363f;
-const float ScoringParams::COST_NEW_WORD = 0.024f;
+const float ScoringParams::SUBSTITUTION_COST = 0.403f;
+const float ScoringParams::COST_NEW_WORD = 0.042f;
 const float ScoringParams::COST_NEW_WORD_CAPITALIZED = 0.174f;
 const float ScoringParams::DISTANCE_WEIGHT_LANGUAGE = 1.123f;
 const float ScoringParams::COST_FIRST_LOOKAHEAD = 0.545f;
 const float ScoringParams::COST_LOOKAHEAD = 0.073f;
-const float ScoringParams::HAS_PROXIMITY_TERMINAL_COST = 0.126f;
-const float ScoringParams::HAS_EDIT_CORRECTION_TERMINAL_COST = 0.056f;
-const float ScoringParams::HAS_MULTI_WORD_TERMINAL_COST = 0.536f;
+const float ScoringParams::HAS_PROXIMITY_TERMINAL_COST = 0.105f;
+const float ScoringParams::HAS_EDIT_CORRECTION_TERMINAL_COST = 0.038f;
+const float ScoringParams::HAS_MULTI_WORD_TERMINAL_COST = 0.444f;
 const float ScoringParams::TYPING_BASE_OUTPUT_SCORE = 1.0f;
 const float ScoringParams::TYPING_MAX_OUTPUT_SCORE_PER_INPUT = 0.1f;
 const float ScoringParams::MAX_NORM_DISTANCE_FOR_EDIT = 0.1f;