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.IInputMethodCallback;
     21 import com.android.internal.view.IInputMethodSession;
     22 
     23 import android.content.Context;
     24 import android.graphics.Rect;
     25 import android.os.Bundle;
     26 import android.os.Message;
     27 import android.os.RemoteException;
     28 import android.util.Log;
     29 import android.view.KeyEvent;
     30 import android.view.MotionEvent;
     31 import android.view.inputmethod.CompletionInfo;
     32 import android.view.inputmethod.ExtractedText;
     33 import android.view.inputmethod.InputMethodSession;
     34 import android.view.inputmethod.EditorInfo;
     35 
     36 class IInputMethodSessionWrapper extends IInputMethodSession.Stub
     37         implements HandlerCaller.Callback {
     38     private static final String TAG = "InputMethodWrapper";
     39     private static final boolean DEBUG = false;
     40 
     41     private static final int DO_FINISH_INPUT = 60;
     42     private static final int DO_DISPLAY_COMPLETIONS = 65;
     43     private static final int DO_UPDATE_EXTRACTED_TEXT = 67;
     44     private static final int DO_DISPATCH_KEY_EVENT = 70;
     45     private static final int DO_DISPATCH_TRACKBALL_EVENT = 80;
     46     private static final int DO_UPDATE_SELECTION = 90;
     47     private static final int DO_UPDATE_CURSOR = 95;
     48     private static final int DO_APP_PRIVATE_COMMAND = 100;
     49     private static final int DO_TOGGLE_SOFT_INPUT = 105;
     50 
     51     final HandlerCaller mCaller;
     52     final InputMethodSession mInputMethodSession;
     53 
     54     // NOTE: we should have a cache of these.
     55     static class InputMethodEventCallbackWrapper implements InputMethodSession.EventCallback {
     56         final IInputMethodCallback mCb;
     57         InputMethodEventCallbackWrapper(IInputMethodCallback cb) {
     58             mCb = cb;
     59         }
     60         public void finishedEvent(int seq, boolean handled) {
     61             try {
     62                 mCb.finishedEvent(seq, handled);
     63             } catch (RemoteException e) {
     64             }
     65         }
     66     }
     67 
     68     public IInputMethodSessionWrapper(Context context,
     69             InputMethodSession inputMethodSession) {
     70         mCaller = new HandlerCaller(context, this);
     71         mInputMethodSession = inputMethodSession;
     72     }
     73 
     74     public InputMethodSession getInternalInputMethodSession() {
     75         return mInputMethodSession;
     76     }
     77 
     78     public void executeMessage(Message msg) {
     79         switch (msg.what) {
     80             case DO_FINISH_INPUT:
     81                 mInputMethodSession.finishInput();
     82                 return;
     83             case DO_DISPLAY_COMPLETIONS:
     84                 mInputMethodSession.displayCompletions((CompletionInfo[])msg.obj);
     85                 return;
     86             case DO_UPDATE_EXTRACTED_TEXT:
     87                 mInputMethodSession.updateExtractedText(msg.arg1,
     88                         (ExtractedText)msg.obj);
     89                 return;
     90             case DO_DISPATCH_KEY_EVENT: {
     91                 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
     92                 mInputMethodSession.dispatchKeyEvent(msg.arg1,
     93                         (KeyEvent)args.arg1,
     94                         new InputMethodEventCallbackWrapper(
     95                                 (IInputMethodCallback)args.arg2));
     96                 mCaller.recycleArgs(args);
     97                 return;
     98             }
     99             case DO_DISPATCH_TRACKBALL_EVENT: {
    100                 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
    101                 mInputMethodSession.dispatchTrackballEvent(msg.arg1,
    102                         (MotionEvent)args.arg1,
    103                         new InputMethodEventCallbackWrapper(
    104                                 (IInputMethodCallback)args.arg2));
    105                 mCaller.recycleArgs(args);
    106                 return;
    107             }
    108             case DO_UPDATE_SELECTION: {
    109                 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
    110                 mInputMethodSession.updateSelection(args.argi1, args.argi2,
    111                         args.argi3, args.argi4, args.argi5, args.argi6);
    112                 mCaller.recycleArgs(args);
    113                 return;
    114             }
    115             case DO_UPDATE_CURSOR: {
    116                 mInputMethodSession.updateCursor((Rect)msg.obj);
    117                 return;
    118             }
    119             case DO_APP_PRIVATE_COMMAND: {
    120                 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj;
    121                 mInputMethodSession.appPrivateCommand((String)args.arg1,
    122                         (Bundle)args.arg2);
    123                 mCaller.recycleArgs(args);
    124                 return;
    125             }
    126             case DO_TOGGLE_SOFT_INPUT: {
    127                 mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2);
    128                 return;
    129             }
    130         }
    131         Log.w(TAG, "Unhandled message code: " + msg.what);
    132     }
    133 
    134     public void finishInput() {
    135         mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_INPUT));
    136     }
    137 
    138     public void displayCompletions(CompletionInfo[] completions) {
    139         mCaller.executeOrSendMessage(mCaller.obtainMessageO(
    140                 DO_DISPLAY_COMPLETIONS, completions));
    141     }
    142 
    143     public void updateExtractedText(int token, ExtractedText text) {
    144         mCaller.executeOrSendMessage(mCaller.obtainMessageIO(
    145                 DO_UPDATE_EXTRACTED_TEXT, token, text));
    146     }
    147 
    148     public void dispatchKeyEvent(int seq, KeyEvent event, IInputMethodCallback callback) {
    149         mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_KEY_EVENT, seq,
    150                 event, callback));
    151     }
    152 
    153     public void dispatchTrackballEvent(int seq, MotionEvent event, IInputMethodCallback callback) {
    154         mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_TRACKBALL_EVENT, seq,
    155                 event, callback));
    156     }
    157 
    158     public void updateSelection(int oldSelStart, int oldSelEnd,
    159             int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) {
    160         mCaller.executeOrSendMessage(mCaller.obtainMessageIIIIII(DO_UPDATE_SELECTION,
    161                 oldSelStart, oldSelEnd, newSelStart, newSelEnd,
    162                 candidatesStart, candidatesEnd));
    163     }
    164 
    165     public void updateCursor(Rect newCursor) {
    166         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_UPDATE_CURSOR,
    167                 newCursor));
    168     }
    169 
    170     public void appPrivateCommand(String action, Bundle data) {
    171         mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data));
    172     }
    173 
    174     public void toggleSoftInput(int showFlags, int hideFlags) {
    175         mCaller.executeOrSendMessage(mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags));
    176     }
    177 }
    178