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