KeyDetector.java revision 3f00c6151fb7140fb7752bad3b978daacd9ec5aa
1/* 2 * Copyright (C) 2010 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; 18 19import com.android.inputmethod.latin.Constants; 20 21 22public class KeyDetector { 23 private final int mKeyHysteresisDistanceSquared; 24 25 private Keyboard mKeyboard; 26 private int mCorrectionX; 27 private int mCorrectionY; 28 29 /** 30 * This class handles key detection. 31 * 32 * @param keyHysteresisDistance if the pointer movement distance is smaller than this, the 33 * movement will not been handled as meaningful movement. The unit is pixel. 34 */ 35 public KeyDetector(float keyHysteresisDistance) { 36 mKeyHysteresisDistanceSquared = (int)(keyHysteresisDistance * keyHysteresisDistance); 37 } 38 39 public void setKeyboard(Keyboard keyboard, float correctionX, float correctionY) { 40 if (keyboard == null) { 41 throw new NullPointerException(); 42 } 43 mCorrectionX = (int)correctionX; 44 mCorrectionY = (int)correctionY; 45 mKeyboard = keyboard; 46 } 47 48 public int getKeyHysteresisDistanceSquared() { 49 return mKeyHysteresisDistanceSquared; 50 } 51 52 public int getTouchX(int x) { 53 return x + mCorrectionX; 54 } 55 56 // TODO: Remove vertical correction. 57 public int getTouchY(int y) { 58 return y + mCorrectionY; 59 } 60 61 public Keyboard getKeyboard() { 62 if (mKeyboard == null) { 63 throw new IllegalStateException("keyboard isn't set"); 64 } 65 return mKeyboard; 66 } 67 68 public boolean alwaysAllowsSlidingInput() { 69 return false; 70 } 71 72 /** 73 * Detect the key whose hitbox the touch point is in. 74 * 75 * @param x The x-coordinate of a touch point 76 * @param y The y-coordinate of a touch point 77 * @return the key that the touch point hits. 78 */ 79 public Key detectHitKey(int x, int y) { 80 final int touchX = getTouchX(x); 81 final int touchY = getTouchY(y); 82 83 int minDistance = Integer.MAX_VALUE; 84 Key primaryKey = null; 85 for (final Key key: mKeyboard.getNearestKeys(touchX, touchY)) { 86 // An edge key always has its enlarged hitbox to respond to an event that occurred in 87 // the empty area around the key. (@see Key#markAsLeftEdge(KeyboardParams)} etc.) 88 if (!key.isOnKey(touchX, touchY)) { 89 continue; 90 } 91 final int distance = key.squaredDistanceToEdge(touchX, touchY); 92 if (distance > minDistance) { 93 continue; 94 } 95 // To take care of hitbox overlaps, we compare mCode here too. 96 if (primaryKey == null || distance < minDistance || key.mCode > primaryKey.mCode) { 97 minDistance = distance; 98 primaryKey = key; 99 } 100 } 101 return primaryKey; 102 } 103 104 public static String printableCode(Key key) { 105 return key != null ? Keyboard.printableCode(key.mCode) : "none"; 106 } 107 108 public static String printableCodes(int[] codes) { 109 final StringBuilder sb = new StringBuilder(); 110 boolean addDelimiter = false; 111 for (final int code : codes) { 112 if (code == Constants.NOT_A_CODE) break; 113 if (addDelimiter) sb.append(", "); 114 sb.append(Keyboard.printableCode(code)); 115 addDelimiter = true; 116 } 117 return "[" + sb + "]"; 118 } 119} 120