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.graphics.Typeface;
     21 
     22 import com.android.inputmethod.latin.utils.ResourceUtils;
     23 
     24 public final class KeyDrawParams {
     25     public Typeface mTypeface;
     26 
     27     public int mLetterSize;
     28     public int mLabelSize;
     29     public int mLargeLetterSize;
     30     public int mLargeLabelSize;
     31     public int mHintLetterSize;
     32     public int mShiftedLetterHintSize;
     33     public int mHintLabelSize;
     34     public int mPreviewTextSize;
     35 
     36     public ColorStateList mTextColorStateList;
     37     public int mTextInactivatedColor;
     38     public int mTextShadowColor;
     39     public int mHintLetterColor;
     40     public int mHintLabelColor;
     41     public int mShiftedLetterHintInactivatedColor;
     42     public int mShiftedLetterHintActivatedColor;
     43     public int mPreviewTextColor;
     44 
     45     public int mAnimAlpha;
     46 
     47     public KeyDrawParams() {}
     48 
     49     private KeyDrawParams(final KeyDrawParams copyFrom) {
     50         mTypeface = copyFrom.mTypeface;
     51 
     52         mLetterSize = copyFrom.mLetterSize;
     53         mLabelSize = copyFrom.mLabelSize;
     54         mLargeLetterSize = copyFrom.mLargeLetterSize;
     55         mLargeLabelSize = copyFrom.mLargeLabelSize;
     56         mHintLetterSize = copyFrom.mHintLetterSize;
     57         mShiftedLetterHintSize = copyFrom.mShiftedLetterHintSize;
     58         mHintLabelSize = copyFrom.mHintLabelSize;
     59         mPreviewTextSize = copyFrom.mPreviewTextSize;
     60 
     61         mTextColorStateList = copyFrom.mTextColorStateList;
     62         mTextInactivatedColor = copyFrom.mTextInactivatedColor;
     63         mTextShadowColor = copyFrom.mTextShadowColor;
     64         mHintLetterColor = copyFrom.mHintLetterColor;
     65         mHintLabelColor = copyFrom.mHintLabelColor;
     66         mShiftedLetterHintInactivatedColor = copyFrom.mShiftedLetterHintInactivatedColor;
     67         mShiftedLetterHintActivatedColor = copyFrom.mShiftedLetterHintActivatedColor;
     68         mPreviewTextColor = copyFrom.mPreviewTextColor;
     69 
     70         mAnimAlpha = copyFrom.mAnimAlpha;
     71     }
     72 
     73     public void updateParams(final int keyHeight, final KeyVisualAttributes attr) {
     74         if (attr == null) {
     75             return;
     76         }
     77 
     78         if (attr.mTypeface != null) {
     79             mTypeface = attr.mTypeface;
     80         }
     81 
     82         mLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight,
     83                 attr.mLetterSize, attr.mLetterRatio, mLetterSize);
     84         mLabelSize = selectTextSizeFromDimensionOrRatio(keyHeight,
     85                 attr.mLabelSize, attr.mLabelRatio, mLabelSize);
     86         mLargeLabelSize = selectTextSize(keyHeight, attr.mLargeLabelRatio, mLargeLabelSize);
     87         mLargeLetterSize = selectTextSize(keyHeight, attr.mLargeLetterRatio, mLargeLetterSize);
     88         mHintLetterSize = selectTextSize(keyHeight, attr.mHintLetterRatio, mHintLetterSize);
     89         mShiftedLetterHintSize = selectTextSize(keyHeight,
     90                 attr.mShiftedLetterHintRatio, mShiftedLetterHintSize);
     91         mHintLabelSize = selectTextSize(keyHeight, attr.mHintLabelRatio, mHintLabelSize);
     92         mPreviewTextSize = selectTextSize(keyHeight, attr.mPreviewTextRatio, mPreviewTextSize);
     93         mTextColorStateList =
     94                 attr.mTextColorStateList != null ? attr.mTextColorStateList : mTextColorStateList;
     95         mTextInactivatedColor = selectColor(attr.mTextInactivatedColor, mTextInactivatedColor);
     96         mTextShadowColor = selectColor(attr.mTextShadowColor, mTextShadowColor);
     97         mHintLetterColor = selectColor(attr.mHintLetterColor, mHintLetterColor);
     98         mHintLabelColor = selectColor(attr.mHintLabelColor, mHintLabelColor);
     99         mShiftedLetterHintInactivatedColor = selectColor(
    100                 attr.mShiftedLetterHintInactivatedColor, mShiftedLetterHintInactivatedColor);
    101         mShiftedLetterHintActivatedColor = selectColor(
    102                 attr.mShiftedLetterHintActivatedColor, mShiftedLetterHintActivatedColor);
    103         mPreviewTextColor = selectColor(attr.mPreviewTextColor, mPreviewTextColor);
    104     }
    105 
    106     public KeyDrawParams mayCloneAndUpdateParams(final int keyHeight,
    107             final KeyVisualAttributes attr) {
    108         if (attr == null) {
    109             return this;
    110         }
    111         final KeyDrawParams newParams = new KeyDrawParams(this);
    112         newParams.updateParams(keyHeight, attr);
    113         return newParams;
    114     }
    115 
    116     private static final int selectTextSizeFromDimensionOrRatio(final int keyHeight,
    117             final int dimens, final float ratio, final int defaultDimens) {
    118         if (ResourceUtils.isValidDimensionPixelSize(dimens)) {
    119             return dimens;
    120         }
    121         if (ResourceUtils.isValidFraction(ratio)) {
    122             return (int)(keyHeight * ratio);
    123         }
    124         return defaultDimens;
    125     }
    126 
    127     private static final int selectTextSize(final int keyHeight, final float ratio,
    128             final int defaultSize) {
    129         if (ResourceUtils.isValidFraction(ratio)) {
    130             return (int)(keyHeight * ratio);
    131         }
    132         return defaultSize;
    133     }
    134 
    135     private static final int selectColor(final int attrColor, final int defaultColor) {
    136         if (attrColor != 0) {
    137             return attrColor;
    138         }
    139         return defaultColor;
    140     }
    141 }
    142