KeyboardParams.java revision 8fbfac4ffb7079e8e71fd4e3ddc04e362239ebb3
1/*
2 * Copyright (C) 2011 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
17package com.android.inputmethod.keyboard.internal;
18
19import android.graphics.drawable.Drawable;
20
21import com.android.inputmethod.keyboard.Key;
22import com.android.inputmethod.keyboard.Keyboard;
23import com.android.inputmethod.keyboard.KeyboardId;
24
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.List;
29import java.util.Map;
30import java.util.Set;
31
32public class KeyboardParams {
33    public KeyboardId mId;
34
35    /** Total height and width of the keyboard, including the paddings and keys */
36    public int mOccupiedHeight;
37    public int mOccupiedWidth;
38
39    /** Base height and width of the keyboard used to calculate rows' or keys' heights and widths */
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 int mDefaultRowHeight;
49    public int mDefaultKeyWidth;
50    public int mHorizontalGap;
51    public int mVerticalGap;
52
53    public boolean mIsRtlKeyboard;
54    public int mMoreKeysTemplate;
55    public int mMaxMiniKeyboardColumn;
56
57    public int GRID_WIDTH;
58    public int GRID_HEIGHT;
59
60    public final List<Key> mKeys = new ArrayList<Key>();
61    public final List<Key> mShiftKeys = new ArrayList<Key>();
62    public final Set<Key> mShiftLockKeys = new HashSet<Key>();
63    public final Map<Key, Drawable> mShiftedIcons = new HashMap<Key, Drawable>();
64    public final Map<Key, Drawable> mUnshiftedIcons = new HashMap<Key, Drawable>();
65    public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet();
66
67    public int mMostCommonKeyHeight = 0;
68    public int mMostCommonKeyWidth = 0;
69
70    protected void clearKeys() {
71        mKeys.clear();
72        mShiftKeys.clear();
73        mShiftLockKeys.clear();
74        mShiftedIcons.clear();
75        mUnshiftedIcons.clear();
76        clearHistogram();
77    }
78
79    public void onAddKey(Key key) {
80        mKeys.add(key);
81        updateHistogram(key);
82        if (key.mCode == Keyboard.CODE_SHIFT) {
83            mShiftKeys.add(key);
84            if (key.isSticky()) {
85                mShiftLockKeys.add(key);
86            }
87        }
88    }
89
90    public void addShiftedIcon(Key key, Drawable icon) {
91        mUnshiftedIcons.put(key, key.getIcon());
92        mShiftedIcons.put(key, icon);
93    }
94
95    private int mMaxHeightCount = 0;
96    private int mMaxWidthCount = 0;
97    private final Map<Integer, Integer> mHeightHistogram = new HashMap<Integer, Integer>();
98    private final Map<Integer, Integer> mWidthHistogram = new HashMap<Integer, Integer>();
99
100    private void clearHistogram() {
101        mMostCommonKeyHeight = 0;
102        mMaxHeightCount = 0;
103        mHeightHistogram.clear();
104
105        mMaxWidthCount = 0;
106        mMostCommonKeyWidth = 0;
107        mWidthHistogram.clear();
108    }
109
110    private static int updateHistogramCounter(Map<Integer, Integer> histogram, Integer key) {
111        final int count = (histogram.containsKey(key) ? histogram.get(key) : 0) + 1;
112        histogram.put(key, count);
113        return count;
114    }
115
116    private void updateHistogram(Key key) {
117        final Integer height = key.mHeight + key.mVerticalGap;
118        final int heightCount = updateHistogramCounter(mHeightHistogram, height);
119        if (heightCount > mMaxHeightCount) {
120            mMaxHeightCount = heightCount;
121            mMostCommonKeyHeight = height;
122        }
123
124        final Integer width = key.mWidth + key.mHorizontalGap;
125        final int widthCount = updateHistogramCounter(mWidthHistogram, width);
126        if (widthCount > mMaxWidthCount) {
127            mMaxWidthCount = widthCount;
128            mMostCommonKeyWidth = width;
129        }
130    }
131}
132