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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
/*
* Copyright (c) 2013 Menny Even-Danan
*
* 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.anysoftkeyboard.keyboards;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Xml;
import android.view.inputmethod.EditorInfo;
import com.anysoftkeyboard.AnySoftKeyboard;
import com.anysoftkeyboard.addons.AddOn;
import com.anysoftkeyboard.api.KeyCodes;
import com.anysoftkeyboard.keyboardextensions.KeyboardExtension;
import com.anysoftkeyboard.keyboardextensions.KeyboardExtensionFactory;
import com.anysoftkeyboard.keyboards.views.KeyDrawableStateProvider;
import com.anysoftkeyboard.quicktextkeys.QuickTextKey;
import com.anysoftkeyboard.quicktextkeys.QuickTextKeyFactory;
import com.anysoftkeyboard.utils.Log;
import com.anysoftkeyboard.utils.Workarounds;
import com.menny.android.anysoftkeyboard.AnyApplication;
import com.menny.android.anysoftkeyboard.R;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public abstract class AnyKeyboard extends Keyboard {
private final static String TAG = "ASK - AK";
public interface HardKeyboardAction {
int getKeyCode();
boolean isAltActive();
boolean isShiftActive();
void setNewKeyCode(int keyCode);
}
public interface HardKeyboardTranslator {
/*
* Gets the current state of the hard keyboard, and may change the
* output key-code.
*/
void translatePhysicalCharacter(HardKeyboardAction action,
AnySoftKeyboard ime);
}
private static final String TAG_ROW = "Row";
private static final String TAG_KEY = "Key";
private static class KeyboardMetadata {
public int keysCount = 0;
public int rowHeight = 0;
public int rowWidth = 0;
public int verticalGap = 0;
public boolean isTopRow = false;
}
private static final int STICKY_KEY_OFF = 0;
private static final int STICKY_KEY_ON = 1;
private static final int STICKY_KEY_LOCKED = 2;
private int mShiftState = STICKY_KEY_OFF;
private int mControlState = STICKY_KEY_OFF;
private Key mShiftKey;
private Key mControlKey;
private EnterKey mEnterKey;
// public Key langSwitch;
private boolean mRightToLeftLayout = false;// the "super" ctor will create
// keys, and we'll set the
// correct value there.
private boolean mTopRowWasCreated;
private boolean mBottomRowWasCreated;
private int mGenericRowsHeight = 0;
private int mTopRowKeysCount = 0;
// max(generic row widths)
private int mMaxGenericRowsWidth = 0;
private KeyboardCondenser mKeyboardCondenser;
// private int mKeyboardActionType = EditorInfo.IME_ACTION_NONE;
// for popup keyboard
// note: the context can be from a different package!
protected AnyKeyboard(@NonNull AddOn keyboardAddOn, @NonNull Context askContext, @NonNull Context context, int xmlLayoutResId) {
// should use the package context for creating the layout
super(keyboardAddOn, askContext, context, xmlLayoutResId, -1);
// no generic rows in popup
}
// for the External
// note: the context can be from a different package!
protected AnyKeyboard(@NonNull AddOn keyboardAddOn, @NonNull Context askContext, @NonNull Context context, int xmlLayoutResId, int mode) {
// should use the package context for creating the layout
super(keyboardAddOn, askContext, context, xmlLayoutResId, mode);
}
public void loadKeyboard(final KeyboardDimens keyboardDimens) {
super.loadKeyboard(keyboardDimens);
addGenericRows(mKeyboardMode, keyboardDimens);
initKeysMembers(mASKContext);
}
private void initKeysMembers(Context askContext) {
for (final Key key : getKeys()) {
if (key.y == 0)
key.edgeFlags |= Keyboard.EDGE_TOP;
if (key.codes.length > 0) {
final int primaryCode = key.getPrimaryCode();
if (key instanceof AnyKey) {
switch (primaryCode) {
case KeyCodes.DELETE:
case KeyCodes.MODE_ALPHABET:
case KeyCodes.KEYBOARD_MODE_CHANGE:
case KeyCodes.KEYBOARD_CYCLE:
case KeyCodes.KEYBOARD_CYCLE_INSIDE_MODE:
case KeyCodes.KEYBOARD_REVERSE_CYCLE:
case KeyCodes.ALT:
case KeyCodes.MODE_SYMOBLS:
case KeyCodes.QUICK_TEXT:
case KeyCodes.DOMAIN:
case KeyCodes.CANCEL:
case KeyCodes.CTRL:
((AnyKey) key).setAsFunctional();
break;
}
}
// detecting LTR languages
if (Workarounds.isRightToLeftCharacter((char) primaryCode))
mRightToLeftLayout = true;// one is enough
switch (primaryCode) {
case KeyCodes.QUICK_TEXT:
QuickTextKey quickKey = QuickTextKeyFactory
.getCurrentQuickTextKey(askContext);
if (quickKey == null) { // No plugins. Weird, but we
// can't do anything
Log.w(TAG, "Could not locate any quick key plugins! Hopefully nothing will crash...");
break;
}
Resources quickTextKeyResources = quickKey.getPackageContext().getResources();
key.label = quickKey.getKeyLabel();
int iconResId = quickKey.getKeyIconResId();
int previewResId = quickKey.getIconPreviewResId();
if (iconResId > 0) {
setKeyIcons(key, quickTextKeyResources, iconResId,
previewResId);
}
/*
* Popup resource may be from another context, requires
* special handling when the key is long-pressed!
*/
key.popupResId = quickKey.getPopupKeyboardResId();
key.externalResourcePopupLayout = key.popupResId != 0;
break;
case KeyCodes.DOMAIN:
// fixing icons
key.label = AnyApplication.getConfig().getDomainText()
.trim();
key.popupResId = R.xml.popup_domains;
break;
default:
// setting the character label
if (isAlphabetKey(key) && (key.icon == null)) {
final boolean labelIsOriginallyEmpty = TextUtils
.isEmpty(key.label);
if (labelIsOriginallyEmpty) {
final char code = (char) key.codes[0];
// check the ASCII table, everything below 32,
// is not printable
if (code > 31 && !Character.isWhitespace(code))
key.label = Character.toString(code);
}
}
}
}
}
mKeyboardCondenser = new KeyboardCondenser(askContext, this);
}
protected void addGenericRows(int mode, final KeyboardDimens keyboardDimens) {
final KeyboardMetadata topMd;
if (!mTopRowWasCreated) {
final KeyboardExtension topRowPlugin =
KeyboardExtensionFactory.getCurrentKeyboardExtension(mASKContext, KeyboardExtension.TYPE_TOP);
if (topRowPlugin == null || // no plugin found
// plugin specified to be empty
topRowPlugin.getKeyboardResId() == 0 ||
// could not parse layout res id
topRowPlugin.getKeyboardResId() == -2) {
Log.d(TAG, "No top row layout");
topMd = null;
// adding EDGE_TOP to top keys. See issue 775
List<Key> keys = getKeys();
for (int keyIndex = 0; keyIndex < keys.size(); keyIndex++) {
final Key key = keys.get(keyIndex);
if (key.y == 0)
key.edgeFlags = key.edgeFlags | Keyboard.EDGE_TOP;
}
} else {
Log.d(TAG, "Top row layout id %s", topRowPlugin.getId());
topMd = addKeyboardRow(topRowPlugin.getResourceMapping(), topRowPlugin.getPackageContext(),
topRowPlugin.getKeyboardResId(), mode, keyboardDimens);
}
if (topMd != null)
fixKeyboardDueToGenericRow(topMd,
(int) keyboardDimens.getRowVerticalGap());
}
if (!mBottomRowWasCreated) {
final KeyboardExtension bottomRowPlugin =
KeyboardExtensionFactory.getCurrentKeyboardExtension(mASKContext, KeyboardExtension.TYPE_BOTTOM);
Log.d(TAG, "Bottom row layout id %s", bottomRowPlugin.getId());
KeyboardMetadata bottomMd = addKeyboardRow(bottomRowPlugin.getResourceMapping(), bottomRowPlugin.getPackageContext(),
bottomRowPlugin.getKeyboardResId(), mode, keyboardDimens);
fixKeyboardDueToGenericRow(bottomMd,
(int) keyboardDimens.getRowVerticalGap());
}
}
private void fixKeyboardDueToGenericRow(KeyboardMetadata md,
int rowVerticalGap) {
final int additionalPixels = (md.rowHeight + md.verticalGap + rowVerticalGap);
mGenericRowsHeight += additionalPixels;
if (md.isTopRow) {
mTopRowKeysCount += md.keysCount;
List<Key> keys = getKeys();
for (int keyIndex = md.keysCount; keyIndex < keys.size(); keyIndex++) {
final Key key = keys.get(keyIndex);
key.y += additionalPixels;
// if (key instanceof LessSensitiveAnyKey)
// ((LessSensitiveAnyKey)key).resetSenitivity();//reseting cause
// the key may be offseted now (generic rows)
}
}/*
* else { // The height should not include any gap below that last row
* // this corresponds to // mTotalHeight = y - mDefaultVerticalGap; //
* in the Keyboard class from Android sources // Note that we are using
* keyboard default vertical gap (instead of row vertical gap) // as
* this is done also in Android sources. mGenericRowsHeight -=
* getVerticalGap(); }
*/
}
private KeyboardMetadata addKeyboardRow(@NonNull AddOn.AddOnResourceMapping resourceMapping, Context context, int rowResId, int mode, final KeyboardDimens keyboardDimens) {
XmlResourceParser parser = context.getResources().getXml(rowResId);
List<Key> keys = getKeys();
boolean inKey = false;
boolean inRow = false;
// boolean leftMostKey = false;
boolean skipRow;
final float keyHorizontalGap = keyboardDimens.getKeyHorizontalGap();
final float rowVerticalGap = keyboardDimens.getRowVerticalGap();
float x = 0;
float y = rowVerticalGap;
Key key = null;
Row currentRow = null;
Resources res = context.getResources();
KeyboardMetadata m = new KeyboardMetadata();
try {
int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) {
String tag = parser.getName();
if (TAG_ROW.equals(tag)) {
inRow = true;
x = 0;
currentRow = createRowFromXml(resourceMapping, res, parser);
skipRow = currentRow.mode != 0 && currentRow.mode != mode;
if (skipRow) {
currentRow = null;
skipToEndOfRow(parser);
inRow = false;
} else {
m.isTopRow = currentRow.rowEdgeFlags == Keyboard.EDGE_TOP;
if (!m.isTopRow) {
// the bottom row Y should be last
// The last coordinate is height + keyboard's
// default vertical gap
// since mTotalHeight = y - mDefaultVerticalGap;
// (see loadKeyboard
// in the android sources)
// We use our overriden getHeight method which
// is just fixed so that it includes the first
// generic row.
y = getHeight() + getVerticalGap();
}
m.rowHeight = 0;
m.verticalGap = currentRow.verticalGap;
}
} else if (TAG_KEY.equals(tag)) {
inKey = true;
x += (keyHorizontalGap / 2);
key = createKeyFromXml(resourceMapping, mASKContext, context, currentRow, keyboardDimens, (int) x, (int) y, parser);
key.width -= keyHorizontalGap;// the gap is on both
// sides
if (m.isTopRow)
keys.add(m.keysCount, key);
else
keys.add(key);
m.keysCount++;
m.rowHeight = Math.max(key.height, m.rowHeight);
}
} else if (event == XmlResourceParser.END_TAG) {
if (inKey) {
inKey = false;
x += (key.gap + key.width);
x += (keyHorizontalGap / 2);
if (x > m.rowWidth) {
m.rowWidth = (int) x;
// We keep generic row max width updated
mMaxGenericRowsWidth = Math.max(mMaxGenericRowsWidth, m.rowWidth);
}
} else if (inRow) {
inRow = false;
y += currentRow.verticalGap;
y += m.rowHeight;
y += rowVerticalGap;
}
}
}
} catch (Exception e) {
Log.e(TAG, "Parse error:" + e);
e.printStackTrace();
}
// mTotalHeight = y - mDefaultVerticalGap;
return m;
}
private void skipToEndOfRow(XmlResourceParser parser)
throws XmlPullParserException, IOException {
int event;
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.END_TAG
&& parser.getName().equals(TAG_ROW)) {
break;
}
}
}
/* required overrides */
@Override
public int getHeight() {
return super.getHeight() + mGenericRowsHeight;
}
// minWidth is actually 'total width', see android framework source code
@Override
public int getMinWidth() {
return Math.max(mMaxGenericRowsWidth, super.getMinWidth());
}
@Override
public int getShiftKeyIndex() {
return super.getShiftKeyIndex() + mTopRowKeysCount;
}
private void setKeyIcons(Key key, Resources localResources, @DrawableRes int iconId,
int iconFeedbackId) {
try {
key.icon = localResources.getDrawable(iconId);
if (iconFeedbackId > 0) {
Drawable preview = localResources.getDrawable(iconFeedbackId);
KeyboardSupport.updateDrawableBounds(preview);
key.iconPreview = preview;
}
} catch (OutOfMemoryError m) {
Log.w(TAG,
"Low memory when trying to load key icon. I'll leave it empty.");
}
if (key.icon != null)
key.label = null;
}
public Context getKeyboardContext() {
return mKeyboardContext;
}
public abstract String getDefaultDictionaryLocale();
public Locale getLocale() {
return Locale.getDefault();
}
// this function is called from within the super constructor.
@Override
protected Key createKeyFromXml(@NonNull AddOn.AddOnResourceMapping resourceMapping, Context askContext, Context keyboardContext,
Row parent, KeyboardDimens keyboardDimens, int x, int y,
XmlResourceParser parser) {
AnyKey key = new AnyKey(resourceMapping, askContext, keyboardContext, parent, keyboardDimens, x, y, parser);
if (key.codes.length > 0) {
final int primaryCode = key.codes[0];
// creating less sensitive keys if required
switch (primaryCode) {
case KeyCodes.DISABLED:// disabled
key.disable();
break;
case KeyCodes.ENTER:// enter
key = mEnterKey = new EnterKey(resourceMapping, mASKContext, keyboardContext, parent, keyboardDimens, x, y, parser);
break;
case KeyCodes.SHIFT:
mShiftKey = key;// I want the reference used by the super.
break;
case KeyCodes.CTRL:
mControlKey = key;
break;
// case KeyCodes.DELETE://delete
// key = new LessSensitiveAnyKey(mASKContext, res, parent, x, y,
// parser);
// break;
}
}
setPopupKeyChars(key);
return key;
}
@Override
protected Row createRowFromXml(@NonNull AddOn.AddOnResourceMapping resourceMapping, Resources res, XmlResourceParser parser) {
Row aRow = super.createRowFromXml(resourceMapping, res, parser);
if (aRow.mode > 0)
aRow.mode = res.getInteger(aRow.mode);// switching to the mode!
if ((aRow.rowEdgeFlags & Keyboard.EDGE_TOP) != 0)
mTopRowWasCreated = true;
if ((aRow.rowEdgeFlags & Keyboard.EDGE_BOTTOM) != 0)
mBottomRowWasCreated = true;
return aRow;
}
private boolean isAlphabetKey(Key key) {
return (!key.modifier) && (!key.sticky) && (!key.repeatable) &&
/* (key.icon == null) && */
(key.getPrimaryCode() > 0);
}
public boolean isStartOfWordLetter(char keyValue) {
return Character.isLetter(keyValue)/* || (keyValue == '\'') */;
}
public boolean isInnerWordLetter(char keyValue) {
return Character.isLetter(keyValue) || (keyValue == '\'');
}
public abstract char[] getSentenceSeparators();
/**
* This looks at the ime options given by the current editor, to set the
* appropriate label on the keyboard's enter key (if it has one).
*/
public void setImeOptions(Resources res, EditorInfo editor) {
if (mEnterKey == null) {
return;
}
mEnterKey.enable();
}
public abstract String getKeyboardName();
public boolean isLeftToRightLanguage() {
return !mRightToLeftLayout;
}
public abstract int getKeyboardIconResId();
public boolean setShiftLocked(boolean shiftLocked) {
if (keyboardSupportShift()) {
final int initialState = mShiftState;
if (shiftLocked) {
mShiftState = STICKY_KEY_LOCKED;
} else if (mShiftState == STICKY_KEY_LOCKED) {
mShiftState = STICKY_KEY_ON;
}
setShiftViewAsState();
return initialState != mShiftState;
}
return false;
}
@Override
public boolean isShifted() {
if (keyboardSupportShift()) {
return mShiftState != STICKY_KEY_OFF;
} else {
return false;
}
}
@Override
public boolean setShifted(boolean shiftState) {
if (keyboardSupportShift()) {
final int initialState = mShiftState;
if (shiftState) {
if (mShiftState == STICKY_KEY_OFF) {// so it is not LOCKED
mShiftState = STICKY_KEY_ON;
}
// else this is already ON, or at caps lock.
} else {
mShiftState = STICKY_KEY_OFF;
}
setShiftViewAsState();
return mShiftState != initialState;
} else {
super.setShifted(shiftState);
return false;
}
}
protected boolean keyboardSupportShift() {
return mShiftKey != null;
}
private void setShiftViewAsState() {
// the "on" led is just like the caps-lock led
if (mShiftKey != null)
mShiftKey.on = (mShiftState == STICKY_KEY_LOCKED);
}
public boolean isShiftLocked() {
return mShiftState == STICKY_KEY_LOCKED;
}
public boolean isControl() {
if (mControlKey != null) {
return mControlState != STICKY_KEY_OFF;
} else {
return false;
}
}
public boolean setControl(boolean control) {
if (mControlKey != null) {
final int initialState = mControlState;
if (control) {
if (mControlState == STICKY_KEY_OFF) {// so it is not LOCKED
mControlState = STICKY_KEY_ON;
}
// else this is already ON, or at caps lock.
} else {
mControlState = STICKY_KEY_OFF;
}
setControlViewAsState();
return mControlState != initialState;
} else {
return false;
}
}
private void setControlViewAsState() {
// the "on" led is just like the caps-lock led
mControlKey.on = (mControlState == STICKY_KEY_LOCKED);
}
/*
* public boolean isControlLocked() { return mControlState ==
* STICKY_KEY_LOCKED; }
*/
protected void setPopupKeyChars(Key aKey) {
}
public static class AnyKey extends Keyboard.Key {
@NonNull
protected int[] shiftedCodes = new int[0];
public CharSequence shiftedKeyLabel;
public CharSequence hintLabel;
public int longPressCode;
private boolean mFunctionalKey;
private boolean mEnabled;
public AnyKey(Row row, KeyboardDimens keyboardDimens) {
super(row, keyboardDimens);
}
public AnyKey(@NonNull AddOn.AddOnResourceMapping resourceMapping, Context askContext, Context keyboardContext, Keyboard.Row parent,
KeyboardDimens keyboardDimens, int x, int y, XmlResourceParser parser) {
super(resourceMapping, askContext, keyboardContext, parent, keyboardDimens, x, y, parser);
//setting up some defaults
mEnabled = true;
mFunctionalKey = false;
longPressCode = 0;
shiftedKeyLabel = null;
hintLabel = null;
TypedArray a = keyboardContext.obtainStyledAttributes(Xml.asAttributeSet(parser), resourceMapping.getRemoteStyleableArrayFromLocal(R.styleable.KeyboardLayout_Key));
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
final int remoteIndex = a.getIndex(i);
final int localAttrId = R.styleable.KeyboardLayout_Key[remoteIndex];
try {
switch (localAttrId) {
case R.attr.shiftedCodes:
shiftedCodes = KeyboardSupport.getKeyCodesFromTypedArray(a, remoteIndex);
break;
case R.attr.longPressCode:
longPressCode = a.getInt(remoteIndex, 0);
break;
case R.attr.isFunctional:
mFunctionalKey = a.getBoolean(remoteIndex, false);
break;
case R.attr.shiftedKeyLabel:
shiftedKeyLabel = a.getString(remoteIndex);
break;
case R.attr.hintLabel:
hintLabel = a.getString(remoteIndex);
break;
}
} catch (Exception e) {
Log.w(TAG, "Failed to set data from XML!", e);
}
}
a.recycle();
// ensuring codes and shiftedCodes are the same size
if (shiftedCodes.length != codes.length) {
int[] wrongSizedShiftCodes = shiftedCodes;
shiftedCodes = new int[codes.length];
int i;
for (i = 0; i < wrongSizedShiftCodes.length && i < codes.length; i++)
shiftedCodes[i] = wrongSizedShiftCodes[i];
for (/* starting from where i finished above */; i < codes.length; i++) {
final int code = codes[i];
if (Character.isLetter(code))
shiftedCodes[i] = Character.toUpperCase(code);
else
shiftedCodes[i] = code;
}
}
if (popupCharacters != null && popupCharacters.length() == 0) {
// If there is a keyboard with no keys specified in
// popupCharacters
popupResId = 0;
}
}
public int getCodeAtIndex(int index, boolean isShifted) {
return isShifted? shiftedCodes[index] : codes[index];
}
public void enable() {
mEnabled = true;
}
public void disable() {
iconPreview = null;
icon = null;
label = " ";// can not use NULL.
mEnabled = false;
}
public boolean isInside(int clickedX, int clickedY) {
return mEnabled && super.isInside(clickedX, clickedY);
}
public void setAsFunctional() {
mFunctionalKey = true;
}
@Override
public int[] getCurrentDrawableState(KeyDrawableStateProvider provider) {
if (mFunctionalKey) {
if (pressed) {
return provider.KEY_STATE_FUNCTIONAL_PRESSED;
} else {
return provider.KEY_STATE_FUNCTIONAL_NORMAL;
}
}
return super.getCurrentDrawableState(provider);
}
}
private static class EnterKey extends AnyKey {
private final int mOriginalHeight;
public EnterKey(@NonNull AddOn.AddOnResourceMapping resourceMapping, Context askContext, Context keyboardContext, Row parent,
KeyboardDimens keyboardDimens, int x, int y,
XmlResourceParser parser) {
super(resourceMapping, askContext, keyboardContext, parent, keyboardDimens, x, y, parser);
mOriginalHeight = this.height;
}
@Override
public void disable() {
if (AnyApplication.getConfig().getActionKeyInvisibleWhenRequested())
this.height = 0;
super.disable();
}
@Override
public void enable() {
this.height = mOriginalHeight;
super.enable();
}
@Override
public int[] getCurrentDrawableState(KeyDrawableStateProvider provider) {
if (pressed) {
return provider.KEY_STATE_ACTION_PRESSED;
} else {
return provider.KEY_STATE_ACTION_NORMAL;
}
}
}
public abstract String getKeyboardPrefId();
public boolean requiresProximityCorrection() {
return getKeys().size() > 20;
}
public int getKeyboardMode() {
return mKeyboardMode;
}
public void setCondensedKeys(CondenseType condenseType) {
if (mKeyboardCondenser.setCondensedKeys(condenseType))
computeNearestNeighbors();//keyboard has changed, so we need to recompute the neighbors.
}
}
|