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