summaryrefslogtreecommitdiff
path: root/core/java/android/inputmethodservice/MultiClientInputMethodServiceDelegateImpl.java
diff options
context:
space:
mode:
authorYohei Yukawa <yukawa@google.com>2018-11-12 15:08:30 -0800
committerYohei Yukawa <yukawa@google.com>2018-11-12 15:20:20 -0800
commitbae5bea23cfac3769569a230b56ad85cdd000675 (patch)
tree2f65b6e6b91999e42b89d83b5ddd6fe84c7edd73 /core/java/android/inputmethodservice/MultiClientInputMethodServiceDelegateImpl.java
parentb2eb6bd683022f2bab48eaaebdf1afc19dd35681 (diff)
Introduce multi-client IME for special form factors
An advanced multi-display support is requested for certain Android form-factors so that user(s) can type text on each display at the same time without losing software keyboard focus in other displays. This is not possible in existing Android IMEs that are built on top of InputMethodService class, because the assumption that a single IME client can be focused at the same time was made before Android IME APIs were introduced in Android 1.5 and many public APIs in InputMethodService have already relied heavily on that assumption. Updating InputMethodService class to support multi-client scenario is, however, quite challenging because: 1. doing so would introduce an unacceptable amount of complexity into InputMethodService, which is already hard to maintain, 2. IME developers still need to update their implementation to be able to support parallel requests from multiple focused IME client, which may require non-trivial redesign in their side (e.g. input decoder, typing history database, ...), and 3. actual use cases for multi IME clients are expected to be evolved rapidly hence the new protocol is not yet stable and not yet ready to be exposed as public APIs. This is why a new type of IME needs to be designed and developed specifically for such special multi-display environments, rather than reusing existing InputMethodService public class. Note that there must be no behavior change unless multi-client IME is explicitly enabled with 'adb shell setprop', which requires root permission. See multi-client-ime.md for details. Fix: 114662040 Test: Manually verified as follows: 1. make -j MultiClientInputMethod 2. adb install -r $OUT/system/priv-app/MultiClientInputMethod/MultiClientInputMethod.apk 3. adb root 4. adb shell setprop persist.debug.multi_client_ime \ com.example.android.multiclientinputmethod/.MultiClientInputMethod 5. adb reboot 6. Try multiple text input scenario Change-Id: I41dfe854557b178d8af740bc2869c936fc88608b
Diffstat (limited to 'core/java/android/inputmethodservice/MultiClientInputMethodServiceDelegateImpl.java')
-rw-r--r--core/java/android/inputmethodservice/MultiClientInputMethodServiceDelegateImpl.java193
1 files changed, 193 insertions, 0 deletions
diff --git a/core/java/android/inputmethodservice/MultiClientInputMethodServiceDelegateImpl.java b/core/java/android/inputmethodservice/MultiClientInputMethodServiceDelegateImpl.java
new file mode 100644
index 000000000000..bbe3a7fe1b31
--- /dev/null
+++ b/core/java/android/inputmethodservice/MultiClientInputMethodServiceDelegateImpl.java
@@ -0,0 +1,193 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.inputmethodservice;
+
+import static java.lang.annotation.RetentionPolicy.SOURCE;
+
+import android.annotation.IntDef;
+import android.content.Context;
+import android.content.Intent;
+import android.os.IBinder;
+import android.os.Looper;
+import android.util.Log;
+import android.view.InputChannel;
+import android.view.KeyEvent;
+
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.inputmethod.IMultiClientInputMethod;
+import com.android.internal.inputmethod.IMultiClientInputMethodPrivilegedOperations;
+import com.android.internal.inputmethod.MultiClientInputMethodPrivilegedOperations;
+
+import java.lang.annotation.Retention;
+import java.lang.ref.WeakReference;
+
+final class MultiClientInputMethodServiceDelegateImpl {
+ private static final String TAG = "MultiClientInputMethodServiceDelegateImpl";
+
+ private final Object mLock = new Object();
+
+ @Retention(SOURCE)
+ @IntDef({InitializationPhase.INSTANTIATED,
+ InitializationPhase.ON_BIND_CALLED,
+ InitializationPhase.INITIALIZE_CALLED,
+ InitializationPhase.ON_UNBIND_CALLED,
+ InitializationPhase.ON_DESTROY_CALLED})
+ private @interface InitializationPhase {
+ int INSTANTIATED = 1;
+ int ON_BIND_CALLED = 2;
+ int INITIALIZE_CALLED = 3;
+ int ON_UNBIND_CALLED = 4;
+ int ON_DESTROY_CALLED = 5;
+ }
+
+ @GuardedBy("mLock")
+ @InitializationPhase
+ private int mInitializationPhase;
+
+ private final MultiClientInputMethodPrivilegedOperations mPrivOps =
+ new MultiClientInputMethodPrivilegedOperations();
+
+ private final MultiClientInputMethodServiceDelegate.ServiceCallback mServiceCallback;
+
+ private final Context mContext;
+
+ MultiClientInputMethodServiceDelegateImpl(Context context,
+ MultiClientInputMethodServiceDelegate.ServiceCallback serviceCallback) {
+ mInitializationPhase = InitializationPhase.INSTANTIATED;
+ mContext = context;
+ mServiceCallback = serviceCallback;
+ }
+
+ void onDestroy() {
+ synchronized (mLock) {
+ switch (mInitializationPhase) {
+ case InitializationPhase.INSTANTIATED:
+ case InitializationPhase.ON_UNBIND_CALLED:
+ mInitializationPhase = InitializationPhase.ON_DESTROY_CALLED;
+ break;
+ default:
+ Log.e(TAG, "unexpected state=" + mInitializationPhase);
+ break;
+ }
+ }
+ }
+
+ private static final class ServiceImpl extends IMultiClientInputMethod.Stub {
+ private final WeakReference<MultiClientInputMethodServiceDelegateImpl> mImpl;
+
+ ServiceImpl(MultiClientInputMethodServiceDelegateImpl service) {
+ mImpl = new WeakReference<>(service);
+ }
+
+ @Override
+ public void initialize(IMultiClientInputMethodPrivilegedOperations privOps) {
+ final MultiClientInputMethodServiceDelegateImpl service = mImpl.get();
+ if (service == null) {
+ return;
+ }
+ synchronized (service.mLock) {
+ switch (service.mInitializationPhase) {
+ case InitializationPhase.ON_BIND_CALLED:
+ service.mPrivOps.set(privOps);
+ service.mInitializationPhase = InitializationPhase.INITIALIZE_CALLED;
+ service.mServiceCallback.initialized();
+ break;
+ default:
+ Log.e(TAG, "unexpected state=" + service.mInitializationPhase);
+ break;
+ }
+ }
+ }
+
+ @Override
+ public void addClient(int clientId, int uid, int pid, int selfReportedDisplayId) {
+ final MultiClientInputMethodServiceDelegateImpl service = mImpl.get();
+ if (service == null) {
+ return;
+ }
+ service.mServiceCallback.addClient(clientId, uid, pid, selfReportedDisplayId);
+ }
+
+ @Override
+ public void removeClient(int clientId) {
+ final MultiClientInputMethodServiceDelegateImpl service = mImpl.get();
+ if (service == null) {
+ return;
+ }
+ service.mServiceCallback.removeClient(clientId);
+ }
+ }
+
+ IBinder onBind(Intent intent) {
+ synchronized (mLock) {
+ switch (mInitializationPhase) {
+ case InitializationPhase.INSTANTIATED:
+ mInitializationPhase = InitializationPhase.ON_BIND_CALLED;
+ return new ServiceImpl(this);
+ default:
+ Log.e(TAG, "unexpected state=" + mInitializationPhase);
+ break;
+ }
+ }
+ return null;
+ }
+
+ boolean onUnbind(Intent intent) {
+ synchronized (mLock) {
+ switch (mInitializationPhase) {
+ case InitializationPhase.ON_BIND_CALLED:
+ case InitializationPhase.INITIALIZE_CALLED:
+ mInitializationPhase = InitializationPhase.ON_UNBIND_CALLED;
+ mPrivOps.dispose();
+ break;
+ default:
+ Log.e(TAG, "unexpected state=" + mInitializationPhase);
+ break;
+ }
+ }
+ return false;
+ }
+
+ IBinder createInputMethodWindowToken(int displayId) {
+ return mPrivOps.createInputMethodWindowToken(displayId);
+ }
+
+ void acceptClient(int clientId,
+ MultiClientInputMethodServiceDelegate.ClientCallback clientCallback,
+ KeyEvent.DispatcherState dispatcherState, Looper looper) {
+ final InputChannel[] channels = InputChannel.openInputChannelPair("MSIMS-session");
+ final InputChannel writeChannel = channels[0];
+ final InputChannel readChannel = channels[1];
+ try {
+ final MultiClientInputMethodClientCallbackAdaptor callbackAdaptor =
+ new MultiClientInputMethodClientCallbackAdaptor(clientCallback, looper,
+ dispatcherState, readChannel);
+ mPrivOps.acceptClient(clientId, callbackAdaptor.createIInputMethodSession(),
+ callbackAdaptor.createIMultiClientInputMethodSession(), writeChannel);
+ } finally {
+ writeChannel.dispose();
+ }
+ }
+
+ void reportImeWindowTarget(int clientId, int targetWindowHandle, IBinder imeWindowToken) {
+ mPrivOps.reportImeWindowTarget(clientId, targetWindowHandle, imeWindowToken);
+ }
+
+ boolean isUidAllowedOnDisplay(int displayId, int uid) {
+ return mPrivOps.isUidAllowedOnDisplay(displayId, uid);
+ }
+}