KeyboardParams.java revision c06d0ef01ddf286080fd421829a587741b1ebc1b
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    public int mOccupiedHeight;
36    public int mOccupiedWidth;
37
38    public int mHeight;
39    public int mWidth;
40
41    public int mTopPadding;
42    public int mBottomPadding;
43    public int mHorizontalEdgesPadding;
44    public int mHorizontalCenterPadding;
45
46    public int mDefaultRowHeight;
47    public int mDefaultKeyWidth;
48    public int mHorizontalGap;
49    public int mVerticalGap;
50
51    public boolean mIsRtlKeyboard;
52    public int mPopupKeyboardResId;
53    public int mMaxPopupColumn;
54
55    public int GRID_WIDTH;
56    public int GRID_HEIGHT;
57
58    public final List<Key> mKeys = new ArrayList<Key>();
59    public final List<Key> mShiftKeys = new ArrayList<Key>();
60    public final Set<Key> mShiftLockKeys = new HashSet<Key>();
61    public final Map<Key, Drawable> mShiftedIcons = new HashMap<Key, Drawable>();
62    public final Map<Key, Drawable> mUnshiftedIcons = new HashMap<Key, Drawable>();
63    public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet();
64
65    public int mMostCommonKeyWidth = 0;
66
67    protected void clearKeys() {
68        mKeys.clear();
69        mShiftKeys.clear();
70        mShiftLockKeys.clear();
71        mShiftedIcons.clear();
72        mUnshiftedIcons.clear();
73        clearHistogram();
74    }
75
76    public void onAddKey(Key key) {
77        mKeys.add(key);
78        updateHistogram(key);
79        if (key.mCode == Keyboard.CODE_SHIFT) {
80            mShiftKeys.add(key);
81            if (key.mSticky) {
82                mShiftLockKeys.add(key);
83            }
84        }
85    }
86
87    public void addShiftedIcon(Key key, Drawable icon) {
88        mUnshiftedIcons.put(key, key.getIcon());
89        mShiftedIcons.put(key, icon);
90    }
91
92    private int mMaxCount = 0;
93    private final Map<Integer, Integer> mHistogram = new HashMap<Integer, Integer>();
94
95    private void clearHistogram() {
96        mMostCommonKeyWidth = 0;
97        mMaxCount = 0;
98        mHistogram.clear();
99    }
100
101    private void updateHistogram(Key key) {
102        final Integer width = key.mWidth + key.mHorizontalGap;
103        final int count = (mHistogram.containsKey(width) ? mHistogram.get(width) : 0) + 1;
104        mHistogram.put(width, count);
105        if (count > mMaxCount) {
106            mMaxCount = count;
107            mMostCommonKeyWidth = width;
108        }
109    }
110}
111