2013-12-24 07:30:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.inputlogic;
|
|
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.HandlerThread;
|
|
|
|
import android.os.Message;
|
|
|
|
|
2014-04-07 13:32:06 +00:00
|
|
|
import com.android.inputmethod.compat.LooperCompatUtils;
|
2013-12-25 12:04:27 +00:00
|
|
|
import com.android.inputmethod.latin.InputPointers;
|
|
|
|
import com.android.inputmethod.latin.LatinIME;
|
|
|
|
import com.android.inputmethod.latin.Suggest;
|
|
|
|
import com.android.inputmethod.latin.SuggestedWords;
|
|
|
|
import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback;
|
|
|
|
|
2013-12-24 07:30:55 +00:00
|
|
|
/**
|
|
|
|
* A helper to manage deferred tasks for the input logic.
|
|
|
|
*/
|
2013-12-27 04:10:57 +00:00
|
|
|
class InputLogicHandler implements Handler.Callback {
|
2013-12-24 07:30:55 +00:00
|
|
|
final Handler mNonUIThreadHandler;
|
2013-12-25 12:04:27 +00:00
|
|
|
// TODO: remove this reference.
|
|
|
|
final LatinIME mLatinIME;
|
|
|
|
final InputLogic mInputLogic;
|
|
|
|
private final Object mLock = new Object();
|
|
|
|
private boolean mInBatchInput; // synchronized using {@link #mLock}.
|
|
|
|
|
|
|
|
private static final int MSG_GET_SUGGESTED_WORDS = 1;
|
2013-12-24 07:30:55 +00:00
|
|
|
|
2014-01-10 04:07:30 +00:00
|
|
|
// A handler that never does anything. This is used for cases where events come before anything
|
|
|
|
// is initialized, though probably only the monkey can actually do this.
|
|
|
|
public static final InputLogicHandler NULL_HANDLER = new InputLogicHandler() {
|
|
|
|
@Override
|
2014-03-31 05:43:08 +00:00
|
|
|
public void reset() {}
|
2014-01-10 04:07:30 +00:00
|
|
|
@Override
|
|
|
|
public boolean handleMessage(final Message msg) { return true; }
|
|
|
|
@Override
|
|
|
|
public void onStartBatchInput() {}
|
|
|
|
@Override
|
|
|
|
public void onUpdateBatchInput(final InputPointers batchPointers,
|
|
|
|
final int sequenceNumber) {}
|
|
|
|
@Override
|
|
|
|
public void onCancelBatchInput() {}
|
|
|
|
@Override
|
2014-03-06 07:10:15 +00:00
|
|
|
public void updateTailBatchInput(final InputPointers batchPointers,
|
|
|
|
final int sequenceNumber) {}
|
2014-01-10 04:07:30 +00:00
|
|
|
@Override
|
|
|
|
public void getSuggestedWords(final int sessionId, final int sequenceNumber,
|
|
|
|
final OnGetSuggestedWordsCallback callback) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
private InputLogicHandler() {
|
|
|
|
mNonUIThreadHandler = null;
|
|
|
|
mLatinIME = null;
|
|
|
|
mInputLogic = null;
|
|
|
|
}
|
|
|
|
|
2013-12-25 12:04:27 +00:00
|
|
|
public InputLogicHandler(final LatinIME latinIME, final InputLogic inputLogic) {
|
2013-12-24 07:30:55 +00:00
|
|
|
final HandlerThread handlerThread = new HandlerThread(
|
|
|
|
InputLogicHandler.class.getSimpleName());
|
|
|
|
handlerThread.start();
|
|
|
|
mNonUIThreadHandler = new Handler(handlerThread.getLooper(), this);
|
2013-12-25 12:04:27 +00:00
|
|
|
mLatinIME = latinIME;
|
|
|
|
mInputLogic = inputLogic;
|
2013-12-24 07:30:55 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 05:43:08 +00:00
|
|
|
public void reset() {
|
|
|
|
mNonUIThreadHandler.removeCallbacksAndMessages(null);
|
2013-12-24 07:30:55 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 13:32:06 +00:00
|
|
|
// In unit tests, we create several instances of LatinIME, which results in several instances
|
|
|
|
// of InputLogicHandler. To avoid these handlers lingering, we call this.
|
|
|
|
public void destroy() {
|
|
|
|
LooperCompatUtils.quitSafely(mNonUIThreadHandler.getLooper());
|
|
|
|
}
|
|
|
|
|
2013-12-24 07:30:55 +00:00
|
|
|
/**
|
|
|
|
* Handle a message.
|
|
|
|
* @see android.os.Handler.Callback#handleMessage(android.os.Message)
|
|
|
|
*/
|
2013-12-25 12:04:27 +00:00
|
|
|
// Called on the Non-UI handler thread by the Handler code.
|
2013-12-24 07:30:55 +00:00
|
|
|
@Override
|
|
|
|
public boolean handleMessage(final Message msg) {
|
2013-12-25 12:04:27 +00:00
|
|
|
switch (msg.what) {
|
|
|
|
case MSG_GET_SUGGESTED_WORDS:
|
|
|
|
mLatinIME.getSuggestedWords(msg.arg1 /* sessionId */,
|
|
|
|
msg.arg2 /* sequenceNumber */, (OnGetSuggestedWordsCallback) msg.obj);
|
|
|
|
break;
|
|
|
|
}
|
2013-12-24 07:30:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-12-25 12:04:27 +00:00
|
|
|
|
|
|
|
// Called on the UI thread by InputLogic.
|
|
|
|
public void onStartBatchInput() {
|
|
|
|
synchronized (mLock) {
|
|
|
|
mInBatchInput = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2014-03-06 07:10:15 +00:00
|
|
|
* @param isTailBatchInput true if this is the end of a batch input, false if it's an update.
|
2013-12-25 12:04:27 +00:00
|
|
|
*/
|
|
|
|
// 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 Non-UI handler
|
|
|
|
// thread to pull suggestions, and get the inlined callback to get called on the Non-UI
|
|
|
|
// handler thread. If this is the end of a batch input, 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,
|
2014-03-06 07:10:15 +00:00
|
|
|
final int sequenceNumber, final boolean isTailBatchInput) {
|
2013-12-25 12:04:27 +00:00
|
|
|
synchronized (mLock) {
|
|
|
|
if (!mInBatchInput) {
|
|
|
|
// Batch input has ended or canceled while the message was being delivered.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mInputLogic.mWordComposer.setBatchInputPointers(batchPointers);
|
|
|
|
getSuggestedWords(Suggest.SESSION_GESTURE, sequenceNumber,
|
|
|
|
new OnGetSuggestedWordsCallback() {
|
|
|
|
@Override
|
|
|
|
public void onGetSuggestedWords(SuggestedWords suggestedWords) {
|
|
|
|
// We're now inside the callback. This always runs on the Non-UI thread,
|
|
|
|
// no matter what thread updateBatchInput was originally called on.
|
|
|
|
if (suggestedWords.isEmpty()) {
|
|
|
|
// Use old suggestions if we don't have any new ones.
|
|
|
|
// Previous suggestions are found in InputLogic#mSuggestedWords.
|
|
|
|
// Since these are the most recent ones and we just recomputed
|
|
|
|
// new ones to update them, then the previous ones are there.
|
|
|
|
suggestedWords = mInputLogic.mSuggestedWords;
|
|
|
|
}
|
|
|
|
mLatinIME.mHandler.showGesturePreviewAndSuggestionStrip(suggestedWords,
|
2014-03-06 07:10:15 +00:00
|
|
|
isTailBatchInput /* dismissGestureFloatingPreviewText */);
|
|
|
|
if (isTailBatchInput) {
|
2013-12-25 12:04:27 +00:00
|
|
|
mInBatchInput = false;
|
2014-02-27 14:21:09 +00:00
|
|
|
// The following call schedules onEndBatchInputInternal
|
2013-12-25 12:04:27 +00:00
|
|
|
// to be called on the UI thread.
|
2014-03-06 07:10:15 +00:00
|
|
|
mLatinIME.mHandler.showTailBatchInputResult(suggestedWords);
|
2013-12-25 12:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 InputLogic.
|
|
|
|
public void onUpdateBatchInput(final InputPointers batchPointers,
|
|
|
|
final int sequenceNumber) {
|
2014-03-06 07:10:15 +00:00
|
|
|
updateBatchInput(batchPointers, sequenceNumber, false /* isTailBatchInput */);
|
2013-12-25 12:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel a batch input.
|
|
|
|
*
|
2014-03-06 07:10:15 +00:00
|
|
|
* Note that as opposed to updateTailBatchInput, we do the UI side of this immediately on the
|
2013-12-25 12:04:27 +00:00
|
|
|
* same thread, rather than get this to call a method in LatinIME. This is because
|
|
|
|
* canceling a batch input does not necessitate the long operation of pulling suggestions.
|
|
|
|
*/
|
|
|
|
// Called on the UI thread by InputLogic.
|
|
|
|
public void onCancelBatchInput() {
|
|
|
|
synchronized (mLock) {
|
|
|
|
mInBatchInput = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-06 07:10:15 +00:00
|
|
|
* Trigger an update for a tail batch input.
|
2013-12-25 12:04:27 +00:00
|
|
|
*
|
2014-03-06 07:10:15 +00:00
|
|
|
* A tail batch input is the last update for a gesture, the one that is triggered after the
|
|
|
|
* user lifts their finger. This method schedules fetching suggestions on the non-UI thread,
|
|
|
|
* then when the suggestions are computed it comes back on the UI thread to update the
|
|
|
|
* suggestion strip, commit the first suggestion, and dismiss the floating text preview.
|
2013-12-25 12:04:27 +00:00
|
|
|
*
|
|
|
|
* @param batchPointers the updated batch pointers.
|
|
|
|
* @param sequenceNumber the sequence number associated with this batch input.
|
|
|
|
*/
|
|
|
|
// Called on the UI thread by InputLogic.
|
2014-03-06 07:10:15 +00:00
|
|
|
public void updateTailBatchInput(final InputPointers batchPointers,
|
|
|
|
final int sequenceNumber) {
|
|
|
|
updateBatchInput(batchPointers, sequenceNumber, true /* isTailBatchInput */);
|
2013-12-25 12:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void getSuggestedWords(final int sessionId, final int sequenceNumber,
|
|
|
|
final OnGetSuggestedWordsCallback callback) {
|
|
|
|
mNonUIThreadHandler.obtainMessage(
|
|
|
|
MSG_GET_SUGGESTED_WORDS, sessionId, sequenceNumber, callback).sendToTarget();
|
|
|
|
}
|
2013-12-24 07:30:55 +00:00
|
|
|
}
|