am eb99b461: am 96db9222: am cabb66e9: follow-up to API change in I4a577bfd02b37b
* commit 'eb99b461f03a48317598289ab12ea30a617c537c': follow-up to API change in I4a577bfd02b37bmain
commit
bf55d9f0e6
|
@ -25,21 +25,12 @@ public final class ProductionFlags {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When true, enable {@link InputMethodService#onUpdateCursorAnchorInfo} callback via
|
* When true, enable {@link InputMethodService#onUpdateCursorAnchorInfo} callback via
|
||||||
* {@link InputConnection#requestCursorAnchorInfo}. This flag has no effect in API Level 20
|
* {@link InputConnection#requestUpdateCursorAnchorInfo}. This flag has no effect in API
|
||||||
* and prior. In general, this callback provides more detailed positional information,
|
* Level 20 and prior. In general, this callback provides detailed positional information,
|
||||||
* even though an explicit support is required by the editor.
|
* even though an explicit support is required by the editor.
|
||||||
*/
|
*/
|
||||||
public static final boolean ENABLE_CURSOR_ANCHOR_INFO_CALLBACK = true;
|
public static final boolean ENABLE_CURSOR_ANCHOR_INFO_CALLBACK = true;
|
||||||
|
|
||||||
/**
|
|
||||||
* When true, enable {@link InputMethodService#onUpdateCursor} callback via
|
|
||||||
* {@link InputConnection#requestCursorAnchorInfo}. Although this callback has been available
|
|
||||||
* since API Level 3, the callback has never been used until API Level 20. Thus it may or may
|
|
||||||
* not work well as expected. Should rely on {@link InputMethodService#onUpdateCursorAnchorInfo}
|
|
||||||
* whenever possible since it is supposed to be more reliable and accurate.
|
|
||||||
*/
|
|
||||||
public static final boolean ENABLE_CURSOR_RECT_CALLBACK = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Include all suggestions from all dictionaries in {@link SuggestedWords#mRawSuggestions}.
|
* Include all suggestions from all dictionaries in {@link SuggestedWords#mRawSuggestions}.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -16,67 +16,35 @@
|
||||||
|
|
||||||
package com.android.inputmethod.compat;
|
package com.android.inputmethod.compat;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.inputmethod.InputConnection;
|
import android.view.inputmethod.InputConnection;
|
||||||
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
public final class InputConnectionCompatUtils {
|
public final class InputConnectionCompatUtils {
|
||||||
private static final String TAG = InputConnectionCompatUtils.class.getSimpleName();
|
private static final CompatUtils.ClassWrapper sInputConnectionType;
|
||||||
|
private static final CompatUtils.ToBooleanMethodWrapper sRequestUpdateCursorAnchorInfoMethod;
|
||||||
// Note that CursorAnchorInfoRequest is supposed to be available in API level 21 and later.
|
|
||||||
private static Class<?> getCursorAnchorInfoRequestClass() {
|
|
||||||
try {
|
|
||||||
return Class.forName("android.view.inputmethod.CursorAnchorInfoRequest");
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final Class<?> TYPE_CursorAnchorInfoRequest;
|
|
||||||
private static final Constructor<?> CONSTRUCTOR_CursorAnchorInfoRequest;
|
|
||||||
private static final Method METHOD_requestCursorAnchorInfo;
|
|
||||||
static {
|
static {
|
||||||
TYPE_CursorAnchorInfoRequest = getCursorAnchorInfoRequestClass();
|
sInputConnectionType = new CompatUtils.ClassWrapper(InputConnection.class);
|
||||||
CONSTRUCTOR_CursorAnchorInfoRequest = CompatUtils.getConstructor(
|
sRequestUpdateCursorAnchorInfoMethod = sInputConnectionType.getPrimitiveMethod(
|
||||||
TYPE_CursorAnchorInfoRequest, int.class, int.class);
|
"requestUpdateCursorAnchorInfo", false, int.class);
|
||||||
METHOD_requestCursorAnchorInfo = CompatUtils.getMethod(InputConnection.class,
|
|
||||||
"requestCursorAnchorInfo", TYPE_CursorAnchorInfoRequest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isRequestCursorAnchorInfoAvailable() {
|
public static boolean isRequestUpdateCursorAnchorInfoAvailable() {
|
||||||
return METHOD_requestCursorAnchorInfo != null &&
|
return sRequestUpdateCursorAnchorInfoMethod != null;
|
||||||
CONSTRUCTOR_CursorAnchorInfoRequest != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local copies of some constants in CursorAnchorInfoRequest until the SDK becomes publicly
|
* Local copies of some constants in CursorAnchorInfoRequest until the SDK becomes publicly
|
||||||
* available.
|
* available.
|
||||||
*/
|
*/
|
||||||
private final static int RESULT_NOT_HANDLED = 0;
|
private static int REQUEST_UPDATE_CURSOR_UPDATE_IMMEDIATE = 1 << 0;
|
||||||
private final static int RESULT_SCHEDULED = 1;
|
private static int REQUEST_UPDATE_CURSOR_UPDATE_MONITOR = 1 << 1;
|
||||||
private final static int TYPE_CURSOR_ANCHOR_INFO = 1;
|
|
||||||
private final static int FLAG_CURSOR_ANCHOR_INFO_MONITOR = 1;
|
|
||||||
private final static int FLAG_CURSOR_ANCHOR_INFO_IMMEDIATE = 2;
|
|
||||||
private final static int TYPE_CURSOR_RECT = 2;
|
|
||||||
private final static int FLAG_CURSOR_RECT_MONITOR = 1;
|
|
||||||
private final static int FLAG_CURSOR_RECT_IN_SCREEN_COORDINATES = 2;
|
|
||||||
private final static int FLAG_CURSOR_RECT_WITH_VIEW_MATRIX = 4;
|
|
||||||
|
|
||||||
private static int requestCursorAnchorInfoImpl(final InputConnection inputConnection,
|
private static boolean requestUpdateCursorAnchorInfoImpl(final InputConnection inputConnection,
|
||||||
final int type, final int flags) {
|
final int cursorUpdateMode) {
|
||||||
if (!isRequestCursorAnchorInfoAvailable()) {
|
if (!isRequestUpdateCursorAnchorInfoAvailable()) {
|
||||||
return RESULT_NOT_HANDLED;
|
return false;
|
||||||
}
|
}
|
||||||
final Object requestObject = CompatUtils.newInstance(
|
return sRequestUpdateCursorAnchorInfoMethod.invoke(inputConnection, cursorUpdateMode);
|
||||||
CONSTRUCTOR_CursorAnchorInfoRequest, type, flags);
|
|
||||||
if (requestObject == null) {
|
|
||||||
return RESULT_NOT_HANDLED;
|
|
||||||
}
|
|
||||||
return (Integer) CompatUtils.invoke(inputConnection,
|
|
||||||
RESULT_NOT_HANDLED /* defaultValue */,
|
|
||||||
METHOD_requestCursorAnchorInfo, requestObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,47 +56,11 @@ public final class InputConnectionCompatUtils {
|
||||||
* as soon as possible to notify the current cursor/anchor position to the input method.
|
* as soon as possible to notify the current cursor/anchor position to the input method.
|
||||||
* @return {@code false} if the request is not handled. Otherwise returns {@code true}.
|
* @return {@code false} if the request is not handled. Otherwise returns {@code true}.
|
||||||
*/
|
*/
|
||||||
public static boolean requestCursorAnchorInfo(final InputConnection inputConnection,
|
public static boolean requestUpdateCursorAnchorInfo(final InputConnection inputConnection,
|
||||||
final boolean enableMonitor, final boolean requestImmediateCallback) {
|
final boolean enableMonitor, final boolean requestImmediateCallback) {
|
||||||
final int requestFlags = (enableMonitor ? FLAG_CURSOR_ANCHOR_INFO_MONITOR : 0)
|
final int cursorUpdateMode = (enableMonitor ? REQUEST_UPDATE_CURSOR_UPDATE_MONITOR : 0)
|
||||||
| (requestImmediateCallback ? FLAG_CURSOR_ANCHOR_INFO_IMMEDIATE : 0);
|
| (requestImmediateCallback ? REQUEST_UPDATE_CURSOR_UPDATE_IMMEDIATE : 0);
|
||||||
final int requestResult = requestCursorAnchorInfoImpl(inputConnection,
|
return requestUpdateCursorAnchorInfoImpl(inputConnection, cursorUpdateMode);
|
||||||
TYPE_CURSOR_ANCHOR_INFO, requestFlags);
|
|
||||||
switch (requestResult) {
|
|
||||||
case RESULT_NOT_HANDLED:
|
|
||||||
return false;
|
|
||||||
case RESULT_SCHEDULED:
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
Log.w(TAG, "requestCursorAnchorInfo returned unknown result=" + requestResult
|
|
||||||
+ " for type=TYPE_CURSOR_ANCHOR_INFO flags=" + requestFlags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Requests the editor to call back {@link InputMethodManager#updateCursor}.
|
|
||||||
* @param inputConnection the input connection to which the request is to be sent.
|
|
||||||
* @param enableMonitor {@code true} to request the editor to call back the method whenever the
|
|
||||||
* cursor position is changed.
|
|
||||||
* @return {@code false} if the request is not handled. Otherwise returns {@code true}.
|
|
||||||
*/
|
|
||||||
public static boolean requestCursorRect(final InputConnection inputConnection,
|
|
||||||
final boolean enableMonitor) {
|
|
||||||
final int requestFlags = enableMonitor ?
|
|
||||||
FLAG_CURSOR_RECT_MONITOR | FLAG_CURSOR_RECT_IN_SCREEN_COORDINATES |
|
|
||||||
FLAG_CURSOR_RECT_WITH_VIEW_MATRIX : 0;
|
|
||||||
final int requestResult = requestCursorAnchorInfoImpl(inputConnection, TYPE_CURSOR_RECT,
|
|
||||||
requestFlags);
|
|
||||||
switch (requestResult) {
|
|
||||||
case RESULT_NOT_HANDLED:
|
|
||||||
return false;
|
|
||||||
case RESULT_SCHEDULED:
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
Log.w(TAG, "requestCursorAnchorInfo returned unknown result=" + requestResult
|
|
||||||
+ " for type=TYPE_CURSOR_RECT flags=" + requestFlags);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -755,14 +755,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
|
||||||
|
|
||||||
private void onStartInputInternal(final EditorInfo editorInfo, final boolean restarting) {
|
private void onStartInputInternal(final EditorInfo editorInfo, final boolean restarting) {
|
||||||
super.onStartInput(editorInfo, restarting);
|
super.onStartInput(editorInfo, restarting);
|
||||||
if (ProductionFlags.ENABLE_CURSOR_RECT_CALLBACK) {
|
|
||||||
InputConnectionCompatUtils.requestCursorRect(getCurrentInputConnection(),
|
|
||||||
true /* enableMonitor */);
|
|
||||||
}
|
|
||||||
if (ProductionFlags.ENABLE_CURSOR_ANCHOR_INFO_CALLBACK) {
|
if (ProductionFlags.ENABLE_CURSOR_ANCHOR_INFO_CALLBACK) {
|
||||||
// AcceptTypedWord feature relies on CursorAnchorInfo.
|
// AcceptTypedWord feature relies on CursorAnchorInfo.
|
||||||
if (mSettings.getCurrent().mShouldShowUiToAcceptTypedWord) {
|
if (mSettings.getCurrent().mShouldShowUiToAcceptTypedWord) {
|
||||||
InputConnectionCompatUtils.requestCursorAnchorInfo(
|
InputConnectionCompatUtils.requestUpdateCursorAnchorInfo(
|
||||||
getCurrentInputConnection(), true /* enableMonitor */,
|
getCurrentInputConnection(), true /* enableMonitor */,
|
||||||
true /* requestImmediateCallback */);
|
true /* requestImmediateCallback */);
|
||||||
}
|
}
|
||||||
|
@ -971,14 +967,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onUpdateCursor(final Rect rect) {
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.i(TAG, "onUpdateCursor:" + rect.toShortString());
|
|
||||||
}
|
|
||||||
super.onUpdateCursor(rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
// We cannot mark this method as @Override until new SDK becomes publicly available.
|
// We cannot mark this method as @Override until new SDK becomes publicly available.
|
||||||
// @Override
|
// @Override
|
||||||
public void onUpdateCursorAnchorInfo(final CursorAnchorInfo info) {
|
public void onUpdateCursorAnchorInfo(final CursorAnchorInfo info) {
|
||||||
|
|
Loading…
Reference in New Issue