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();
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(final int x, final int y) {
37        final Keyboard keyboard = getKeyboard();
38        if (keyboard == null) {
39            return null;
40        }
41        final int touchX = getTouchX(x);
42        final int touchY = getTouchY(y);
43
44        Key nearestKey = null;
45        int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare;
46        for (final Key key : keyboard.getSortedKeys()) {
47            final int dist = key.squaredDistanceToEdge(touchX, touchY);
48            if (dist < nearestDist) {
49                nearestKey = key;
50                nearestDist = dist;
51            }
52        }
53        return nearestKey;
54    }
55}
56