Mady Mellor | ef044dd | 2015-06-02 15:35:07 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import com.android.launcher3.Utilities; |
| 4 | |
| 5 | import android.util.Log; |
| 6 | import android.view.MotionEvent; |
| 7 | import android.view.View; |
| 8 | import android.view.ViewConfiguration; |
| 9 | |
| 10 | /** |
| 11 | * Helper for identifying when a stylus touches a view while the primary stylus button is pressed. |
| 12 | * This can occur in {@value MotionEvent#ACTION_DOWN} or {@value MotionEvent#ACTION_MOVE}. On a |
| 13 | * stylus button press this performs the view's {@link View#performLongClick()} method, if the view |
| 14 | * is long clickable. |
| 15 | */ |
| 16 | public class StylusEventHelper { |
| 17 | private boolean mIsButtonPressed; |
| 18 | private View mView; |
| 19 | |
| 20 | public StylusEventHelper(View view) { |
| 21 | mView = view; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Call this in onTouchEvent method of a view to identify a stylus button press and perform a |
| 26 | * long click (if the view is long clickable). |
| 27 | * |
| 28 | * @param event The event to check for a stylus button press. |
| 29 | * @return Whether a stylus event occurred and was handled. |
| 30 | */ |
| 31 | public boolean checkAndPerformStylusEvent(MotionEvent event) { |
| 32 | final float slop = ViewConfiguration.get(mView.getContext()).getScaledTouchSlop(); |
| 33 | |
| 34 | if (!mView.isLongClickable()) { |
| 35 | // We don't do anything unless the view is long clickable. |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | final boolean stylusButtonPressed = isStylusButtonPressed(event); |
| 40 | switch (event.getAction()) { |
| 41 | case MotionEvent.ACTION_DOWN: |
| 42 | mIsButtonPressed = false; |
| 43 | if (stylusButtonPressed && mView.performLongClick()) { |
| 44 | mIsButtonPressed = true; |
| 45 | return true; |
| 46 | } |
| 47 | break; |
| 48 | case MotionEvent.ACTION_MOVE: |
| 49 | if (Utilities.pointInView(mView, event.getX(), event.getY(), slop)) { |
| 50 | if (!mIsButtonPressed && stylusButtonPressed && mView.performLongClick()) { |
| 51 | mIsButtonPressed = true; |
| 52 | return true; |
| 53 | } else if (mIsButtonPressed && !stylusButtonPressed) { |
| 54 | mIsButtonPressed = false; |
| 55 | } |
| 56 | } |
| 57 | break; |
| 58 | case MotionEvent.ACTION_UP: |
| 59 | case MotionEvent.ACTION_CANCEL: |
| 60 | mIsButtonPressed = false; |
| 61 | break; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Whether a stylus button press is occurring. |
| 68 | */ |
| 69 | public boolean inStylusButtonPressed() { |
| 70 | return mIsButtonPressed; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button |
| 75 | * pressed. |
| 76 | * |
| 77 | * @param event The event to check. |
| 78 | * @return Whether a stylus button press occurred. |
| 79 | */ |
| 80 | public static boolean isStylusButtonPressed(MotionEvent event) { |
| 81 | return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS |
| 82 | && event.isButtonPressed(MotionEvent.BUTTON_SECONDARY); |
| 83 | } |
| 84 | } |