OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.content.browser.input; | |
6 | |
7 import android.os.Handler; | |
8 import android.os.HandlerThread; | |
9 import android.view.View; | |
10 import android.view.inputmethod.EditorInfo; | |
11 | |
12 import org.chromium.base.Log; | |
13 | |
14 /** | |
15 * Default factory for ChromiumBaseInputConnection classes. | |
16 */ | |
17 public class ThreadedInputConnectionFactory implements ChromiumBaseInputConnecti on.Factory { | |
18 private static final String TAG = "cr_Ime"; | |
19 private static final boolean DEBUG_LOGS = false; | |
20 | |
21 private ThreadedInputConnection mThreadedInputConnection; | |
22 private final Handler mHandler; | |
23 private final InputMethodManagerWrapper mInputMethodManagerWrapper; | |
24 private ThreadedInputConnectionProxyView mProxyView; | |
25 | |
26 ThreadedInputConnectionFactory(InputMethodManagerWrapper inputMethodManagerW rapper) { | |
27 HandlerThread thread = | |
28 new HandlerThread("InputConnectionHandlerThread", HandlerThread. NORM_PRIORITY); | |
29 thread.start(); | |
30 mHandler = new Handler(thread.getLooper()); | |
31 mInputMethodManagerWrapper = inputMethodManagerWrapper; | |
32 } | |
33 | |
34 private boolean isCalledFromProxyView() { | |
35 for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { | |
36 String className = ste.getClassName(); | |
37 if (className != null | |
38 && className.contains(ThreadedInputConnectionProxyView.class .getName())) { | |
Ted C
2016/02/17 19:09:34
What view is passed into initializeAndGet? Is it
Changwan Ryu
2016/02/18 06:03:27
I think it should always be the container view.
Th
| |
39 return true; | |
40 } | |
41 } | |
42 return false; | |
43 } | |
44 | |
45 @Override | |
46 public ChromiumBaseInputConnection initializeAndGet( | |
47 View view, ImeAdapter imeAdapter, int inputType, int inputFlags, Edi torInfo outAttrs) { | |
48 ImeUtils.checkOnUiThread(); | |
49 if (!isCalledFromProxyView()) { | |
50 if (DEBUG_LOGS) Log.w(TAG, "initializeAndGet: not called from proxy view"); | |
51 triggerDelayedOnCreateInputConnection(view); | |
52 return null; | |
53 } | |
54 if (DEBUG_LOGS) Log.w(TAG, "initializeAndGet: called from proxy view"); | |
55 if (mThreadedInputConnection == null) { | |
56 if (DEBUG_LOGS) Log.w(TAG, "Creating ThreadedInputConnection..."); | |
57 mThreadedInputConnection = new ThreadedInputConnection(imeAdapter, m Handler); | |
58 } | |
59 mThreadedInputConnection.initializeOutAttrsOnUiThread(inputType, inputFl ags, outAttrs); | |
60 return mThreadedInputConnection; | |
61 } | |
62 | |
63 private void triggerDelayedOnCreateInputConnection(final View view) { | |
64 mProxyView = new ThreadedInputConnectionProxyView(view.getContext(), mHa ndler, view); | |
65 mProxyView.requestFocus(); | |
66 view.getHandler().post(new Runnable() { | |
67 @Override | |
68 public void run() { | |
69 // This is a hack to make InputMethodManager believe that the pr oxy view | |
70 // now has a focus. As a result, InputMethodManager will think t hat mProxyView | |
71 // is focused, and will call getHandler() of the view when creat ing input | |
72 // connection. | |
73 | |
74 // Step 1: Set InputMethodManager#mNextServedView as mProxyView. | |
75 mProxyView.onWindowFocusChanged(true); | |
76 // Step 2: Have InputMethodManager focus in on mNextServedView. | |
77 mInputMethodManagerWrapper.isActive(view); | |
78 // Step 3: Verify that the above hack worked. | |
79 mHandler.post(new Runnable() { | |
80 @Override | |
81 public void run() { | |
82 ImeUtils.checkCondition(mInputMethodManagerWrapper.isAct ive(mProxyView)); | |
83 ImeUtils.checkCondition(mInputMethodManagerWrapper.isAct ive(view)); | |
84 } | |
85 }); | |
86 | |
87 // TODO(changwan): come up with fallback plan or crash when the above hack fails. | |
Ted C
2016/02/17 19:09:34
we can't actually launch this until we have a solu
Changwan Ryu
2016/02/18 06:03:27
Agreed. I'm planning to do the following:
- disabl
Changwan Ryu
2016/02/18 10:38:42
Added code to fall back to ReplicaInputConnection.
| |
88 } | |
89 }); | |
90 } | |
91 | |
92 @Override | |
93 public Handler getHandler() { | |
94 return mHandler; | |
95 } | |
96 } | |
OLD | NEW |