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