summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/StylusEventHelper.java
blob: e03273a6ea75406ab664ae00930721c83362b484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.android.launcher3;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

/**
 * Helper for identifying when a stylus touches a view while the primary stylus button is pressed.
 * This can occur in {@value MotionEvent#ACTION_DOWN} or {@value MotionEvent#ACTION_MOVE}. On a
 * stylus button press this performs the view's {@link View#performLongClick()} method, if the view
 * is long clickable.
 */
public class StylusEventHelper {
    private boolean mIsButtonPressed;
    private View mView;

    public StylusEventHelper(View view) {
        mView = view;
    }

    /**
     * Call this in onTouchEvent method of a view to identify a stylus button press and perform a
     * long click (if the view is long clickable).
     *
     * @param event The event to check for a stylus button press.
     * @return Whether a stylus event occurred and was handled.
     */
    public boolean checkAndPerformStylusEvent(MotionEvent event) {
        final float slop = ViewConfiguration.get(mView.getContext()).getScaledTouchSlop();

        if (!mView.isLongClickable()) {
            // We don't do anything unless the view is long clickable.
            return false;
        }

        final boolean stylusButtonPressed = isStylusButtonPressed(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mIsButtonPressed = false;
                if (stylusButtonPressed && mView.performLongClick()) {
                    mIsButtonPressed = true;
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (Utilities.pointInView(mView, event.getX(), event.getY(), slop)) {
                    if (!mIsButtonPressed && stylusButtonPressed && mView.performLongClick()) {
                        mIsButtonPressed = true;
                        return true;
                    } else if (mIsButtonPressed && !stylusButtonPressed) {
                        mIsButtonPressed = false;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mIsButtonPressed = false;
                break;
        }
        return false;
    }

    /**
     * Whether a stylus button press is occurring.
     */
    public boolean inStylusButtonPressed() {
        return mIsButtonPressed;
    }

    /**
     * Identifies if the provided {@link MotionEvent} is a stylus with the primary stylus button
     * pressed.
     *
     * @param event The event to check.
     * @return Whether a stylus button press occurred.
     */
    private static boolean isStylusButtonPressed(MotionEvent event) {
        return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS
                && ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY)
                        == MotionEvent.BUTTON_SECONDARY);
    }
}