Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      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 
     17 package com.android.inputmethod.latin;
     18 
     19 import android.inputmethodservice.Keyboard.Key;
     20 
     21 class MiniKeyboardKeyDetector extends KeyDetector {
     22     private static final int MAX_NEARBY_KEYS = 1;
     23 
     24     private final int mSlideAllowanceSquare;
     25     private final int mSlideAllowanceSquareTop;
     26 
     27     public MiniKeyboardKeyDetector(float slideAllowance) {
     28         super();
     29         mSlideAllowanceSquare = (int)(slideAllowance * slideAllowance);
     30         // Top slide allowance is slightly longer (sqrt(2) times) than other edges.
     31         mSlideAllowanceSquareTop = mSlideAllowanceSquare * 2;
     32     }
     33 
     34     @Override
     35     protected int getMaxNearbyKeys() {
     36         return MAX_NEARBY_KEYS;
     37     }
     38 
     39     @Override
     40     public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) {
     41         final Key[] keys = getKeys();
     42         final int touchX = getTouchX(x);
     43         final int touchY = getTouchY(y);
     44         int closestKeyIndex = LatinKeyboardBaseView.NOT_A_KEY;
     45         int closestKeyDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare;
     46         final int keyCount = keys.length;
     47         for (int i = 0; i < keyCount; i++) {
     48             final Key key = keys[i];
     49             int dist = key.squaredDistanceFrom(touchX, touchY);
     50             if (dist < closestKeyDist) {
     51                 closestKeyIndex = i;
     52                 closestKeyDist = dist;
     53             }
     54         }
     55         if (allKeys != null && closestKeyIndex != LatinKeyboardBaseView.NOT_A_KEY)
     56             allKeys[0] = keys[closestKeyIndex].codes[0];
     57         return closestKeyIndex;
     58     }
     59 }
     60