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 25 import java.util.ArrayList; 26 import java.util.Comparator; 27 import java.util.SortedSet; 28 import java.util.TreeSet; 29 30 public class KeyboardParams { 31 public KeyboardId mId; 32 public int mThemeId; 33 34 /** Total height and width of the keyboard, including the paddings and keys */ 35 public int mOccupiedHeight; 36 public int mOccupiedWidth; 37 38 /** Base height and width of the keyboard used to calculate rows' or keys' heights and 39 * widths 40 */ 41 public int mBaseHeight; 42 public int mBaseWidth; 43 44 public int mTopPadding; 45 public int mBottomPadding; 46 public int mLeftPadding; 47 public int mRightPadding; 48 49 public KeyVisualAttributes mKeyVisualAttributes; 50 51 public int mDefaultRowHeight; 52 public int mDefaultKeyWidth; 53 public int mHorizontalGap; 54 public int mVerticalGap; 55 56 public int mMoreKeysTemplate; 57 public int mMaxMoreKeysKeyboardColumn; 58 59 public int GRID_WIDTH; 60 public int GRID_HEIGHT; 61 62 // Keys are sorted from top-left to bottom-right order. 63 public final SortedSet<Key> mSortedKeys = new TreeSet<>(ROW_COLUMN_COMPARATOR); 64 public final ArrayList<Key> mShiftKeys = new ArrayList<>(); 65 public final ArrayList<Key> mAltCodeKeysWhileTyping = new ArrayList<>(); 66 public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet(); 67 public final KeyboardTextsSet mTextsSet = new KeyboardTextsSet(); 68 public final KeyStylesSet mKeyStyles = new KeyStylesSet(mTextsSet); 69 70 public KeysCache mKeysCache; 71 72 public int mMostCommonKeyHeight = 0; 73 public int mMostCommonKeyWidth = 0; 74 75 public boolean mProximityCharsCorrectionEnabled; 76 77 public final TouchPositionCorrection mTouchPositionCorrection = 78 new TouchPositionCorrection(); 79 80 // Comparator to sort {@link Key}s from top-left to bottom-right order. 81 private static final Comparator<Key> ROW_COLUMN_COMPARATOR = new Comparator<Key>() { 82 @Override 83 public int compare(final Key lhs, final Key rhs) { 84 if (lhs.getY() < rhs.getY()) return -1; 85 if (lhs.getY() > rhs.getY()) return 1; 86 if (lhs.getX() < rhs.getX()) return -1; 87 if (lhs.getX() > rhs.getX()) return 1; 88 return 0; 89 } 90 }; 91 clearKeys()92 protected void clearKeys() { 93 mSortedKeys.clear(); 94 mShiftKeys.clear(); 95 clearHistogram(); 96 } 97 onAddKey(final Key newKey)98 public void onAddKey(final Key newKey) { 99 final Key key = (mKeysCache != null) ? mKeysCache.get(newKey) : newKey; 100 final boolean isSpacer = key.isSpacer(); 101 if (isSpacer && key.getWidth() == 0) { 102 // Ignore zero width {@link Spacer}. 103 return; 104 } 105 mSortedKeys.add(key); 106 if (isSpacer) { 107 return; 108 } 109 updateHistogram(key); 110 if (key.getCode() == Constants.CODE_SHIFT) { 111 mShiftKeys.add(key); 112 } 113 if (key.altCodeWhileTyping()) { 114 mAltCodeKeysWhileTyping.add(key); 115 } 116 } 117 118 private int mMaxHeightCount = 0; 119 private int mMaxWidthCount = 0; 120 private final SparseIntArray mHeightHistogram = new SparseIntArray(); 121 private final SparseIntArray mWidthHistogram = new SparseIntArray(); 122 clearHistogram()123 private void clearHistogram() { 124 mMostCommonKeyHeight = 0; 125 mMaxHeightCount = 0; 126 mHeightHistogram.clear(); 127 128 mMaxWidthCount = 0; 129 mMostCommonKeyWidth = 0; 130 mWidthHistogram.clear(); 131 } 132 updateHistogramCounter(final SparseIntArray histogram, final int key)133 private static int updateHistogramCounter(final SparseIntArray histogram, final int key) { 134 final int index = histogram.indexOfKey(key); 135 final int count = (index >= 0 ? histogram.get(key) : 0) + 1; 136 histogram.put(key, count); 137 return count; 138 } 139 updateHistogram(final Key key)140 private void updateHistogram(final Key key) { 141 final int height = key.getHeight() + mVerticalGap; 142 final int heightCount = updateHistogramCounter(mHeightHistogram, height); 143 if (heightCount > mMaxHeightCount) { 144 mMaxHeightCount = heightCount; 145 mMostCommonKeyHeight = height; 146 } 147 148 final int width = key.getWidth() + mHorizontalGap; 149 final int widthCount = updateHistogramCounter(mWidthHistogram, width); 150 if (widthCount > mMaxWidthCount) { 151 mMaxWidthCount = widthCount; 152 mMostCommonKeyWidth = width; 153 } 154 } 155 } 156