BaseInputConnection.java revision b798689749c64baba81f02e10cf2157c747d6b46
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) {
61                h.sendMessage(h.obtainMessage(ViewRoot.DISPATCH_KEY_FROM_IME,
62                        event));
63            }
64        }
65        return false;
66    }
67
68    /**
69     * Provides standard implementation for hiding the status icon associated
70     * with the current input method.
71     */
72    public boolean hideStatusIcon() {
73        mIMM.updateStatusIcon(0, null);
74        return true;
75    }
76
77    /**
78     * Provides standard implementation for showing the status icon associated
79     * with the current input method.
80     */
81    public boolean showStatusIcon(String packageName, int resId) {
82        mIMM.updateStatusIcon(resId, packageName);
83        return true;
84    }
85}
86