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.utils.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 mHintLetterRatio;
     35     public final float mShiftedLetterHintRatio;
     36     public final float mHintLabelRatio;
     37     public final float mPreviewTextRatio;
     38 
     39     public final int mTextColor;
     40     public final int mTextInactivatedColor;
     41     public final int mTextShadowColor;
     42     public final int mFunctionalTextColor;
     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     public final float mHintLabelVerticalAdjustment;
     50     public final float mLabelOffCenterRatio;
     51     public final float mHintLabelOffCenterRatio;
     52 
     53     private static final int[] VISUAL_ATTRIBUTE_IDS = {
     54         R.styleable.Keyboard_Key_keyTypeface,
     55         R.styleable.Keyboard_Key_keyLetterSize,
     56         R.styleable.Keyboard_Key_keyLabelSize,
     57         R.styleable.Keyboard_Key_keyLargeLetterRatio,
     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         R.styleable.Keyboard_Key_keyLabelOffCenterRatio,
     73         R.styleable.Keyboard_Key_keyHintLabelOffCenterRatio
     74     };
     75     private static final SparseIntArray sVisualAttributeIds = new SparseIntArray();
     76     private static final int ATTR_DEFINED = 1;
     77     private static final int ATTR_NOT_FOUND = 0;
     78     static {
     79         for (final int attrId : VISUAL_ATTRIBUTE_IDS) {
     80             sVisualAttributeIds.put(attrId, ATTR_DEFINED);
     81         }
     82     }
     83 
     84     public static KeyVisualAttributes newInstance(final TypedArray keyAttr) {
     85         final int indexCount = keyAttr.getIndexCount();
     86         for (int i = 0; i < indexCount; i++) {
     87             final int attrId = keyAttr.getIndex(i);
     88             if (sVisualAttributeIds.get(attrId, ATTR_NOT_FOUND) == ATTR_NOT_FOUND) {
     89                 continue;
     90             }
     91             return new KeyVisualAttributes(keyAttr);
     92         }
     93         return null;
     94     }
     95 
     96     private KeyVisualAttributes(final TypedArray keyAttr) {
     97         if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyTypeface)) {
     98             mTypeface = Typeface.defaultFromStyle(
     99                     keyAttr.getInt(R.styleable.Keyboard_Key_keyTypeface, Typeface.NORMAL));
    100         } else {
    101             mTypeface = null;
    102         }
    103 
    104         mLetterRatio = ResourceUtils.getFraction(keyAttr,
    105                 R.styleable.Keyboard_Key_keyLetterSize);
    106         mLetterSize = ResourceUtils.getDimensionPixelSize(keyAttr,
    107                 R.styleable.Keyboard_Key_keyLetterSize);
    108         mLabelRatio = ResourceUtils.getFraction(keyAttr,
    109                 R.styleable.Keyboard_Key_keyLabelSize);
    110         mLabelSize = ResourceUtils.getDimensionPixelSize(keyAttr,
    111                 R.styleable.Keyboard_Key_keyLabelSize);
    112         mLargeLetterRatio = ResourceUtils.getFraction(keyAttr,
    113                 R.styleable.Keyboard_Key_keyLargeLetterRatio);
    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         mLabelOffCenterRatio = ResourceUtils.getFraction(keyAttr,
    139                 R.styleable.Keyboard_Key_keyLabelOffCenterRatio, 0.0f);
    140         mHintLabelOffCenterRatio = ResourceUtils.getFraction(keyAttr,
    141                 R.styleable.Keyboard_Key_keyHintLabelOffCenterRatio, 0.0f);
    142     }
    143 }
    144