1// Copyright 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.chrome.browser.util;
6
7import android.view.KeyEvent;
8
9/**
10 * This is a helper class to handle navigation related checks for key events.
11 */
12public class KeyNavigationUtil {
13
14    /**
15     * This is a helper class with no instance.
16     */
17    private KeyNavigationUtil() {
18    }
19
20    /**
21     * Checks whether the given event is any of DPAD down or NUMPAD down.
22     * @param event Event to be checked.
23     * @return Whether the event should be processed as a navigation down.
24     */
25    public static boolean isGoDown(KeyEvent event) {
26        return isActionDown(event) && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN
27                || (!event.isNumLockOn() && event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_2));
28    }
29
30    /**
31     * Checks whether the given event is any of DPAD up or NUMPAD up.
32     * @param event Event to be checked.
33     * @return Whether the event should be processed as a navigation up.
34     */
35    public static boolean isGoUp(KeyEvent event) {
36        return isActionDown(event) && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
37                || (!event.isNumLockOn() && event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_8));
38    }
39
40    /**
41     * Checks whether the given event is any of DPAD right or NUMPAD right.
42     * @param event Event to be checked.
43     * @return Whether the event should be processed as a navigation right.
44     */
45    public static boolean isGoRight(KeyEvent event) {
46        return isActionDown(event) && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT
47                || (!event.isNumLockOn() && event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_6));
48    }
49
50    /**
51     * Checks whether the given event is any of DPAD down, DPAD up, NUMPAD down or NUMPAD up.
52     * @param event Event to be checked.
53     * @return Whether the event should be processed as any of navigation up or navigation down.
54     */
55    public static boolean isGoUpOrDown(KeyEvent event) {
56        return isGoDown(event) || isGoUp(event);
57    }
58
59    /**
60     * Checks whether the given event is any of ENTER or NUMPAD ENTER.
61     * @param event Event to be checked.
62     * @return Whether the event should be processed as ENTER.
63     */
64    public static boolean isEnter(KeyEvent event) {
65        return isActionUp(event) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
66                || event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER);
67    }
68
69    /**
70     * Checks whether the given event is an ACTION_DOWN event.
71     * @param event Event to be checked.
72     * @return Whether the event is an ACTION_DOWN event.
73     */
74    public static boolean isActionDown(KeyEvent event) {
75        return event.getAction() == KeyEvent.ACTION_DOWN;
76    }
77
78    /**
79     * Checks whether the given event is an ACTION_UP event.
80     * @param event Event to be checked.
81     * @return Whether the event is an ACTION_UP event.
82     */
83    public static boolean isActionUp(KeyEvent event) {
84        return event.getAction() == KeyEvent.ACTION_UP;
85    }
86}
87