KeyVisualAttributes.java revision 787e9a37b41c67e8683e854538b2743a2bc8fdcd
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.content.res.TypedArray;
20import android.graphics.Typeface;
21import android.util.SparseIntArray;
22
23import com.android.inputmethod.latin.R;
24import com.android.inputmethod.latin.utils.ResourceUtils;
25
26public final class KeyVisualAttributes {
27    public final Typeface mTypeface;
28
29    public final float mLetterRatio;
30    public final int mLetterSize;
31    public final float mLabelRatio;
32    public final int mLabelSize;
33    public final float mLargeLetterRatio;
34    public final float mLargeLabelRatio;
35    public final float mHintLetterRatio;
36    public final float mShiftedLetterHintRatio;
37    public final float mHintLabelRatio;
38    public final float mPreviewTextRatio;
39
40    public final int mTextColor;
41    public final int mTextInactivatedColor;
42    public final int mTextShadowColor;
43    public final int mFunctionalTextColor;
44    public final int mHintLetterColor;
45    public final int mHintLabelColor;
46    public final int mShiftedLetterHintInactivatedColor;
47    public final int mShiftedLetterHintActivatedColor;
48    public final int mPreviewTextColor;
49
50    public final float mHintLabelVerticalAdjustment;
51
52    private static final int[] VISUAL_ATTRIBUTE_IDS = {
53        R.styleable.Keyboard_Key_keyTypeface,
54        R.styleable.Keyboard_Key_keyLetterSize,
55        R.styleable.Keyboard_Key_keyLabelSize,
56        R.styleable.Keyboard_Key_keyLargeLetterRatio,
57        R.styleable.Keyboard_Key_keyLargeLabelRatio,
58        R.styleable.Keyboard_Key_keyHintLetterRatio,
59        R.styleable.Keyboard_Key_keyShiftedLetterHintRatio,
60        R.styleable.Keyboard_Key_keyHintLabelRatio,
61        R.styleable.Keyboard_Key_keyPreviewTextRatio,
62        R.styleable.Keyboard_Key_keyTextColor,
63        R.styleable.Keyboard_Key_keyTextInactivatedColor,
64        R.styleable.Keyboard_Key_keyTextShadowColor,
65        R.styleable.Keyboard_Key_functionalTextColor,
66        R.styleable.Keyboard_Key_keyHintLetterColor,
67        R.styleable.Keyboard_Key_keyHintLabelColor,
68        R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor,
69        R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor,
70        R.styleable.Keyboard_Key_keyPreviewTextColor,
71        R.styleable.Keyboard_Key_keyHintLabelVerticalAdjustment,
72    };
73    private static final SparseIntArray sVisualAttributeIds = new SparseIntArray();
74    private static final int ATTR_DEFINED = 1;
75    private static final int ATTR_NOT_FOUND = 0;
76    static {
77        for (final int attrId : VISUAL_ATTRIBUTE_IDS) {
78            sVisualAttributeIds.put(attrId, ATTR_DEFINED);
79        }
80    }
81
82    public static KeyVisualAttributes newInstance(final TypedArray keyAttr) {
83        final int indexCount = keyAttr.getIndexCount();
84        for (int i = 0; i < indexCount; i++) {
85            final int attrId = keyAttr.getIndex(i);
86            if (sVisualAttributeIds.get(attrId, ATTR_NOT_FOUND) == ATTR_NOT_FOUND) {
87                continue;
88            }
89            return new KeyVisualAttributes(keyAttr);
90        }
91        return null;
92    }
93
94    private KeyVisualAttributes(final TypedArray keyAttr) {
95        if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyTypeface)) {
96            mTypeface = Typeface.defaultFromStyle(
97                    keyAttr.getInt(R.styleable.Keyboard_Key_keyTypeface, Typeface.NORMAL));
98        } else {
99            mTypeface = null;
100        }
101
102        mLetterRatio = ResourceUtils.getFraction(keyAttr,
103                R.styleable.Keyboard_Key_keyLetterSize);
104        mLetterSize = ResourceUtils.getDimensionPixelSize(keyAttr,
105                R.styleable.Keyboard_Key_keyLetterSize);
106        mLabelRatio = ResourceUtils.getFraction(keyAttr,
107                R.styleable.Keyboard_Key_keyLabelSize);
108        mLabelSize = ResourceUtils.getDimensionPixelSize(keyAttr,
109                R.styleable.Keyboard_Key_keyLabelSize);
110        mLargeLetterRatio = ResourceUtils.getFraction(keyAttr,
111                R.styleable.Keyboard_Key_keyLargeLetterRatio);
112        mLargeLabelRatio = ResourceUtils.getFraction(keyAttr,
113                R.styleable.Keyboard_Key_keyLargeLabelRatio);
114        mHintLetterRatio = ResourceUtils.getFraction(keyAttr,
115                R.styleable.Keyboard_Key_keyHintLetterRatio);
116        mShiftedLetterHintRatio = ResourceUtils.getFraction(keyAttr,
117                R.styleable.Keyboard_Key_keyShiftedLetterHintRatio);
118        mHintLabelRatio = ResourceUtils.getFraction(keyAttr,
119                R.styleable.Keyboard_Key_keyHintLabelRatio);
120        mPreviewTextRatio = ResourceUtils.getFraction(keyAttr,
121                R.styleable.Keyboard_Key_keyPreviewTextRatio);
122
123        mTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyTextColor, 0);
124        mTextInactivatedColor = keyAttr.getColor(
125                R.styleable.Keyboard_Key_keyTextInactivatedColor, 0);
126        mTextShadowColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyTextShadowColor, 0);
127        mFunctionalTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_functionalTextColor, 0);
128        mHintLetterColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyHintLetterColor, 0);
129        mHintLabelColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyHintLabelColor, 0);
130        mShiftedLetterHintInactivatedColor = keyAttr.getColor(
131                R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor, 0);
132        mShiftedLetterHintActivatedColor = keyAttr.getColor(
133                R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor, 0);
134        mPreviewTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyPreviewTextColor, 0);
135
136        mHintLabelVerticalAdjustment = ResourceUtils.getFraction(keyAttr,
137                R.styleable.Keyboard_Key_keyHintLabelVerticalAdjustment, 0.0f);
138    }
139}
140