Commit 4df6fe2b authored by changwan's avatar changwan Committed by Commit bot

Simplify handler initialization for InputConnectionFactory

As a follow-up to https://codereview.chromium.org/2543893002/,
we make the code easy to read and remove unnecessary findbug suppression.

BUG=668692

Review-Url: https://codereview.chromium.org/2544163003
Cr-Commit-Position: refs/heads/master@{#436218}
parent 8ad45afb
......@@ -11,7 +11,6 @@ import android.view.inputmethod.EditorInfo;
import org.chromium.base.Log;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.SuppressFBWarnings;
/**
* A factory class for {@link ThreadedInputConnection}. The class also includes triggering
......@@ -31,11 +30,6 @@ public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti
// UI message loop until View#hasWindowFocus() is aligned with what IMMS sees.
private static final int CHECK_REGISTER_RETRY = 1;
// Reused for multiple WebView instances. TODO(changwan): check if we need to quit the loop
// for the last webview instance.
private static HandlerThread sHandlerThread;
private final Handler mHandler;
private final InputMethodManagerWrapper mInputMethodManagerWrapper;
private final InputMethodUma mInputMethodUma;
private ThreadedInputConnectionProxyView mProxyView;
......@@ -43,6 +37,18 @@ public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti
private CheckInvalidator mCheckInvalidator;
private boolean mReentrantTriggering;
// Initialization-on-demand holder for Handler.
private static class LazyHandlerHolder {
// Note that we never exit this thread to avoid lifetime or thread-safety issues.
private static final Handler sHandler;
static {
HandlerThread handlerThread =
new HandlerThread("InputConnectionHandlerThread", HandlerThread.NORM_PRIORITY);
handlerThread.start();
sHandler = new Handler(handlerThread.getLooper());
}
}
// A small class that can be updated to invalidate the check when there is an external event
// such as window focus loss or view focus loss.
private static class CheckInvalidator {
......@@ -61,19 +67,12 @@ public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti
ThreadedInputConnectionFactory(InputMethodManagerWrapper inputMethodManagerWrapper) {
mInputMethodManagerWrapper = inputMethodManagerWrapper;
mHandler = createHandler();
mInputMethodUma = createInputMethodUma();
}
@VisibleForTesting
@SuppressFBWarnings("LI_LAZY_INIT_UPDATE_STATIC")
protected Handler createHandler() {
if (sHandlerThread == null) {
sHandlerThread =
new HandlerThread("InputConnectionHandlerThread", HandlerThread.NORM_PRIORITY);
sHandlerThread.start();
}
return new Handler(sHandlerThread.getLooper());
@Override
public Handler getHandler() {
return LazyHandlerHolder.sHandler;
}
@VisibleForTesting
......@@ -133,7 +132,7 @@ public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti
if (mThreadedInputConnection == null) {
if (DEBUG_LOGS) Log.w(TAG, "Creating ThreadedInputConnection...");
mThreadedInputConnection = new ThreadedInputConnection(view, imeAdapter, mHandler);
mThreadedInputConnection = new ThreadedInputConnection(view, imeAdapter, getHandler());
} else {
mThreadedInputConnection.resetOnUiThread();
}
......@@ -154,7 +153,7 @@ public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti
if (!view.hasWindowFocus()) mCheckInvalidator.invalidate();
// We cannot reuse the existing proxy view, if any, due to crbug.com/664402.
mProxyView = createProxyView(mHandler, view);
mProxyView = createProxyView(getHandler(), view);
mReentrantTriggering = true;
// This does not affect view focus of the real views.
......@@ -181,7 +180,7 @@ public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti
// Step 3: Check that the above hack worked.
// Do not check until activation finishes inside InputMethodManager (on IME thread).
mHandler.post(new Runnable() {
getHandler().post(new Runnable() {
@Override
public void run() {
postCheckRegisterResultOnUiThread(view, mCheckInvalidator,
......@@ -236,11 +235,6 @@ public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti
mInputMethodUma.recordProxyViewFailure();
}
@Override
public Handler getHandler() {
return mHandler;
}
@Override
public void onWindowFocusChanged(boolean gainFocus) {
if (DEBUG_LOGS) Log.d(TAG, "onWindowFocusChanged: " + gainFocus);
......
......@@ -58,13 +58,6 @@ public class ThreadedInputConnectionFactoryTest {
super(inputMethodManagerWrapper);
}
@Override
protected Handler createHandler() {
mImeHandler = super.createHandler();
mImeShadowLooper = (ShadowLooper) ShadowExtractor.extract(mImeHandler.getLooper());
return mImeHandler;
}
@Override
protected ThreadedInputConnectionProxyView createProxyView(Handler handler,
View containerView) {
......@@ -135,6 +128,8 @@ public class ThreadedInputConnectionFactoryTest {
mFactory = new TestFactory(new InputMethodManagerWrapper(mContext));
mFactory.onWindowFocusChanged(true);
mImeHandler = mFactory.getHandler();
mImeShadowLooper = (ShadowLooper) ShadowExtractor.extract(mImeHandler.getLooper());
when(mContext.getSystemService(Context.INPUT_METHOD_SERVICE))
.thenReturn(mInputMethodManager);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment