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