Refactor Tutorial class

Change-Id: Ib8dbcf8f36966988fb0d4efdc6bdb7e30b776a68
main
Tadashi G. Takaoka 2010-11-13 00:46:45 -08:00
parent 1679432d1c
commit 71c353aa87
2 changed files with 75 additions and 71 deletions

View File

@ -82,7 +82,8 @@ import java.util.Map;
public class LatinIME extends InputMethodService public class LatinIME extends InputMethodService
implements BaseKeyboardView.OnKeyboardActionListener, implements BaseKeyboardView.OnKeyboardActionListener,
VoiceInput.UiListener, VoiceInput.UiListener,
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener,
Tutorial.TutorialListener {
private static final String TAG = "LatinIME"; private static final String TAG = "LatinIME";
private static final boolean PERF_DEBUG = false; private static final boolean PERF_DEBUG = false;
static final boolean DEBUG = false; static final boolean DEBUG = false;
@ -162,7 +163,7 @@ public class LatinIME extends InputMethodService
private AlertDialog mOptionsDialog; private AlertDialog mOptionsDialog;
private AlertDialog mVoiceWarningDialog; private AlertDialog mVoiceWarningDialog;
/* package */ KeyboardSwitcher mKeyboardSwitcher; private KeyboardSwitcher mKeyboardSwitcher;
private UserDictionary mUserDictionary; private UserDictionary mUserDictionary;
private UserBigramDictionary mUserBigramDictionary; private UserBigramDictionary mUserBigramDictionary;
@ -177,7 +178,7 @@ public class LatinIME extends InputMethodService
private String mSystemLocale; private String mSystemLocale;
private LanguageSwitcher mLanguageSwitcher; private LanguageSwitcher mLanguageSwitcher;
private StringBuilder mComposing = new StringBuilder(); private final StringBuilder mComposing = new StringBuilder();
private WordComposer mWord = new WordComposer(); private WordComposer mWord = new WordComposer();
private int mCommittedLength; private int mCommittedLength;
private boolean mPredicting; private boolean mPredicting;
@ -226,21 +227,21 @@ public class LatinIME extends InputMethodService
private long mLastKeyTime; private long mLastKeyTime;
// Modifier keys state // Modifier keys state
private ModifierKeyState mShiftKeyState = new ModifierKeyState(); private final ModifierKeyState mShiftKeyState = new ModifierKeyState();
private ModifierKeyState mSymbolKeyState = new ModifierKeyState(); private final ModifierKeyState mSymbolKeyState = new ModifierKeyState();
private Tutorial mTutorial; private Tutorial mTutorial;
private AudioManager mAudioManager; private AudioManager mAudioManager;
// Align sound effect volume on music volume // Align sound effect volume on music volume
private final float FX_VOLUME = -1.0f; private static final float FX_VOLUME = -1.0f;
private boolean mSilentMode; private boolean mSilentMode;
/* package */ String mWordSeparators; /* package */ String mWordSeparators;
private String mSentenceSeparators; private String mSentenceSeparators;
private String mSuggestPuncs; private String mSuggestPuncs;
private VoiceInput mVoiceInput; private VoiceInput mVoiceInput;
private VoiceResults mVoiceResults = new VoiceResults(); private final VoiceResults mVoiceResults = new VoiceResults();
private boolean mConfigurationChanging; private boolean mConfigurationChanging;
// Keeps track of most recently inserted text (multi-character key) for reverting // Keeps track of most recently inserted text (multi-character key) for reverting
@ -248,10 +249,10 @@ public class LatinIME extends InputMethodService
private boolean mRefreshKeyboardRequired; private boolean mRefreshKeyboardRequired;
// For each word, a list of potential replacements, usually from voice. // For each word, a list of potential replacements, usually from voice.
private Map<String, List<CharSequence>> mWordToSuggestions = private final Map<String, List<CharSequence>> mWordToSuggestions =
new HashMap<String, List<CharSequence>>(); new HashMap<String, List<CharSequence>>();
private ArrayList<WordAlternatives> mWordHistory = new ArrayList<WordAlternatives>(); private final ArrayList<WordAlternatives> mWordHistory = new ArrayList<WordAlternatives>();
private class VoiceResults { private class VoiceResults {
List<String> candidates; List<String> candidates;
@ -319,8 +320,7 @@ public class LatinIME extends InputMethodService
case MSG_START_TUTORIAL: case MSG_START_TUTORIAL:
if (mTutorial == null) { if (mTutorial == null) {
if (mKeyboardSwitcher.isInputViewShown()) { if (mKeyboardSwitcher.isInputViewShown()) {
mTutorial = new Tutorial( mTutorial = new Tutorial(LatinIME.this, mKeyboardSwitcher);
LatinIME.this, mKeyboardSwitcher.getInputView());
mTutorial.start(); mTutorial.start();
} else { } else {
// Try again soon if the view is not yet showing // Try again soon if the view is not yet showing
@ -395,7 +395,7 @@ public class LatinIME extends InputMethodService
* Loads a dictionary or multiple separated dictionary * Loads a dictionary or multiple separated dictionary
* @return returns array of dictionary resource ids * @return returns array of dictionary resource ids
*/ */
/* package */ static int[] getDictionary(Resources res) { public static int[] getDictionary(Resources res) {
String packageName = LatinIME.class.getPackage().getName(); String packageName = LatinIME.class.getPackage().getName();
XmlResourceParser xrp = res.getXml(R.xml.dictionary); XmlResourceParser xrp = res.getXml(R.xml.dictionary);
ArrayList<Integer> dictionaries = new ArrayList<Integer>(); ArrayList<Integer> dictionaries = new ArrayList<Integer>();
@ -2430,20 +2430,22 @@ public class LatinIME extends InputMethodService
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_TUTORIAL), 500); mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_TUTORIAL), 500);
} }
/* package */ void tutorialDone() { // Tutorial.TutorialListener
public void onTutorialDone() {
sendDownUpKeyEvents(-1); // Inform the setupwizard that tutorial is in last bubble
mTutorial = null; mTutorial = null;
} }
/* package */ void promoteToUserDictionary(String word, int frequency) { public void promoteToUserDictionary(String word, int frequency) {
if (mUserDictionary.isValidWord(word)) return; if (mUserDictionary.isValidWord(word)) return;
mUserDictionary.addWord(word, frequency); mUserDictionary.addWord(word, frequency);
} }
/* package */ WordComposer getCurrentWord() { public WordComposer getCurrentWord() {
return mWord; return mWord;
} }
/* package */ boolean getPopupOn() { public boolean getPopupOn() {
return mPopupOn; return mPopupOn;
} }

View File

@ -32,20 +32,24 @@ import android.widget.PopupWindow;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class Tutorial implements OnTouchListener { public class Tutorial implements OnTouchListener {
private List<Bubble> mBubbles = new ArrayList<Bubble>(); public interface TutorialListener {
private View mInputView; public void onTutorialDone();
private LatinIME mIme; }
private int[] mLocation = new int[2];
private final ArrayList<Bubble> mBubbles = new ArrayList<Bubble>();
private final KeyboardSwitcher mKeyboardSwitcher;
private final View mInputView;
private final TutorialListener mListener;
private final int[] mLocation = new int[2];
private static final int MSG_SHOW_BUBBLE = 0; private static final int MSG_SHOW_BUBBLE = 0;
private int mBubbleIndex; private int mBubbleIndex;
Handler mHandler = new Handler() { private final Handler mHandler = new Handler() {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
@ -57,20 +61,18 @@ public class Tutorial implements OnTouchListener {
} }
}; };
class Bubble { private class Bubble {
Drawable bubbleBackground; private final Drawable bubbleBackground;
int x; private final int x;
int y; private final int y;
int width; private final int width;
int gravity; private final int gravity;
CharSequence text; private final CharSequence text;
boolean dismissOnTouch; private final PopupWindow window;
boolean dismissOnClose; private final TextView textView;
PopupWindow window; private final View inputView;
TextView textView;
View inputView; private Bubble(Context context, View inputView,
Bubble(Context context, View inputView,
int backgroundResource, int bx, int by, int textResource1, int textResource2) { int backgroundResource, int bx, int by, int textResource1, int textResource2) {
bubbleBackground = context.getResources().getDrawable(backgroundResource); bubbleBackground = context.getResources().getDrawable(backgroundResource);
x = bx; x = bx;
@ -81,8 +83,6 @@ public class Tutorial implements OnTouchListener {
.append(context.getResources().getText(textResource1)) .append(context.getResources().getText(textResource1))
.append("\n") .append("\n")
.append(context.getResources().getText(textResource2)); .append(context.getResources().getText(textResource2));
this.dismissOnTouch = true;
this.dismissOnClose = false;
this.inputView = inputView; this.inputView = inputView;
window = new PopupWindow(context); window = new PopupWindow(context);
window.setBackgroundDrawable(null); window.setBackgroundDrawable(null);
@ -124,7 +124,7 @@ public class Tutorial implements OnTouchListener {
return l.getHeight(); return l.getHeight();
} }
void show(int offx, int offy) { private void show(int offx, int offy) {
int textHeight = chooseSize(window, inputView, text, textView); int textHeight = chooseSize(window, inputView, text, textView);
offy -= textView.getPaddingTop() + textHeight; offy -= textView.getPaddingTop() + textHeight;
if (inputView.getVisibility() == View.VISIBLE if (inputView.getVisibility() == View.VISIBLE
@ -144,63 +144,66 @@ public class Tutorial implements OnTouchListener {
} }
} }
} }
void hide() { private void hide() {
if (window.isShowing()) { if (window.isShowing()) {
textView.setOnTouchListener(null); textView.setOnTouchListener(null);
window.dismiss(); window.dismiss();
} }
} }
boolean isShowing() { private boolean isShowing() {
return window.isShowing(); return window.isShowing();
} }
} }
public Tutorial(LatinIME ime, LatinKeyboardView inputView) { public Tutorial(TutorialListener listener, KeyboardSwitcher keyboardSwitcher) {
mListener = listener;
mKeyboardSwitcher = keyboardSwitcher;
LatinKeyboardView inputView = keyboardSwitcher.getInputView();
mInputView = inputView;
Context context = inputView.getContext(); Context context = inputView.getContext();
mIme = ime;
int inputWidth = inputView.getWidth(); int inputWidth = inputView.getWidth();
final int x = inputWidth / 20; // Half of 1/10th final int x = inputWidth / 20; // Half of 1/10th
ArrayList<Bubble> bubbles = mBubbles;
Bubble bWelcome = new Bubble(context, inputView, Bubble bWelcome = new Bubble(context, inputView,
R.drawable.dialog_bubble_step02, x, 0, R.drawable.dialog_bubble_step02, x, 0,
R.string.tip_to_open_keyboard, R.string.touch_to_continue); R.string.tip_to_open_keyboard, R.string.touch_to_continue);
mBubbles.add(bWelcome); bubbles.add(bWelcome);
Bubble bAccents = new Bubble(context, inputView, Bubble bAccents = new Bubble(context, inputView,
R.drawable.dialog_bubble_step02, x, 0, R.drawable.dialog_bubble_step02, x, 0,
R.string.tip_to_view_accents, R.string.touch_to_continue); R.string.tip_to_view_accents, R.string.touch_to_continue);
mBubbles.add(bAccents); bubbles.add(bAccents);
Bubble b123 = new Bubble(context, inputView, Bubble b123 = new Bubble(context, inputView,
R.drawable.dialog_bubble_step07, x, 0, R.drawable.dialog_bubble_step07, x, 0,
R.string.tip_to_open_symbols, R.string.touch_to_continue); R.string.tip_to_open_symbols, R.string.touch_to_continue);
mBubbles.add(b123); bubbles.add(b123);
Bubble bABC = new Bubble(context, inputView, Bubble bABC = new Bubble(context, inputView,
R.drawable.dialog_bubble_step07, x, 0, R.drawable.dialog_bubble_step07, x, 0,
R.string.tip_to_close_symbols, R.string.touch_to_continue); R.string.tip_to_close_symbols, R.string.touch_to_continue);
mBubbles.add(bABC); bubbles.add(bABC);
Bubble bSettings = new Bubble(context, inputView, Bubble bSettings = new Bubble(context, inputView,
R.drawable.dialog_bubble_step07, x, 0, R.drawable.dialog_bubble_step07, x, 0,
R.string.tip_to_launch_settings, R.string.touch_to_continue); R.string.tip_to_launch_settings, R.string.touch_to_continue);
mBubbles.add(bSettings); bubbles.add(bSettings);
Bubble bDone = new Bubble(context, inputView, Bubble bDone = new Bubble(context, inputView,
R.drawable.dialog_bubble_step02, x, 0, R.drawable.dialog_bubble_step02, x, 0,
R.string.tip_to_start_typing, R.string.touch_to_finish); R.string.tip_to_start_typing, R.string.touch_to_finish);
mBubbles.add(bDone); bubbles.add(bDone);
mInputView = inputView;
} }
void start() { public void start() {
mInputView.getLocationInWindow(mLocation); mInputView.getLocationInWindow(mLocation);
mBubbleIndex = -1; mBubbleIndex = -1;
mInputView.setOnTouchListener(this); mInputView.setOnTouchListener(this);
next(); next();
} }
boolean next() { private void next() {
if (mBubbleIndex >= 0) { if (mBubbleIndex >= 0) {
// If the bubble is not yet showing, don't move to the next. // If the bubble is not yet showing, don't move to the next.
if (!mBubbles.get(mBubbleIndex).isShowing()) { if (!mBubbles.get(mBubbleIndex).isShowing()) {
return true; return;
} }
// Hide all previous bubbles as well, as they may have had a delayed show // Hide all previous bubbles as well, as they may have had a delayed show
for (int i = 0; i <= mBubbleIndex; i++) { for (int i = 0; i <= mBubbleIndex; i++) {
@ -210,26 +213,25 @@ public class Tutorial implements OnTouchListener {
mBubbleIndex++; mBubbleIndex++;
if (mBubbleIndex >= mBubbles.size()) { if (mBubbleIndex >= mBubbles.size()) {
mInputView.setOnTouchListener(null); mInputView.setOnTouchListener(null);
mIme.sendDownUpKeyEvents(-1); // Inform the setupwizard that tutorial is in last bubble mListener.onTutorialDone();
mIme.tutorialDone(); return;
return false;
} }
if (mBubbleIndex == 3 || mBubbleIndex == 4) { if (mBubbleIndex == 3 || mBubbleIndex == 4) {
mIme.mKeyboardSwitcher.toggleSymbols(); mKeyboardSwitcher.toggleSymbols();
} }
mHandler.sendMessageDelayed( mHandler.sendMessageDelayed(
mHandler.obtainMessage(MSG_SHOW_BUBBLE, mBubbles.get(mBubbleIndex)), 500); mHandler.obtainMessage(MSG_SHOW_BUBBLE, mBubbles.get(mBubbleIndex)), 500);
return true; return;
} }
void hide() { private void hide() {
for (int i = 0; i < mBubbles.size(); i++) { for (Bubble bubble : mBubbles) {
mBubbles.get(i).hide(); bubble.hide();
} }
mInputView.setOnTouchListener(null); mInputView.setOnTouchListener(null);
} }
boolean close() { public boolean close() {
mHandler.removeMessages(MSG_SHOW_BUBBLE); mHandler.removeMessages(MSG_SHOW_BUBBLE);
hide(); hide();
return true; return true;