@@ -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 }
0 commit comments