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