| Index: content/public/android/java/src/org/chromium/content/browser/input/ImeAdapter.java
|
| diff --git a/content/public/android/java/src/org/chromium/content/browser/input/ImeAdapter.java b/content/public/android/java/src/org/chromium/content/browser/input/ImeAdapter.java
|
| index db0e84c923bdc66a374a3c3260a6a03482da1809..138fdd5ab669995db92acc4fbcc4dd79faa51f62 100644
|
| --- a/content/public/android/java/src/org/chromium/content/browser/input/ImeAdapter.java
|
| +++ b/content/public/android/java/src/org/chromium/content/browser/input/ImeAdapter.java
|
| @@ -118,6 +118,9 @@ public class ImeAdapter {
|
| private int mLastCompositionEnd;
|
| private boolean mRestartInputOnNextStateUpdate;
|
|
|
| + // True if ImeAdapter is connected to render process.
|
| + private boolean mIsConnected;
|
| +
|
| /**
|
| * @param wrapper InputMethodManagerWrapper that should receive all the call directed to
|
| * InputMethodManager.
|
| @@ -157,6 +160,11 @@ public class ImeAdapter {
|
| } else {
|
| mCursorAnchorInfoController = null;
|
| }
|
| + mNativeImeAdapterAndroid = nativeInit();
|
| + }
|
| +
|
| + public long getNativePointer() {
|
| + return mNativeImeAdapterAndroid;
|
| }
|
|
|
| private void createInputConnectionFactory() {
|
| @@ -164,6 +172,13 @@ public class ImeAdapter {
|
| mInputConnectionFactory = new ThreadedInputConnectionFactory(mInputMethodManagerWrapper);
|
| }
|
|
|
| + // Tells if the ImeAdapter in valid state (i.e. not in destroyed state), and is
|
| + // connected to render process. The former check guards against the call via
|
| + // ThreadedInputConnection from Android framework after ImeAdapter.destroy() is called.
|
| + private boolean isValid() {
|
| + return mNativeImeAdapterAndroid != 0 && mIsConnected;
|
| + }
|
| +
|
| /**
|
| * @see View#onCreateInputConnection(EditorInfo)
|
| */
|
| @@ -190,7 +205,7 @@ public class ImeAdapter {
|
| false /* not an immediate request */, false /* disable monitoring */,
|
| mViewEmbedder.getAttachedView());
|
| }
|
| - if (mNativeImeAdapterAndroid != 0) {
|
| + if (mIsConnected) {
|
| nativeRequestCursorUpdate(mNativeImeAdapterAndroid,
|
| false /* not an immediate request */, false /* disable monitoring */);
|
| }
|
| @@ -325,27 +340,6 @@ public class ImeAdapter {
|
| }
|
|
|
| /**
|
| - * Attaches the imeAdapter to its native counterpart. This is needed to start forwarding
|
| - * keyboard events to WebKit.
|
| - * @param nativeImeAdapter The pointer to the native ImeAdapter object.
|
| - */
|
| - public void attach(long nativeImeAdapter) {
|
| - if (DEBUG_LOGS) Log.i(TAG, "attach");
|
| - if (mNativeImeAdapterAndroid == nativeImeAdapter) return;
|
| - if (mNativeImeAdapterAndroid != 0) {
|
| - nativeResetImeAdapter(mNativeImeAdapterAndroid);
|
| - }
|
| - if (nativeImeAdapter != 0) {
|
| - nativeAttachImeAdapter(nativeImeAdapter);
|
| - }
|
| - mNativeImeAdapterAndroid = nativeImeAdapter;
|
| - if (nativeImeAdapter != 0) {
|
| - createInputConnectionFactory();
|
| - }
|
| - resetAndHideKeyboard();
|
| - }
|
| -
|
| - /**
|
| * Show soft keyboard only if it is the current keyboard configuration.
|
| */
|
| private void showSoftKeyboard() {
|
| @@ -485,6 +479,16 @@ public class ImeAdapter {
|
| hideKeyboard();
|
| }
|
|
|
| + public void destroy() {
|
| + resetAndHideKeyboard();
|
| + nativeDestroy(mNativeImeAdapterAndroid);
|
| + mNativeImeAdapterAndroid = 0;
|
| + mIsConnected = false;
|
| + if (mCursorAnchorInfoController != null) {
|
| + mCursorAnchorInfoController.focusedNodeChanged(false);
|
| + }
|
| + }
|
| +
|
| /**
|
| * Update selection to input method manager.
|
| *
|
| @@ -517,7 +521,7 @@ public class ImeAdapter {
|
| }
|
|
|
| boolean performEditorAction(int actionCode) {
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
| if (actionCode == EditorInfo.IME_ACTION_NEXT) {
|
| sendSyntheticKeyPress(KeyEvent.KEYCODE_TAB,
|
| KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE
|
| @@ -549,7 +553,7 @@ public class ImeAdapter {
|
|
|
| boolean sendCompositionToNative(
|
| CharSequence text, int newCursorPosition, boolean isCommit, int unicodeFromKeyEvent) {
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
|
|
| // One WebView app detects Enter in JS by looking at KeyDown (http://crbug/577967).
|
| if (TextUtils.equals(text, "\n")) {
|
| @@ -577,13 +581,13 @@ public class ImeAdapter {
|
|
|
| @VisibleForTesting
|
| boolean finishComposingText() {
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
| nativeFinishComposingText(mNativeImeAdapterAndroid);
|
| return true;
|
| }
|
|
|
| boolean sendKeyEvent(KeyEvent event) {
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
|
|
| int action = event.getAction();
|
| int type;
|
| @@ -615,7 +619,7 @@ public class ImeAdapter {
|
| */
|
| boolean deleteSurroundingText(int beforeLength, int afterLength) {
|
| mViewEmbedder.onImeEvent();
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
| nativeSendKeyEvent(mNativeImeAdapterAndroid, null, WebInputEventType.RawKeyDown, 0,
|
| SystemClock.uptimeMillis(), COMPOSITION_KEY_CODE, 0, false, 0);
|
| nativeDeleteSurroundingText(mNativeImeAdapterAndroid, beforeLength, afterLength);
|
| @@ -634,7 +638,7 @@ public class ImeAdapter {
|
| */
|
| boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
|
| mViewEmbedder.onImeEvent();
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
| nativeSendKeyEvent(mNativeImeAdapterAndroid, null, WebInputEventType.RawKeyDown, 0,
|
| SystemClock.uptimeMillis(), COMPOSITION_KEY_CODE, 0, false, 0);
|
| nativeDeleteSurroundingTextInCodePoints(
|
| @@ -651,7 +655,7 @@ public class ImeAdapter {
|
| * @return Whether the native counterpart of ImeAdapter received the call.
|
| */
|
| boolean setEditableSelectionOffsets(int start, int end) {
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
| nativeSetEditableSelectionOffsets(mNativeImeAdapterAndroid, start, end);
|
| return true;
|
| }
|
| @@ -663,7 +667,7 @@ public class ImeAdapter {
|
| * @return Whether the native counterpart of ImeAdapter received the call.
|
| */
|
| boolean setComposingRegion(int start, int end) {
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
| if (start <= end) {
|
| nativeSetComposingRegion(mNativeImeAdapterAndroid, start, end);
|
| } else {
|
| @@ -690,7 +694,7 @@ public class ImeAdapter {
|
| * Send a request to the native counterpart to give the latest text input state update.
|
| */
|
| boolean requestTextInputStateUpdate() {
|
| - if (mNativeImeAdapterAndroid == 0) return false;
|
| + if (!isValid()) return false;
|
| // You won't get state update anyways.
|
| if (mInputConnection == null) return false;
|
| return nativeRequestTextInputStateUpdate(mNativeImeAdapterAndroid);
|
| @@ -705,7 +709,7 @@ public class ImeAdapter {
|
| final boolean monitorRequest =
|
| (cursorUpdateMode & InputConnection.CURSOR_UPDATE_MONITOR) != 0;
|
|
|
| - if (mNativeImeAdapterAndroid != 0) {
|
| + if (isValid()) {
|
| nativeRequestCursorUpdate(mNativeImeAdapterAndroid, immediateRequest, monitorRequest);
|
| }
|
| if (mCursorAnchorInfoController == null) return false;
|
| @@ -770,14 +774,15 @@ public class ImeAdapter {
|
| }
|
|
|
| @CalledByNative
|
| - private void detach() {
|
| - if (DEBUG_LOGS) Log.i(TAG, "detach");
|
| - mNativeImeAdapterAndroid = 0;
|
| - if (mCursorAnchorInfoController != null) {
|
| - mCursorAnchorInfoController.focusedNodeChanged(false);
|
| - }
|
| + private void onConnectedToRenderProcess() {
|
| + if (DEBUG_LOGS) Log.i(TAG, "onConnectedToRenderProcess");
|
| + mIsConnected = true;
|
| + createInputConnectionFactory();
|
| + resetAndHideKeyboard();
|
| }
|
|
|
| + private native long nativeInit();
|
| + private native void nativeDestroy(long nativeImeAdapterAndroid);
|
| private native boolean nativeSendKeyEvent(long nativeImeAdapterAndroid, KeyEvent event,
|
| int type, int modifiers, long timestampMs, int keyCode, int scanCode,
|
| boolean isSystemKey, int unicodeChar);
|
| @@ -789,7 +794,6 @@ public class ImeAdapter {
|
| private native void nativeCommitText(
|
| long nativeImeAdapterAndroid, CharSequence text, String textStr, int newCursorPosition);
|
| private native void nativeFinishComposingText(long nativeImeAdapterAndroid);
|
| - private native void nativeAttachImeAdapter(long nativeImeAdapterAndroid);
|
| private native void nativeSetEditableSelectionOffsets(long nativeImeAdapterAndroid,
|
| int start, int end);
|
| private native void nativeSetComposingRegion(long nativeImeAdapterAndroid, int start, int end);
|
| @@ -797,7 +801,6 @@ public class ImeAdapter {
|
| int before, int after);
|
| private native void nativeDeleteSurroundingTextInCodePoints(
|
| long nativeImeAdapterAndroid, int before, int after);
|
| - private native void nativeResetImeAdapter(long nativeImeAdapterAndroid);
|
| private native boolean nativeRequestTextInputStateUpdate(long nativeImeAdapterAndroid);
|
| private native void nativeRequestCursorUpdate(long nativeImeAdapterAndroid,
|
| boolean immediateRequest, boolean monitorRequest);
|
|
|