1 /* 2 * Copyright (C) 2017 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 package com.android.documentsui.selection; 17 18 import android.graphics.Point; 19 import android.view.KeyEvent; 20 import android.view.MotionEvent; 21 22 /** 23 * Utility methods for working with {@link MotionEvent} instances. 24 */ 25 final class MotionEvents { 26 27 private MotionEvents() {} 28 29 static boolean isMouseEvent(MotionEvent e) { 30 return e.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE; 31 } 32 33 static boolean isTouchEvent(MotionEvent e) { 34 return e.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER; 35 } 36 37 static boolean isActionMove(MotionEvent e) { 38 return e.getActionMasked() == MotionEvent.ACTION_MOVE; 39 } 40 41 static boolean isActionDown(MotionEvent e) { 42 return e.getActionMasked() == MotionEvent.ACTION_DOWN; 43 } 44 45 static boolean isActionUp(MotionEvent e) { 46 return e.getActionMasked() == MotionEvent.ACTION_UP; 47 } 48 49 static boolean isActionPointerUp(MotionEvent e) { 50 return e.getActionMasked() == MotionEvent.ACTION_POINTER_UP; 51 } 52 53 static boolean isActionPointerDown(MotionEvent e) { 54 return e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN; 55 } 56 57 static boolean isActionCancel(MotionEvent e) { 58 return e.getActionMasked() == MotionEvent.ACTION_CANCEL; 59 } 60 61 static Point getOrigin(MotionEvent e) { 62 return new Point((int) e.getX(), (int) e.getY()); 63 } 64 65 static boolean isPrimaryButtonPressed(MotionEvent e) { 66 return e.isButtonPressed(MotionEvent.BUTTON_PRIMARY); 67 } 68 69 public static boolean isSecondaryButtonPressed(MotionEvent e) { 70 return e.isButtonPressed(MotionEvent.BUTTON_SECONDARY); 71 } 72 73 public static boolean isTertiaryButtonPressed(MotionEvent e) { 74 return e.isButtonPressed(MotionEvent.BUTTON_TERTIARY); 75 } 76 77 static boolean isShiftKeyPressed(MotionEvent e) { 78 return hasBit(e.getMetaState(), KeyEvent.META_SHIFT_ON); 79 } 80 81 static boolean isCtrlKeyPressed(MotionEvent e) { 82 return hasBit(e.getMetaState(), KeyEvent.META_CTRL_ON); 83 } 84 85 static boolean isAltKeyPressed(MotionEvent e) { 86 return hasBit(e.getMetaState(), KeyEvent.META_ALT_ON); 87 } 88 89 public static boolean isTouchpadScroll(MotionEvent e) { 90 // Touchpad inputs are treated as mouse inputs, and when scrolling, there are no buttons 91 // returned. 92 return isMouseEvent(e) && isActionMove(e) && e.getButtonState() == 0; 93 } 94 95 private static boolean hasBit(int metaState, int bit) { 96 return (metaState & bit) != 0; 97 } 98 } 99