2011-08-24 05:44:46 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-12-16 02:26:14 +00:00
|
|
|
package com.android.inputmethod.latin.suggestions;
|
2011-08-24 05:44:46 +00:00
|
|
|
|
2011-09-08 09:57:44 +00:00
|
|
|
import android.content.res.Resources;
|
2011-08-24 05:44:46 +00:00
|
|
|
import android.graphics.Paint;
|
2011-09-08 09:57:44 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2011-08-24 05:44:46 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import com.android.inputmethod.keyboard.Key;
|
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
|
|
|
import com.android.inputmethod.keyboard.KeyboardSwitcher;
|
|
|
|
import com.android.inputmethod.keyboard.KeyboardView;
|
2012-02-02 15:18:47 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
|
2011-12-16 02:26:14 +00:00
|
|
|
import com.android.inputmethod.latin.LatinImeLogger;
|
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
import com.android.inputmethod.latin.SuggestedWords;
|
2011-08-24 05:44:46 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
|
|
|
|
|
|
|
public class MoreSuggestions extends Keyboard {
|
|
|
|
public static final int SUGGESTION_CODE_BASE = 1024;
|
|
|
|
|
2012-02-29 13:21:57 +00:00
|
|
|
MoreSuggestions(Builder.MoreSuggestionsParam params) {
|
2011-08-24 05:44:46 +00:00
|
|
|
super(params);
|
|
|
|
}
|
|
|
|
|
2011-12-18 10:54:08 +00:00
|
|
|
public static class Builder extends Keyboard.Builder<Builder.MoreSuggestionsParam> {
|
2011-12-16 02:26:14 +00:00
|
|
|
private static final boolean DBG = LatinImeLogger.sDBG;
|
|
|
|
|
2011-08-24 05:44:46 +00:00
|
|
|
private final MoreSuggestionsView mPaneView;
|
|
|
|
private SuggestedWords mSuggestions;
|
|
|
|
private int mFromPos;
|
|
|
|
private int mToPos;
|
|
|
|
|
2011-12-18 10:54:08 +00:00
|
|
|
public static class MoreSuggestionsParam extends Keyboard.Params {
|
2011-09-01 05:54:28 +00:00
|
|
|
private final int[] mWidths = new int[SuggestionsView.MAX_SUGGESTIONS];
|
|
|
|
private final int[] mRowNumbers = new int[SuggestionsView.MAX_SUGGESTIONS];
|
|
|
|
private final int[] mColumnOrders = new int[SuggestionsView.MAX_SUGGESTIONS];
|
|
|
|
private final int[] mNumColumnsInRow = new int[SuggestionsView.MAX_SUGGESTIONS];
|
2011-08-24 05:44:46 +00:00
|
|
|
private static final int MAX_COLUMNS_IN_ROW = 3;
|
|
|
|
private int mNumRows;
|
2011-09-08 09:57:44 +00:00
|
|
|
public Drawable mDivider;
|
|
|
|
public int mDividerWidth;
|
2011-08-24 05:44:46 +00:00
|
|
|
|
2011-09-05 08:22:34 +00:00
|
|
|
public int layout(SuggestedWords suggestions, int fromPos, int maxWidth, int minWidth,
|
|
|
|
int maxRow, KeyboardView view) {
|
2011-08-24 05:44:46 +00:00
|
|
|
clearKeys();
|
|
|
|
final Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
2011-09-08 09:57:44 +00:00
|
|
|
final Resources res = view.getContext().getResources();
|
|
|
|
mDivider = res.getDrawable(R.drawable.more_suggestions_divider);
|
2012-02-29 13:21:57 +00:00
|
|
|
// TODO: Drawable itself should have an alpha value.
|
2011-09-08 09:57:44 +00:00
|
|
|
mDivider.setAlpha(128);
|
|
|
|
mDividerWidth = mDivider.getIntrinsicWidth();
|
|
|
|
final int padding = (int) res.getDimension(
|
|
|
|
R.dimen.more_suggestions_key_horizontal_padding);
|
2011-08-24 05:44:46 +00:00
|
|
|
|
|
|
|
int row = 0;
|
|
|
|
int pos = fromPos, rowStartPos = fromPos;
|
2011-09-01 05:54:28 +00:00
|
|
|
final int size = Math.min(suggestions.size(), SuggestionsView.MAX_SUGGESTIONS);
|
2011-08-24 05:44:46 +00:00
|
|
|
while (pos < size) {
|
2012-01-25 08:01:53 +00:00
|
|
|
final String word = suggestions.getWord(pos).toString();
|
2011-08-24 05:44:46 +00:00
|
|
|
// TODO: Should take care of text x-scaling.
|
|
|
|
mWidths[pos] = (int)view.getDefaultLabelWidth(word, paint) + padding;
|
|
|
|
final int numColumn = pos - rowStartPos + 1;
|
2011-09-08 09:57:44 +00:00
|
|
|
final int columnWidth =
|
|
|
|
(maxWidth - mDividerWidth * (numColumn - 1)) / numColumn;
|
2011-08-24 05:44:46 +00:00
|
|
|
if (numColumn > MAX_COLUMNS_IN_ROW
|
2011-09-08 09:57:44 +00:00
|
|
|
|| !fitInWidth(rowStartPos, pos + 1, columnWidth)) {
|
2011-09-05 08:22:34 +00:00
|
|
|
if ((row + 1) >= maxRow) {
|
2011-08-24 05:44:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mNumColumnsInRow[row] = pos - rowStartPos;
|
|
|
|
rowStartPos = pos;
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
mColumnOrders[pos] = pos - rowStartPos;
|
|
|
|
mRowNumbers[pos] = row;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
mNumColumnsInRow[row] = pos - rowStartPos;
|
|
|
|
mNumRows = row + 1;
|
2011-09-22 10:29:58 +00:00
|
|
|
mBaseWidth = mOccupiedWidth = Math.max(
|
|
|
|
minWidth, calcurateMaxRowWidth(fromPos, pos));
|
|
|
|
mBaseHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight + mVerticalGap;
|
2011-08-24 05:44:46 +00:00
|
|
|
return pos - fromPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean fitInWidth(int startPos, int endPos, int width) {
|
|
|
|
for (int pos = startPos; pos < endPos; pos++) {
|
|
|
|
if (mWidths[pos] > width)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int calcurateMaxRowWidth(int startPos, int endPos) {
|
|
|
|
int maxRowWidth = 0;
|
|
|
|
int pos = startPos;
|
|
|
|
for (int row = 0; row < mNumRows; row++) {
|
2011-09-08 09:57:44 +00:00
|
|
|
final int numColumnInRow = mNumColumnsInRow[row];
|
2011-08-24 05:44:46 +00:00
|
|
|
int maxKeyWidth = 0;
|
|
|
|
while (pos < endPos && mRowNumbers[pos] == row) {
|
|
|
|
maxKeyWidth = Math.max(maxKeyWidth, mWidths[pos]);
|
|
|
|
pos++;
|
|
|
|
}
|
2011-09-08 09:57:44 +00:00
|
|
|
maxRowWidth = Math.max(maxRowWidth,
|
|
|
|
maxKeyWidth * numColumnInRow + mDividerWidth * (numColumnInRow - 1));
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
|
|
|
return maxRowWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final int[][] COLUMN_ORDER_TO_NUMBER = {
|
|
|
|
{ 0, },
|
|
|
|
{ 1, 0, },
|
|
|
|
{ 2, 0, 1},
|
|
|
|
};
|
|
|
|
|
2011-09-08 09:57:44 +00:00
|
|
|
public int getNumColumnInRow(int pos) {
|
|
|
|
return mNumColumnsInRow[mRowNumbers[pos]];
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getColumnNumber(int pos) {
|
2011-08-24 05:44:46 +00:00
|
|
|
final int columnOrder = mColumnOrders[pos];
|
2011-09-08 09:57:44 +00:00
|
|
|
final int numColumn = getNumColumnInRow(pos);
|
2011-08-24 05:44:46 +00:00
|
|
|
return COLUMN_ORDER_TO_NUMBER[numColumn - 1][columnOrder];
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getX(int pos) {
|
|
|
|
final int columnNumber = getColumnNumber(pos);
|
2011-09-08 09:57:44 +00:00
|
|
|
return columnNumber * (getWidth(pos) + mDividerWidth);
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getY(int pos) {
|
|
|
|
final int row = mRowNumbers[pos];
|
|
|
|
return (mNumRows -1 - row) * mDefaultRowHeight + mTopPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getWidth(int pos) {
|
2011-09-08 09:57:44 +00:00
|
|
|
final int numColumnInRow = getNumColumnInRow(pos);
|
2011-09-22 10:29:58 +00:00
|
|
|
return (mOccupiedWidth - mDividerWidth * (numColumnInRow - 1)) / numColumnInRow;
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
|
|
|
|
2011-09-28 09:51:57 +00:00
|
|
|
public void markAsEdgeKey(Key key, int pos) {
|
2011-08-24 05:44:46 +00:00
|
|
|
final int row = mRowNumbers[pos];
|
|
|
|
if (row == 0)
|
2011-09-28 09:51:57 +00:00
|
|
|
key.markAsBottomEdge(this);
|
2011-08-24 05:44:46 +00:00
|
|
|
if (row == mNumRows - 1)
|
2011-09-28 09:51:57 +00:00
|
|
|
key.markAsTopEdge(this);
|
2011-08-24 05:44:46 +00:00
|
|
|
|
2011-09-08 09:57:44 +00:00
|
|
|
final int numColumnInRow = mNumColumnsInRow[row];
|
2011-08-24 05:44:46 +00:00
|
|
|
final int column = getColumnNumber(pos);
|
|
|
|
if (column == 0)
|
2011-09-28 09:51:57 +00:00
|
|
|
key.markAsLeftEdge(this);
|
2011-09-08 09:57:44 +00:00
|
|
|
if (column == numColumnInRow - 1)
|
2011-09-28 09:51:57 +00:00
|
|
|
key.markAsRightEdge(this);
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder(MoreSuggestionsView paneView) {
|
|
|
|
super(paneView.getContext(), new MoreSuggestionsParam());
|
|
|
|
mPaneView = paneView;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder layout(SuggestedWords suggestions, int fromPos, int maxWidth,
|
2011-09-05 08:22:34 +00:00
|
|
|
int minWidth, int maxRow) {
|
2011-12-17 23:36:16 +00:00
|
|
|
final Keyboard keyboard = KeyboardSwitcher.getInstance().getKeyboard();
|
2011-08-24 05:44:46 +00:00
|
|
|
final int xmlId = R.xml.kbd_suggestions_pane_template;
|
2011-12-15 05:45:14 +00:00
|
|
|
load(xmlId, keyboard.mId);
|
2011-08-24 05:44:46 +00:00
|
|
|
mParams.mVerticalGap = mParams.mTopPadding = keyboard.mVerticalGap / 2;
|
|
|
|
|
2011-09-05 08:22:34 +00:00
|
|
|
final int count = mParams.layout(suggestions, fromPos, maxWidth, minWidth, maxRow,
|
|
|
|
mPaneView);
|
2011-08-24 05:44:46 +00:00
|
|
|
mFromPos = fromPos;
|
|
|
|
mToPos = fromPos + count;
|
|
|
|
mSuggestions = suggestions;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getDebugInfo(SuggestedWords suggestions, int pos) {
|
|
|
|
if (!DBG) return null;
|
|
|
|
final SuggestedWordInfo wordInfo = suggestions.getInfo(pos);
|
|
|
|
if (wordInfo == null) return null;
|
|
|
|
final String info = wordInfo.getDebugString();
|
|
|
|
if (TextUtils.isEmpty(info)) return null;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
private static class Divider extends Key.Spacer {
|
|
|
|
private final Drawable mIcon;
|
|
|
|
|
|
|
|
public Divider(Keyboard.Params params, Drawable icon, int x, int y, int width,
|
|
|
|
int height) {
|
|
|
|
super(params, x, y, width, height);
|
|
|
|
mIcon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Drawable getIcon(KeyboardIconsSet iconSet) {
|
|
|
|
return mIcon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 05:44:46 +00:00
|
|
|
@Override
|
|
|
|
public MoreSuggestions build() {
|
|
|
|
final MoreSuggestionsParam params = mParams;
|
|
|
|
for (int pos = mFromPos; pos < mToPos; pos++) {
|
2011-09-08 09:57:44 +00:00
|
|
|
final int x = params.getX(pos);
|
|
|
|
final int y = params.getY(pos);
|
|
|
|
final int width = params.getWidth(pos);
|
2011-08-24 05:44:46 +00:00
|
|
|
final String word = mSuggestions.getWord(pos).toString();
|
|
|
|
final String info = getDebugInfo(mSuggestions, pos);
|
|
|
|
final int index = pos + SUGGESTION_CODE_BASE;
|
|
|
|
final Key key = new Key(
|
2012-02-02 15:18:47 +00:00
|
|
|
params, word, info, KeyboardIconsSet.ICON_UNDEFINED, index, null, x, y,
|
2012-02-29 09:45:13 +00:00
|
|
|
width, params.mDefaultRowHeight, 0);
|
2011-09-28 09:51:57 +00:00
|
|
|
params.markAsEdgeKey(key, pos);
|
2011-08-24 05:44:46 +00:00
|
|
|
params.onAddKey(key);
|
2011-09-08 09:57:44 +00:00
|
|
|
final int columnNumber = params.getColumnNumber(pos);
|
|
|
|
final int numColumnInRow = params.getNumColumnInRow(pos);
|
|
|
|
if (columnNumber < numColumnInRow - 1) {
|
2012-02-02 15:18:47 +00:00
|
|
|
final Divider divider = new Divider(params, params.mDivider, x + width, y,
|
2011-09-08 09:57:44 +00:00
|
|
|
params.mDividerWidth, params.mDefaultRowHeight);
|
2012-02-02 15:18:47 +00:00
|
|
|
params.onAddKey(divider);
|
2011-09-08 09:57:44 +00:00
|
|
|
}
|
2011-08-24 05:44:46 +00:00
|
|
|
}
|
|
|
|
return new MoreSuggestions(params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|