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 MotionEvents()27 private MotionEvents() {} 28 isMouseEvent(MotionEvent e)29 static boolean isMouseEvent(MotionEvent e) { 30 return e.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE; 31 } 32 isTouchEvent(MotionEvent e)33 static boolean isTouchEvent(MotionEvent e) { 34 return e.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER; 35 } 36 isActionMove(MotionEvent e)37 static boolean isActionMove(MotionEvent e) { 38 return e.getActionMasked() == MotionEvent.ACTION_MOVE; 39 } 40 isActionDown(MotionEvent e)41 static boolean isActionDown(MotionEvent e) { 42 return e.getActionMasked() == MotionEvent.ACTION_DOWN; 43 } 44 isActionUp(MotionEvent e)45 static boolean isActionUp(MotionEvent e) { 46 return e.getActionMasked() == MotionEvent.ACTION_UP; 47 } 48 isActionPointerUp(MotionEvent e)49 static boolean isActionPointerUp(MotionEvent e) { 50 return e.getActionMasked() == MotionEvent.ACTION_POINTER_UP; 51 } 52 isActionPointerDown(MotionEvent e)53 static boolean isActionPointerDown(MotionEvent e) { 54 return e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN; 55 } 56 isActionCancel(MotionEvent e)57 static boolean isActionCancel(MotionEvent e) { 58 return e.getActionMasked() == MotionEvent.ACTION_CANCEL; 59 } 60 getOrigin(MotionEvent e)61 static Point getOrigin(MotionEvent e) { 62 return new Point((int) e.getX(), (int) e.getY()); 63 } 64 isPrimaryButtonPressed(MotionEvent e)65 static boolean isPrimaryButtonPressed(MotionEvent e) { 66 return e.isButtonPressed(MotionEvent.BUTTON_PRIMARY); 67 } 68 isSecondaryButtonPressed(MotionEvent e)69 public static boolean isSecondaryButtonPressed(MotionEvent e) { 70 return e.isButtonPressed(MotionEvent.BUTTON_SECONDARY); 71 } 72 isTertiaryButtonPressed(MotionEvent e)73 public static boolean isTertiaryButtonPressed(MotionEvent e) { 74 return e.isButtonPressed(MotionEvent.BUTTON_TERTIARY); 75 } 76 isShiftKeyPressed(MotionEvent e)77 static boolean isShiftKeyPressed(MotionEvent e) { 78 return hasBit(e.getMetaState(), KeyEvent.META_SHIFT_ON); 79 } 80 isCtrlKeyPressed(MotionEvent e)81 static boolean isCtrlKeyPressed(MotionEvent e) { 82 return hasBit(e.getMetaState(), KeyEvent.META_CTRL_ON); 83 } 84 isAltKeyPressed(MotionEvent e)85 static boolean isAltKeyPressed(MotionEvent e) { 86 return hasBit(e.getMetaState(), KeyEvent.META_ALT_ON); 87 } 88 isTouchpadScroll(MotionEvent e)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 hasBit(int metaState, int bit)95 private static boolean hasBit(int metaState, int bit) { 96 return (metaState & bit) != 0; 97 } 98 } 99