BaseInputConnection.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package android.view.inputmethod; 18 19import android.content.Context; 20import android.os.Handler; 21import android.os.Message; 22import android.view.KeyEvent; 23import android.view.View; 24import android.view.ViewRoot; 25 26/** 27 * Base class for implementors of the InputConnection interface, taking care 28 * of implementing common system-oriented parts of the functionality. 29 */ 30public abstract class BaseInputConnection implements InputConnection { 31 final InputMethodManager mIMM; 32 final Handler mH; 33 final View mTargetView; 34 35 BaseInputConnection(InputMethodManager mgr) { 36 mIMM = mgr; 37 mTargetView = null; 38 mH = null; 39 } 40 41 public BaseInputConnection(View targetView) { 42 mIMM = (InputMethodManager)targetView.getContext().getSystemService( 43 Context.INPUT_METHOD_SERVICE); 44 mH = targetView.getHandler(); 45 mTargetView = targetView; 46 } 47 48 /** 49 * Provides standard implementation for sending a key event to the window 50 * attached to the input connection's view. 51 */ 52 public boolean sendKeyEvent(KeyEvent event) { 53 synchronized (mIMM.mH) { 54 Handler h = mH; 55 if (h == null) { 56 if (mIMM.mServedView != null) { 57 h = mIMM.mServedView.getHandler(); 58 } 59 } 60 if (h != null && mTargetView != null) { 61 h.post(new DispatchKey(event, mTargetView.getRootView())); 62 } 63 } 64 return false; 65 } 66 67 /** 68 * Provides standard implementation for hiding the status icon associated 69 * with the current input method. 70 */ 71 public boolean hideStatusIcon() { 72 mIMM.updateStatusIcon(0, null); 73 return true; 74 } 75 76 /** 77 * Provides standard implementation for showing the status icon associated 78 * with the current input method. 79 */ 80 public boolean showStatusIcon(String packageName, int resId) { 81 mIMM.updateStatusIcon(resId, packageName); 82 return true; 83 } 84 85 static class DispatchKey implements Runnable { 86 KeyEvent mEvent; 87 View mView; 88 89 DispatchKey(KeyEvent event, View v) { 90 mEvent = event; 91 mView = v; 92 } 93 94 public void run() { 95 mView.dispatchKeyEvent(mEvent); 96 } 97 } 98}