2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.inputmethodservice;
19 import com.android.internal.os.HandlerCaller;
20 import com.android.internal.os.SomeArgs;
21 import com.android.internal.view.IInputContext;
22 import com.android.internal.view.IInputMethod;
23 import com.android.internal.view.IInputMethodSession;
24 import com.android.internal.view.IInputSessionCallback;
25 import com.android.internal.view.InputConnectionWrapper;
27 import android.content.Context;
28 import android.content.pm.PackageManager;
29 import android.os.Binder;
30 import android.os.IBinder;
31 import android.os.Message;
32 import android.os.RemoteException;
33 import android.os.ResultReceiver;
34 import android.util.Log;
35 import android.view.InputChannel;
36 import android.view.inputmethod.EditorInfo;
37 import android.view.inputmethod.InputBinding;
38 import android.view.inputmethod.InputConnection;
39 import android.view.inputmethod.InputConnectionInspector;
40 import android.view.inputmethod.InputMethod;
41 import android.view.inputmethod.InputMethodSession;
42 import android.view.inputmethod.InputMethodSubtype;
44 import java.io.FileDescriptor;
45 import java.io.PrintWriter;
46 import java.lang.ref.WeakReference;
47 import java.util.concurrent.CountDownLatch;
48 import java.util.concurrent.TimeUnit;
51 * Implements the internal IInputMethod interface to convert incoming calls
52 * on to it back to calls on the public InputMethod interface, scheduling
53 * them on the main thread of the process.
55 class IInputMethodWrapper extends IInputMethod.Stub
56 implements HandlerCaller.Callback {
57 private static final String TAG = "InputMethodWrapper";
59 private static final int DO_DUMP = 1;
60 private static final int DO_ATTACH_TOKEN = 10;
61 private static final int DO_SET_INPUT_CONTEXT = 20;
62 private static final int DO_UNSET_INPUT_CONTEXT = 30;
63 private static final int DO_START_INPUT = 32;
64 private static final int DO_CREATE_SESSION = 40;
65 private static final int DO_SET_SESSION_ENABLED = 45;
66 private static final int DO_REVOKE_SESSION = 50;
67 private static final int DO_SHOW_SOFT_INPUT = 60;
68 private static final int DO_HIDE_SOFT_INPUT = 70;
69 private static final int DO_CHANGE_INPUTMETHOD_SUBTYPE = 80;
71 final WeakReference<AbstractInputMethodService> mTarget;
72 final Context mContext;
73 final HandlerCaller mCaller;
74 final WeakReference<InputMethod> mInputMethod;
75 final int mTargetSdkVersion;
77 static class Notifier {
81 // NOTE: we should have a cache of these.
82 static final class InputMethodSessionCallbackWrapper implements InputMethod.SessionCallback {
83 final Context mContext;
84 final InputChannel mChannel;
85 final IInputSessionCallback mCb;
87 InputMethodSessionCallbackWrapper(Context context, InputChannel channel,
88 IInputSessionCallback cb) {
95 public void sessionCreated(InputMethodSession session) {
97 if (session != null) {
98 IInputMethodSessionWrapper wrap =
99 new IInputMethodSessionWrapper(mContext, session, mChannel);
100 mCb.sessionCreated(wrap);
102 if (mChannel != null) {
105 mCb.sessionCreated(null);
107 } catch (RemoteException e) {
112 public IInputMethodWrapper(AbstractInputMethodService context,
113 InputMethod inputMethod) {
114 mTarget = new WeakReference<AbstractInputMethodService>(context);
115 mContext = context.getApplicationContext();
116 mCaller = new HandlerCaller(mContext, null, this, true /*asyncHandler*/);
117 mInputMethod = new WeakReference<InputMethod>(inputMethod);
118 mTargetSdkVersion = context.getApplicationInfo().targetSdkVersion;
121 public InputMethod getInternalInputMethod() {
122 return mInputMethod.get();
126 public void executeMessage(Message msg) {
127 InputMethod inputMethod = mInputMethod.get();
128 // Need a valid reference to the inputMethod for everything except a dump.
129 if (inputMethod == null && msg.what != DO_DUMP) {
130 Log.w(TAG, "Input method reference was null, ignoring message: " + msg.what);
136 AbstractInputMethodService target = mTarget.get();
137 if (target == null) {
140 SomeArgs args = (SomeArgs)msg.obj;
142 target.dump((FileDescriptor)args.arg1,
143 (PrintWriter)args.arg2, (String[])args.arg3);
144 } catch (RuntimeException e) {
145 ((PrintWriter)args.arg2).println("Exception: " + e);
147 synchronized (args.arg4) {
148 ((CountDownLatch)args.arg4).countDown();
154 case DO_ATTACH_TOKEN: {
155 inputMethod.attachToken((IBinder)msg.obj);
158 case DO_SET_INPUT_CONTEXT: {
159 inputMethod.bindInput((InputBinding)msg.obj);
162 case DO_UNSET_INPUT_CONTEXT:
163 inputMethod.unbindInput();
165 case DO_START_INPUT: {
166 final SomeArgs args = (SomeArgs) msg.obj;
167 final int missingMethods = msg.arg1;
168 final boolean restarting = msg.arg2 != 0;
169 final IInputContext inputContext = (IInputContext) args.arg1;
170 final EditorInfo info = (EditorInfo) args.arg2;
171 final InputConnection ic = inputContext != null
172 ? new InputConnectionWrapper(mTarget, inputContext, missingMethods) : null;
173 info.makeCompatible(mTargetSdkVersion);
175 inputMethod.restartInput(ic, info);
177 inputMethod.startInput(ic, info);
182 case DO_CREATE_SESSION: {
183 SomeArgs args = (SomeArgs)msg.obj;
184 inputMethod.createSession(new InputMethodSessionCallbackWrapper(
185 mContext, (InputChannel)args.arg1,
186 (IInputSessionCallback)args.arg2));
190 case DO_SET_SESSION_ENABLED:
191 inputMethod.setSessionEnabled((InputMethodSession)msg.obj,
194 case DO_REVOKE_SESSION:
195 inputMethod.revokeSession((InputMethodSession)msg.obj);
197 case DO_SHOW_SOFT_INPUT:
198 inputMethod.showSoftInput(msg.arg1, (ResultReceiver)msg.obj);
200 case DO_HIDE_SOFT_INPUT:
201 inputMethod.hideSoftInput(msg.arg1, (ResultReceiver)msg.obj);
203 case DO_CHANGE_INPUTMETHOD_SUBTYPE:
204 inputMethod.changeInputMethodSubtype((InputMethodSubtype)msg.obj);
207 Log.w(TAG, "Unhandled message code: " + msg.what);
211 protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
212 AbstractInputMethodService target = mTarget.get();
213 if (target == null) {
216 if (target.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
217 != PackageManager.PERMISSION_GRANTED) {
219 fout.println("Permission Denial: can't dump InputMethodManager from from pid="
220 + Binder.getCallingPid()
221 + ", uid=" + Binder.getCallingUid());
225 CountDownLatch latch = new CountDownLatch(1);
226 mCaller.executeOrSendMessage(mCaller.obtainMessageOOOO(DO_DUMP,
227 fd, fout, args, latch));
229 if (!latch.await(5, TimeUnit.SECONDS)) {
230 fout.println("Timeout waiting for dump");
232 } catch (InterruptedException e) {
233 fout.println("Interrupted waiting for dump");
238 public void attachToken(IBinder token) {
239 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_ATTACH_TOKEN, token));
243 public void bindInput(InputBinding binding) {
244 // This IInputContext is guaranteed to implement all the methods.
245 final int missingMethodFlags = 0;
246 InputConnection ic = new InputConnectionWrapper(mTarget,
247 IInputContext.Stub.asInterface(binding.getConnectionToken()), missingMethodFlags);
248 InputBinding nu = new InputBinding(ic, binding);
249 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_INPUT_CONTEXT, nu));
253 public void unbindInput() {
254 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_UNSET_INPUT_CONTEXT));
258 public void startInput(IInputContext inputContext,
259 @InputConnectionInspector.MissingMethodFlags final int missingMethods,
260 EditorInfo attribute, boolean restarting) {
261 mCaller.executeOrSendMessage(mCaller.obtainMessageIIOO(DO_START_INPUT,
262 missingMethods, restarting ? 1 : 0, inputContext, attribute));
266 public void createSession(InputChannel channel, IInputSessionCallback callback) {
267 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_CREATE_SESSION,
272 public void setSessionEnabled(IInputMethodSession session, boolean enabled) {
274 InputMethodSession ls = ((IInputMethodSessionWrapper)
275 session).getInternalInputMethodSession();
277 Log.w(TAG, "Session is already finished: " + session);
280 mCaller.executeOrSendMessage(mCaller.obtainMessageIO(
281 DO_SET_SESSION_ENABLED, enabled ? 1 : 0, ls));
282 } catch (ClassCastException e) {
283 Log.w(TAG, "Incoming session not of correct type: " + session, e);
288 public void revokeSession(IInputMethodSession session) {
290 InputMethodSession ls = ((IInputMethodSessionWrapper)
291 session).getInternalInputMethodSession();
293 Log.w(TAG, "Session is already finished: " + session);
296 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_REVOKE_SESSION, ls));
297 } catch (ClassCastException e) {
298 Log.w(TAG, "Incoming session not of correct type: " + session, e);
303 public void showSoftInput(int flags, ResultReceiver resultReceiver) {
304 mCaller.executeOrSendMessage(mCaller.obtainMessageIO(DO_SHOW_SOFT_INPUT,
305 flags, resultReceiver));
309 public void hideSoftInput(int flags, ResultReceiver resultReceiver) {
310 mCaller.executeOrSendMessage(mCaller.obtainMessageIO(DO_HIDE_SOFT_INPUT,
311 flags, resultReceiver));
315 public void changeInputMethodSubtype(InputMethodSubtype subtype) {
316 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_CHANGE_INPUTMETHOD_SUBTYPE,