• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.pinyin;
18 
19 import android.graphics.drawable.Drawable;
20 
21 /**
22  * Class for soft keys which defined in the keyboard xml file. A soft key can be
23  * a basic key or a toggling key.
24  *
25  * @see com.android.inputmethod.pinyin.SoftKeyToggle
26  */
27 public class SoftKey {
28     protected static final int KEYMASK_REPEAT = 0x10000000;
29     protected static final int KEYMASK_BALLOON = 0x20000000;
30 
31     /**
32      * For a finger touch device, after user presses a key, there will be some
33      * consequent moving events because of the changing in touching pressure. If
34      * the moving distance in x is within this threshold, the moving events will
35      * be ignored.
36      */
37     public static final int MAX_MOVE_TOLERANCE_X = 0;
38 
39     /**
40      * For a finger touch device, after user presses a key, there will be some
41      * consequent moving events because of the changing in touching pressure. If
42      * the moving distance in y is within this threshold, the moving events will
43      * be ignored.
44      */
45     public static final int MAX_MOVE_TOLERANCE_Y = 0;
46 
47     /**
48      * Used to indicate the type and attributes of this key. the lowest 8 bits
49      * should be reserved for SoftkeyToggle.
50      */
51     protected int mKeyMask;
52 
53     protected SoftKeyType mKeyType;
54 
55     protected Drawable mKeyIcon;
56 
57     protected Drawable mKeyIconPopup;
58 
59     protected String mKeyLabel;
60 
61     protected int mKeyCode;
62 
63     /**
64      * If this value is not 0, this key can be used to popup a sub soft keyboard
65      * when user presses it for some time.
66      */
67     public int mPopupSkbId;
68 
69     public float mLeftF;
70     public float mRightF;
71     public float mTopF;
72     public float mBottomF;
73     public int mLeft;
74     public int mRight;
75     public int mTop;
76     public int mBottom;
77 
setKeyType(SoftKeyType keyType, Drawable keyIcon, Drawable keyIconPopup)78     public void setKeyType(SoftKeyType keyType, Drawable keyIcon,
79             Drawable keyIconPopup) {
80         mKeyType = keyType;
81         mKeyIcon = keyIcon;
82         mKeyIconPopup = keyIconPopup;
83     }
84 
85     // The caller guarantees that all parameters are in [0, 1]
setKeyDimensions(float left, float top, float right, float bottom)86     public void setKeyDimensions(float left, float top, float right,
87             float bottom) {
88         mLeftF = left;
89         mTopF = top;
90         mRightF = right;
91         mBottomF = bottom;
92     }
93 
setKeyAttribute(int keyCode, String label, boolean repeat, boolean balloon)94     public void setKeyAttribute(int keyCode, String label, boolean repeat,
95             boolean balloon) {
96         mKeyCode = keyCode;
97         mKeyLabel = label;
98 
99         if (repeat) {
100             mKeyMask |= KEYMASK_REPEAT;
101         } else {
102             mKeyMask &= (~KEYMASK_REPEAT);
103         }
104 
105         if (balloon) {
106             mKeyMask |= KEYMASK_BALLOON;
107         } else {
108             mKeyMask &= (~KEYMASK_BALLOON);
109         }
110     }
111 
setPopupSkbId(int popupSkbId)112     public void setPopupSkbId(int popupSkbId) {
113         mPopupSkbId = popupSkbId;
114     }
115 
116     // Call after setKeyDimensions(). The caller guarantees that the
117     // keyboard with and height are valid.
setSkbCoreSize(int skbWidth, int skbHeight)118     public void setSkbCoreSize(int skbWidth, int skbHeight) {
119         mLeft = (int) (mLeftF * skbWidth);
120         mRight = (int) (mRightF * skbWidth);
121         mTop = (int) (mTopF * skbHeight);
122         mBottom = (int) (mBottomF * skbHeight);
123     }
124 
getKeyIcon()125     public Drawable getKeyIcon() {
126         return mKeyIcon;
127     }
128 
getKeyIconPopup()129     public Drawable getKeyIconPopup() {
130         if (null != mKeyIconPopup) {
131             return mKeyIconPopup;
132         }
133         return mKeyIcon;
134     }
135 
getKeyCode()136     public int getKeyCode() {
137         return mKeyCode;
138     }
139 
getKeyLabel()140     public String getKeyLabel() {
141         return mKeyLabel;
142     }
143 
changeCase(boolean upperCase)144     public void changeCase(boolean upperCase) {
145         if (null != mKeyLabel) {
146             if (upperCase)
147                 mKeyLabel = mKeyLabel.toUpperCase();
148             else
149                 mKeyLabel = mKeyLabel.toLowerCase();
150         }
151     }
152 
getKeyBg()153     public Drawable getKeyBg() {
154         return mKeyType.mKeyBg;
155     }
156 
getKeyHlBg()157     public Drawable getKeyHlBg() {
158         return mKeyType.mKeyHlBg;
159     }
160 
getColor()161     public int getColor() {
162         return mKeyType.mColor;
163     }
164 
getColorHl()165     public int getColorHl() {
166         return mKeyType.mColorHl;
167     }
168 
getColorBalloon()169     public int getColorBalloon() {
170         return mKeyType.mColorBalloon;
171     }
172 
isKeyCodeKey()173     public boolean isKeyCodeKey() {
174         if (mKeyCode > 0) return true;
175         return false;
176     }
177 
isUserDefKey()178     public boolean isUserDefKey() {
179         if (mKeyCode < 0) return true;
180         return false;
181     }
182 
isUniStrKey()183     public boolean isUniStrKey() {
184         if (null != mKeyLabel && mKeyCode == 0) return true;
185         return false;
186     }
187 
needBalloon()188     public boolean needBalloon() {
189         return (mKeyMask & KEYMASK_BALLOON) != 0;
190     }
191 
repeatable()192     public boolean repeatable() {
193         return (mKeyMask & KEYMASK_REPEAT) != 0;
194     }
195 
getPopupResId()196     public int getPopupResId() {
197         return mPopupSkbId;
198     }
199 
width()200     public int width() {
201         return mRight - mLeft;
202     }
203 
height()204     public int height() {
205         return mBottom - mTop;
206     }
207 
moveWithinKey(int x, int y)208     public boolean moveWithinKey(int x, int y) {
209         if (mLeft - MAX_MOVE_TOLERANCE_X <= x
210                 && mTop - MAX_MOVE_TOLERANCE_Y <= y
211                 && mRight + MAX_MOVE_TOLERANCE_X > x
212                 && mBottom + MAX_MOVE_TOLERANCE_Y > y) {
213             return true;
214         }
215         return false;
216     }
217 
218     @Override
toString()219     public String toString() {
220         String str = "\n";
221         str += "  keyCode: " + String.valueOf(mKeyCode) + "\n";
222         str += "  keyMask: " + String.valueOf(mKeyMask) + "\n";
223         str += "  keyLabel: " + (mKeyLabel == null ? "null" : mKeyLabel) + "\n";
224         str += "  popupResId: " + String.valueOf(mPopupSkbId) + "\n";
225         str += "  Position: " + String.valueOf(mLeftF) + ", "
226                 + String.valueOf(mTopF) + ", " + String.valueOf(mRightF) + ", "
227                 + String.valueOf(mBottomF) + "\n";
228         return str;
229     }
230 }
231