From e918bf07a0562183d84ed04d8224a4ea732056ce Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Wed, 24 Apr 2013 11:38:44 +0900 Subject: [PATCH] Refactor SetupStepGroup Bug: 8159728 Change-Id: I1ab846d0910aacbd2f979289d72bafea8a8e0378 --- .../latin/setup/SetupActivity.java | 67 ++++++++++--------- .../latin/setup/SetupStepIndicatorView.java | 10 ++- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java index a7a41719e..66c0b5f56 100644 --- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java +++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java @@ -40,18 +40,17 @@ import com.android.inputmethod.latin.RichInputMethodManager; import com.android.inputmethod.latin.SettingsActivity; import com.android.inputmethod.latin.StaticInnerHandlerWrapper; -import java.util.HashMap; +import java.util.ArrayList; // TODO: Use Fragment to implement welcome screen and setup steps. public final class SetupActivity extends Activity implements View.OnClickListener { private View mWelcomeScreen; private View mSetupScreen; - private SetupStepIndicatorView mStepIndicatorView; private Uri mWelcomeVideoUri; private VideoView mWelcomeVideoView; private View mActionStart; private TextView mActionFinish; - private final SetupStepGroup mSetupStepGroup = new SetupStepGroup(); + private SetupStepGroup mSetupStepGroup; private static final String STATE_STEP = "step"; private int mStepNumber; private static final int STEP_0 = 0; @@ -129,9 +128,11 @@ public final class SetupActivity extends Activity implements View.OnClickListene final TextView stepsTitle = (TextView)findViewById(R.id.setup_title); stepsTitle.setText(getString(R.string.setup_steps_title, applicationName)); - mStepIndicatorView = (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator); + final SetupStepIndicatorView indicatorView = + (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator); + mSetupStepGroup = new SetupStepGroup(indicatorView); - final SetupStep step1 = new SetupStep(applicationName, + final SetupStep step1 = new SetupStep(STEP_1, applicationName, (TextView)findViewById(R.id.setup_step1_bullet), 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); @@ -142,9 +143,9 @@ public final class SetupActivity extends Activity implements View.OnClickListene mHandler.startPollingImeSettings(); } }); - mSetupStepGroup.addStep(STEP_1, step1); + mSetupStepGroup.addStep(step1); - final SetupStep step2 = new SetupStep(applicationName, + 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); @@ -156,9 +157,9 @@ public final class SetupActivity extends Activity implements View.OnClickListene .showInputMethodPicker(); } }); - mSetupStepGroup.addStep(STEP_2, step2); + mSetupStepGroup.addStep(step2); - final SetupStep step3 = new SetupStep(applicationName, + 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); @@ -168,7 +169,7 @@ public final class SetupActivity extends Activity implements View.OnClickListene invokeSubtypeEnablerOfThisIme(); } }); - mSetupStepGroup.addStep(STEP_3, step3); + mSetupStepGroup.addStep(step3); mWelcomeVideoUri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) @@ -274,10 +275,10 @@ public final class SetupActivity extends Activity implements View.OnClickListene return myImi.getId().equals(currentImeId); } - private int determineSetupStepNumber() { + private int determineSetupState() { mHandler.cancelPollingImeSettings(); if (!isThisImeEnabled(this)) { - return mWasLanguageAndInputSettingsInvoked ? STEP_1 : STEP_0; + return STEP_1; } if (!isThisImeCurrent(this)) { return STEP_2; @@ -285,6 +286,14 @@ public final class SetupActivity extends Activity implements View.OnClickListene return STEP_3; } + private int determineSetupStepNumber() { + final int stepState = determineSetupState(); + if (stepState == STEP_1) { + return mWasLanguageAndInputSettingsInvoked ? STEP_1 : STEP_0; + } + return stepState; + } + @Override protected void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); @@ -351,20 +360,12 @@ public final class SetupActivity extends Activity implements View.OnClickListene return; } mWelcomeVideoView.stopPlayback(); - final int layoutDirection = ViewCompatUtils.getLayoutDirection(mStepIndicatorView); - mStepIndicatorView.setIndicatorPosition( - getIndicatorPosition(mStepNumber, mSetupStepGroup.getTotalStep(), layoutDirection)); mSetupStepGroup.enableStep(mStepNumber); mActionFinish.setVisibility((mStepNumber == STEP_3) ? View.VISIBLE : View.GONE); } - private static float getIndicatorPosition(final int step, final int totalStep, - final int layoutDirection) { - final float pos = ((step - STEP_1) * 2 + 1) / (float)(totalStep * 2); - return (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos; - } - static final class SetupStep implements View.OnClickListener { + public final int mStepNo; private final View mStepView; private final TextView mBulletView; private final int mActivatedColor; @@ -372,9 +373,10 @@ public final class SetupActivity extends Activity implements View.OnClickListene private final TextView mActionLabel; private Runnable mAction; - public SetupStep(final String applicationName, final TextView bulletView, + 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) { + mStepNo = stepNo; mStepView = stepView; mBulletView = bulletView; final Resources res = stepView.getResources(); @@ -423,21 +425,22 @@ public final class SetupActivity extends Activity implements View.OnClickListene } static final class SetupStepGroup { - private final HashMap mGroup = CollectionUtils.newHashMap(); + private final SetupStepIndicatorView mIndicatorView; + private final ArrayList mGroup = CollectionUtils.newArrayList(); - public void addStep(final int stepNo, final SetupStep step) { - mGroup.put(stepNo, step); + public SetupStepGroup(final SetupStepIndicatorView indicatorView) { + mIndicatorView = indicatorView; + } + + public void addStep(final SetupStep step) { + mGroup.add(step); } public void enableStep(final int enableStepNo) { - for (final Integer stepNo : mGroup.keySet()) { - final SetupStep step = mGroup.get(stepNo); - step.setEnabled(stepNo == enableStepNo); + for (final SetupStep step : mGroup) { + step.setEnabled(step.mStepNo == enableStepNo); } - } - - public int getTotalStep() { - return mGroup.size(); + mIndicatorView.setIndicatorPosition(enableStepNo - STEP_1, mGroup.size()); } } } diff --git a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java b/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java index 077a21793..c909507c6 100644 --- a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java +++ b/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java @@ -23,6 +23,7 @@ import android.graphics.Path; import android.util.AttributeSet; import android.view.View; +import com.android.inputmethod.compat.ViewCompatUtils; import com.android.inputmethod.latin.R; public final class SetupStepIndicatorView extends View { @@ -36,8 +37,13 @@ public final class SetupStepIndicatorView extends View { mIndicatorPaint.setStyle(Paint.Style.FILL); } - public void setIndicatorPosition(final float xRatio) { - mXRatio = xRatio; + public void setIndicatorPosition(final int stepPos, final int totalStepNum) { + final int layoutDirection = ViewCompatUtils.getLayoutDirection(this); + // The indicator position is the center of the partition that is equally divided into + // the total step number. + final float partionWidth = 1.0f / totalStepNum; + final float pos = stepPos * partionWidth + partionWidth / 2.0f; + mXRatio = (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos; invalidate(); }