diff --git a/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/ClientCallbackImpl.java b/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/ClientCallbackImpl.java index 0b7e7acd1..cf805bfbc 100644 --- a/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/ClientCallbackImpl.java +++ b/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/ClientCallbackImpl.java @@ -41,10 +41,13 @@ final class ClientCallbackImpl implements MultiClientInputMethodServiceDelegate. private final int mSelfReportedDisplayId; private final KeyEvent.DispatcherState mDispatcherState; private final Looper mLooper; + private final MultiClientInputMethod mInputMethod; - ClientCallbackImpl(MultiClientInputMethodServiceDelegate delegate, + ClientCallbackImpl(MultiClientInputMethod inputMethod, + MultiClientInputMethodServiceDelegate delegate, SoftInputWindowManager softInputWindowManager, int clientId, int uid, int pid, int selfReportedDisplayId) { + mInputMethod = inputMethod; mDelegate = delegate; mSoftInputWindowManager = softInputWindowManager; mClientId = clientId; @@ -53,7 +56,9 @@ final class ClientCallbackImpl implements MultiClientInputMethodServiceDelegate. mSelfReportedDisplayId = selfReportedDisplayId; mDispatcherState = new KeyEvent.DispatcherState(); // For simplicity, we use the main looper for this sample. - // To use other looper thread, make sure that the IME Window also runs on the same looper. + // To use other looper thread, make sure that the IME Window also runs on the same looper + // and introduce an appropriate synchronization mechanism instead of directly accessing + // MultiClientInputMethod#mLastClientId. mLooper = Looper.getMainLooper(); } @@ -154,8 +159,8 @@ final class ClientCallbackImpl implements MultiClientInputMethodServiceDelegate. } if (inputConnection == null || editorInfo == null) { // deactivate previous client. - if (MultiClientInputMethod.sLastClientId != mClientId) { - mDelegate.setActive(MultiClientInputMethod.sLastClientId, false /* active */); + if (mInputMethod.mLastClientId != mClientId) { + mDelegate.setActive(mInputMethod.mLastClientId, false /* active */); } // Dummy InputConnection case. if (window.getClientId() == mClientId) { @@ -166,7 +171,7 @@ final class ClientCallbackImpl implements MultiClientInputMethodServiceDelegate. window.onDummyStartInput(mClientId, targetWindowHandle); } } else { - if (MultiClientInputMethod.sLastClientId != mClientId) { + if (mInputMethod.mLastClientId != mClientId) { mDelegate.setActive(mClientId, true /* active */); } window.onStartInput(mClientId, targetWindowHandle, inputConnection); @@ -190,7 +195,7 @@ final class ClientCallbackImpl implements MultiClientInputMethodServiceDelegate. window.hide(); break; } - MultiClientInputMethod.sLastClientId = mClientId; + mInputMethod.mLastClientId = mClientId; } @Override diff --git a/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/MultiClientInputMethod.java b/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/MultiClientInputMethod.java index bf468e262..5beee7e49 100644 --- a/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/MultiClientInputMethod.java +++ b/samples/MultiClientInputMethod/src/com/example/android/multiclientinputmethod/MultiClientInputMethod.java @@ -28,8 +28,9 @@ import android.util.Log; public final class MultiClientInputMethod extends Service { private static final String TAG = "MultiClientInputMethod"; private static final boolean DEBUG = false; - static int sLastClientId = MultiClientInputMethodServiceDelegate.INVALID_CLIENT_ID; + // last client that had active InputConnection. + int mLastClientId = MultiClientInputMethodServiceDelegate.INVALID_CLIENT_ID; SoftInputWindowManager mSoftInputWindowManager; MultiClientInputMethodServiceDelegate mDelegate; @@ -50,7 +51,8 @@ public final class MultiClientInputMethod extends Service { @Override public void addClient(int clientId, int uid, int pid, int selfReportedDisplayId) { - final ClientCallbackImpl callback = new ClientCallbackImpl(mDelegate, + final ClientCallbackImpl callback = new ClientCallbackImpl( + MultiClientInputMethod.this, mDelegate, mSoftInputWindowManager, clientId, uid, pid, selfReportedDisplayId); if (DEBUG) { Log.v(TAG, "addClient clientId=" + clientId + " uid=" + uid