Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 19b1bad2 authored by Mady Mellor's avatar Mady Mellor Committed by Android (Google) Code Review
Browse files

Merge "Stylus support: creating and setting listeners for stylus button press"...

Merge "Stylus support: creating and setting listeners for stylus button press" into ub-launcher3-burnaby
parents ca51aaad ef044dd3
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLayoutChangeListener;
@@ -891,6 +892,15 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {

    private void addLongPressHandler(View v) {
        v.setOnLongClickListener(mLongClickListener);

        // Enable stylus button to also trigger long click.
        final StylusEventHelper stylusEventHelper = new StylusEventHelper(v);
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                return stylusEventHelper.checkAndPerformStylusEvent(event);
            }
        });
    }

    private ArrayList<WallpaperTileInfo> findBundledWallpapers() {
+12 −1
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ public class BubbleTextView extends TextView {
    private final Drawable mBackground;
    private final CheckLongPressHelper mLongPressHelper;
    private final HolographicOutlineHelper mOutlineHelper;
    private final StylusEventHelper mStylusEventHelper;

    private boolean mBackgroundSizeChanged;

@@ -123,6 +124,7 @@ public class BubbleTextView extends TextView {
        }

        mLongPressHelper = new CheckLongPressHelper(this);
        mStylusEventHelper = new StylusEventHelper(this);

        mOutlineHelper = HolographicOutlineHelper.obtain(getContext());
        if (mCustomShadowsEnabled) {
@@ -234,6 +236,12 @@ public class BubbleTextView extends TextView {
        // isPressed() on an ACTION_UP
        boolean result = super.onTouchEvent(event);

        // Check for a stylus button press, if it occurs cancel any long press checks.
        if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
            mLongPressHelper.cancelLongPress();
            result = true;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // So that the pressed outline is visible immediately on setStayPressed(),
@@ -243,7 +251,10 @@ public class BubbleTextView extends TextView {
                    mPressedBackground = mOutlineHelper.createMediumDropShadow(this);
                }

                // If we're in a stylus button press, don't check for long press.
                if (!mStylusEventHelper.inStylusButtonPressed()) {
                    mLongPressHelper.postCheckForLongPress();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
+17 −0
Original line number Diff line number Diff line
@@ -95,6 +95,7 @@ public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
    boolean[][] mTmpOccupied;

    private OnTouchListener mInterceptTouchListener;
    private StylusEventHelper mStylusEventHelper;

    private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
    private int[] mFolderLeaveBehindCell = {-1, -1};
@@ -284,6 +285,8 @@ public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
        mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
                mCountX, mCountY);

        mStylusEventHelper = new StylusEventHelper(this);

        mTouchFeedbackView = new ClickShadowView(context);
        addView(mTouchFeedbackView);
        addView(mShortcutsAndWidgets);
@@ -336,6 +339,20 @@ public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean handled = super.onTouchEvent(ev);
        // Stylus button press on a home screen should not switch between overview mode and
        // the home screen mode, however, once in overview mode stylus button press should be
        // enabled to allow rearranging the different home screens. So check what mode
        // the workspace is in, and only perform stylus button presses while in overview mode.
        if (mLauncher.mWorkspace.isInOverviewMode()
                && mStylusEventHelper.checkAndPerformStylusEvent(ev)) {
            return true;
        }
        return handled;
    }

    public void enableHardwareLayer(boolean hasLayer) {
        mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
    }
+8 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ public class FolderIcon extends FrameLayout implements FolderListener {
    @Thunk static boolean sStaticValuesDirty = true;

    private CheckLongPressHelper mLongPressHelper;
    private StylusEventHelper mStylusEventHelper;

    // The number of icons to display in the
    public static final int NUM_ITEMS_IN_PREVIEW = 3;
@@ -128,6 +129,7 @@ public class FolderIcon extends FrameLayout implements FolderListener {

    private void init() {
        mLongPressHelper = new CheckLongPressHelper(this);
        mStylusEventHelper = new StylusEventHelper(this);
        setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
    }

@@ -719,6 +721,12 @@ public class FolderIcon extends FrameLayout implements FolderListener {
        // isPressed() on an ACTION_UP
        boolean result = super.onTouchEvent(event);

        // Check for a stylus button press, if it occurs cancel any long press checks.
        if (mStylusEventHelper.checkAndPerformStylusEvent(event)) {
            mLongPressHelper.cancelLongPress();
            return true;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLongPressHelper.postCheckForLongPress();
+84 −0
Original line number Diff line number Diff line
package com.android.launcher3;

import com.android.launcher3.Utilities;

import android.util.Log;
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.
     */
    public static boolean isStylusButtonPressed(MotionEvent event) {
        return event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS
                && event.isButtonPressed(MotionEvent.BUTTON_SECONDARY);
    }
}
 No newline at end of file
Loading