KeyVisualAttributes.java revision 0b5d12c6b4df6ddea2514a6646feb7db68e37ff4
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    public final float mLabelOffCenterRatio;
52    public final float mHintLabelOffCenterRatio;
53
54    private static final int[] VISUAL_ATTRIBUTE_IDS = {
55        R.styleable.Keyboard_Key_keyTypeface,
56        R.styleable.Keyboard_Key_keyLetterSize,
57        R.styleable.Keyboard_Key_keyLabelSize,
58        R.styleable.Keyboard_Key_keyLargeLetterRatio,
59        R.styleable.Keyboard_Key_keyLargeLabelRatio,
60        R.styleable.Keyboard_Key_keyHintLetterRatio,
61        R.styleable.Keyboard_Key_keyShiftedLetterHintRatio,
62        R.styleable.Keyboard_Key_keyHintLabelRatio,
63        R.styleable.Keyboard_Key_keyPreviewTextRatio,
64        R.styleable.Keyboard_Key_keyTextColor,
65        R.styleable.Keyboard_Key_keyTextInactivatedColor,
66        R.styleable.Keyboard_Key_keyTextShadowColor,
67        R.styleable.Keyboard_Key_functionalTextColor,
68        R.styleable.Keyboard_Key_keyHintLetterColor,
69        R.styleable.Keyboard_Key_keyHintLabelColor,
70        R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor,
71        R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor,
72        R.styleable.Keyboard_Key_keyPreviewTextColor,
73        R.styleable.Keyboard_Key_keyHintLabelVerticalAdjustment,
74        R.styleable.Keyboard_Key_keyLabelOffCenterRatio,
75        R.styleable.Keyboard_Key_keyHintLabelOffCenterRatio
76    };
77    private static final SparseIntArray sVisualAttributeIds = new SparseIntArray();
78    private static final int ATTR_DEFINED = 1;
79    private static final int ATTR_NOT_FOUND = 0;
80    static {
81        for (final int attrId : VISUAL_ATTRIBUTE_IDS) {
82            sVisualAttributeIds.put(attrId, ATTR_DEFINED);
83        }
84    }
85
86    public static KeyVisualAttributes newInstance(final TypedArray keyAttr) {
87        final int indexCount = keyAttr.getIndexCount();
88        for (int i = 0; i < indexCount; i++) {
89            final int attrId = keyAttr.getIndex(i);
90            if (sVisualAttributeIds.get(attrId, ATTR_NOT_FOUND) == ATTR_NOT_FOUND) {
91                continue;
92            }
93            return new KeyVisualAttributes(keyAttr);
94        }
95        return null;
96    }
97
98    private KeyVisualAttributes(final TypedArray keyAttr) {
99        if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyTypeface)) {
100            mTypeface = Typeface.defaultFromStyle(
101                    keyAttr.getInt(R.styleable.Keyboard_Key_keyTypeface, Typeface.NORMAL));
102        } else {
103            mTypeface = null;
104        }
105
106        mLetterRatio = ResourceUtils.getFraction(keyAttr,
107                R.styleable.Keyboard_Key_keyLetterSize);
108        mLetterSize = ResourceUtils.getDimensionPixelSize(keyAttr,
109                R.styleable.Keyboard_Key_keyLetterSize);
110        mLabelRatio = ResourceUtils.getFraction(keyAttr,
111                R.styleable.Keyboard_Key_keyLabelSize);
112        mLabelSize = ResourceUtils.getDimensionPixelSize(keyAttr,
113                R.styleable.Keyboard_Key_keyLabelSize);
114        mLargeLetterRatio = ResourceUtils.getFraction(keyAttr,
115                R.styleable.Keyboard_Key_keyLargeLetterRatio);
116        mLargeLabelRatio = ResourceUtils.getFraction(keyAttr,
117                R.styleable.Keyboard_Key_keyLargeLabelRatio);
118        mHintLetterRatio = ResourceUtils.getFraction(keyAttr,
119                R.styleable.Keyboard_Key_keyHintLetterRatio);
120        mShiftedLetterHintRatio = ResourceUtils.getFraction(keyAttr,
121                R.styleable.Keyboard_Key_keyShiftedLetterHintRatio);
122        mHintLabelRatio = ResourceUtils.getFraction(keyAttr,
123                R.styleable.Keyboard_Key_keyHintLabelRatio);
124        mPreviewTextRatio = ResourceUtils.getFraction(keyAttr,
125                R.styleable.Keyboard_Key_keyPreviewTextRatio);
126
127        mTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyTextColor, 0);
128        mTextInactivatedColor = keyAttr.getColor(
129                R.styleable.Keyboard_Key_keyTextInactivatedColor, 0);
130        mTextShadowColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyTextShadowColor, 0);
131        mFunctionalTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_functionalTextColor, 0);
132        mHintLetterColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyHintLetterColor, 0);
133        mHintLabelColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyHintLabelColor, 0);
134        mShiftedLetterHintInactivatedColor = keyAttr.getColor(
135                R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor, 0);
136        mShiftedLetterHintActivatedColor = keyAttr.getColor(
137                R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor, 0);
138        mPreviewTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyPreviewTextColor, 0);
139
140        mHintLabelVerticalAdjustment = ResourceUtils.getFraction(keyAttr,
141                R.styleable.Keyboard_Key_keyHintLabelVerticalAdjustment, 0.0f);
142        mLabelOffCenterRatio = ResourceUtils.getFraction(keyAttr,
143                R.styleable.Keyboard_Key_keyLabelOffCenterRatio, 0.0f);
144        mHintLabelOffCenterRatio = ResourceUtils.getFraction(keyAttr,
145                R.styleable.Keyboard_Key_keyHintLabelOffCenterRatio, 0.0f);
146    }
147}
148