Split binding main and visible connection

Bug: 205676419
Test: make
Change-Id: I5b80604c52dec6fbfcdf3214a915f8281ec0a73b
This commit is contained in:
Nikolas Havrikov
2021-11-11 18:26:51 +01:00
parent 5c232e4970
commit 4deaaf588d
2 changed files with 68 additions and 55 deletions

View File

@@ -27,6 +27,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManagerInternal;
import android.content.res.Resources;
import android.inputmethodservice.InputMethodService;
import android.os.IBinder;
import android.os.Process;
import android.os.SystemClock;
@@ -53,6 +55,7 @@ final class InputMethodBindingController {
@NonNull private final ArrayMap<String, InputMethodInfo> mMethodMap;
@NonNull private final InputMethodUtils.InputMethodSettings mSettings;
@NonNull private final PackageManagerInternal mPackageManagerInternal;
@NonNull private final Resources mRes;
private long mLastBindTime;
private boolean mHasConnection;
@@ -65,6 +68,41 @@ final class InputMethodBindingController {
private int mCurSeq;
private boolean mVisibleBound;
/**
* Binding flags for establishing connection to the {@link InputMethodService}.
*/
private static final int IME_CONNECTION_BIND_FLAGS =
Context.BIND_AUTO_CREATE
| Context.BIND_NOT_VISIBLE
| Context.BIND_NOT_FOREGROUND
| Context.BIND_IMPORTANT_BACKGROUND;
/**
* Binding flags for establishing connection to the {@link InputMethodService} when
* config_killableInputMethods is enabled.
*/
private static final int IME_CONNECTION_LOW_PRIORITY_BIND_FLAGS =
Context.BIND_AUTO_CREATE
| Context.BIND_REDUCTION_FLAGS;
/**
* Binding flags used only while the {@link InputMethodService} is showing window.
*/
private static final int IME_VISIBLE_BIND_FLAGS =
Context.BIND_AUTO_CREATE
| Context.BIND_TREAT_LIKE_ACTIVITY
| Context.BIND_FOREGROUND_SERVICE
| Context.BIND_INCLUDE_CAPABILITIES
| Context.BIND_SHOWING_UI
| Context.BIND_SCHEDULE_LIKE_TOP_APP;
/**
* Binding flags for establishing connection to the {@link InputMethodService}.
*
* <p>
* This defaults to {@link InputMethodBindingController#IME_CONNECTION_BIND_FLAGS} unless
* config_killableInputMethods is enabled, in which case this takes the value of
* {@link InputMethodBindingController#IME_CONNECTION_LOW_PRIORITY_BIND_FLAGS}.
*/
private final int mImeConnectionBindFlags;
InputMethodBindingController(@NonNull InputMethodManagerService service) {
mService = service;
@@ -72,6 +110,18 @@ final class InputMethodBindingController {
mMethodMap = mService.mMethodMap;
mSettings = mService.mSettings;
mPackageManagerInternal = mService.mPackageManagerInternal;
mRes = mService.mRes;
// If configured, use low priority flags to make the IME killable by the lowmemorykiller
final boolean lowerIMEPriority = mRes.getBoolean(
com.android.internal.R.bool.config_killableInputMethods);
if (lowerIMEPriority) {
mImeConnectionBindFlags =
InputMethodBindingController.IME_CONNECTION_LOW_PRIORITY_BIND_FLAGS;
} else {
mImeConnectionBindFlags = InputMethodBindingController.IME_CONNECTION_BIND_FLAGS;
}
}
/**
@@ -344,7 +394,7 @@ final class InputMethodBindingController {
}
@GuardedBy("mMethodMap")
boolean bindCurrentInputMethodServiceLocked(ServiceConnection conn, int flags) {
private boolean bindCurrentInputMethodServiceLocked(ServiceConnection conn, int flags) {
if (mCurIntent == null || conn == null) {
Slog.e(TAG, "--- bind failed: service = " + mCurIntent + ", conn = " + conn);
return false;
@@ -353,4 +403,18 @@ final class InputMethodBindingController {
new UserHandle(mSettings.getCurrentUserId()));
}
@GuardedBy("mMethodMap")
boolean bindCurrentInputMethodServiceVisibleConnectionLocked() {
return bindCurrentInputMethodServiceLocked(mVisibleConnection,
IME_VISIBLE_BIND_FLAGS);
}
@GuardedBy("mMethodMap")
boolean bindCurrentInputMethodServiceMainConnectionLocked() {
return bindCurrentInputMethodServiceLocked(mMainConnection,
mImeConnectionBindFlags);
}
}

View File

@@ -259,44 +259,6 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
private static final String HANDLER_THREAD_NAME = "android.imms";
/**
* Binding flags for establishing connection to the {@link InputMethodService}.
*/
private static final int IME_CONNECTION_BIND_FLAGS =
Context.BIND_AUTO_CREATE
| Context.BIND_NOT_VISIBLE
| Context.BIND_NOT_FOREGROUND
| Context.BIND_IMPORTANT_BACKGROUND;
/**
* Binding flags for establishing connection to the {@link InputMethodService} when
* config_killableInputMethods is enabled.
*/
private static final int IME_CONNECTION_LOW_PRIORITY_BIND_FLAGS =
Context.BIND_AUTO_CREATE
| Context.BIND_REDUCTION_FLAGS;
/**
* Binding flags for establishing connection to the {@link InputMethodService}.
*
* <p>
* This defaults to {@link #IME_CONNECTION_BIND_FLAGS} unless config_killableInputMethods is
* enabled, in which case this takes the value of
* {@link #IME_CONNECTION_LOW_PRIORITY_BIND_FLAGS}.
*/
private final int mImeConnectionBindFlags;
/**
* Binding flags used only while the {@link InputMethodService} is showing window.
*/
private static final int IME_VISIBLE_BIND_FLAGS =
Context.BIND_AUTO_CREATE
| Context.BIND_TREAT_LIKE_ACTIVITY
| Context.BIND_FOREGROUND_SERVICE
| Context.BIND_INCLUDE_CAPABILITIES
| Context.BIND_SHOWING_UI
| Context.BIND_SCHEDULE_LIKE_TOP_APP;
/**
* A protected broadcast intent action for internal use for {@link PendingIntent} in
* the notification.
@@ -1752,16 +1714,6 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
mSettings, context);
mMenuController = new InputMethodMenuController(this);
mBindingController = new InputMethodBindingController(this);
// If configured, use low priority flags to make the IME killable by the lowmemorykiller
final boolean lowerIMEPriority = mRes.getBoolean(
com.android.internal.R.bool.config_killableInputMethods);
if (lowerIMEPriority) {
mImeConnectionBindFlags = IME_CONNECTION_LOW_PRIORITY_BIND_FLAGS;
} else {
mImeConnectionBindFlags = IME_CONNECTION_BIND_FLAGS;
}
}
@GuardedBy("mMethodMap")
@@ -2500,8 +2452,7 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
Intent intent = createImeBindingIntent(info.getComponent());
setCurIntent(intent);
if (mBindingController.bindCurrentInputMethodServiceLocked(intent, getMainConnection(),
mImeConnectionBindFlags)) {
if (mBindingController.bindCurrentInputMethodServiceMainConnectionLocked()) {
addFreshWindowTokenLocked(displayIdToShowIme, info.getId());
return new InputBindResult(
InputBindResult.ResultCode.SUCCESS_WAITING_IME_BINDING,
@@ -3200,8 +3151,7 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
showInputToken));
mInputShown = true;
if (hasConnection() && !isVisibleBound()) {
mBindingController.bindCurrentInputMethodServiceLocked(getVisibleConnection(),
IME_VISIBLE_BIND_FLAGS);
mBindingController.bindCurrentInputMethodServiceVisibleConnectionLocked();
setVisibleBound(true);
}
res = true;
@@ -3217,8 +3167,7 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
ServiceConnection connection = getMainConnection();
mContext.unbindService(connection);
mBindingController.bindCurrentInputMethodServiceLocked(connection,
mImeConnectionBindFlags);
mBindingController.bindCurrentInputMethodServiceMainConnectionLocked();
} else {
if (DEBUG) {
Slog.d(TAG, "Can't show input: connection = " + hasConnection() + ", time = "