Home | History | Annotate | Download | only in inputmethodservice
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 package android.inputmethodservice;
     18 
     19 import com.android.internal.os.HandlerCaller;
     20 import com.android.internal.view.IInputContext;
     21 import com.android.internal.view.IInputMethod;
     22 import com.android.internal.view.IInputMethodCallback;
     23 import com.android.internal.view.IInputMethodSession;
     24 import com.android.internal.view.InputConnectionWrapper;
     25 
     26 import android.content.Context;
     27 import android.content.pm.PackageManager;
     28 import android.os.Binder;
     29 import android.os.IBinder;
     30 import android.os.Message;
     31 import android.os.RemoteException;
     32 import android.os.ResultReceiver;
     33 import android.util.Log;
     34 import android.view.inputmethod.EditorInfo;
     35 import android.view.inputmethod.InputBinding;
     36 import android.view.inputmethod.InputConnection;
     37 import android.view.inputmethod.InputMethod;
     38 import android.view.inputmethod.InputMethodSession;
     39 
     40 import java.io.FileDescriptor;
     41 import java.io.PrintWriter;
     42 import java.util.concurrent.CountDownLatch;
     43 import java.util.concurrent.TimeUnit;
     44 
     45 /**
     46  * Implements the internal IInputMethod interface to convert incoming calls
     47  * on to it back to calls on the public InputMethod interface, scheduling
     48  * them on the main thread of the process.
     49  */
     50 class IInputMethodWrapper extends IInputMethod.Stub
     51         implements HandlerCaller.Callback {
     52     private static final String TAG = "InputMethodWrapper";
     53     private static final boolean DEBUG = false;
     54 
     55     private static final int DO_DUMP = 1;
     56     private static final int DO_ATTACH_TOKEN = 10;
     57     private static final int DO_SET_INPUT_CONTEXT = 20;
     58     private static final int DO_UNSET_INPUT_CONTEXT = 30;
     59     private static final int DO_START_INPUT = 32;
     60     private static final int DO_RESTART_INPUT = 34;
     61     private static final int DO_CREATE_SESSION = 40;
     62     private static final int DO_SET_SESSION_ENABLED = 45;
     63     private static final int DO_REVOKE_SESSION = 50;
     64     private static final int DO_SHOW_SOFT_INPUT = 60;
     65     private static final int DO_HIDE_SOFT_INPUT = 70;
     66 
     67     final AbstractInputMethodService mTarget;
     68     final HandlerCaller mCaller;
     69     final InputMethod mInputMethod;
     70 
     71     static class Notifier {
     72         boolean notified;
     73     }
     74 
     75     // NOTE: we should have a cache of these.
     76     static class InputMethodSessionCallbackWrapper implements InputMethod.SessionCallback {
     77         final Context mContext;
     78         final IInputMethodCallback mCb;
     79         InputMethodSessionCallbackWrapper(Context context, IInputMethodCallback cb) {
     80             mContext = context;
     81             mCb = cb;
     82         }
     83         public void sessionCreated(InputMethodSession session) {
     84             try {
     85                 if (session != null) {
     86                     IInputMethodSessionWrapper wrap =
     87                             new IInputMethodSessionWrapper(mContext, session);
     88                     mCb.sessionCreated(wrap);
     89                 } else {
     90                     mCb.sessionCreated(null);
     91                 }
     92             } catch (RemoteException e) {
     93             }
     94         }
     95     }
     96 
     97     public IInputMethodWrapper(AbstractInputMethodService context,
     98             InputMethod inputMethod) {
     99         mTarget = context;
    100         mCaller = new HandlerCaller(context, this);
    101         mInputMethod = inputMethod;
    102     }
    103 
    104     public InputMethod getInternalInputMethod() {
    105         return mInputMethod;
    106     }
    107 
    108     public void executeMessage(Message msg) {
    109         switch (msg.what) {
    110             case DO_DUMP: {
    111                 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
    112                 try {
    113                     mTarget.dump((FileDescriptor)args.arg1,
    114                             (PrintWriter)args.arg2, (String[])args.arg3);
    115                 } catch (RuntimeException e) {
    116                     ((PrintWriter)args.arg2).println("Exception: " + e);
    117                 }
    118                 synchronized (args.arg4) {
    119                     ((CountDownLatch)args.arg4).countDown();
    120                 }
    121                 return;
    122             }
    123 
    124             case DO_ATTACH_TOKEN: {
    125                 mInputMethod.attachToken((IBinder)msg.obj);
    126                 return;
    127             }
    128             case DO_SET_INPUT_CONTEXT: {
    129                 mInputMethod.bindInput((InputBinding)msg.obj);
    130                 return;
    131             }
    132             case DO_UNSET_INPUT_CONTEXT:
    133                 mInputMethod.unbindInput();
    134                 return;
    135             case DO_START_INPUT: {
    136                 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
    137                 IInputContext inputContext = (IInputContext)args.arg1;
    138                 InputConnection ic = inputContext != null
    139                         ? new InputConnectionWrapper(inputContext) : null;
    140                 mInputMethod.startInput(ic, (EditorInfo)args.arg2);
    141                 return;
    142             }
    143             case DO_RESTART_INPUT: {
    144                 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
    145                 IInputContext inputContext = (IInputContext)args.arg1;
    146                 InputConnection ic = inputContext != null
    147                         ? new InputConnectionWrapper(inputContext) : null;
    148                 mInputMethod.restartInput(ic, (EditorInfo)args.arg2);
    149                 return;
    150             }
    151             case DO_CREATE_SESSION: {
    152                 mInputMethod.createSession(new InputMethodSessionCallbackWrapper(
    153                         mCaller.mContext, (IInputMethodCallback)msg.obj));
    154                 return;
    155             }
    156             case DO_SET_SESSION_ENABLED:
    157                 mInputMethod.setSessionEnabled((InputMethodSession)msg.obj,
    158                         msg.arg1 != 0);
    159                 return;
    160             case DO_REVOKE_SESSION:
    161                 mInputMethod.revokeSession((InputMethodSession)msg.obj);
    162                 return;
    163             case DO_SHOW_SOFT_INPUT:
    164                 mInputMethod.showSoftInput(msg.arg1, (ResultReceiver)msg.obj);
    165                 return;
    166             case DO_HIDE_SOFT_INPUT:
    167                 mInputMethod.hideSoftInput(msg.arg1, (ResultReceiver)msg.obj);
    168                 return;
    169         }
    170         Log.w(TAG, "Unhandled message code: " + msg.what);
    171     }
    172 
    173     @Override protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
    174         if (mTarget.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
    175                 != PackageManager.PERMISSION_GRANTED) {
    176 
    177             fout.println("Permission Denial: can't dump InputMethodManager from from pid="
    178                     + Binder.getCallingPid()
    179                     + ", uid=" + Binder.getCallingUid());
    180             return;
    181         }
    182 
    183         CountDownLatch latch = new CountDownLatch(1);
    184         mCaller.executeOrSendMessage(mCaller.obtainMessageOOOO(DO_DUMP,
    185                 fd, fout, args, latch));
    186         try {
    187             if (!latch.await(5, TimeUnit.SECONDS)) {
    188                 fout.println("Timeout waiting for dump");
    189             }
    190         } catch (InterruptedException e) {
    191             fout.println("Interrupted waiting for dump");
    192         }
    193     }
    194 
    195     public void attachToken(IBinder token) {
    196         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_ATTACH_TOKEN, token));
    197     }
    198 
    199     public void bindInput(InputBinding binding) {
    200         InputConnection ic = new InputConnectionWrapper(
    201                 IInputContext.Stub.asInterface(binding.getConnectionToken()));
    202         InputBinding nu = new InputBinding(ic, binding);
    203         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_INPUT_CONTEXT, nu));
    204     }
    205 
    206     public void unbindInput() {
    207         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_UNSET_INPUT_CONTEXT));
    208     }
    209 
    210     public void startInput(IInputContext inputContext, EditorInfo attribute) {
    211         mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_START_INPUT,
    212                 inputContext, attribute));
    213     }
    214 
    215     public void restartInput(IInputContext inputContext, EditorInfo attribute) {
    216         mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_RESTART_INPUT,
    217                 inputContext, attribute));
    218     }
    219 
    220     public void createSession(IInputMethodCallback callback) {
    221         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_CREATE_SESSION, callback));
    222     }
    223 
    224     public void setSessionEnabled(IInputMethodSession session, boolean enabled) {
    225         try {
    226             InputMethodSession ls = ((IInputMethodSessionWrapper)
    227                     session).getInternalInputMethodSession();
    228             mCaller.executeOrSendMessage(mCaller.obtainMessageIO(
    229                     DO_SET_SESSION_ENABLED, enabled ? 1 : 0, ls));
    230         } catch (ClassCastException e) {
    231             Log.w(TAG, "Incoming session not of correct type: " + session, e);
    232         }
    233     }
    234 
    235     public void revokeSession(IInputMethodSession session) {
    236         try {
    237             InputMethodSession ls = ((IInputMethodSessionWrapper)
    238                     session).getInternalInputMethodSession();
    239             mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_REVOKE_SESSION, ls));
    240         } catch (ClassCastException e) {
    241             Log.w(TAG, "Incoming session not of correct type: " + session, e);
    242         }
    243     }
    244 
    245     public void showSoftInput(int flags, ResultReceiver resultReceiver) {
    246         mCaller.executeOrSendMessage(mCaller.obtainMessageIO(DO_SHOW_SOFT_INPUT,
    247                 flags, resultReceiver));
    248     }
    249 
    250     public void hideSoftInput(int flags, ResultReceiver resultReceiver) {
    251         mCaller.executeOrSendMessage(mCaller.obtainMessageIO(DO_HIDE_SOFT_INPUT,
    252                 flags, resultReceiver));
    253     }
    254 }
    255