Skip to content

Commit bc700ad

Browse files
satok16Android (Google) Code Review
authored andcommitted
Merge "Fix a crash in InputMethodManager when switching the IME in the system process"
2 parents 3a136fc + 4e5184f commit bc700ad

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

core/java/android/view/inputmethod/InputMethodManager.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ public void handleMessage(Message msg) {
355355
}
356356
case MSG_BIND: {
357357
final InputBindResult res = (InputBindResult)msg.obj;
358+
if (DEBUG) {
359+
Log.i(TAG, "handleMessage: MSG_BIND " + res.sequence + "," + res.id);
360+
}
358361
synchronized (mH) {
359362
if (mBindSequence < 0 || mBindSequence != res.sequence) {
360363
Log.w(TAG, "Ignoring onBind: cur seq=" + mBindSequence
@@ -371,6 +374,9 @@ public void handleMessage(Message msg) {
371374
}
372375
case MSG_UNBIND: {
373376
final int sequence = msg.arg1;
377+
if (DEBUG) {
378+
Log.i(TAG, "handleMessage: MSG_UNBIND " + sequence);
379+
}
374380
boolean startInput = false;
375381
synchronized (mH) {
376382
if (mBindSequence == sequence) {
@@ -403,6 +409,9 @@ public void handleMessage(Message msg) {
403409
}
404410
case MSG_SET_ACTIVE: {
405411
final boolean active = msg.arg1 != 0;
412+
if (DEBUG) {
413+
Log.i(TAG, "handleMessage: MSG_SET_ACTIVE " + active + ", was " + mActive);
414+
}
406415
synchronized (mH) {
407416
mActive = active;
408417
mFullscreenMode = false;
@@ -420,7 +429,16 @@ public void handleMessage(Message msg) {
420429
// Check focus again in case that "onWindowFocus" is called before
421430
// handling this message.
422431
if (mServedView != null && mServedView.hasWindowFocus()) {
423-
checkFocus(mHasBeenInactive);
432+
// "finishComposingText" has been already called above. So we
433+
// should not call mServedInputConnection.finishComposingText here.
434+
// Also, please note that this handler thread could be different
435+
// from a thread that created mServedView. That could happen
436+
// the current activity is running in the system process.
437+
// In that case, we really should not call
438+
// mServedInputConnection.finishComposingText.
439+
if (checkFocusNoStartInput(mHasBeenInactive, false)) {
440+
startInputInner(null, 0, 0, 0);
441+
}
424442
}
425443
}
426444
}
@@ -1231,20 +1249,16 @@ static void scheduleCheckFocusLocked(View view) {
12311249
}
12321250
}
12331251

1234-
private void checkFocus(boolean forceNewFocus) {
1235-
if (checkFocusNoStartInput(forceNewFocus)) {
1236-
startInputInner(null, 0, 0, 0);
1237-
}
1238-
}
1239-
12401252
/**
12411253
* @hide
12421254
*/
12431255
public void checkFocus() {
1244-
checkFocus(false);
1256+
if (checkFocusNoStartInput(false, true)) {
1257+
startInputInner(null, 0, 0, 0);
1258+
}
12451259
}
12461260

1247-
private boolean checkFocusNoStartInput(boolean forceNewFocus) {
1261+
private boolean checkFocusNoStartInput(boolean forceNewFocus, boolean finishComposingText) {
12481262
// This is called a lot, so short-circuit before locking.
12491263
if (mServedView == mNextServedView && !forceNewFocus) {
12501264
return false;
@@ -1278,7 +1292,7 @@ private boolean checkFocusNoStartInput(boolean forceNewFocus) {
12781292
mServedConnecting = true;
12791293
}
12801294

1281-
if (ic != null) {
1295+
if (finishComposingText && ic != null) {
12821296
ic.finishComposingText();
12831297
}
12841298

@@ -1323,7 +1337,7 @@ public void onWindowFocus(View rootView, View focusedView, int softInputMode,
13231337
controlFlags |= CONTROL_WINDOW_FIRST;
13241338
}
13251339

1326-
if (checkFocusNoStartInput(forceNewFocus)) {
1340+
if (checkFocusNoStartInput(forceNewFocus, true)) {
13271341
// We need to restart input on the current focus view. This
13281342
// should be done in conjunction with telling the system service
13291343
// about the window gaining focus, to help make the transition

0 commit comments

Comments
 (0)