Merge "Revert "Revert "Move resource related methods to ResourceUtils""" into jb-mr1-dev

This commit is contained in:
Tadashi G. Takaoka 2012-08-29 19:29:36 -07:00 committed by Android (Google) Code Review
commit b590d69e1f
5 changed files with 123 additions and 118 deletions

View file

@ -36,6 +36,7 @@ import com.android.inputmethod.keyboard.internal.KeySpecParser.MoreKeySpec;
import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.ResourceUtils;
import com.android.inputmethod.latin.StringUtils;
import org.xmlpull.v1.XmlPullParser;
@ -229,9 +230,9 @@ public class Key {
mBackgroundType = style.getInt(keyAttr,
R.styleable.Keyboard_Key_backgroundType, row.getDefaultBackgroundType());
mVisualInsetsLeft = Math.round(Keyboard.Builder.getDimensionOrFraction(keyAttr,
mVisualInsetsLeft = Math.round(ResourceUtils.getDimensionOrFraction(keyAttr,
R.styleable.Keyboard_Key_visualInsetsLeft, params.mBaseWidth, 0));
mVisualInsetsRight = Math.round(Keyboard.Builder.getDimensionOrFraction(keyAttr,
mVisualInsetsRight = Math.round(ResourceUtils.getDimensionOrFraction(keyAttr,
R.styleable.Keyboard_Key_visualInsetsRight, params.mBaseWidth, 0));
mIconId = KeySpecParser.getIconId(style.getString(keyAttr,
R.styleable.Keyboard_Key_keyIcon));

View file

@ -39,6 +39,7 @@ import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.ResourceUtils;
import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.SubtypeLocale;
import com.android.inputmethod.latin.XmlParseUtils;
@ -565,13 +566,13 @@ public class Keyboard {
mParams = params;
TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard);
mRowHeight = (int)Builder.getDimensionOrFraction(keyboardAttr,
mRowHeight = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_rowHeight,
params.mBaseHeight, params.mDefaultRowHeight);
keyboardAttr.recycle();
TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard_Key);
mDefaultKeyWidth = Builder.getDimensionOrFraction(keyAttr,
mDefaultKeyWidth = ResourceUtils.getDimensionOrFraction(keyAttr,
R.styleable.Keyboard_Key_keyWidth,
params.mBaseWidth, params.mDefaultKeyWidth);
mDefaultBackgroundType = keyAttr.getInt(R.styleable.Keyboard_Key_backgroundType,
@ -624,7 +625,7 @@ public class Keyboard {
final int keyboardRightEdge = mParams.mOccupiedWidth
- mParams.mHorizontalEdgesPadding;
if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyXPos)) {
final float keyXPos = Builder.getDimensionOrFraction(keyAttr,
final float keyXPos = ResourceUtils.getDimensionOrFraction(keyAttr,
R.styleable.Keyboard_Key_keyXPos, mParams.mBaseWidth, 0);
if (keyXPos < 0) {
// If keyXPos is negative, the actual x-coordinate will be
@ -645,7 +646,7 @@ public class Keyboard {
}
public float getKeyWidth(TypedArray keyAttr, float keyXPos) {
final int widthType = Builder.getEnumValue(keyAttr,
final int widthType = ResourceUtils.getEnumValue(keyAttr,
R.styleable.Keyboard_Key_keyWidth, KEYWIDTH_NOT_ENUM);
switch (widthType) {
case KEYWIDTH_FILL_RIGHT:
@ -655,7 +656,7 @@ public class Keyboard {
// out the area up to the right edge of the keyboard.
return keyboardRightEdge - keyXPos;
default: // KEYWIDTH_NOT_ENUM
return Builder.getDimensionOrFraction(keyAttr,
return ResourceUtils.getDimensionOrFraction(keyAttr,
R.styleable.Keyboard_Key_keyWidth,
mParams.mBaseWidth, mDefaultKeyWidth);
}
@ -768,14 +769,14 @@ public class Keyboard {
keyboardHeight = keyboardAttr.getDimension(
R.styleable.Keyboard_keyboardHeight, displayHeight / 2);
}
final float maxKeyboardHeight = getDimensionOrFraction(keyboardAttr,
final float maxKeyboardHeight = ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_maxKeyboardHeight, displayHeight, displayHeight / 2);
float minKeyboardHeight = getDimensionOrFraction(keyboardAttr,
float minKeyboardHeight = ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_minKeyboardHeight, displayHeight, displayHeight / 2);
if (minKeyboardHeight < 0) {
// Specified fraction was negative, so it should be calculated against display
// width.
minKeyboardHeight = -getDimensionOrFraction(keyboardAttr,
minKeyboardHeight = -ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_minKeyboardHeight, displayWidth, displayWidth / 2);
}
final Params params = mParams;
@ -784,26 +785,27 @@ public class Keyboard {
params.mOccupiedHeight = (int)Math.max(
Math.min(keyboardHeight, maxKeyboardHeight), minKeyboardHeight);
params.mOccupiedWidth = params.mId.mWidth;
params.mTopPadding = (int)getDimensionOrFraction(keyboardAttr,
params.mTopPadding = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_keyboardTopPadding, params.mOccupiedHeight, 0);
params.mBottomPadding = (int)getDimensionOrFraction(keyboardAttr,
params.mBottomPadding = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_keyboardBottomPadding, params.mOccupiedHeight, 0);
params.mHorizontalEdgesPadding = (int)getDimensionOrFraction(keyboardAttr,
params.mHorizontalEdgesPadding = (int)ResourceUtils.getDimensionOrFraction(
keyboardAttr,
R.styleable.Keyboard_keyboardHorizontalEdgesPadding,
mParams.mOccupiedWidth, 0);
params.mBaseWidth = params.mOccupiedWidth - params.mHorizontalEdgesPadding * 2
- params.mHorizontalCenterPadding;
params.mDefaultKeyWidth = (int)getDimensionOrFraction(keyAttr,
params.mDefaultKeyWidth = (int)ResourceUtils.getDimensionOrFraction(keyAttr,
R.styleable.Keyboard_Key_keyWidth, params.mBaseWidth,
params.mBaseWidth / DEFAULT_KEYBOARD_COLUMNS);
params.mHorizontalGap = (int)getDimensionOrFraction(keyboardAttr,
params.mHorizontalGap = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_horizontalGap, params.mBaseWidth, 0);
params.mVerticalGap = (int)getDimensionOrFraction(keyboardAttr,
params.mVerticalGap = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_verticalGap, params.mOccupiedHeight, 0);
params.mBaseHeight = params.mOccupiedHeight - params.mTopPadding
- params.mBottomPadding + params.mVerticalGap;
params.mDefaultRowHeight = (int)getDimensionOrFraction(keyboardAttr,
params.mDefaultRowHeight = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_rowHeight, params.mBaseHeight,
params.mBaseHeight / DEFAULT_KEYBOARD_ROWS);
@ -1221,7 +1223,7 @@ public class Keyboard {
// If <case> does not have "index" attribute, that means this <case> is wild-card for
// the attribute.
return !a.hasValue(index)
|| stringArrayContains(a.getString(index).split("\\|"), value);
|| StringUtils.containsInArray(value, a.getString(index).split("\\|"));
}
private static boolean matchTypedValue(TypedArray a, int index, int intValue,
@ -1232,19 +1234,10 @@ public class Keyboard {
if (v == null) {
return true;
}
if (isIntegerValue(v)) {
if (ResourceUtils.isIntegerValue(v)) {
return intValue == a.getInt(index, 0);
} else if (isStringValue(v)) {
return stringArrayContains(a.getString(index).split("\\|"), strValue);
}
return false;
}
private static boolean stringArrayContains(String[] array, String value) {
for (final String elem : array) {
if (elem.equals(value)) {
return true;
}
} else if (ResourceUtils.isStringValue(v)) {
return StringUtils.containsInArray(strValue, a.getString(index).split("\\|"));
}
return false;
}
@ -1334,47 +1327,6 @@ public class Keyboard {
mRightEdgeKey = null;
}
public static float getDimensionOrFraction(TypedArray a, int index, int base,
float defValue) {
final TypedValue value = a.peekValue(index);
if (value == null) {
return defValue;
}
if (isFractionValue(value)) {
return a.getFraction(index, base, base, defValue);
} else if (isDimensionValue(value)) {
return a.getDimension(index, defValue);
}
return defValue;
}
public static int getEnumValue(TypedArray a, int index, int defValue) {
final TypedValue value = a.peekValue(index);
if (value == null) {
return defValue;
}
if (isIntegerValue(value)) {
return a.getInt(index, defValue);
}
return defValue;
}
private static boolean isFractionValue(TypedValue v) {
return v.type == TypedValue.TYPE_FRACTION;
}
private static boolean isDimensionValue(TypedValue v) {
return v.type == TypedValue.TYPE_DIMENSION;
}
private static boolean isIntegerValue(TypedValue v) {
return v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT;
}
private static boolean isStringValue(TypedValue v) {
return v.type == TypedValue.TYPE_STRING;
}
private static String textAttr(String value, String name) {
return value != null ? String.format(" %s=%s", name, value) : "";
}

View file

@ -43,6 +43,7 @@ import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.ResourceUtils;
import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.define.ProductionFlag;
@ -84,9 +85,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
// Miscellaneous constants
private static final int[] LONG_PRESSABLE_STATE_SET = { android.R.attr.state_long_pressable };
private static final float UNDEFINED_RATIO = -1.0f;
private static final int UNDEFINED_DIMENSION = -1;
// XML attributes
protected final float mVerticalCorrection;
protected final int mMoreKeysLayout;
@ -219,20 +217,26 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
public KeyDrawParams(final TypedArray a) {
mKeyBackground = a.getDrawable(R.styleable.KeyboardView_keyBackground);
if (!isValidFraction(mKeyLetterRatio = getFraction(a,
if (!ResourceUtils.isValidFraction(mKeyLetterRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyLetterSize))) {
mKeyLetterSize = getDimensionPixelSize(a, R.styleable.KeyboardView_keyLetterSize);
mKeyLetterSize = ResourceUtils.getDimensionPixelSize(a,
R.styleable.KeyboardView_keyLetterSize);
}
if (!isValidFraction(mKeyLabelRatio = getFraction(a,
if (!ResourceUtils.isValidFraction(mKeyLabelRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyLabelSize))) {
mKeyLabelSize = getDimensionPixelSize(a, R.styleable.KeyboardView_keyLabelSize);
mKeyLabelSize = ResourceUtils.getDimensionPixelSize(a,
R.styleable.KeyboardView_keyLabelSize);
}
mKeyLargeLabelRatio = getFraction(a, R.styleable.KeyboardView_keyLargeLabelRatio);
mKeyLargeLetterRatio = getFraction(a, R.styleable.KeyboardView_keyLargeLetterRatio);
mKeyHintLetterRatio = getFraction(a, R.styleable.KeyboardView_keyHintLetterRatio);
mKeyShiftedLetterHintRatio = getFraction(a,
mKeyLargeLabelRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyLargeLabelRatio);
mKeyLargeLetterRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyLargeLetterRatio);
mKeyHintLetterRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyHintLetterRatio);
mKeyShiftedLetterHintRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyShiftedLetterHintRatio);
mKeyHintLabelRatio = getFraction(a, R.styleable.KeyboardView_keyHintLabelRatio);
mKeyHintLabelRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyHintLabelRatio);
mKeyLabelHorizontalPadding = a.getDimension(
R.styleable.KeyboardView_keyLabelHorizontalPadding, 0);
mKeyHintLetterPadding = a.getDimension(
@ -263,10 +267,10 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
mKeyTypeface = (keyboard.mKeyTypeface != null)
? keyboard.mKeyTypeface : mKeyTypefaceFromKeyboardView;
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
if (isValidFraction(mKeyLetterRatio)) {
if (ResourceUtils.isValidFraction(mKeyLetterRatio)) {
mKeyLetterSize = (int)(keyHeight * mKeyLetterRatio);
}
if (isValidFraction(mKeyLabelRatio)) {
if (ResourceUtils.isValidFraction(mKeyLabelRatio)) {
mKeyLabelSize = (int)(keyHeight * mKeyLabelRatio);
}
mKeyLargeLabelSize = (int)(keyHeight * mKeyLargeLabelRatio);
@ -341,14 +345,15 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
R.styleable.KeyboardView_keyPreviewOffset, 0);
mPreviewHeight = a.getDimensionPixelSize(
R.styleable.KeyboardView_keyPreviewHeight, 80);
mPreviewTextRatio = getFraction(a, R.styleable.KeyboardView_keyPreviewTextRatio);
mPreviewTextRatio = ResourceUtils.getFraction(a,
R.styleable.KeyboardView_keyPreviewTextRatio);
mPreviewTextColor = a.getColor(R.styleable.KeyboardView_keyPreviewTextColor, 0);
mLingerTimeout = a.getInt(R.styleable.KeyboardView_keyPreviewLingerTimeout, 0);
}
public void updateParams(final Keyboard keyboard, final KeyDrawParams keyDrawParams) {
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
if (isValidFraction(mPreviewTextRatio)) {
if (ResourceUtils.isValidFraction(mPreviewTextRatio)) {
mPreviewTextSize = (int)(keyHeight * mPreviewTextRatio);
}
mKeyLetterSize = keyDrawParams.mKeyLetterSize;
@ -387,26 +392,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
mPaint.setAntiAlias(true);
}
static boolean isValidFraction(final float fraction) {
return fraction >= 0.0f;
}
static float getFraction(final TypedArray a, final int index) {
final TypedValue value = a.peekValue(index);
if (value == null || value.type != TypedValue.TYPE_FRACTION) {
return UNDEFINED_RATIO;
}
return a.getFraction(index, 1, 1, UNDEFINED_RATIO);
}
public static int getDimensionPixelSize(final TypedArray a, final int index) {
final TypedValue value = a.peekValue(index);
if (value == null || value.type != TypedValue.TYPE_DIMENSION) {
return UNDEFINED_DIMENSION;
}
return a.getDimensionPixelSize(index, UNDEFINED_DIMENSION);
}
/**
* Attaches a keyboard to this view. The keyboard can be switched at any time and the
* view will re-layout itself to accommodate the keyboard.

View file

@ -17,11 +17,16 @@
package com.android.inputmethod.latin;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.TypedValue;
import java.util.HashMap;
public final class ResourceUtils {
public static final float UNDEFINED_RATIO = -1.0f;
public static final int UNDEFINED_DIMENSION = -1;
private ResourceUtils() {
// This utility class is not publicly instantiable.
}
@ -45,4 +50,69 @@ public final class ResourceUtils {
}
return sDeviceOverrideValueMap.get(key);
}
public static boolean isValidFraction(final float fraction) {
return fraction >= 0.0f;
}
public static float getFraction(final TypedArray a, final int index, final float defValue) {
final TypedValue value = a.peekValue(index);
if (value == null || !isFractionValue(value)) {
return defValue;
}
return a.getFraction(index, 1, 1, defValue);
}
public static float getFraction(final TypedArray a, final int index) {
return getFraction(a, index, UNDEFINED_RATIO);
}
public static int getDimensionPixelSize(final TypedArray a, final int index) {
final TypedValue value = a.peekValue(index);
if (value == null || !isDimensionValue(value)) {
return ResourceUtils.UNDEFINED_DIMENSION;
}
return a.getDimensionPixelSize(index, ResourceUtils.UNDEFINED_DIMENSION);
}
public static float getDimensionOrFraction(TypedArray a, int index, int base,
float defValue) {
final TypedValue value = a.peekValue(index);
if (value == null) {
return defValue;
}
if (isFractionValue(value)) {
return a.getFraction(index, base, base, defValue);
} else if (isDimensionValue(value)) {
return a.getDimension(index, defValue);
}
return defValue;
}
public static int getEnumValue(TypedArray a, int index, int defValue) {
final TypedValue value = a.peekValue(index);
if (value == null) {
return defValue;
}
if (isIntegerValue(value)) {
return a.getInt(index, defValue);
}
return defValue;
}
public static boolean isFractionValue(TypedValue v) {
return v.type == TypedValue.TYPE_FRACTION;
}
public static boolean isDimensionValue(TypedValue v) {
return v.type == TypedValue.TYPE_DIMENSION;
}
public static boolean isIntegerValue(TypedValue v) {
return v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT;
}
public static boolean isStringValue(TypedValue v) {
return v.type == TypedValue.TYPE_STRING;
}
}

View file

@ -61,6 +61,7 @@ import com.android.inputmethod.latin.AutoCorrection;
import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.ResourceUtils;
import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.Utils;
@ -196,15 +197,15 @@ public class SuggestionStripView extends RelativeLayout implements OnClickListen
R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripViewStyle);
mSuggestionStripOption = a.getInt(
R.styleable.SuggestionStripView_suggestionStripOption, 0);
final float alphaValidTypedWord = getFraction(a,
final float alphaValidTypedWord = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaValidTypedWord, 1.0f);
final float alphaTypedWord = getFraction(a,
final float alphaTypedWord = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaTypedWord, 1.0f);
final float alphaAutoCorrect = getFraction(a,
final float alphaAutoCorrect = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaAutoCorrect, 1.0f);
final float alphaSuggested = getFraction(a,
final float alphaSuggested = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaSuggested, 1.0f);
mAlphaObsoleted = getFraction(a,
mAlphaObsoleted = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaSuggested, 1.0f);
mColorValidTypedWord = applyAlpha(a.getColor(
R.styleable.SuggestionStripView_colorValidTypedWord, 0), alphaValidTypedWord);
@ -217,13 +218,13 @@ public class SuggestionStripView extends RelativeLayout implements OnClickListen
mSuggestionsCountInStrip = a.getInt(
R.styleable.SuggestionStripView_suggestionsCountInStrip,
DEFAULT_SUGGESTIONS_COUNT_IN_STRIP);
mCenterSuggestionWeight = getFraction(a,
mCenterSuggestionWeight = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_centerSuggestionPercentile,
DEFAULT_CENTER_SUGGESTION_PERCENTILE);
mMaxMoreSuggestionsRow = a.getInt(
R.styleable.SuggestionStripView_maxMoreSuggestionsRow,
DEFAULT_MAX_MORE_SUGGESTIONS_ROW);
mMinMoreSuggestionsWidth = getFraction(a,
mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f);
a.recycle();
@ -278,10 +279,6 @@ public class SuggestionStripView extends RelativeLayout implements OnClickListen
return new BitmapDrawable(res, buffer);
}
static float getFraction(final TypedArray a, final int index, final float defValue) {
return a.getFraction(index, 1, 1, defValue);
}
private CharSequence getStyledSuggestionWord(SuggestedWords suggestedWords, int pos) {
final CharSequence word = suggestedWords.getWord(pos);
final boolean isAutoCorrect = pos == 1 && suggestedWords.willAutoCorrect();