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
|
/*
* 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.latin.suggestions;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.utils.TypefaceUtils;
public final class MoreSuggestions extends Keyboard {
public final SuggestedWords mSuggestedWords;
MoreSuggestions(final MoreSuggestionsParam params, final SuggestedWords suggestedWords) {
super(params);
mSuggestedWords = suggestedWords;
}
private static final class MoreSuggestionsParam extends KeyboardParams {
private final int[] mWidths = new int[SuggestedWords.MAX_SUGGESTIONS];
private final int[] mRowNumbers = new int[SuggestedWords.MAX_SUGGESTIONS];
private final int[] mColumnOrders = new int[SuggestedWords.MAX_SUGGESTIONS];
private final int[] mNumColumnsInRow = new int[SuggestedWords.MAX_SUGGESTIONS];
private static final int MAX_COLUMNS_IN_ROW = 3;
private int mNumRows;
public Drawable mDivider;
public int mDividerWidth;
public MoreSuggestionsParam() {
super();
}
public int layout(final SuggestedWords suggestedWords, final int fromIndex,
final int maxWidth, final int minWidth, final int maxRow, final Paint paint,
final Resources res) {
clearKeys();
mDivider = res.getDrawable(R.drawable.more_suggestions_divider);
mDividerWidth = mDivider.getIntrinsicWidth();
final float padding = res.getDimension(
R.dimen.config_more_suggestions_key_horizontal_padding);
int row = 0;
int index = fromIndex;
int rowStartIndex = fromIndex;
final int size = Math.min(suggestedWords.size(), SuggestedWords.MAX_SUGGESTIONS);
while (index < size) {
final String word;
if (isIndexSubjectToAutoCorrection(suggestedWords, index)) {
// INDEX_OF_AUTO_CORRECTION and INDEX_OF_TYPED_WORD got swapped.
word = suggestedWords.getLabel(SuggestedWords.INDEX_OF_TYPED_WORD);
} else {
word = suggestedWords.getLabel(index);
}
// TODO: Should take care of text x-scaling.
mWidths[index] = (int)(TypefaceUtils.getStringWidth(word, paint) + padding);
final int numColumn = index - rowStartIndex + 1;
final int columnWidth =
(maxWidth - mDividerWidth * (numColumn - 1)) / numColumn;
if (numColumn > MAX_COLUMNS_IN_ROW
|| !fitInWidth(rowStartIndex, index + 1, columnWidth)) {
if ((row + 1) >= maxRow) {
break;
}
mNumColumnsInRow[row] = index - rowStartIndex;
rowStartIndex = index;
row++;
}
mColumnOrders[index] = index - rowStartIndex;
mRowNumbers[index] = row;
index++;
}
mNumColumnsInRow[row] = index - rowStartIndex;
mNumRows = row + 1;
mBaseWidth = mOccupiedWidth = Math.max(
minWidth, calcurateMaxRowWidth(fromIndex, index));
mBaseHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight + mVerticalGap;
return index - fromIndex;
}
private boolean fitInWidth(final int startIndex, final int endIndex, final int width) {
for (int index = startIndex; index < endIndex; index++) {
if (mWidths[index] > width)
return false;
}
return true;
}
private int calcurateMaxRowWidth(final int startIndex, final int endIndex) {
int maxRowWidth = 0;
int index = startIndex;
for (int row = 0; row < mNumRows; row++) {
final int numColumnInRow = mNumColumnsInRow[row];
int maxKeyWidth = 0;
while (index < endIndex && mRowNumbers[index] == row) {
maxKeyWidth = Math.max(maxKeyWidth, mWidths[index]);
index++;
}
maxRowWidth = Math.max(maxRowWidth,
maxKeyWidth * numColumnInRow + mDividerWidth * (numColumnInRow - 1));
}
return maxRowWidth;
}
private static final int[][] COLUMN_ORDER_TO_NUMBER = {
{ 0 }, // center
{ 1, 0 }, // right-left
{ 1, 0, 2 }, // center-left-right
};
public int getNumColumnInRow(final int index) {
return mNumColumnsInRow[mRowNumbers[index]];
}
public int getColumnNumber(final int index) {
final int columnOrder = mColumnOrders[index];
final int numColumn = getNumColumnInRow(index);
return COLUMN_ORDER_TO_NUMBER[numColumn - 1][columnOrder];
}
public int getX(final int index) {
final int columnNumber = getColumnNumber(index);
return columnNumber * (getWidth(index) + mDividerWidth);
}
public int getY(final int index) {
final int row = mRowNumbers[index];
return (mNumRows -1 - row) * mDefaultRowHeight + mTopPadding;
}
public int getWidth(final int index) {
final int numColumnInRow = getNumColumnInRow(index);
return (mOccupiedWidth - mDividerWidth * (numColumnInRow - 1)) / numColumnInRow;
}
public void markAsEdgeKey(final Key key, final int index) {
final int row = mRowNumbers[index];
if (row == 0)
key.markAsBottomEdge(this);
if (row == mNumRows - 1)
key.markAsTopEdge(this);
final int numColumnInRow = mNumColumnsInRow[row];
final int column = getColumnNumber(index);
if (column == 0)
key.markAsLeftEdge(this);
if (column == numColumnInRow - 1)
key.markAsRightEdge(this);
}
}
static boolean isIndexSubjectToAutoCorrection(final SuggestedWords suggestedWords,
final int index) {
return suggestedWords.mWillAutoCorrect && index == SuggestedWords.INDEX_OF_AUTO_CORRECTION;
}
public static final class Builder extends KeyboardBuilder<MoreSuggestionsParam> {
private final MoreSuggestionsView mPaneView;
private SuggestedWords mSuggestedWords;
private int mFromIndex;
private int mToIndex;
public Builder(final Context context, final MoreSuggestionsView paneView) {
super(context, new MoreSuggestionsParam());
mPaneView = paneView;
}
public Builder layout(final SuggestedWords suggestedWords, final int fromIndex,
final int maxWidth, final int minWidth, final int maxRow,
final Keyboard parentKeyboard) {
final int xmlId = R.xml.kbd_suggestions_pane_template;
load(xmlId, parentKeyboard.mId);
mParams.mVerticalGap = mParams.mTopPadding = parentKeyboard.mVerticalGap / 2;
mPaneView.updateKeyboardGeometry(mParams.mDefaultRowHeight);
final int count = mParams.layout(suggestedWords, fromIndex, maxWidth, minWidth, maxRow,
mPaneView.newLabelPaint(null /* key */), mResources);
mFromIndex = fromIndex;
mToIndex = fromIndex + count;
mSuggestedWords = suggestedWords;
return this;
}
@Override
public MoreSuggestions build() {
final MoreSuggestionsParam params = mParams;
for (int index = mFromIndex; index < mToIndex; index++) {
final int x = params.getX(index);
final int y = params.getY(index);
final int width = params.getWidth(index);
final String word;
final String info;
if (isIndexSubjectToAutoCorrection(mSuggestedWords, index)) {
// INDEX_OF_AUTO_CORRECTION and INDEX_OF_TYPED_WORD got swapped.
word = mSuggestedWords.getLabel(SuggestedWords.INDEX_OF_TYPED_WORD);
info = mSuggestedWords.getDebugString(SuggestedWords.INDEX_OF_TYPED_WORD);
} else {
word = mSuggestedWords.getLabel(index);
info = mSuggestedWords.getDebugString(index);
}
final Key key = new MoreSuggestionKey(word, info, index, params);
params.markAsEdgeKey(key, index);
params.onAddKey(key);
final int columnNumber = params.getColumnNumber(index);
final int numColumnInRow = params.getNumColumnInRow(index);
if (columnNumber < numColumnInRow - 1) {
final Divider divider = new Divider(params, params.mDivider, x + width, y,
params.mDividerWidth, params.mDefaultRowHeight);
params.onAddKey(divider);
}
}
return new MoreSuggestions(params, mSuggestedWords);
}
}
static final class MoreSuggestionKey extends Key {
public final int mSuggestedWordIndex;
public MoreSuggestionKey(final String word, final String info, final int index,
final MoreSuggestionsParam params) {
super(word /* label */, KeyboardIconsSet.ICON_UNDEFINED, Constants.CODE_OUTPUT_TEXT,
word /* outputText */, info, 0 /* labelFlags */, Key.BACKGROUND_TYPE_NORMAL,
params.getX(index), params.getY(index), params.getWidth(index),
params.mDefaultRowHeight, params.mHorizontalGap, params.mVerticalGap);
mSuggestedWordIndex = index;
}
}
private static final class Divider extends Key.Spacer {
private final Drawable mIcon;
public Divider(final KeyboardParams params, final Drawable icon, final int x,
final int y, final int width, final int height) {
super(params, x, y, width, height);
mIcon = icon;
}
@Override
public Drawable getIcon(final KeyboardIconsSet iconSet, final int alpha) {
// KeyboardIconsSet and alpha are unused. Use the icon that has been passed to the
// constructor.
// TODO: Drawable itself should have an alpha value.
mIcon.setAlpha(128);
return mIcon;
}
}
}
|