KeyboardParams.java revision 7bd714c086a78e2058543b0971ac92f5a30b2362
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 com.android.inputmethod.keyboard.Key;
20import com.android.inputmethod.keyboard.Keyboard;
21import com.android.inputmethod.keyboard.KeyboardId;
22import com.android.inputmethod.latin.LatinImeLogger;
23
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Map;
27import java.util.Set;
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 widths */
38    public int mBaseHeight;
39    public int mBaseWidth;
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 mMoreKeysTemplate;
53    public int mMaxMiniKeyboardColumn;
54
55    public int GRID_WIDTH;
56    public int GRID_HEIGHT;
57
58    public final Set<Key> mKeys = new HashSet<Key>();
59    public final Set<Key> mShiftKeys = new HashSet<Key>();
60    public final Set<Key> mShiftLockKeys = new HashSet<Key>();
61    public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet();
62
63    public int mMostCommonKeyHeight = 0;
64    public int mMostCommonKeyWidth = 0;
65
66    public final TouchPositionCorrection mTouchPositionCorrection = new TouchPositionCorrection();
67
68    public static class TouchPositionCorrection {
69        private static final int TOUCH_POSITION_CORRECTION_RECORD_SIZE = 3;
70
71        public boolean mEnabled;
72        public float[] mXs;
73        public float[] mYs;
74        public float[] mRadii;
75
76        public void load(String[] data) {
77            final int dataLength = data.length;
78            if (dataLength % TOUCH_POSITION_CORRECTION_RECORD_SIZE != 0) {
79                if (LatinImeLogger.sDBG)
80                    throw new RuntimeException(
81                            "the size of touch position correction data is invalid");
82                return;
83            }
84
85            final int length = dataLength / TOUCH_POSITION_CORRECTION_RECORD_SIZE;
86            mXs = new float[length];
87            mYs = new float[length];
88            mRadii = new float[length];
89            try {
90                for (int i = 0; i < dataLength; ++i) {
91                    final int type = i % TOUCH_POSITION_CORRECTION_RECORD_SIZE;
92                    final int index = i / TOUCH_POSITION_CORRECTION_RECORD_SIZE;
93                    final float value = Float.parseFloat(data[i]);
94                    if (type == 0) {
95                        mXs[index] = value;
96                    } else if (type == 1) {
97                        mYs[index] = value;
98                    } else {
99                        mRadii[index] = value;
100                    }
101                }
102            } catch (NumberFormatException e) {
103                if (LatinImeLogger.sDBG) {
104                    throw new RuntimeException(
105                            "the number format for touch position correction data is invalid");
106                }
107                mXs = null;
108                mYs = null;
109                mRadii = null;
110            }
111        }
112
113        public void setEnabled(boolean enabled) {
114            mEnabled = enabled;
115        }
116
117        public boolean isValid() {
118            return mEnabled && mXs != null && mYs != null && mRadii != null
119                && mXs.length > 0 && mYs.length > 0 && mRadii.length > 0;
120        }
121    }
122
123    protected void clearKeys() {
124        mKeys.clear();
125        mShiftKeys.clear();
126        mShiftLockKeys.clear();
127        clearHistogram();
128    }
129
130    public void onAddKey(Key key) {
131        mKeys.add(key);
132        updateHistogram(key);
133        if (key.mCode == Keyboard.CODE_SHIFT) {
134            mShiftKeys.add(key);
135            if (key.isSticky()) {
136                mShiftLockKeys.add(key);
137            }
138        }
139    }
140
141    private int mMaxHeightCount = 0;
142    private int mMaxWidthCount = 0;
143    private final Map<Integer, Integer> mHeightHistogram = new HashMap<Integer, Integer>();
144    private final Map<Integer, Integer> mWidthHistogram = new HashMap<Integer, Integer>();
145
146    private void clearHistogram() {
147        mMostCommonKeyHeight = 0;
148        mMaxHeightCount = 0;
149        mHeightHistogram.clear();
150
151        mMaxWidthCount = 0;
152        mMostCommonKeyWidth = 0;
153        mWidthHistogram.clear();
154    }
155
156    private static int updateHistogramCounter(Map<Integer, Integer> histogram, Integer key) {
157        final int count = (histogram.containsKey(key) ? histogram.get(key) : 0) + 1;
158        histogram.put(key, count);
159        return count;
160    }
161
162    private void updateHistogram(Key key) {
163        final Integer height = key.mHeight + key.mVerticalGap;
164        final int heightCount = updateHistogramCounter(mHeightHistogram, height);
165        if (heightCount > mMaxHeightCount) {
166            mMaxHeightCount = heightCount;
167            mMostCommonKeyHeight = height;
168        }
169
170        final Integer width = key.mWidth + key.mHorizontalGap;
171        final int widthCount = updateHistogramCounter(mWidthHistogram, width);
172        if (widthCount > mMaxWidthCount) {
173            mMaxWidthCount = widthCount;
174            mMostCommonKeyWidth = width;
175        }
176    }
177}
178