MoreKeysDetector.java revision 2fa3693c264a4c150ac307d9bb7f6f8f18cc4ffc
1/*
2 * Copyright (C) 2011 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
17package com.android.inputmethod.keyboard;
18
19public final class MoreKeysDetector extends KeyDetector {
20    private final int mSlideAllowanceSquare;
21    private final int mSlideAllowanceSquareTop;
22
23    public MoreKeysDetector(float slideAllowance) {
24        super(/* keyHysteresisDistance */0);
25        mSlideAllowanceSquare = (int)(slideAllowance * slideAllowance);
26        // Top slide allowance is slightly longer (sqrt(2) times) than other edges.
27        mSlideAllowanceSquareTop = mSlideAllowanceSquare * 2;
28    }
29
30    @Override
31    public boolean alwaysAllowsKeySelectionByDraggingFinger() {
32        return true;
33    }
34
35    @Override
36    public Key detectHitKey(int x, int y) {
37        final int touchX = getTouchX(x);
38        final int touchY = getTouchY(y);
39
40        Key nearestKey = null;
41        int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare;
42        for (final Key key : getKeyboard().getKeys()) {
43            final int dist = key.squaredDistanceToEdge(touchX, touchY);
44            if (dist < nearestDist) {
45                nearestKey = key;
46                nearestDist = dist;
47            }
48        }
49        return nearestKey;
50    }
51}
52