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 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.KeyboardId; 23 import com.android.inputmethod.latin.Constants; 24 import com.android.inputmethod.latin.utils.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 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 isSpacer = key.isSpacer(); 88 if (isSpacer && key.getWidth() == 0) { 89 // Ignore zero width {@link Spacer}. 90 return; 91 } 92 mKeys.add(key); 93 if (isSpacer) { 94 return; 95 } 96 updateHistogram(key); 97 if (key.getCode() == Constants.CODE_SHIFT) { 98 mShiftKeys.add(key); 99 } 100 if (key.altCodeWhileTyping()) { 101 mAltCodeKeysWhileTyping.add(key); 102 } 103 } 104 105 private int mMaxHeightCount = 0; 106 private int mMaxWidthCount = 0; 107 private final SparseIntArray mHeightHistogram = new SparseIntArray(); 108 private final SparseIntArray mWidthHistogram = new SparseIntArray(); 109 110 private void clearHistogram() { 111 mMostCommonKeyHeight = 0; 112 mMaxHeightCount = 0; 113 mHeightHistogram.clear(); 114 115 mMaxWidthCount = 0; 116 mMostCommonKeyWidth = 0; 117 mWidthHistogram.clear(); 118 } 119 120 private static int updateHistogramCounter(final SparseIntArray histogram, final int key) { 121 final int index = histogram.indexOfKey(key); 122 final int count = (index >= 0 ? histogram.get(key) : 0) + 1; 123 histogram.put(key, count); 124 return count; 125 } 126 127 private void updateHistogram(final Key key) { 128 final int height = key.getHeight() + mVerticalGap; 129 final int heightCount = updateHistogramCounter(mHeightHistogram, height); 130 if (heightCount > mMaxHeightCount) { 131 mMaxHeightCount = heightCount; 132 mMostCommonKeyHeight = height; 133 } 134 135 final int width = key.getWidth() + mHorizontalGap; 136 final int widthCount = updateHistogramCounter(mWidthHistogram, width); 137 if (widthCount > mMaxWidthCount) { 138 mMaxWidthCount = widthCount; 139 mMostCommonKeyWidth = width; 140 } 141 } 142 } 143