Refactor UI Handler of LatinIME

Change-Id: Iabae338c32969997cb3c7f612c2e5a6999420e28
main
Tadashi G. Takaoka 2010-11-16 01:07:06 -08:00
parent 89cfd23f34
commit d7641636db
1 changed files with 95 additions and 73 deletions

View File

@ -132,14 +132,10 @@ public class LatinIME extends InputMethodService
public static final String PREF_INPUT_LANGUAGE = "input_language"; public static final String PREF_INPUT_LANGUAGE = "input_language";
private static final String PREF_RECORRECTION_ENABLED = "recorrection_enabled"; private static final String PREF_RECORRECTION_ENABLED = "recorrection_enabled";
private static final int MSG_UPDATE_SUGGESTIONS = 0;
private static final int MSG_START_TUTORIAL = 1;
private static final int MSG_UPDATE_SHIFT_STATE = 2;
private static final int MSG_VOICE_RESULTS = 3;
private static final int MSG_UPDATE_OLD_SUGGESTIONS = 4;
private static final int DELAY_UPDATE_SUGGESTIONS = 180; private static final int DELAY_UPDATE_SUGGESTIONS = 180;
private static final int DELAY_UPDATE_OLD_SUGGESTIONS = 300; private static final int DELAY_UPDATE_OLD_SUGGESTIONS = 300;
private static final int DELAY_UPDATE_SHIFT_STATE = 300;
private static final int DELAY_START_TUTORIAL = 500;
// How many continuous deletes at which to start deleting at a higher speed. // How many continuous deletes at which to start deleting at a higher speed.
private static final int DELETE_ACCELERATE_AT = 20; private static final int DELETE_ACCELERATE_AT = 20;
@ -319,36 +315,84 @@ public class LatinIME extends InputMethodService
} }
} }
/* package */ Handler mHandler = new Handler() { /* package */ UIHandler mHandler = new UIHandler();
/* package */ class UIHandler extends Handler {
private static final int MSG_UPDATE_SUGGESTIONS = 0;
private static final int MSG_UPDATE_OLD_SUGGESTIONS = 1;
private static final int MSG_UPDATE_SHIFT_STATE = 2;
private static final int MSG_VOICE_RESULTS = 3;
private static final int MSG_START_TUTORIAL = 4;
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case MSG_UPDATE_SUGGESTIONS: case MSG_UPDATE_SUGGESTIONS:
updateSuggestions(); updateSuggestions();
break; break;
case MSG_UPDATE_OLD_SUGGESTIONS: case MSG_UPDATE_OLD_SUGGESTIONS:
setOldSuggestions(); setOldSuggestions();
break; break;
case MSG_START_TUTORIAL: case MSG_UPDATE_SHIFT_STATE:
if (mTutorial == null) { updateShiftKeyState(getCurrentInputEditorInfo());
if (mKeyboardSwitcher.isInputViewShown()) { break;
mTutorial = new Tutorial(LatinIME.this, mKeyboardSwitcher); case MSG_VOICE_RESULTS:
mTutorial.start(); handleVoiceResults();
} else { break;
// Try again soon if the view is not yet showing case MSG_START_TUTORIAL:
sendMessageDelayed(obtainMessage(MSG_START_TUTORIAL), 100); if (mTutorial == null) {
} if (mKeyboardSwitcher.isInputViewShown()) {
mTutorial = new Tutorial(LatinIME.this, mKeyboardSwitcher);
mTutorial.start();
} else {
// Try again soon if the view is not yet showing
sendMessageDelayed(obtainMessage(MSG_START_TUTORIAL), 100);
} }
break; }
case MSG_UPDATE_SHIFT_STATE: break;
updateShiftKeyState(getCurrentInputEditorInfo());
break;
case MSG_VOICE_RESULTS:
handleVoiceResults();
break;
} }
} }
};
public void postUpdateSuggestions() {
removeMessages(MSG_UPDATE_SUGGESTIONS);
sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTIONS), DELAY_UPDATE_SUGGESTIONS);
}
public void cancelUpdateSuggestions() {
removeMessages(MSG_UPDATE_SUGGESTIONS);
}
public boolean hasPendingUpdateSuggestions() {
return hasMessages(MSG_UPDATE_SUGGESTIONS);
}
public void postUpdateOldSuggestions() {
removeMessages(MSG_UPDATE_OLD_SUGGESTIONS);
sendMessageDelayed(obtainMessage(MSG_UPDATE_OLD_SUGGESTIONS),
DELAY_UPDATE_OLD_SUGGESTIONS);
}
public void cancelUpdateOldSuggestions() {
removeMessages(MSG_UPDATE_OLD_SUGGESTIONS);
}
public void postUpdateShiftKeyState() {
removeMessages(MSG_UPDATE_SHIFT_STATE);
sendMessageDelayed(obtainMessage(MSG_UPDATE_SHIFT_STATE), DELAY_UPDATE_SHIFT_STATE);
}
public void cancelUpdateShiftState() {
removeMessages(MSG_UPDATE_SHIFT_STATE);
}
public void updateVoiceResults() {
sendMessage(obtainMessage(MSG_VOICE_RESULTS));
}
public void startTutorial() {
sendMessageDelayed(obtainMessage(MSG_START_TUTORIAL), DELAY_START_TUTORIAL);
}
}
@Override @Override
public void onCreate() { public void onCreate() {
@ -712,7 +756,7 @@ public class LatinIME extends InputMethodService
// Then look for possible corrections in a delayed fashion // Then look for possible corrections in a delayed fashion
if (!TextUtils.isEmpty(et.text) && isCursorTouchingWord()) { if (!TextUtils.isEmpty(et.text) && isCursorTouchingWord()) {
postUpdateOldSuggestions(); mHandler.postUpdateOldSuggestions();
} }
} }
} }
@ -745,9 +789,9 @@ public class LatinIME extends InputMethodService
BaseKeyboardView inputView = mKeyboardSwitcher.getInputView(); BaseKeyboardView inputView = mKeyboardSwitcher.getInputView();
if (inputView != null) if (inputView != null)
inputView.setForeground(false); inputView.setForeground(false);
// Remove penging messages related to update suggestions // Remove pending messages related to update suggestions
mHandler.removeMessages(MSG_UPDATE_SUGGESTIONS); mHandler.cancelUpdateSuggestions();
mHandler.removeMessages(MSG_UPDATE_OLD_SUGGESTIONS); mHandler.cancelUpdateOldSuggestions();
} }
@Override @Override
@ -791,7 +835,7 @@ public class LatinIME extends InputMethodService
&& mLastSelectionStart != newSelStart)) { && mLastSelectionStart != newSelStart)) {
mComposing.setLength(0); mComposing.setLength(0);
mPredicting = false; mPredicting = false;
postUpdateSuggestions(); mHandler.postUpdateSuggestions();
TextEntryState.reset(); TextEntryState.reset();
InputConnection ic = getCurrentInputConnection(); InputConnection ic = getCurrentInputConnection();
if (ic != null) { if (ic != null) {
@ -809,7 +853,7 @@ public class LatinIME extends InputMethodService
} }
} }
mJustAccepted = false; mJustAccepted = false;
postUpdateShiftKeyState(); mHandler.postUpdateShiftKeyState();
// Make a note of the cursor position // Make a note of the cursor position
mLastSelectionStart = newSelStart; mLastSelectionStart = newSelStart;
@ -826,7 +870,7 @@ public class LatinIME extends InputMethodService
&& (newSelStart < newSelEnd - 1 || (!mPredicting)) && (newSelStart < newSelEnd - 1 || (!mPredicting))
&& !mVoiceInputHighlighted) { && !mVoiceInputHighlighted) {
if (isCursorTouchingWord() || mLastSelectionStart < mLastSelectionEnd) { if (isCursorTouchingWord() || mLastSelectionStart < mLastSelectionEnd) {
postUpdateOldSuggestions(); mHandler.postUpdateOldSuggestions();
} else { } else {
abortCorrection(false); abortCorrection(false);
// Show the punctuation suggestions list if the current one is not // Show the punctuation suggestions list if the current one is not
@ -1041,12 +1085,6 @@ public class LatinIME extends InputMethodService
} }
} }
private void postUpdateShiftKeyState() {
mHandler.removeMessages(MSG_UPDATE_SHIFT_STATE);
// TODO: Should remove this 300ms delay?
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_UPDATE_SHIFT_STATE), 300);
}
public void updateShiftKeyState(EditorInfo attr) { public void updateShiftKeyState(EditorInfo attr) {
InputConnection ic = getCurrentInputConnection(); InputConnection ic = getCurrentInputConnection();
KeyboardSwitcher switcher = mKeyboardSwitcher; KeyboardSwitcher switcher = mKeyboardSwitcher;
@ -1146,7 +1184,7 @@ public class LatinIME extends InputMethodService
mUserDictionary.addWord(word, 128); mUserDictionary.addWord(word, 128);
// Suggestion strip should be updated after the operation of adding word to the // Suggestion strip should be updated after the operation of adding word to the
// user dictionary // user dictionary
postUpdateSuggestions(); mHandler.postUpdateSuggestions();
return true; return true;
} }
@ -1319,14 +1357,14 @@ public class LatinIME extends InputMethodService
if (mComposing.length() == 0) { if (mComposing.length() == 0) {
mPredicting = false; mPredicting = false;
} }
postUpdateSuggestions(); mHandler.postUpdateSuggestions();
} else { } else {
ic.deleteSurroundingText(1, 0); ic.deleteSurroundingText(1, 0);
} }
} else { } else {
deleteChar = true; deleteChar = true;
} }
postUpdateShiftKeyState(); mHandler.postUpdateShiftKeyState();
TextEntryState.backspace(); TextEntryState.backspace();
if (TextEntryState.getState() == TextEntryState.State.UNDO_COMMIT) { if (TextEntryState.getState() == TextEntryState.State.UNDO_COMMIT) {
revertLastWord(deleteChar); revertLastWord(deleteChar);
@ -1364,7 +1402,7 @@ public class LatinIME extends InputMethodService
} }
private void handleShiftInternal(boolean forceNormal) { private void handleShiftInternal(boolean forceNormal) {
mHandler.removeMessages(MSG_UPDATE_SHIFT_STATE); mHandler.cancelUpdateShiftState();
KeyboardSwitcher switcher = mKeyboardSwitcher; KeyboardSwitcher switcher = mKeyboardSwitcher;
if (switcher.isAlphabetMode()) { if (switcher.isAlphabetMode()) {
if (!switcher.isKeyboardAvailable()) if (!switcher.isKeyboardAvailable())
@ -1380,7 +1418,7 @@ public class LatinIME extends InputMethodService
} }
private void handleCapsLock() { private void handleCapsLock() {
mHandler.removeMessages(MSG_UPDATE_SHIFT_STATE); mHandler.cancelUpdateShiftState();
KeyboardSwitcher switcher = mKeyboardSwitcher; KeyboardSwitcher switcher = mKeyboardSwitcher;
if (switcher.isAlphabetMode()) { if (switcher.isAlphabetMode()) {
if (!switcher.isKeyboardAvailable()) if (!switcher.isKeyboardAvailable())
@ -1461,7 +1499,7 @@ public class LatinIME extends InputMethodService
} }
ic.setComposingText(mComposing, 1); ic.setComposingText(mComposing, 1);
} }
postUpdateSuggestions(); mHandler.postUpdateSuggestions();
} else { } else {
sendKeyChar((char)primaryCode); sendKeyChar((char)primaryCode);
} }
@ -1482,7 +1520,7 @@ public class LatinIME extends InputMethodService
// Should dismiss the "Touch again to save" message when handling separator // Should dismiss the "Touch again to save" message when handling separator
if (mCandidateView != null && mCandidateView.dismissAddToDictionaryHint()) { if (mCandidateView != null && mCandidateView.dismissAddToDictionaryHint()) {
postUpdateSuggestions(); mHandler.postUpdateSuggestions();
} }
boolean pickedDefault = false; boolean pickedDefault = false;
@ -1569,18 +1607,6 @@ public class LatinIME extends InputMethodService
mWordHistory.add(entry); mWordHistory.add(entry);
} }
private void postUpdateSuggestions() {
mHandler.removeMessages(MSG_UPDATE_SUGGESTIONS);
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_UPDATE_SUGGESTIONS),
DELAY_UPDATE_SUGGESTIONS);
}
private void postUpdateOldSuggestions() {
mHandler.removeMessages(MSG_UPDATE_OLD_SUGGESTIONS);
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_UPDATE_OLD_SUGGESTIONS),
DELAY_UPDATE_OLD_SUGGESTIONS);
}
private boolean isPredictionOn() { private boolean isPredictionOn() {
return mPredictionOn; return mPredictionOn;
} }
@ -1617,7 +1643,7 @@ public class LatinIME extends InputMethodService
} }
setCandidatesViewShown(isCandidateStripVisible()); setCandidatesViewShown(isCandidateStripVisible());
updateInputViewShown(); updateInputViewShown();
postUpdateSuggestions(); mHandler.postUpdateSuggestions();
}}); }});
} }
@ -1730,7 +1756,7 @@ public class LatinIME extends InputMethodService
} }
mVoiceResults.candidates = candidates; mVoiceResults.candidates = candidates;
mVoiceResults.alternatives = alternatives; mVoiceResults.alternatives = alternatives;
mHandler.sendMessage(mHandler.obtainMessage(MSG_VOICE_RESULTS)); mHandler.updateVoiceResults();
} }
private void handleVoiceResults() { private void handleVoiceResults() {
@ -1877,8 +1903,8 @@ public class LatinIME extends InputMethodService
private boolean pickDefaultSuggestion() { private boolean pickDefaultSuggestion() {
// Complete any pending candidate query first // Complete any pending candidate query first
if (mHandler.hasMessages(MSG_UPDATE_SUGGESTIONS)) { if (mHandler.hasPendingUpdateSuggestions()) {
mHandler.removeMessages(MSG_UPDATE_SUGGESTIONS); mHandler.cancelUpdateSuggestions();
updateSuggestions(); updateSuggestions();
} }
if (mBestWord != null && mBestWord.length() > 0) { if (mBestWord != null && mBestWord.length() > 0) {
@ -1971,7 +1997,7 @@ public class LatinIME extends InputMethodService
// If we're not showing the "Touch again to save", then show corrections again. // If we're not showing the "Touch again to save", then show corrections again.
// In case the cursor position doesn't change, make sure we show the suggestions again. // In case the cursor position doesn't change, make sure we show the suggestions again.
clearSuggestions(); clearSuggestions();
postUpdateOldSuggestions(); mHandler.postUpdateOldSuggestions();
} }
if (showingAddToDictionaryHint) { if (showingAddToDictionaryHint) {
mCandidateView.showAddToDictionaryHint(suggestion); mCandidateView.showAddToDictionaryHint(suggestion);
@ -2223,7 +2249,7 @@ public class LatinIME extends InputMethodService
ic.deleteSurroundingText(toDelete, 0); ic.deleteSurroundingText(toDelete, 0);
ic.setComposingText(mComposing, 1); ic.setComposingText(mComposing, 1);
TextEntryState.backspace(); TextEntryState.backspace();
postUpdateSuggestions(); mHandler.postUpdateSuggestions();
} else { } else {
sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL); sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
mJustRevertedSeparator = null; mJustRevertedSeparator = null;
@ -2439,7 +2465,7 @@ public class LatinIME extends InputMethodService
private void checkTutorial(String privateImeOptions) { private void checkTutorial(String privateImeOptions) {
if (privateImeOptions == null) return; if (privateImeOptions == null) return;
if (privateImeOptions.equals("com.android.setupwizard:ShowTutorial")) { if (privateImeOptions.equals("com.android.setupwizard:ShowTutorial")) {
if (mTutorial == null) startTutorial(); if (mTutorial == null) mHandler.startTutorial();
} else if (privateImeOptions.equals("com.android.setupwizard:HideTutorial")) { } else if (privateImeOptions.equals("com.android.setupwizard:HideTutorial")) {
if (mTutorial != null) { if (mTutorial != null) {
if (mTutorial.close()) { if (mTutorial.close()) {
@ -2449,10 +2475,6 @@ public class LatinIME extends InputMethodService
} }
} }
private void startTutorial() {
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_TUTORIAL), 500);
}
// Tutorial.TutorialListener // Tutorial.TutorialListener
public void onTutorialDone() { public void onTutorialDone() {
sendDownUpKeyEvents(-1); // Inform the setupwizard that tutorial is in last bubble sendDownUpKeyEvents(-1); // Inform the setupwizard that tutorial is in last bubble