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
17package com.android.inputmethod.keyboard.internal;
18
19import android.util.SparseIntArray;
20
21import com.android.inputmethod.keyboard.Key;
22import com.android.inputmethod.keyboard.KeyboardId;
23import com.android.inputmethod.latin.CollectionUtils;
24import com.android.inputmethod.latin.Constants;
25
26import java.util.ArrayList;
27import java.util.TreeSet;
28
29public 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 mLeftPadding;
46    public int mRightPadding;
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 == Constants.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