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