Merge "[IL44] Some factorization"

main
Jean Chalard 2013-12-27 06:58:12 +00:00 committed by Android (Google) Code Review
commit 0d21223521
2 changed files with 63 additions and 27 deletions

View File

@ -1305,13 +1305,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private static final int MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP = 1; private static final int MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP = 1;
private static final int MSG_GET_SUGGESTED_WORDS = 2; private static final int MSG_GET_SUGGESTED_WORDS = 2;
// Called on the InputUpdater thread by the Handler code.
@Override @Override
public boolean handleMessage(final Message msg) { public boolean handleMessage(final Message msg) {
// TODO: straighten message passing - we don't need two kinds of messages calling // TODO: straighten message passing - we don't need two kinds of messages calling
// each other. // each other.
switch (msg.what) { switch (msg.what) {
case MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP: case MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP:
updateBatchInput((InputPointers)msg.obj, msg.arg2 /* sequenceNumber */); updateBatchInput((InputPointers)msg.obj, msg.arg2 /* sequenceNumber */,
false /* forEnd */);
break; break;
case MSG_GET_SUGGESTED_WORDS: case MSG_GET_SUGGESTED_WORDS:
mLatinIme.getSuggestedWords(msg.arg1 /* sessionId */, mLatinIme.getSuggestedWords(msg.arg1 /* sessionId */,
@ -1321,7 +1323,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return true; return true;
} }
// Run on the UI thread. // Called on the UI thread by LatinIME.
public void onStartBatchInput() { public void onStartBatchInput() {
synchronized (mLock) { synchronized (mLock) {
mHandler.removeMessages(MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP); mHandler.removeMessages(MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP);
@ -1329,26 +1331,54 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
} }
// Run on the Handler thread. /**
private void updateBatchInput(final InputPointers batchPointers, final int sequenceNumber) { * Fetch suggestions corresponding to an update of a batch input.
* @param batchPointers the updated pointers, including the part that was passed last time.
* @param sequenceNumber the sequence number associated with this batch input.
* @param forEnd true if this is the end of a batch input, false if it's an update.
*/
// This method can be called from any thread and will see to it that the correct threads
// are used for parts that require it. This method will send a message to the
// InputUpdater thread to pull suggestions, and get the inlined callback to get called
// on the InputUpdater thread. The callback will then proceed to send a message to the
// UI handler in LatinIME so that showing suggestions can be done on the UI thread.
private void updateBatchInput(final InputPointers batchPointers,
final int sequenceNumber, final boolean forEnd) {
synchronized (mLock) { synchronized (mLock) {
if (!mInBatchInput) { if (!mInBatchInput) {
// Batch input has ended or canceled while the message was being delivered. // Batch input has ended or canceled while the message was being delivered.
return; return;
} }
getSuggestedWordsGestureLocked(batchPointers, sequenceNumber, getSuggestedWordsGestureLocked(batchPointers, sequenceNumber,
new OnGetSuggestedWordsCallback() { new OnGetSuggestedWordsCallback() {
@Override @Override
public void onGetSuggestedWords(final SuggestedWords suggestedWords) { public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip( // We're now inside the callback. This always runs on the
suggestedWords, false /* dismissGestureFloatingPreviewText */); // InputUpdater thread, no matter what thread updateBatchInput
} // was called on.
}); mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
suggestedWords,
forEnd /* dismissGestureFloatingPreviewText */);
if (forEnd) {
mInBatchInput = false;
// The following call schedules onEndBatchInputAsyncInternal
// to be called on the UI thread.
mLatinIme.mHandler.onEndBatchInput(suggestedWords);
}
}
});
} }
} }
// Run on the UI thread. /**
* Update a batch input.
*
* This fetches suggestions and updates the suggestion strip and the floating text preview.
*
* @param batchPointers the updated batch pointers.
* @param sequenceNumber the sequence number associated with this batch input.
*/
// Called on the UI thread by LatinIME.
public void onUpdateBatchInput(final InputPointers batchPointers, public void onUpdateBatchInput(final InputPointers batchPointers,
final int sequenceNumber) { final int sequenceNumber) {
if (mHandler.hasMessages(MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP)) { if (mHandler.hasMessages(MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP)) {
@ -1358,26 +1388,32 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
sequenceNumber /* arg2 */, batchPointers /* obj */).sendToTarget(); sequenceNumber /* arg2 */, batchPointers /* obj */).sendToTarget();
} }
/**
* Cancel a batch input.
*
* Note that as opposed to onEndBatchInput, we do the UI side of this immediately on the
* same thread, rather than get this to call a method in LatinIME. This is because
* cancelling a batch input does not necessitate the long operation of pulling suggestions.
*/
// Called on the UI thread by LatinIME.
public void onCancelBatchInput() { public void onCancelBatchInput() {
synchronized (mLock) { synchronized (mLock) {
mInBatchInput = false; mInBatchInput = false;
} }
} }
// Run on the UI thread. /**
public void onEndBatchInput(final InputPointers batchPointers) { * Finish a batch input.
synchronized(mLock) { *
getSuggestedWordsGestureLocked(batchPointers, SuggestedWords.NOT_A_SEQUENCE_NUMBER, * This fetches suggestions, updates the suggestion strip and commits the first suggestion.
new OnGetSuggestedWordsCallback() { * It also dismisses the floating text preview.
@Override *
public void onGetSuggestedWords(final SuggestedWords suggestedWords) { * @param batchPointers the updated batch pointers.
mInBatchInput = false; * @param sequenceNumber the sequence number associated with this batch input.
mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(suggestedWords, */
true /* dismissGestureFloatingPreviewText */); // Called on the UI thread by LatinIME.
mLatinIme.mHandler.onEndBatchInput(suggestedWords); public void onEndBatchInput(final InputPointers batchPointers, final int sequenceNumber) {
} updateBatchInput(batchPointers, sequenceNumber, true /* forEnd */);
});
}
} }
// {@link LatinIME#getSuggestedWords(int)} method calls with same session id have to // {@link LatinIME#getSuggestedWords(int)} method calls with same session id have to

View File

@ -402,7 +402,7 @@ public final class InputLogic {
final InputPointers batchPointers, final InputPointers batchPointers,
// TODO: remove these arguments // TODO: remove these arguments
final LatinIME.InputUpdater inputUpdater) { final LatinIME.InputUpdater inputUpdater) {
inputUpdater.onEndBatchInput(batchPointers); inputUpdater.onEndBatchInput(batchPointers, mAutoCommitSequenceNumber);
} }
// TODO: remove these arguments // TODO: remove these arguments