MoreKeysKeyboardView.java revision 359c35e0f6e534d096efc1d9f2ff585139e3b3ac
1/*
2 * Copyright (C) 2011 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 com.android.inputmethod.keyboard;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.AttributeSet;
22import android.view.Gravity;
23import android.view.View;
24import android.widget.PopupWindow;
25
26import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy;
27import com.android.inputmethod.keyboard.PointerTracker.TimerProxy;
28import com.android.inputmethod.latin.Constants;
29import com.android.inputmethod.latin.CoordinateUtils;
30import com.android.inputmethod.latin.InputPointers;
31import com.android.inputmethod.latin.R;
32
33/**
34 * A view that renders a virtual {@link MoreKeysKeyboard}. It handles rendering of keys and
35 * detecting key presses and touch movements.
36 */
37public final class MoreKeysKeyboardView extends KeyboardView implements MoreKeysPanel {
38    private final int[] mCoordinates = CoordinateUtils.newInstance();
39
40    private final KeyDetector mKeyDetector;
41
42    private Controller mController;
43    private KeyboardActionListener mListener;
44    private int mOriginX;
45    private int mOriginY;
46
47    private static final TimerProxy EMPTY_TIMER_PROXY = new TimerProxy.Adapter();
48
49    private final KeyboardActionListener mMoreKeysKeyboardListener =
50            new KeyboardActionListener.Adapter() {
51        @Override
52        public void onCodeInput(final int primaryCode, final int x, final int y) {
53            // Because a more keys keyboard doesn't need proximity characters correction, we don't
54            // send touch event coordinates.
55            mListener.onCodeInput(
56                    primaryCode, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
57        }
58
59        @Override
60        public void onTextInput(final String text) {
61            mListener.onTextInput(text);
62        }
63
64        @Override
65        public void onStartBatchInput() {
66            mListener.onStartBatchInput();
67        }
68
69        @Override
70        public void onUpdateBatchInput(final InputPointers batchPointers) {
71            mListener.onUpdateBatchInput(batchPointers);
72        }
73
74        @Override
75        public void onEndBatchInput(final InputPointers batchPointers) {
76            mListener.onEndBatchInput(batchPointers);
77        }
78
79        @Override
80        public void onCancelInput() {
81            mListener.onCancelInput();
82        }
83
84        @Override
85        public void onPressKey(final int primaryCode) {
86            mListener.onPressKey(primaryCode);
87        }
88
89        @Override
90        public void onReleaseKey(final int primaryCode, final boolean withSliding) {
91            mListener.onReleaseKey(primaryCode, withSliding);
92        }
93    };
94
95    public MoreKeysKeyboardView(final Context context, final AttributeSet attrs) {
96        this(context, attrs, R.attr.moreKeysKeyboardViewStyle);
97    }
98
99    public MoreKeysKeyboardView(final Context context, final AttributeSet attrs,
100            final int defStyle) {
101        super(context, attrs, defStyle);
102
103        final Resources res = context.getResources();
104        mKeyDetector = new MoreKeysDetector(
105                res.getDimension(R.dimen.more_keys_keyboard_slide_allowance));
106        setKeyPreviewPopupEnabled(false, 0);
107    }
108
109    @Override
110    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
111        final Keyboard keyboard = getKeyboard();
112        if (keyboard != null) {
113            final int width = keyboard.mOccupiedWidth + getPaddingLeft() + getPaddingRight();
114            final int height = keyboard.mOccupiedHeight + getPaddingTop() + getPaddingBottom();
115            setMeasuredDimension(width, height);
116        } else {
117            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
118        }
119    }
120
121    @Override
122    public void setKeyboard(final Keyboard keyboard) {
123        super.setKeyboard(keyboard);
124        mKeyDetector.setKeyboard(keyboard, -getPaddingLeft(),
125                -getPaddingTop() + mVerticalCorrection);
126    }
127
128    @Override
129    public KeyDetector getKeyDetector() {
130        return mKeyDetector;
131    }
132
133    @Override
134    public KeyboardActionListener getKeyboardActionListener() {
135        return mMoreKeysKeyboardListener;
136    }
137
138    @Override
139    public DrawingProxy getDrawingProxy() {
140        return this;
141    }
142
143    @Override
144    public TimerProxy getTimerProxy() {
145        return EMPTY_TIMER_PROXY;
146    }
147
148    @Override
149    public void setKeyPreviewPopupEnabled(final boolean previewEnabled, final int delay) {
150        // More keys keyboard needs no pop-up key preview displayed, so we pass always false with a
151        // delay of 0. The delay does not matter actually since the popup is not shown anyway.
152        super.setKeyPreviewPopupEnabled(false, 0);
153    }
154
155    @Override
156    public void showMoreKeysPanel(final View parentView, final Controller controller,
157            final int pointX, final int pointY, final PopupWindow window,
158            final KeyboardActionListener listener) {
159        mController = controller;
160        mListener = listener;
161        final View container = (View)getParent();
162        final MoreKeysKeyboard pane = (MoreKeysKeyboard)getKeyboard();
163        final int defaultCoordX = pane.getDefaultCoordX();
164        // The coordinates of panel's left-top corner in parentView's coordinate system.
165        final int x = pointX - defaultCoordX - container.getPaddingLeft();
166        final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom();
167
168        window.setContentView(container);
169        window.setWidth(container.getMeasuredWidth());
170        window.setHeight(container.getMeasuredHeight());
171        parentView.getLocationInWindow(mCoordinates);
172        window.showAtLocation(parentView, Gravity.NO_GRAVITY,
173                x + CoordinateUtils.x(mCoordinates), y + CoordinateUtils.y(mCoordinates));
174
175        mOriginX = x + container.getPaddingLeft();
176        mOriginY = y + container.getPaddingTop();
177    }
178
179    private boolean mIsDismissing;
180
181    @Override
182    public boolean dismissMoreKeysPanel() {
183        if (mIsDismissing || mController == null) return false;
184        mIsDismissing = true;
185        final boolean dismissed = mController.dismissMoreKeysPanel();
186        mIsDismissing = false;
187        return dismissed;
188    }
189
190    @Override
191    public int translateX(final int x) {
192        return x - mOriginX;
193    }
194
195    @Override
196    public int translateY(final int y) {
197        return y - mOriginY;
198    }
199}
200