Annotate threads for some IME handling methods
This is a preparation to work on Bug 36897707.
For instance, the reason why most of IME-related callbacks in
InputMethodService get called on the main thread is because
IInputMethodWrapper keeps forwarding incoming IPCs into the
main looper of the IME process as follows:
InputMethodManagerService (IMMS)
------
-> one-way binder IPCs over IInputMethod
------
-> IInputMethodWrapper (on the binder thread(s))
-> Handler (to dispatch tasks to main thread)
-> InputMethodImpl.* (on the main thread)
-> InputMethodService.* (on the main thread)
By adding explicit annotations such as @BinderThread and @MainThread
in relevant methods, this CL makes that kind of investigation much
easier than before.
Bug: 36897707
Test: compile
Change-Id: I8f9afe9a1986a9fa41fb66fdc64e8f0f67e45c2e
diff --git a/core/java/com/android/internal/view/InputConnectionWrapper.java b/core/java/com/android/internal/view/InputConnectionWrapper.java
index cc0ef75..2098ebd 100644
--- a/core/java/com/android/internal/view/InputConnectionWrapper.java
+++ b/core/java/com/android/internal/view/InputConnectionWrapper.java
@@ -16,6 +16,8 @@
package com.android.internal.view;
+import android.annotation.AnyThread;
+import android.annotation.BinderThread;
import android.annotation.NonNull;
import android.inputmethodservice.AbstractInputMethodService;
import android.os.Bundle;
@@ -67,6 +69,7 @@
* sequence number is set to a new integer. We use a sequence number so that replies that
* occur after a timeout has expired are not interpreted as replies to a later request.
*/
+ @AnyThread
private static InputContextCallback getInstance() {
synchronized (InputContextCallback.class) {
// Return sInstance if it's non-null, otherwise construct a new callback
@@ -90,6 +93,7 @@
/**
* Makes the given InputContextCallback available for use in the future.
*/
+ @AnyThread
private void dispose() {
synchronized (InputContextCallback.class) {
// If sInstance is non-null, just let this object be garbage-collected
@@ -102,7 +106,8 @@
}
}
}
-
+
+ @BinderThread
public void setTextBeforeCursor(CharSequence textBeforeCursor, int seq) {
synchronized (this) {
if (seq == mSeq) {
@@ -116,6 +121,7 @@
}
}
+ @BinderThread
public void setTextAfterCursor(CharSequence textAfterCursor, int seq) {
synchronized (this) {
if (seq == mSeq) {
@@ -129,6 +135,7 @@
}
}
+ @BinderThread
public void setSelectedText(CharSequence selectedText, int seq) {
synchronized (this) {
if (seq == mSeq) {
@@ -142,6 +149,7 @@
}
}
+ @BinderThread
public void setCursorCapsMode(int capsMode, int seq) {
synchronized (this) {
if (seq == mSeq) {
@@ -155,6 +163,7 @@
}
}
+ @BinderThread
public void setExtractedText(ExtractedText extractedText, int seq) {
synchronized (this) {
if (seq == mSeq) {
@@ -168,6 +177,7 @@
}
}
+ @BinderThread
public void setRequestUpdateCursorAnchorInfoResult(boolean result, int seq) {
synchronized (this) {
if (seq == mSeq) {
@@ -181,6 +191,7 @@
}
}
+ @BinderThread
public void setCommitContentResult(boolean result, int seq) {
synchronized (this) {
if (seq == mSeq) {
@@ -199,6 +210,7 @@
*
* <p>The caller must be synchronized on this callback object.
*/
+ @AnyThread
void waitForResultLocked() {
long startTime = SystemClock.uptimeMillis();
long endTime = startTime + MAX_WAIT_TIME_MILLIS;
@@ -225,6 +237,7 @@
mMissingMethods = missingMethods;
}
+ @AnyThread
public CharSequence getTextAfterCursor(int length, int flags) {
CharSequence value = null;
try {
@@ -242,7 +255,8 @@
}
return value;
}
-
+
+ @AnyThread
public CharSequence getTextBeforeCursor(int length, int flags) {
CharSequence value = null;
try {
@@ -261,6 +275,7 @@
return value;
}
+ @AnyThread
public CharSequence getSelectedText(int flags) {
if (isMethodMissing(MissingMethodFlags.GET_SELECTED_TEXT)) {
// This method is not implemented.
@@ -283,6 +298,7 @@
return value;
}
+ @AnyThread
public int getCursorCapsMode(int reqModes) {
int value = 0;
try {
@@ -301,6 +317,7 @@
return value;
}
+ @AnyThread
public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
ExtractedText value = null;
try {
@@ -318,7 +335,8 @@
}
return value;
}
-
+
+ @AnyThread
public boolean commitText(CharSequence text, int newCursorPosition) {
try {
mIInputContext.commitText(text, newCursorPosition);
@@ -328,6 +346,7 @@
}
}
+ @AnyThread
public boolean commitCompletion(CompletionInfo text) {
if (isMethodMissing(MissingMethodFlags.COMMIT_CORRECTION)) {
// This method is not implemented.
@@ -341,6 +360,7 @@
}
}
+ @AnyThread
public boolean commitCorrection(CorrectionInfo correctionInfo) {
try {
mIInputContext.commitCorrection(correctionInfo);
@@ -350,6 +370,7 @@
}
}
+ @AnyThread
public boolean setSelection(int start, int end) {
try {
mIInputContext.setSelection(start, end);
@@ -358,7 +379,8 @@
return false;
}
}
-
+
+ @AnyThread
public boolean performEditorAction(int actionCode) {
try {
mIInputContext.performEditorAction(actionCode);
@@ -367,7 +389,8 @@
return false;
}
}
-
+
+ @AnyThread
public boolean performContextMenuAction(int id) {
try {
mIInputContext.performContextMenuAction(id);
@@ -377,6 +400,7 @@
}
}
+ @AnyThread
public boolean setComposingRegion(int start, int end) {
if (isMethodMissing(MissingMethodFlags.SET_COMPOSING_REGION)) {
// This method is not implemented.
@@ -390,6 +414,7 @@
}
}
+ @AnyThread
public boolean setComposingText(CharSequence text, int newCursorPosition) {
try {
mIInputContext.setComposingText(text, newCursorPosition);
@@ -399,6 +424,7 @@
}
}
+ @AnyThread
public boolean finishComposingText() {
try {
mIInputContext.finishComposingText();
@@ -408,6 +434,7 @@
}
}
+ @AnyThread
public boolean beginBatchEdit() {
try {
mIInputContext.beginBatchEdit();
@@ -416,7 +443,8 @@
return false;
}
}
-
+
+ @AnyThread
public boolean endBatchEdit() {
try {
mIInputContext.endBatchEdit();
@@ -425,7 +453,8 @@
return false;
}
}
-
+
+ @AnyThread
public boolean sendKeyEvent(KeyEvent event) {
try {
mIInputContext.sendKeyEvent(event);
@@ -435,6 +464,7 @@
}
}
+ @AnyThread
public boolean clearMetaKeyStates(int states) {
try {
mIInputContext.clearMetaKeyStates(states);
@@ -444,6 +474,7 @@
}
}
+ @AnyThread
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
try {
mIInputContext.deleteSurroundingText(beforeLength, afterLength);
@@ -453,6 +484,7 @@
}
}
+ @AnyThread
public boolean deleteSurroundingTextInCodePoints(int beforeLength, int afterLength) {
if (isMethodMissing(MissingMethodFlags.DELETE_SURROUNDING_TEXT_IN_CODE_POINTS)) {
// This method is not implemented.
@@ -466,11 +498,13 @@
}
}
+ @AnyThread
public boolean reportFullscreenMode(boolean enabled) {
// Nothing should happen when called from input method.
return false;
}
+ @AnyThread
public boolean performPrivateCommand(String action, Bundle data) {
try {
mIInputContext.performPrivateCommand(action, data);
@@ -480,6 +514,7 @@
}
}
+ @AnyThread
public boolean requestCursorUpdates(int cursorUpdateMode) {
boolean result = false;
if (isMethodMissing(MissingMethodFlags.REQUEST_CURSOR_UPDATES)) {
@@ -502,15 +537,18 @@
return result;
}
+ @AnyThread
public Handler getHandler() {
// Nothing should happen when called from input method.
return null;
}
+ @AnyThread
public void closeConnection() {
// Nothing should happen when called from input method.
}
+ @AnyThread
public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) {
boolean result = false;
if (isMethodMissing(MissingMethodFlags.COMMIT_CONTENT)) {
@@ -542,10 +580,12 @@
return result;
}
+ @AnyThread
private boolean isMethodMissing(@MissingMethodFlags final int methodFlag) {
return (mMissingMethods & methodFlag) == methodFlag;
}
+ @AnyThread
@Override
public String toString() {
return "InputConnectionWrapper{idHash=#"