KeyDrawParams.java revision cdcaa37a372799eecf796f7002bf49bf3a5f42c1
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.keyboard.internal;
18
19import android.graphics.Typeface;
20
21import com.android.inputmethod.latin.utils.ResourceUtils;
22
23public final class KeyDrawParams {
24    public Typeface mTypeface;
25
26    public int mLetterSize;
27    public int mLabelSize;
28    public int mLargeLetterSize;
29    public int mHintLetterSize;
30    public int mShiftedLetterHintSize;
31    public int mHintLabelSize;
32    public int mPreviewTextSize;
33
34    public int mTextColor;
35    public int mTextInactivatedColor;
36    public int mTextShadowColor;
37    public int mFunctionalTextColor;
38    public int mHintLetterColor;
39    public int mHintLabelColor;
40    public int mShiftedLetterHintInactivatedColor;
41    public int mShiftedLetterHintActivatedColor;
42    public int mPreviewTextColor;
43
44    public int mAnimAlpha;
45
46    public KeyDrawParams() {}
47
48    private KeyDrawParams(final KeyDrawParams copyFrom) {
49        mTypeface = copyFrom.mTypeface;
50
51        mLetterSize = copyFrom.mLetterSize;
52        mLabelSize = copyFrom.mLabelSize;
53        mLargeLetterSize = copyFrom.mLargeLetterSize;
54        mHintLetterSize = copyFrom.mHintLetterSize;
55        mShiftedLetterHintSize = copyFrom.mShiftedLetterHintSize;
56        mHintLabelSize = copyFrom.mHintLabelSize;
57        mPreviewTextSize = copyFrom.mPreviewTextSize;
58
59        mTextColor = copyFrom.mTextColor;
60        mTextInactivatedColor = copyFrom.mTextInactivatedColor;
61        mTextShadowColor = copyFrom.mTextShadowColor;
62        mFunctionalTextColor = copyFrom.mFunctionalTextColor;
63        mHintLetterColor = copyFrom.mHintLetterColor;
64        mHintLabelColor = copyFrom.mHintLabelColor;
65        mShiftedLetterHintInactivatedColor = copyFrom.mShiftedLetterHintInactivatedColor;
66        mShiftedLetterHintActivatedColor = copyFrom.mShiftedLetterHintActivatedColor;
67        mPreviewTextColor = copyFrom.mPreviewTextColor;
68
69        mAnimAlpha = copyFrom.mAnimAlpha;
70    }
71
72    public void updateParams(final int keyHeight, final KeyVisualAttributes attr) {
73        if (attr == null) {
74            return;
75        }
76
77        if (attr.mTypeface != null) {
78            mTypeface = attr.mTypeface;
79        }
80
81        mLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight,
82                attr.mLetterSize, attr.mLetterRatio, mLetterSize);
83        mLabelSize = selectTextSizeFromDimensionOrRatio(keyHeight,
84                attr.mLabelSize, attr.mLabelRatio, mLabelSize);
85        mLargeLetterSize = selectTextSize(keyHeight, attr.mLargeLetterRatio, mLargeLetterSize);
86        mHintLetterSize = selectTextSize(keyHeight, attr.mHintLetterRatio, mHintLetterSize);
87        mShiftedLetterHintSize = selectTextSize(keyHeight,
88                attr.mShiftedLetterHintRatio, mShiftedLetterHintSize);
89        mHintLabelSize = selectTextSize(keyHeight, attr.mHintLabelRatio, mHintLabelSize);
90        mPreviewTextSize = selectTextSize(keyHeight, attr.mPreviewTextRatio, mPreviewTextSize);
91
92        mTextColor = selectColor(attr.mTextColor, mTextColor);
93        mTextInactivatedColor = selectColor(attr.mTextInactivatedColor, mTextInactivatedColor);
94        mTextShadowColor = selectColor(attr.mTextShadowColor, mTextShadowColor);
95        mFunctionalTextColor = selectColor(attr.mFunctionalTextColor, mFunctionalTextColor);
96        mHintLetterColor = selectColor(attr.mHintLetterColor, mHintLetterColor);
97        mHintLabelColor = selectColor(attr.mHintLabelColor, mHintLabelColor);
98        mShiftedLetterHintInactivatedColor = selectColor(
99                attr.mShiftedLetterHintInactivatedColor, mShiftedLetterHintInactivatedColor);
100        mShiftedLetterHintActivatedColor = selectColor(
101                attr.mShiftedLetterHintActivatedColor, mShiftedLetterHintActivatedColor);
102        mPreviewTextColor = selectColor(attr.mPreviewTextColor, mPreviewTextColor);
103    }
104
105    public KeyDrawParams mayCloneAndUpdateParams(final int keyHeight,
106            final KeyVisualAttributes attr) {
107        if (attr == null) {
108            return this;
109        }
110        final KeyDrawParams newParams = new KeyDrawParams(this);
111        newParams.updateParams(keyHeight, attr);
112        return newParams;
113    }
114
115    private static final int selectTextSizeFromDimensionOrRatio(final int keyHeight,
116            final int dimens, final float ratio, final int defaultDimens) {
117        if (ResourceUtils.isValidDimensionPixelSize(dimens)) {
118            return dimens;
119        }
120        if (ResourceUtils.isValidFraction(ratio)) {
121            return (int)(keyHeight * ratio);
122        }
123        return defaultDimens;
124    }
125
126    private static final int selectTextSize(final int keyHeight, final float ratio,
127            final int defaultSize) {
128        if (ResourceUtils.isValidFraction(ratio)) {
129            return (int)(keyHeight * ratio);
130        }
131        return defaultSize;
132    }
133
134    private static final int selectColor(final int attrColor, final int defaultColor) {
135        if (attrColor != 0) {
136            return attrColor;
137        }
138        return defaultColor;
139    }
140}
141