aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/keyboard/MiniKeyboard.java
blob: 6119fa232de7be91a56aebc4b8b5f9cf8f280b57 (about) (plain) (blame)
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
/*
 * Copyright (C) 2011 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.graphics.Paint;

import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.keyboard.internal.PopupCharactersParser;
import com.android.inputmethod.latin.R;

public class MiniKeyboard extends Keyboard {
    private final int mDefaultKeyCoordX;

    private MiniKeyboard(Builder.MiniKeyboardParams params) {
        super(params);
        mDefaultKeyCoordX = params.getDefaultKeyCoordX() + params.mDefaultKeyWidth / 2;
    }

    public int getDefaultCoordX() {
        return mDefaultKeyCoordX;
    }

    public static class Builder extends KeyboardBuilder<Builder.MiniKeyboardParams> {
        private final CharSequence[] mPopupCharacters;

        public static class MiniKeyboardParams extends KeyboardParams {
            /* package */int mTopRowAdjustment;
            public int mNumRows;
            public int mNumColumns;
            public int mLeftKeys;
            public int mRightKeys; // includes default key.

            public MiniKeyboardParams() {
                super();
            }

            /* package for test */MiniKeyboardParams(int numKeys, int maxColumns, int keyWidth,
                    int rowHeight, int coordXInParent, int parentKeyboardWidth) {
                super();
                setParameters(numKeys, maxColumns, keyWidth, rowHeight, coordXInParent,
                        parentKeyboardWidth);
            }

            /**
             * Set keyboard parameters of mini keyboard.
             *
             * @param numKeys number of keys in this mini keyboard.
             * @param maxColumns number of maximum columns of this mini keyboard.
             * @param keyWidth mini keyboard key width in pixel, including horizontal gap.
             * @param rowHeight mini keyboard row height in pixel, including vertical gap.
             * @param coordXInParent coordinate x of the popup key in parent keyboard.
             * @param parentKeyboardWidth parent keyboard width in pixel.
             */
            public void setParameters(int numKeys, int maxColumns, int keyWidth, int rowHeight,
                    int coordXInParent, int parentKeyboardWidth) {
                if (parentKeyboardWidth / keyWidth < maxColumns) {
                    throw new IllegalArgumentException(
                            "Keyboard is too small to hold mini keyboard: " + parentKeyboardWidth
                                    + " " + keyWidth + " " + maxColumns);
                }
                mDefaultKeyWidth = keyWidth;
                mDefaultRowHeight = rowHeight;

                final int numRows = (numKeys + maxColumns - 1) / maxColumns;
                mNumRows = numRows;
                final int numColumns = getOptimizedColumns(numKeys, maxColumns);
                mNumColumns = numColumns;

                final int numLeftKeys = (numColumns - 1) / 2;
                final int numRightKeys = numColumns - numLeftKeys; // including default key.
                final int maxLeftKeys = coordXInParent / keyWidth;
                final int maxRightKeys = Math.max(1, (parentKeyboardWidth - coordXInParent)
                        / keyWidth);
                int leftKeys, rightKeys;
                if (numLeftKeys > maxLeftKeys) {
                    leftKeys = maxLeftKeys;
                    rightKeys = numColumns - maxLeftKeys;
                } else if (numRightKeys > maxRightKeys) {
                    leftKeys = numColumns - maxRightKeys;
                    rightKeys = maxRightKeys;
                } else {
                    leftKeys = numLeftKeys;
                    rightKeys = numRightKeys;
                }
                // Shift right if the left edge of mini keyboard is on the edge of parent keyboard
                // unless the parent key is on the left edge.
                if (leftKeys * keyWidth >= coordXInParent && leftKeys > 0) {
                    leftKeys--;
                    rightKeys++;
                }
                // Shift left if the right edge of mini keyboard is on the edge of parent keyboard
                // unless the parent key is on the right edge.
                if (rightKeys * keyWidth + coordXInParent >= parentKeyboardWidth && rightKeys > 1) {
                    leftKeys++;
                    rightKeys--;
                }
                mLeftKeys = leftKeys;
                mRightKeys = rightKeys;

                // Centering of the top row.
                final boolean onEdge = (leftKeys == 0 || rightKeys == 1);
                if (numRows < 2 || onEdge || getTopRowEmptySlots(numKeys, numColumns) % 2 == 0) {
                    mTopRowAdjustment = 0;
                } else if (mLeftKeys < mRightKeys - 1) {
                    mTopRowAdjustment = 1;
                } else {
                    mTopRowAdjustment = -1;
                }

                mWidth = mOccupiedWidth = mNumColumns * mDefaultKeyWidth;
                mHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight + mVerticalGap;
            }

            // Return key position according to column count (0 is default).
            /* package */int getColumnPos(int n) {
                final int col = n % mNumColumns;
                if (col == 0) {
                    // default position.
                    return 0;
                }
                int pos = 0;
                int right = 1; // include default position key.
                int left = 0;
                int i = 0;
                while (true) {
                    // Assign right key if available.
                    if (right < mRightKeys) {
                        pos = right;
                        right++;
                        i++;
                    }
                    if (i >= col)
                        break;
                    // Assign left key if available.
                    if (left < mLeftKeys) {
                        left++;
                        pos = -left;
                        i++;
                    }
                    if (i >= col)
                        break;
                }
                return pos;
            }

            private static int getTopRowEmptySlots(int numKeys, int numColumns) {
                final int remainingKeys = numKeys % numColumns;
                if (remainingKeys == 0) {
                    return 0;
                } else {
                    return numColumns - remainingKeys;
                }
            }

            private int getOptimizedColumns(int numKeys, int maxColumns) {
                int numColumns = Math.min(numKeys, maxColumns);
                while (getTopRowEmptySlots(numKeys, numColumns) >= mNumRows) {
                    numColumns--;
                }
                return numColumns;
            }

            public int getDefaultKeyCoordX() {
                return mLeftKeys * mDefaultKeyWidth;
            }

            public int getX(int n, int row) {
                final int x = getColumnPos(n) * mDefaultKeyWidth + getDefaultKeyCoordX();
                if (isTopRow(row)) {
                    return x + mTopRowAdjustment * (mDefaultKeyWidth / 2);
                }
                return x;
            }

            public int getY(int row) {
                return (mNumRows - 1 - row) * mDefaultRowHeight + mTopPadding;
            }

            public int getRowFlags(int row) {
                int rowFlags = 0;
                if (row == 0)
                    rowFlags |= Keyboard.EDGE_TOP;
                if (isTopRow(row))
                    rowFlags |= Keyboard.EDGE_BOTTOM;
                return rowFlags;
            }

            private boolean isTopRow(int rowCount) {
                return rowCount == mNumRows - 1;
            }
        }

        public Builder(KeyboardView view, int xmlId, Key parentKey, Keyboard parentKeyboard) {
            super(view.getContext(), new MiniKeyboardParams());
            load(parentKeyboard.mId.cloneWithNewXml(mResources.getResourceEntryName(xmlId), xmlId));

            // HACK: Current mini keyboard design totally relies on the 9-patch
            // padding about horizontal
            // and vertical key spacing. To keep the visual of mini keyboard as
            // is, these hacks are
            // needed to keep having the same horizontal and vertical key
            // spacing.
            mParams.mHorizontalGap = 0;
            mParams.mVerticalGap = mParams.mTopPadding = parentKeyboard.mVerticalGap / 2;
            // TODO: When we have correctly padded key background 9-patch
            // drawables for mini keyboard,
            // revert the above hacks and uncomment the following lines.
            // mParams.mHorizontalGap = parentKeyboard.mHorizontalGap;
            // mParams.mVerticalGap = parentKeyboard.mVerticalGap;

            mParams.mIsRtlKeyboard = parentKeyboard.mIsRtlKeyboard;
            mPopupCharacters = parentKey.mPopupCharacters;

            final int keyWidth = getMaxKeyWidth(view, mPopupCharacters, mParams.mDefaultKeyWidth);
            mParams.setParameters(mPopupCharacters.length, parentKey.mMaxMiniKeyboardColumn,
                    keyWidth, parentKeyboard.mDefaultRowHeight, parentKey.mX
                            + (mParams.mDefaultKeyWidth - keyWidth) / 2, view.getMeasuredWidth());
        }

        private static int getMaxKeyWidth(KeyboardView view, CharSequence[] popupCharacters,
                int minKeyWidth) {
            final int padding = (int) view.getContext().getResources()
                    .getDimension(R.dimen.mini_keyboard_key_horizontal_padding);
            Paint paint = null;
            int maxWidth = minKeyWidth;
            for (CharSequence popupSpec : popupCharacters) {
                final CharSequence label = PopupCharactersParser.getLabel(popupSpec.toString());
                // If the label is single letter, minKeyWidth is enough to hold
                // the label.
                if (label != null && label.length() > 1) {
                    if (paint == null) {
                        paint = new Paint();
                        paint.setAntiAlias(true);
                    }
                    final int width = (int)view.getDefaultLabelWidth(label, paint) + padding;
                    if (maxWidth < width) {
                        maxWidth = width;
                    }
                }
            }
            return maxWidth;
        }

        @Override
        public MiniKeyboard build() {
            final MiniKeyboardParams params = mParams;
            for (int n = 0; n < mPopupCharacters.length; n++) {
                final String popupSpec = mPopupCharacters[n].toString();
                final int row = n / params.mNumColumns;
                final Key key = new Key(mResources, params, popupSpec, params.getX(n, row),
                        params.getY(row), params.mDefaultKeyWidth, params.mDefaultRowHeight,
                        params.getRowFlags(row));
                params.onAddKey(key);
            }
            return new MiniKeyboard(params);
        }
    }
}