1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.inputmethod.keyboard;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.Utils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
// TODO: We should remove this class
public class LatinKeyboard extends Keyboard {
private static final int SPACE_LED_LENGTH_PERCENT = 80;
private final Resources mRes;
/* Space key and its icons, drawables and colors. */
private final Key mSpaceKey;
private final Drawable mSpaceIcon;
private final boolean mAutoCorrectionSpacebarLedEnabled;
private final Drawable mAutoCorrectionSpacebarLedIcon;
private final float mSpacebarTextSize;
private final int mSpacebarTextColor;
private final int mSpacebarTextShadowColor;
private final HashMap<Integer, BitmapDrawable> mSpaceDrawableCache =
new HashMap<Integer, BitmapDrawable>();
private boolean mAutoCorrectionSpacebarLedOn;
private boolean mNeedsToDisplayLanguage;
private float mSpacebarTextFadeFactor = 0.0f;
// Height in space key the language name will be drawn. (proportional to space key height)
public static final float SPACEBAR_LANGUAGE_BASELINE = 0.6f;
// If the full language name needs to be smaller than this value to be drawn on space key,
// its short language name will be used instead.
private static final float MINIMUM_SCALE_OF_LANGUAGE_NAME = 0.8f;
private LatinKeyboard(Context context, KeyboardParams params) {
super(params);
mRes = context.getResources();
// The index of space key is available only after Keyboard constructor has finished.
mSpaceKey = getKey(CODE_SPACE);
mSpaceIcon = (mSpaceKey != null) ? mSpaceKey.getIcon() : null;
// TODO: Move these to LatinKeyboardView attributes.
final TypedArray a = context.obtainStyledAttributes(
null, R.styleable.LatinKeyboard, R.attr.latinKeyboardStyle, R.style.LatinKeyboard);
mAutoCorrectionSpacebarLedEnabled = a.getBoolean(
R.styleable.LatinKeyboard_autoCorrectionSpacebarLedEnabled, false);
mAutoCorrectionSpacebarLedIcon = a.getDrawable(
R.styleable.LatinKeyboard_autoCorrectionSpacebarLedIcon);
final float spacebarTextRatio = a.getFraction(R.styleable.LatinKeyboard_spacebarTextRatio,
1000, 1000, 1) / 1000.0f;
final int keyHeight = mMostCommonKeyHeight - mVerticalGap;
mSpacebarTextSize = keyHeight * spacebarTextRatio;
mSpacebarTextColor = a.getColor(R.styleable.LatinKeyboard_spacebarTextColor, 0);
mSpacebarTextShadowColor = a.getColor(
R.styleable.LatinKeyboard_spacebarTextShadowColor, 0);
a.recycle();
}
public static class Builder extends KeyboardBuilder<KeyboardParams> {
public Builder(Context context) {
super(context, new KeyboardParams());
}
@Override
public Builder load(int xmlId, KeyboardId id) {
super.load(xmlId, id);
return this;
}
@Override
public LatinKeyboard build() {
return new LatinKeyboard(mContext, mParams);
}
}
// TODO: Move this drawing method to LatinKeyboardView.
// TODO: Use Key.keyLabel to draw language name of spacebar.
public Key updateSpacebarLanguage(float fadeFactor, boolean needsToDisplayLanguage) {
mSpacebarTextFadeFactor = fadeFactor;
mNeedsToDisplayLanguage = needsToDisplayLanguage;
updateSpacebarIcon();
return mSpaceKey;
}
private static int getSpacebarTextColor(int color, float fadeFactor) {
final int newColor = Color.argb((int)(Color.alpha(color) * fadeFactor),
Color.red(color), Color.green(color), Color.blue(color));
return newColor;
}
// TODO: Get rid of this method
public boolean needsAutoCorrectionSpacebarLed() {
return mAutoCorrectionSpacebarLedEnabled;
}
// TODO: Move this drawing method to LatinKeyboardView.
/**
* @return a key which should be invalidated.
*/
public Key updateAutoCorrectionState(boolean isAutoCorrection) {
mAutoCorrectionSpacebarLedOn = isAutoCorrection;
updateSpacebarIcon();
return mSpaceKey;
}
private void updateSpacebarIcon() {
if (mSpaceKey == null) return;
if (mNeedsToDisplayLanguage) {
mSpaceKey.setIcon(getSpaceDrawable(mId.mLocale));
} else if (mAutoCorrectionSpacebarLedOn) {
mSpaceKey.setIcon(getSpaceDrawable(null));
} else {
mSpaceKey.setIcon(mSpaceIcon);
}
}
// Compute width of text with specified text size using paint.
private static int getTextWidth(Paint paint, String text, float textSize, Rect bounds) {
paint.setTextSize(textSize);
paint.getTextBounds(text, 0, text.length(), bounds);
return bounds.width();
}
// Layout local language name and left and right arrow on spacebar.
private static String layoutSpacebar(Paint paint, Locale locale, int width,
float origTextSize) {
final Rect bounds = new Rect();
// Estimate appropriate language name text size to fit in maxTextWidth.
String language = Utils.getFullDisplayName(locale, true);
int textWidth = getTextWidth(paint, language, origTextSize, bounds);
// Assuming text width and text size are proportional to each other.
float textSize = origTextSize * Math.min(width / textWidth, 1.0f);
// allow variable text size
textWidth = getTextWidth(paint, language, textSize, bounds);
// If text size goes too small or text does not fit, use middle or short name
final boolean useMiddleName = (textSize / origTextSize < MINIMUM_SCALE_OF_LANGUAGE_NAME)
|| (textWidth > width);
final boolean useShortName;
if (useMiddleName) {
language = Utils.getMiddleDisplayLanguage(locale);
textWidth = getTextWidth(paint, language, origTextSize, bounds);
textSize = origTextSize * Math.min(width / textWidth, 1.0f);
useShortName = (textSize / origTextSize < MINIMUM_SCALE_OF_LANGUAGE_NAME)
|| (textWidth > width);
} else {
useShortName = false;
}
if (useShortName) {
language = Utils.getShortDisplayLanguage(locale);
textWidth = getTextWidth(paint, language, origTextSize, bounds);
textSize = origTextSize * Math.min(width / textWidth, 1.0f);
}
paint.setTextSize(textSize);
return language;
}
private BitmapDrawable getSpaceDrawable(Locale locale) {
final Integer hashCode = Arrays.hashCode(
new Object[] { locale, mAutoCorrectionSpacebarLedOn, mSpacebarTextFadeFactor });
final BitmapDrawable cached = mSpaceDrawableCache.get(hashCode);
if (cached != null) {
return cached;
}
final BitmapDrawable drawable = new BitmapDrawable(mRes, drawSpacebar(
locale, mAutoCorrectionSpacebarLedOn, mSpacebarTextFadeFactor));
mSpaceDrawableCache.put(hashCode, drawable);
return drawable;
}
private Bitmap drawSpacebar(Locale inputLocale, boolean isAutoCorrection,
float textFadeFactor) {
final int width = mSpaceKey.mWidth;
final int height = mSpaceIcon != null ? mSpaceIcon.getIntrinsicHeight() : mSpaceKey.mHeight;
final Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(buffer);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
// If application locales are explicitly selected.
if (inputLocale != null) {
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextAlign(Align.CENTER);
final String language = layoutSpacebar(paint, inputLocale, width, mSpacebarTextSize);
// Draw language text with shadow
// In case there is no space icon, we will place the language text at the center of
// spacebar.
final float descent = paint.descent();
final float textHeight = -paint.ascent() + descent;
final float baseline = (mSpaceIcon != null) ? height * SPACEBAR_LANGUAGE_BASELINE
: height / 2 + textHeight / 2;
paint.setColor(getSpacebarTextColor(mSpacebarTextShadowColor, textFadeFactor));
canvas.drawText(language, width / 2, baseline - descent - 1, paint);
paint.setColor(getSpacebarTextColor(mSpacebarTextColor, textFadeFactor));
canvas.drawText(language, width / 2, baseline - descent, paint);
}
// Draw the spacebar icon at the bottom
if (isAutoCorrection) {
final int iconWidth = width * SPACE_LED_LENGTH_PERCENT / 100;
final int iconHeight = mAutoCorrectionSpacebarLedIcon.getIntrinsicHeight();
int x = (width - iconWidth) / 2;
int y = height - iconHeight;
mAutoCorrectionSpacebarLedIcon.setBounds(x, y, x + iconWidth, y + iconHeight);
mAutoCorrectionSpacebarLedIcon.draw(canvas);
} else if (mSpaceIcon != null) {
final int iconWidth = mSpaceIcon.getIntrinsicWidth();
final int iconHeight = mSpaceIcon.getIntrinsicHeight();
int x = (width - iconWidth) / 2;
int y = height - iconHeight;
mSpaceIcon.setBounds(x, y, x + iconWidth, y + iconHeight);
mSpaceIcon.draw(canvas);
}
return buffer;
}
@Override
public Key[] getNearestKeys(int x, int y) {
// Avoid dead pixels at edges of the keyboard
return super.getNearestKeys(Math.max(0, Math.min(x, mOccupiedWidth - 1)),
Math.max(0, Math.min(y, mOccupiedHeight - 1)));
}
}
|