Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Add a new API IMM#dispatchKeyEventFromInputMethod().
Browse files Browse the repository at this point in the history
This is a part of effort to reduce the number of dependencies on @hide
method in BaseInputConnection.

Currently BaseInputConnection#sendKeyEvent() cannot be implemented
without relying on @hide internal fields in InputMethodManager.

This is not ideal because app developers cannot implement their own
InputConnection without relying on BaseInputConnection.  If this
functionality needs to be exposed to app developers anyway, then it
should be a public API.

Bug: 24688781
Change-Id: Ib5ea8131de5ba792b10e1a4e51b4d163cf3649e3
  • Loading branch information
yukawa committed Jan 8, 2016
1 parent 159dd47 commit 2afe2aa
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions api/current.txt
Expand Up @@ -43492,6 +43492,7 @@ package android.view.inputmethod {
}

public final class InputMethodManager {
method public void dispatchKeyEventFromInputMethod(android.view.View, android.view.KeyEvent);
method public void displayCompletions(android.view.View, android.view.inputmethod.CompletionInfo[]);
method public android.view.inputmethod.InputMethodSubtype getCurrentInputMethodSubtype();
method public java.util.List<android.view.inputmethod.InputMethodInfo> getEnabledInputMethodList();
Expand Down
1 change: 1 addition & 0 deletions api/system-current.txt
Expand Up @@ -45842,6 +45842,7 @@ package android.view.inputmethod {
}

public final class InputMethodManager {
method public void dispatchKeyEventFromInputMethod(android.view.View, android.view.KeyEvent);
method public void displayCompletions(android.view.View, android.view.inputmethod.CompletionInfo[]);
method public android.view.inputmethod.InputMethodSubtype getCurrentInputMethodSubtype();
method public java.util.List<android.view.inputmethod.InputMethodInfo> getEnabledInputMethodList();
Expand Down
1 change: 1 addition & 0 deletions api/test-current.txt
Expand Up @@ -43508,6 +43508,7 @@ package android.view.inputmethod {
}

public final class InputMethodManager {
method public void dispatchKeyEventFromInputMethod(android.view.View, android.view.KeyEvent);
method public void displayCompletions(android.view.View, android.view.inputmethod.CompletionInfo[]);
method public android.view.inputmethod.InputMethodSubtype getCurrentInputMethodSubtype();
method public java.util.List<android.view.inputmethod.InputMethodInfo> getEnabledInputMethodList();
Expand Down
12 changes: 1 addition & 11 deletions core/java/android/view/inputmethod/BaseInputConnection.java
Expand Up @@ -514,17 +514,7 @@ public boolean setSelection(int start, int end) {
* attached to the input connection's view.
*/
public boolean sendKeyEvent(KeyEvent event) {
synchronized (mIMM.mH) {
ViewRootImpl viewRootImpl = mTargetView != null ? mTargetView.getViewRootImpl() : null;
if (viewRootImpl == null) {
if (mIMM.mServedView != null) {
viewRootImpl = mIMM.mServedView.getViewRootImpl();
}
}
if (viewRootImpl != null) {
viewRootImpl.dispatchKeyFromIme(event);
}
}
mIMM.dispatchKeyEventFromInputMethod(mTargetView, event);
return false;
}

Expand Down
30 changes: 30 additions & 0 deletions core/java/android/view/inputmethod/InputMethodManager.java
Expand Up @@ -25,6 +25,8 @@
import com.android.internal.view.InputBindResult;
import com.android.internal.view.InputMethodClient;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.content.Context;
import android.graphics.Rect;
Expand Down Expand Up @@ -1823,6 +1825,34 @@ public int dispatchInputEvent(InputEvent event, Object token,
return DISPATCH_NOT_HANDLED;
}

/**
* Provides the default implementation of {@link InputConnection#sendKeyEvent(KeyEvent)}, which
* is expected to dispatch an keyboard event sent from the IME to an appropriate event target
* depending on the given {@link View} and the current focus state.
*
* <p>CAUTION: This method is provided only for the situation where
* {@link InputConnection#sendKeyEvent(KeyEvent)} needs to be implemented without relying on
* {@link BaseInputConnection}. Do not use this API for anything else.</p>
*
* @param targetView the default target view. If {@code null} is specified, then this method
* tries to find a good event target based on the current focus state.
* @param event the key event to be dispatched.
*/
public void dispatchKeyEventFromInputMethod(@Nullable View targetView,
@NonNull KeyEvent event) {
synchronized (mH) {
ViewRootImpl viewRootImpl = targetView != null ? targetView.getViewRootImpl() : null;
if (viewRootImpl == null) {
if (mServedView != null) {
viewRootImpl = mServedView.getViewRootImpl();
}
}
if (viewRootImpl != null) {
viewRootImpl.dispatchKeyFromIme(event);
}
}
}

// Must be called on the main looper
void sendInputEventAndReportResultOnMainLooper(PendingEvent p) {
final boolean handled;
Expand Down

0 comments on commit 2afe2aa

Please sign in to comment.