Skip to content

Commit a95e108

Browse files
committed
Fix 5335993, calculate correct size of lockscreen buttons
The buttons on the lockscreen were sized at startup time, before the actual size of the keyboard's container (KeyboardView) was known. Also, horizontal/vertical gaps were not taken into account in calculating perecent sizes of the keys. This change causes resize events (including the first one where the container size is finally known) to recalculate the keys' sizes and positions according to correct sizing of the container and the keyboard's gaps. Change-Id: I5ba7a401226ed4b100e5739f3405388955d97997
1 parent 4b5441a commit a95e108

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

core/java/android/inputmethodservice/Keyboard.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ public class Keyboard {
144144
/** Number of key widths from current touch point to search for nearest keys. */
145145
private static float SEARCH_DISTANCE = 1.8f;
146146

147+
private ArrayList<Row> rows = new ArrayList<Row>();
148+
147149
/**
148150
* Container for keys in the keyboard. All keys in a row are at the same Y-coordinate.
149151
* Some of the key size defaults can be overridden per row from what the {@link Keyboard}
@@ -164,6 +166,9 @@ public static class Row {
164166
public int defaultHorizontalGap;
165167
/** Vertical gap following this row. */
166168
public int verticalGap;
169+
170+
ArrayList<Key> mKeys = new ArrayList<Key>();
171+
167172
/**
168173
* Edge flags for this row of keys. Possible values that can be assigned are
169174
* {@link Keyboard#EDGE_TOP EDGE_TOP} and {@link Keyboard#EDGE_BOTTOM EDGE_BOTTOM}
@@ -256,7 +261,7 @@ public static class Key {
256261
public CharSequence text;
257262
/** Popup characters */
258263
public CharSequence popupCharacters;
259-
264+
260265
/**
261266
* Flags that specify the anchoring to edges of the keyboard for detecting touch events
262267
* that are just out of the boundary of the key. This is a bit mask of
@@ -596,11 +601,44 @@ public Keyboard(Context context, int layoutTemplateResId,
596601
column++;
597602
x += key.width + key.gap;
598603
mKeys.add(key);
604+
row.mKeys.add(key);
599605
if (x > mTotalWidth) {
600606
mTotalWidth = x;
601607
}
602608
}
603-
mTotalHeight = y + mDefaultHeight;
609+
mTotalHeight = y + mDefaultHeight;
610+
rows.add(row);
611+
}
612+
613+
final void resize(int newWidth, int newHeight) {
614+
int numRows = rows.size();
615+
for (int rowIndex = 0; rowIndex < numRows; ++rowIndex) {
616+
Row row = rows.get(rowIndex);
617+
int numKeys = row.mKeys.size();
618+
int totalGap = 0;
619+
int totalWidth = 0;
620+
for (int keyIndex = 0; keyIndex < numKeys; ++keyIndex) {
621+
Key key = row.mKeys.get(keyIndex);
622+
if (keyIndex > 0) {
623+
totalGap += key.gap;
624+
}
625+
totalWidth += key.width;
626+
}
627+
if (totalGap + totalWidth > newWidth) {
628+
int x = 0;
629+
float scaleFactor = (float)(newWidth - totalGap) / totalWidth;
630+
for (int keyIndex = 0; keyIndex < numKeys; ++keyIndex) {
631+
Key key = row.mKeys.get(keyIndex);
632+
key.width *= scaleFactor;
633+
key.x = x;
634+
x += key.width + key.gap;
635+
}
636+
}
637+
}
638+
mTotalWidth = newWidth;
639+
// TODO: This does not adjust the vertical placement according to the new size.
640+
// The main problem in the previous code was horizontal placement/size, but we should
641+
// also recalculate the vertical sizes/positions when we get this resize call.
604642
}
605643

606644
public List<Key> getKeys() {
@@ -749,7 +787,7 @@ private void loadKeyboard(Context context, XmlResourceParser parser) {
749787
Row currentRow = null;
750788
Resources res = context.getResources();
751789
boolean skipRow = false;
752-
790+
753791
try {
754792
int event;
755793
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
@@ -759,6 +797,7 @@ private void loadKeyboard(Context context, XmlResourceParser parser) {
759797
inRow = true;
760798
x = 0;
761799
currentRow = createRowFromXml(res, parser);
800+
rows.add(currentRow);
762801
skipRow = currentRow.mode != 0 && currentRow.mode != mKeyboardMode;
763802
if (skipRow) {
764803
skipToEndOfRow(parser);
@@ -781,6 +820,7 @@ private void loadKeyboard(Context context, XmlResourceParser parser) {
781820
} else if (key.codes[0] == KEYCODE_ALT) {
782821
mModifierKeys.add(key);
783822
}
823+
currentRow.mKeys.add(key);
784824
} else if (TAG_KEYBOARD.equals(tag)) {
785825
parseKeyboardAttributes(res, parser);
786826
}

core/java/android/inputmethodservice/KeyboardView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ public KeyboardView(Context context, AttributeSet attrs, int defStyle) {
376376
initGestureDetector();
377377
}
378378

379+
379380
private void initGestureDetector() {
380381
mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
381382
@Override
@@ -615,6 +616,9 @@ private void computeProximityThreshold(Keyboard keyboard) {
615616
@Override
616617
public void onSizeChanged(int w, int h, int oldw, int oldh) {
617618
super.onSizeChanged(w, h, oldw, oldh);
619+
if (mKeyboard != null) {
620+
mKeyboard.resize(w, h);
621+
}
618622
// Release the buffer, if any and it will be reallocated on the next draw
619623
mBuffer = null;
620624
}

core/res/res/layout/keyguard_screen_password_landscape.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
<com.android.internal.widget.PasswordEntryKeyboardView android:id="@+id/keyboard"
171171
android:layout_width="270dip"
172172
android:layout_height="wrap_content"
173+
android:layout_marginLeft="4dip"
173174
android:layout_marginRight="4dip"
174175
android:background="#40000000"
175176
android:layout_marginTop="5dip"

core/res/res/layout/keyguard_screen_password_portrait.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
<!-- Numeric keyboard -->
133133
<com.android.internal.widget.PasswordEntryKeyboardView android:id="@+id/keyboard"
134134
android:layout_width="match_parent"
135-
android:layout_height="wrap_content"
135+
android:layout_marginLeft="4dip"
136136
android:layout_marginRight="4dip"
137137
android:paddingTop="4dip"
138138
android:paddingBottom="4dip"

0 commit comments

Comments
 (0)