DO NOT MERGE: Move startInput for WINDOW_FOCUS_GAIN to background thread
For a typical window with Editor, IMM#startInputOrWindowGainedFocus
is called twice: first for WINDOW_FOCUS_GAIN (dummyInput), then
CHECK_FOCUS when actual editor receives focus and startsInput.
The first once can be moved to background thread and second startInput
will wait till its finished.
Bug: 139806621
Test: Manually launch activities with editors and confirm no change in
behavior.
Change-Id: I6aa4a664cfd0c86f75cee2457715317194bbe5e2
(cherry picked from commit e0172102b9)
This commit is contained in:
committed by
Taran Singh
parent
659f477f65
commit
4b6acb4f39
@@ -92,7 +92,10 @@ import java.util.Comparator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.CancellationException;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -421,6 +424,13 @@ public final class InputMethodManager {
|
|||||||
int mCursorCandStart;
|
int mCursorCandStart;
|
||||||
int mCursorCandEnd;
|
int mCursorCandEnd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initial startInput with {@link StartInputReason.WINDOW_FOCUS_GAIN} is executed
|
||||||
|
* in a background thread. Later, if there is an actual startInput it will wait on
|
||||||
|
* main thread till the background thread completes.
|
||||||
|
*/
|
||||||
|
private CompletableFuture<Void> mWindowFocusGainFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The instance that has previously been sent to the input method.
|
* The instance that has previously been sent to the input method.
|
||||||
*/
|
*/
|
||||||
@@ -1598,6 +1608,18 @@ public final class InputMethodManager {
|
|||||||
boolean startInputInner(@StartInputReason int startInputReason,
|
boolean startInputInner(@StartInputReason int startInputReason,
|
||||||
@Nullable IBinder windowGainingFocus, @StartInputFlags int startInputFlags,
|
@Nullable IBinder windowGainingFocus, @StartInputFlags int startInputFlags,
|
||||||
@SoftInputModeFlags int softInputMode, int windowFlags) {
|
@SoftInputModeFlags int softInputMode, int windowFlags) {
|
||||||
|
if (startInputReason != StartInputReason.WINDOW_FOCUS_GAIN
|
||||||
|
&& mWindowFocusGainFuture != null) {
|
||||||
|
try {
|
||||||
|
mWindowFocusGainFuture.get();
|
||||||
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
|
// do nothing
|
||||||
|
} catch (CancellationException e) {
|
||||||
|
// window no longer has focus.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final View view;
|
final View view;
|
||||||
synchronized (mH) {
|
synchronized (mH) {
|
||||||
view = mServedView;
|
view = mServedView;
|
||||||
@@ -1951,13 +1973,19 @@ public final class InputMethodManager {
|
|||||||
startInputFlags |= StartInputFlags.FIRST_WINDOW_FOCUS_GAIN;
|
startInputFlags |= StartInputFlags.FIRST_WINDOW_FOCUS_GAIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkFocusNoStartInput(forceNewFocus)) {
|
final boolean forceNewFocus1 = forceNewFocus;
|
||||||
|
final int startInputFlags1 = startInputFlags;
|
||||||
|
if (mWindowFocusGainFuture != null) {
|
||||||
|
mWindowFocusGainFuture.cancel(false/* mayInterruptIfRunning */);
|
||||||
|
}
|
||||||
|
mWindowFocusGainFuture = CompletableFuture.runAsync(() -> {
|
||||||
|
if (checkFocusNoStartInput(forceNewFocus1)) {
|
||||||
// We need to restart input on the current focus view. This
|
// We need to restart input on the current focus view. This
|
||||||
// should be done in conjunction with telling the system service
|
// should be done in conjunction with telling the system service
|
||||||
// about the window gaining focus, to help make the transition
|
// about the window gaining focus, to help make the transition
|
||||||
// smooth.
|
// smooth.
|
||||||
if (startInputInner(StartInputReason.WINDOW_FOCUS_GAIN, rootView.getWindowToken(),
|
if (startInputInner(StartInputReason.WINDOW_FOCUS_GAIN, rootView.getWindowToken(),
|
||||||
startInputFlags, softInputMode, windowFlags)) {
|
startInputFlags1, softInputMode, windowFlags)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1969,13 +1997,14 @@ public final class InputMethodManager {
|
|||||||
if (DEBUG) Log.v(TAG, "Reporting focus gain, without startInput");
|
if (DEBUG) Log.v(TAG, "Reporting focus gain, without startInput");
|
||||||
mService.startInputOrWindowGainedFocus(
|
mService.startInputOrWindowGainedFocus(
|
||||||
StartInputReason.WINDOW_FOCUS_GAIN_REPORT_ONLY, mClient,
|
StartInputReason.WINDOW_FOCUS_GAIN_REPORT_ONLY, mClient,
|
||||||
rootView.getWindowToken(), startInputFlags, softInputMode, windowFlags,
|
rootView.getWindowToken(), startInputFlags1, softInputMode, windowFlags,
|
||||||
null, null, 0 /* missingMethodFlags */,
|
null, null, 0 /* missingMethodFlags */,
|
||||||
rootView.getContext().getApplicationInfo().targetSdkVersion);
|
rootView.getContext().getApplicationInfo().targetSdkVersion);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw e.rethrowFromSystemServer();
|
throw e.rethrowFromSystemServer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
@@ -1990,6 +2019,10 @@ public final class InputMethodManager {
|
|||||||
// If the mCurRootView is losing window focus, release the strong reference to it
|
// If the mCurRootView is losing window focus, release the strong reference to it
|
||||||
// so as not to prevent it from being garbage-collected.
|
// so as not to prevent it from being garbage-collected.
|
||||||
mCurRootView = null;
|
mCurRootView = null;
|
||||||
|
if (mWindowFocusGainFuture != null) {
|
||||||
|
mWindowFocusGainFuture.cancel(false /* mayInterruptIfRunning */);
|
||||||
|
mWindowFocusGainFuture = null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.v(TAG, "Ignoring onPreWindowFocus()."
|
Log.v(TAG, "Ignoring onPreWindowFocus()."
|
||||||
|
|||||||
Reference in New Issue
Block a user