From ef2bfad5903fb55adca61dbea51984fbc7e4375f Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 28 Aug 2012 13:10:03 +0900 Subject: [PATCH] Use TypedArray.getFraction to read percentile values in resource Change-Id: Ifc8b024d190efba4cd536fd639621e841eb9970e --- java/res/values/attrs.xml | 12 +++--- java/res/values/dimens.xml | 2 +- java/res/values/styles.xml | 14 +++---- .../inputmethod/keyboard/KeyboardView.java | 31 ++++++++------- .../keyboard/MainKeyboardView.java | 4 +- .../suggestions/SuggestionStripView.java | 38 ++++++++----------- 6 files changed, 49 insertions(+), 52 deletions(-) diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml index cced45d9f..de732fb63 100644 --- a/java/res/values/attrs.xml +++ b/java/res/values/attrs.xml @@ -187,13 +187,13 @@ - - - - - + + + + + - + diff --git a/java/res/values/dimens.xml b/java/res/values/dimens.xml index c59bad302..565059b10 100644 --- a/java/res/values/dimens.xml +++ b/java/res/values/dimens.xml @@ -92,7 +92,7 @@ 18dp 27dp 3 - 36 + 36% 2.5dp diff --git a/java/res/values/styles.xml b/java/res/values/styles.xml index e5e7fed80..ccf378a69 100644 --- a/java/res/values/styles.xml +++ b/java/res/values/styles.xml @@ -138,9 +138,9 @@ @android:color/white #FFFCAE00 #FFFCAE00 - 50 + 50% @integer/suggestions_count_in_strip - @integer/center_suggestion_percentile + @fraction/center_suggestion_percentile @integer/max_more_suggestions_row @fraction/min_more_suggestions_width @@ -373,12 +373,12 @@ @android:color/holo_blue_light @android:color/holo_blue_light @android:color/holo_blue_light - 85 - 85 - 70 - 70 + 85% + 85% + 70% + 70% @integer/suggestions_count_in_strip - @integer/center_suggestion_percentile + @fraction/center_suggestion_percentile @integer/max_more_suggestions_row @fraction/min_more_suggestions_width diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 0a70605d7..f233a8625 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -84,6 +84,7 @@ 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; // XML attributes protected final float mVerticalCorrection; @@ -202,7 +203,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { private final float mKeyHintLetterRatio; private final float mKeyShiftedLetterHintRatio; private final float mKeyHintLabelRatio; - private static final float UNDEFINED_RATIO = -1.0f; public final Rect mPadding = new Rect(); public int mKeyLetterSize; @@ -220,20 +220,20 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { mKeyLetterRatio = UNDEFINED_RATIO; mKeyLetterSize = a.getDimensionPixelSize(R.styleable.KeyboardView_keyLetterSize, 0); } else { - mKeyLetterRatio = getRatio(a, R.styleable.KeyboardView_keyLetterRatio); + mKeyLetterRatio = getFraction(a, R.styleable.KeyboardView_keyLetterRatio); } if (a.hasValue(R.styleable.KeyboardView_keyLabelSize)) { mKeyLabelRatio = UNDEFINED_RATIO; mKeyLabelSize = a.getDimensionPixelSize(R.styleable.KeyboardView_keyLabelSize, 0); } else { - mKeyLabelRatio = getRatio(a, R.styleable.KeyboardView_keyLabelRatio); + mKeyLabelRatio = getFraction(a, R.styleable.KeyboardView_keyLabelRatio); } - mKeyLargeLabelRatio = getRatio(a, R.styleable.KeyboardView_keyLargeLabelRatio); - mKeyLargeLetterRatio = getRatio(a, R.styleable.KeyboardView_keyLargeLetterRatio); - mKeyHintLetterRatio = getRatio(a, R.styleable.KeyboardView_keyHintLetterRatio); - mKeyShiftedLetterHintRatio = getRatio(a, + mKeyLargeLabelRatio = getFraction(a, R.styleable.KeyboardView_keyLargeLabelRatio); + mKeyLargeLetterRatio = getFraction(a, R.styleable.KeyboardView_keyLargeLetterRatio); + mKeyHintLetterRatio = getFraction(a, R.styleable.KeyboardView_keyHintLetterRatio); + mKeyShiftedLetterHintRatio = getFraction(a, R.styleable.KeyboardView_keyShiftedLetterHintRatio); - mKeyHintLabelRatio = getRatio(a, R.styleable.KeyboardView_keyHintLabelRatio); + mKeyHintLabelRatio = getFraction(a, R.styleable.KeyboardView_keyHintLabelRatio); mKeyLabelHorizontalPadding = a.getDimension( R.styleable.KeyboardView_keyLabelHorizontalPadding, 0); mKeyHintLetterPadding = a.getDimension( @@ -260,10 +260,10 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } public void updateKeyHeight(int keyHeight) { - if (mKeyLetterRatio >= 0.0f) { + if (isValidFraction(mKeyLetterRatio)) { mKeyLetterSize = (int)(keyHeight * mKeyLetterRatio); } - if (mKeyLabelRatio >= 0.0f) { + if (isValidFraction(mKeyLabelRatio)) { mKeyLabelSize = (int)(keyHeight * mKeyLabelRatio); } mKeyLargeLabelSize = (int)(keyHeight * mKeyLargeLabelRatio); @@ -338,7 +338,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { R.styleable.KeyboardView_keyPreviewOffset, 0); mPreviewHeight = a.getDimensionPixelSize( R.styleable.KeyboardView_keyPreviewHeight, 80); - mPreviewTextRatio = getRatio(a, R.styleable.KeyboardView_keyPreviewTextRatio); + mPreviewTextRatio = getFraction(a, R.styleable.KeyboardView_keyPreviewTextRatio); mPreviewTextColor = a.getColor(R.styleable.KeyboardView_keyPreviewTextColor, 0); mLingerTimeout = a.getInt(R.styleable.KeyboardView_keyPreviewLingerTimeout, 0); @@ -387,9 +387,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { mPaint.setAntiAlias(true); } - // Read fraction value in TypedArray as float. - /* package */ static float getRatio(TypedArray a, int index) { - return a.getFraction(index, 1000, 1000, 1) / 1000.0f; + static boolean isValidFraction(final float fraction) { + return fraction >= 0.0f; + } + + static float getFraction(final TypedArray a, final int index) { + return a.getFraction(index, 1, 1, UNDEFINED_RATIO); } /** diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index 0cc0b6320..df84271e8 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -344,8 +344,8 @@ public class MainKeyboardView extends KeyboardView implements PointerTracker.Key R.styleable.MainKeyboardView_autoCorrectionSpacebarLedEnabled, false); mAutoCorrectionSpacebarLedIcon = a.getDrawable( R.styleable.MainKeyboardView_autoCorrectionSpacebarLedIcon); - mSpacebarTextRatio = a.getFraction(R.styleable.MainKeyboardView_spacebarTextRatio, - 1000, 1000, 1) / 1000.0f; + mSpacebarTextRatio = a.getFraction( + R.styleable.MainKeyboardView_spacebarTextRatio, 1, 1, 1.0f); mSpacebarTextColor = a.getColor(R.styleable.MainKeyboardView_spacebarTextColor, 0); mSpacebarTextShadowColor = a.getColor( R.styleable.MainKeyboardView_spacebarTextShadowColor, 0); diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java index afc4293c0..03263d274 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java @@ -132,7 +132,7 @@ public class SuggestionStripView extends RelativeLayout implements OnClickListen private static class SuggestionStripViewParams { private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3; - private static final int DEFAULT_CENTER_SUGGESTION_PERCENTILE = 40; + private static final float DEFAULT_CENTER_SUGGESTION_PERCENTILE = 0.40f; private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2; private static final int PUNCTUATIONS_IN_STRIP = 5; @@ -196,16 +196,16 @@ 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 = getPercent(a, - R.styleable.SuggestionStripView_alphaValidTypedWord, 100); - final float alphaTypedWord = getPercent(a, - R.styleable.SuggestionStripView_alphaTypedWord, 100); - final float alphaAutoCorrect = getPercent(a, - R.styleable.SuggestionStripView_alphaAutoCorrect, 100); - final float alphaSuggested = getPercent(a, - R.styleable.SuggestionStripView_alphaSuggested, 100); - mAlphaObsoleted = getPercent(a, - R.styleable.SuggestionStripView_alphaSuggested, 100); + final float alphaValidTypedWord = getFraction(a, + R.styleable.SuggestionStripView_alphaValidTypedWord, 1.0f); + final float alphaTypedWord = getFraction(a, + R.styleable.SuggestionStripView_alphaTypedWord, 1.0f); + final float alphaAutoCorrect = getFraction(a, + R.styleable.SuggestionStripView_alphaAutoCorrect, 1.0f); + final float alphaSuggested = getFraction(a, + R.styleable.SuggestionStripView_alphaSuggested, 1.0f); + mAlphaObsoleted = getFraction(a, + R.styleable.SuggestionStripView_alphaSuggested, 1.0f); mColorValidTypedWord = applyAlpha(a.getColor( R.styleable.SuggestionStripView_colorValidTypedWord, 0), alphaValidTypedWord); mColorTypedWord = applyAlpha(a.getColor( @@ -217,14 +217,14 @@ public class SuggestionStripView extends RelativeLayout implements OnClickListen mSuggestionsCountInStrip = a.getInt( R.styleable.SuggestionStripView_suggestionsCountInStrip, DEFAULT_SUGGESTIONS_COUNT_IN_STRIP); - mCenterSuggestionWeight = getPercent(a, + mCenterSuggestionWeight = getFraction(a, R.styleable.SuggestionStripView_centerSuggestionPercentile, DEFAULT_CENTER_SUGGESTION_PERCENTILE); mMaxMoreSuggestionsRow = a.getInt( R.styleable.SuggestionStripView_maxMoreSuggestionsRow, DEFAULT_MAX_MORE_SUGGESTIONS_ROW); - mMinMoreSuggestionsWidth = getRatio(a, - R.styleable.SuggestionStripView_minMoreSuggestionsWidth); + mMinMoreSuggestionsWidth = getFraction(a, + R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f); a.recycle(); mMoreSuggestionsHint = getMoreSuggestionsHint(res, @@ -278,14 +278,8 @@ public class SuggestionStripView extends RelativeLayout implements OnClickListen return new BitmapDrawable(res, buffer); } - // Read integer value in TypedArray as percent. - private static float getPercent(TypedArray a, int index, int defValue) { - return a.getInt(index, defValue) / 100.0f; - } - - // Read fraction value in TypedArray as float. - private static float getRatio(TypedArray a, int index) { - return a.getFraction(index, 1000, 1000, 1) / 1000.0f; + 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) {