IInputMethodSessionWrapper.java revision a308c0326bef49293396ceca843bb595139ba38f
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 17package android.inputmethodservice; 18 19import com.android.internal.os.HandlerCaller; 20import com.android.internal.view.IInputMethodCallback; 21import com.android.internal.view.IInputMethodSession; 22 23import android.content.Context; 24import android.graphics.Rect; 25import android.os.Bundle; 26import android.os.Message; 27import android.os.RemoteException; 28import android.util.Log; 29import android.view.KeyEvent; 30import android.view.MotionEvent; 31import android.view.inputmethod.CompletionInfo; 32import android.view.inputmethod.ExtractedText; 33import android.view.inputmethod.InputMethodSession; 34 35class IInputMethodSessionWrapper extends IInputMethodSession.Stub 36 implements HandlerCaller.Callback { 37 private static final String TAG = "InputMethodWrapper"; 38 private static final boolean DEBUG = false; 39 40 private static final int DO_FINISH_INPUT = 60; 41 private static final int DO_DISPLAY_COMPLETIONS = 65; 42 private static final int DO_UPDATE_EXTRACTED_TEXT = 67; 43 private static final int DO_DISPATCH_KEY_EVENT = 70; 44 private static final int DO_DISPATCH_TRACKBALL_EVENT = 80; 45 private static final int DO_UPDATE_SELECTION = 90; 46 private static final int DO_UPDATE_CURSOR = 95; 47 private static final int DO_APP_PRIVATE_COMMAND = 100; 48 private static final int DO_TOGGLE_SOFT_INPUT = 105; 49 private static final int DO_FINISH_SESSION = 110; 50 51 HandlerCaller mCaller; 52 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 if (mInputMethodSession == null) return; 80 81 switch (msg.what) { 82 case DO_FINISH_INPUT: 83 mInputMethodSession.finishInput(); 84 return; 85 case DO_DISPLAY_COMPLETIONS: 86 mInputMethodSession.displayCompletions((CompletionInfo[])msg.obj); 87 return; 88 case DO_UPDATE_EXTRACTED_TEXT: 89 mInputMethodSession.updateExtractedText(msg.arg1, 90 (ExtractedText)msg.obj); 91 return; 92 case DO_DISPATCH_KEY_EVENT: { 93 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj; 94 mInputMethodSession.dispatchKeyEvent(msg.arg1, 95 (KeyEvent)args.arg1, 96 new InputMethodEventCallbackWrapper( 97 (IInputMethodCallback)args.arg2)); 98 mCaller.recycleArgs(args); 99 return; 100 } 101 case DO_DISPATCH_TRACKBALL_EVENT: { 102 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj; 103 mInputMethodSession.dispatchTrackballEvent(msg.arg1, 104 (MotionEvent)args.arg1, 105 new InputMethodEventCallbackWrapper( 106 (IInputMethodCallback)args.arg2)); 107 mCaller.recycleArgs(args); 108 return; 109 } 110 case DO_UPDATE_SELECTION: { 111 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj; 112 mInputMethodSession.updateSelection(args.argi1, args.argi2, 113 args.argi3, args.argi4, args.argi5, args.argi6); 114 mCaller.recycleArgs(args); 115 return; 116 } 117 case DO_UPDATE_CURSOR: { 118 mInputMethodSession.updateCursor((Rect)msg.obj); 119 return; 120 } 121 case DO_APP_PRIVATE_COMMAND: { 122 HandlerCaller.SomeArgs args = (HandlerCaller.SomeArgs)msg.obj; 123 mInputMethodSession.appPrivateCommand((String)args.arg1, 124 (Bundle)args.arg2); 125 mCaller.recycleArgs(args); 126 return; 127 } 128 case DO_TOGGLE_SOFT_INPUT: { 129 mInputMethodSession.toggleSoftInput(msg.arg1, msg.arg2); 130 return; 131 } 132 case DO_FINISH_SESSION: { 133 mInputMethodSession = null; 134 return; 135 } 136 } 137 Log.w(TAG, "Unhandled message code: " + msg.what); 138 } 139 140 public void finishInput() { 141 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_INPUT)); 142 } 143 144 public void displayCompletions(CompletionInfo[] completions) { 145 mCaller.executeOrSendMessage(mCaller.obtainMessageO( 146 DO_DISPLAY_COMPLETIONS, completions)); 147 } 148 149 public void updateExtractedText(int token, ExtractedText text) { 150 mCaller.executeOrSendMessage(mCaller.obtainMessageIO( 151 DO_UPDATE_EXTRACTED_TEXT, token, text)); 152 } 153 154 public void dispatchKeyEvent(int seq, KeyEvent event, IInputMethodCallback callback) { 155 mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_KEY_EVENT, seq, 156 event, callback)); 157 } 158 159 public void dispatchTrackballEvent(int seq, MotionEvent event, IInputMethodCallback callback) { 160 mCaller.executeOrSendMessage(mCaller.obtainMessageIOO(DO_DISPATCH_TRACKBALL_EVENT, seq, 161 event, callback)); 162 } 163 164 public void updateSelection(int oldSelStart, int oldSelEnd, 165 int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) { 166 mCaller.executeOrSendMessage(mCaller.obtainMessageIIIIII(DO_UPDATE_SELECTION, 167 oldSelStart, oldSelEnd, newSelStart, newSelEnd, 168 candidatesStart, candidatesEnd)); 169 } 170 171 public void updateCursor(Rect newCursor) { 172 mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_UPDATE_CURSOR, 173 newCursor)); 174 } 175 176 public void appPrivateCommand(String action, Bundle data) { 177 mCaller.executeOrSendMessage(mCaller.obtainMessageOO(DO_APP_PRIVATE_COMMAND, action, data)); 178 } 179 180 public void toggleSoftInput(int showFlags, int hideFlags) { 181 mCaller.executeOrSendMessage(mCaller.obtainMessageII(DO_TOGGLE_SOFT_INPUT, showFlags, hideFlags)); 182 } 183 184 public void finishSession() { 185 mCaller.executeOrSendMessage(mCaller.obtainMessage(DO_FINISH_SESSION)); 186 } 187} 188