• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 
17 package com.android.documentsui.base;
18 
19 import android.graphics.Point;
20 import android.view.KeyEvent;
21 import android.view.MotionEvent;
22 
23 /**
24  * Utility code for dealing with MotionEvents.
25  */
26 public final class Events {
27 
isMouseEvent(MotionEvent e)28     public static boolean isMouseEvent(MotionEvent e) {
29         return e.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE;
30     }
31 
isActionMove(MotionEvent e)32     public static boolean isActionMove(MotionEvent e) {
33         return e.getActionMasked() == MotionEvent.ACTION_MOVE;
34     }
35 
isActionDown(MotionEvent e)36     public static boolean isActionDown(MotionEvent e) {
37         return e.getActionMasked() == MotionEvent.ACTION_DOWN;
38     }
39 
isActionUp(MotionEvent e)40     public static boolean isActionUp(MotionEvent e) {
41         return e.getActionMasked() == MotionEvent.ACTION_UP;
42     }
43 
isMultiPointerActionDown(MotionEvent e)44     public static boolean isMultiPointerActionDown(MotionEvent e) {
45         return e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN;
46     }
47 
isMultiPointerActionUp(MotionEvent e)48     public static boolean isMultiPointerActionUp(MotionEvent e) {
49         return e.getActionMasked() == MotionEvent.ACTION_POINTER_UP;
50     }
51 
isActionCancel(MotionEvent e)52     public static boolean isActionCancel(MotionEvent e) {
53         return e.getActionMasked() == MotionEvent.ACTION_CANCEL;
54     }
55 
isPrimaryButtonPressed(MotionEvent e)56     public static boolean isPrimaryButtonPressed(MotionEvent e) {
57         return e.isButtonPressed(MotionEvent.BUTTON_PRIMARY);
58     }
59 
isSecondaryButtonPressed(MotionEvent e)60     public static boolean isSecondaryButtonPressed(MotionEvent e) {
61         return e.isButtonPressed(MotionEvent.BUTTON_SECONDARY);
62     }
63 
isTertiaryButtonPressed(MotionEvent e)64     public static boolean isTertiaryButtonPressed(MotionEvent e) {
65         return e.isButtonPressed(MotionEvent.BUTTON_TERTIARY);
66     }
67 
isCtrlKeyPressed(MotionEvent e)68     public static boolean isCtrlKeyPressed(MotionEvent e) {
69         return hasBit(e.getMetaState(), KeyEvent.META_CTRL_ON);
70     }
71 
isAltKeyPressed(MotionEvent e)72     public static boolean isAltKeyPressed(MotionEvent e) {
73         return hasBit(e.getMetaState(), KeyEvent.META_ALT_ON);
74     }
75 
isShiftKeyPressed(MotionEvent e)76     public static boolean isShiftKeyPressed(MotionEvent e) {
77         return hasBit(e.getMetaState(), KeyEvent.META_SHIFT_ON);
78     }
79 
isTouchpadScroll(MotionEvent e)80     public static boolean isTouchpadScroll(MotionEvent e) {
81         // Touchpad inputs are treated as mouse inputs, and when scrolling, there are no buttons
82         // returned.
83         return isMouseEvent(e) && isActionMove(e) && e.getButtonState() == 0;
84     }
85 
hasBit(int metaState, int bit)86     private static boolean hasBit(int metaState, int bit) {
87         return (metaState & bit) != 0;
88     }
89 
getOrigin(MotionEvent e)90     public static Point getOrigin(MotionEvent e) {
91         return new Point((int) e.getX(), (int) e.getY());
92     }
93 
94     /**
95      * @return true if keyCode is a known navigation code (e.g. up, down, home).
96      */
isNavigationKeyCode(int keyCode)97     public static boolean isNavigationKeyCode(int keyCode) {
98         switch (keyCode) {
99             case KeyEvent.KEYCODE_DPAD_UP:
100             case KeyEvent.KEYCODE_DPAD_DOWN:
101             case KeyEvent.KEYCODE_DPAD_LEFT:
102             case KeyEvent.KEYCODE_DPAD_RIGHT:
103             case KeyEvent.KEYCODE_MOVE_HOME:
104             case KeyEvent.KEYCODE_MOVE_END:
105             case KeyEvent.KEYCODE_PAGE_UP:
106             case KeyEvent.KEYCODE_PAGE_DOWN:
107                 return true;
108             default:
109                 return false;
110         }
111     }
112 
113     /**
114      * Returns true if the event is a mouse drag event.
115      * @param e
116      * @return
117      */
isMouseDragEvent(MotionEvent e)118     public static boolean isMouseDragEvent(MotionEvent e) {
119         return isMouseEvent(e)
120                 && isActionMove(e)
121                 && isPrimaryButtonPressed(e);
122     }
123 }
124