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"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.keyboard.internal;
     18 
     19 import android.util.SparseIntArray;
     20 
     21 import com.android.inputmethod.keyboard.Key;
     22 import com.android.inputmethod.keyboard.Keyboard;
     23 import com.android.inputmethod.keyboard.KeyboardId;
     24 import com.android.inputmethod.latin.CollectionUtils;
     25 
     26 import java.util.ArrayList;
     27 import java.util.TreeSet;
     28 
     29 public class KeyboardParams {
     30     public KeyboardId mId;
     31     public int mThemeId;
     32 
     33     /** Total height and width of the keyboard, including the paddings and keys */
     34     public int mOccupiedHeight;
     35     public int mOccupiedWidth;
     36 
     37     /** Base height and width of the keyboard used to calculate rows' or keys' heights and
     38      *  widths
     39      */
     40     public int mBaseHeight;
     41     public int mBaseWidth;
     42 
     43     public int mTopPadding;
     44     public int mBottomPadding;
     45     public int mHorizontalEdgesPadding;
     46     public int mHorizontalCenterPadding;
     47 
     48     public KeyVisualAttributes mKeyVisualAttributes;
     49 
     50     public int mDefaultRowHeight;
     51     public int mDefaultKeyWidth;
     52     public int mHorizontalGap;
     53     public int mVerticalGap;
     54 
     55     public int mMoreKeysTemplate;
     56     public int mMaxMoreKeysKeyboardColumn;
     57 
     58     public int GRID_WIDTH;
     59     public int GRID_HEIGHT;
     60 
     61     public final TreeSet<Key> mKeys = CollectionUtils.newTreeSet(); // ordered set
     62     public final ArrayList<Key> mShiftKeys = CollectionUtils.newArrayList();
     63     public final ArrayList<Key> mAltCodeKeysWhileTyping = CollectionUtils.newArrayList();
     64     public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet();
     65     public final KeyboardCodesSet mCodesSet = new KeyboardCodesSet();
     66     public final KeyboardTextsSet mTextsSet = new KeyboardTextsSet();
     67     public final KeyStylesSet mKeyStyles = new KeyStylesSet(mTextsSet);
     68 
     69     public KeysCache mKeysCache;
     70 
     71     public int mMostCommonKeyHeight = 0;
     72     public int mMostCommonKeyWidth = 0;
     73 
     74     public boolean mProximityCharsCorrectionEnabled;
     75 
     76     public final TouchPositionCorrection mTouchPositionCorrection =
     77             new TouchPositionCorrection();
     78 
     79     protected void clearKeys() {
     80         mKeys.clear();
     81         mShiftKeys.clear();
     82         clearHistogram();
     83     }
     84 
     85     public void onAddKey(final Key newKey) {
     86         final Key key = (mKeysCache != null) ? mKeysCache.get(newKey) : newKey;
     87         final boolean zeroWidthSpacer = key.isSpacer() && key.mWidth == 0;
     88         if (!zeroWidthSpacer) {
     89             mKeys.add(key);
     90             updateHistogram(key);
     91         }
     92         if (key.mCode == Keyboard.CODE_SHIFT) {
     93             mShiftKeys.add(key);
     94         }
     95         if (key.altCodeWhileTyping()) {
     96             mAltCodeKeysWhileTyping.add(key);
     97         }
     98     }
     99 
    100     private int mMaxHeightCount = 0;
    101     private int mMaxWidthCount = 0;
    102     private final SparseIntArray mHeightHistogram = new SparseIntArray();
    103     private final SparseIntArray mWidthHistogram = new SparseIntArray();
    104 
    105     private void clearHistogram() {
    106         mMostCommonKeyHeight = 0;
    107         mMaxHeightCount = 0;
    108         mHeightHistogram.clear();
    109 
    110         mMaxWidthCount = 0;
    111         mMostCommonKeyWidth = 0;
    112         mWidthHistogram.clear();
    113     }
    114 
    115     private static int updateHistogramCounter(final SparseIntArray histogram, final int key) {
    116         final int index = histogram.indexOfKey(key);
    117         final int count = (index >= 0 ? histogram.get(key) : 0) + 1;
    118         histogram.put(key, count);
    119         return count;
    120     }
    121 
    122     private void updateHistogram(final Key key) {
    123         final int height = key.mHeight + mVerticalGap;
    124         final int heightCount = updateHistogramCounter(mHeightHistogram, height);
    125         if (heightCount > mMaxHeightCount) {
    126             mMaxHeightCount = heightCount;
    127             mMostCommonKeyHeight = height;
    128         }
    129 
    130         final int width = key.mWidth + mHorizontalGap;
    131         final int widthCount = updateHistogramCounter(mWidthHistogram, width);
    132         if (widthCount > mMaxWidthCount) {
    133             mMaxWidthCount = widthCount;
    134             mMostCommonKeyWidth = width;
    135         }
    136     }
    137 }
    138