diff options
author | 2012-08-29 17:15:58 +0900 | |
---|---|---|
committer | 2012-08-29 17:51:07 +0900 | |
commit | 160dc0f98e513819a6ebf11a2d65cdc851389344 (patch) | |
tree | 61e44afb67c37a7b3b80feba49cb3f07b871ef7b /java/src/com/android/inputmethod/latin | |
parent | f7a83d55845ff2e62808b78643bbaa966a98e29e (diff) | |
download | latinime-160dc0f98e513819a6ebf11a2d65cdc851389344.tar.gz latinime-160dc0f98e513819a6ebf11a2d65cdc851389344.tar.xz latinime-160dc0f98e513819a6ebf11a2d65cdc851389344.zip |
Move resource related methods to ResourceUtils
Change-Id: I14535b030e58363315dd2daecaaef4edd6990cd9
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
-rw-r--r-- | java/src/com/android/inputmethod/latin/ResourceUtils.java | 70 | ||||
-rw-r--r-- | java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java | 19 |
2 files changed, 78 insertions, 11 deletions
diff --git a/java/src/com/android/inputmethod/latin/ResourceUtils.java b/java/src/com/android/inputmethod/latin/ResourceUtils.java index e01ac3dd4..b3d7ac5bf 100644 --- a/java/src/com/android/inputmethod/latin/ResourceUtils.java +++ b/java/src/com/android/inputmethod/latin/ResourceUtils.java @@ -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; + } } diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java index 03263d274..9e8ab81b0 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java @@ -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(); |