ViewDragHelper.java revision 1538310a6c51ee7cbebc151df16798aa65f57f07
1c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell/*
2c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * Copyright (C) 2013 The Android Open Source Project
3c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell *
4c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * you may not use this file except in compliance with the License.
6c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * You may obtain a copy of the License at
7c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell *
8c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell *
10c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * Unless required by applicable law or agreed to in writing, software
11c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * See the License for the specific language governing permissions and
14c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * limitations under the License.
15c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell */
16c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
17c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
18c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellpackage android.support.v4.widget;
19c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
20c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.content.Context;
21c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.support.v4.view.MotionEventCompat;
22c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.support.v4.view.VelocityTrackerCompat;
23c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.support.v4.view.ViewCompat;
24c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.view.MotionEvent;
25c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.view.VelocityTracker;
26c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.view.View;
27c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.view.ViewConfiguration;
28c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.view.ViewGroup;
29c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport android.view.animation.Interpolator;
30c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
31c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellimport java.util.Arrays;
32c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
33c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell/**
34c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number
35c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * of useful operations and state tracking for allowing a user to drag and reposition
36c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell * views within their parent ViewGroup.
37c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell */
38c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powellpublic class ViewDragHelper {
39c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private static final String TAG = "ViewDragHelper";
40c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
41c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
42c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * A null/invalid pointer ID.
43c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
44c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int INVALID_POINTER = -1;
45c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
46c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
47c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * A view is not currently being dragged or animating as a result of a fling/snap.
48c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
49c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int STATE_IDLE = 0;
50c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
51c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
52c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * A view is currently being dragged. The position is currently changing as a result
53c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * of user input or simulated user input.
54c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
55c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int STATE_DRAGGING = 1;
56c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
57c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
58c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * A view is currently settling into place as a result of a fling or
59c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * predefined non-interactive motion.
60c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
61c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int STATE_SETTLING = 2;
62c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
63c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
64c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Edge flag indicating that the left edge should be affected.
65c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
66c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int EDGE_LEFT = 1 << 0;
67c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
68c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
69c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Edge flag indicating that the right edge should be affected.
70c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
71c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int EDGE_RIGHT = 1 << 1;
72c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
73c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
74c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Edge flag indicating that the top edge should be affected.
75c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
76c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int EDGE_TOP = 1 << 2;
77c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
78c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
79c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Edge flag indicating that the bottom edge should be affected.
80c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
81c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static final int EDGE_BOTTOM = 1 << 3;
82c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
831732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
841732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Edge flag set indicating all edges should be affected.
851732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
861732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public static final int EDGE_ALL = EDGE_LEFT | EDGE_TOP | EDGE_RIGHT | EDGE_BOTTOM;
871732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
881732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
891732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Indicates that a check should occur along the horizontal axis
901732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
911732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public static final int DIRECTION_HORIZONTAL = 1 << 0;
921732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
931732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
941732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Indicates that a check should occur along the vertical axis
951732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
961732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public static final int DIRECTION_VERTICAL = 1 << 1;
971732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
981732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
991732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Indicates that a check should occur along all axes
1001732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
1011732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public static final int DIRECTION_ALL = DIRECTION_HORIZONTAL | DIRECTION_VERTICAL;
1021732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
1031538310a6c51ee7cbebc151df16798aa65f57f07Adam Powell    private static final int EDGE_SIZE = 20; // dp
104c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
105c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private static final int BASE_SETTLE_DURATION = 256; // ms
106c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private static final int MAX_SETTLE_DURATION = 600; // ms
107c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
108c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    // Current drag state; idle, dragging or settling
109c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int mDragState;
110c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
111c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    // Distance to travel before a drag may begin
112c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int mTouchSlop;
113c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
114c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    // Last known position/pointer tracking
115c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int mActivePointerId = INVALID_POINTER;
116c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float[] mInitialMotionX;
117c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float[] mInitialMotionY;
118c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float[] mLastMotionX;
119c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float[] mLastMotionY;
120c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int[] mInitialEdgesTouched;
121c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int[] mEdgeDragsInProgress;
122acc82321ad119706485db342eaa12b225fa9b667Adam Powell    private int[] mEdgeDragsLocked;
1231732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    private int mPointersDown;
124c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
125c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private VelocityTracker mVelocityTracker;
126c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float mMaxVelocity;
127c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float mMinVelocity;
128c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
129c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int mEdgeSize;
130c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int mTrackingEdges;
131c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
132c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private ScrollerCompat mScroller;
133c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
134c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private final Callback mCallback;
135c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
136c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private View mCapturedView;
137c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private boolean mReleaseInProgress;
138c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
139c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private final ViewGroup mParentView;
140c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
141c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
142c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * A Callback is used as a communication channel with the ViewDragHelper back to the
143c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * parent view using it. <code>on*</code>methods are invoked on siginficant events and several
144c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * accessor methods are expected to provide the ViewDragHelper with more information
145c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * about the state of the parent view upon request. The callback also makes decisions
146c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * governing the range and draggability of child views.
147c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
148c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static abstract class Callback {
149c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
150c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called when the drag state changes. See the <code>STATE_*</code> constants
151c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * for more information.
152c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
153c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param state The new drag state
154c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
155c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #STATE_IDLE
156c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #STATE_DRAGGING
157c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #STATE_SETTLING
158c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
159c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public void onViewDragStateChanged(int state) {}
160c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
161c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
162c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called when the captured view's position changes as the result of a drag or settle.
163c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
1641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param changedView View whose position changed
1651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param left New X coordinate of the left edge of the view
1661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param top New Y coordinate of the top edge of the view
167c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param dx Change in X position from the last call
168c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param dy Change in Y position from the last call
169c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
1701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {}
171c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
172c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
173c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called when a child view is captured for dragging or settling. The ID of the pointer
174c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * currently dragging the captured view is supplied. If activePointerId is
175c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * identified as {@link #INVALID_POINTER} the capture is programmatic instead of
176c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * pointer-initiated.
177c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
178c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param capturedChild Child view that was captured
179c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param activePointerId Pointer id tracking the child capture
180c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
181c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public void onViewCaptured(View capturedChild, int activePointerId) {}
182c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
183c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
184c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called when the child view is no longer being actively dragged.
185c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * The fling velocity is also supplied, if relevant. The velocity values may
186c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * be clamped to system minimums or maximums.
187c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
188c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * <p>Calling code may decide to fling or otherwise release the view to let it
189c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * settle into place. It should do so using {@link #settleCapturedViewAt(int, int)}
190c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * or {@link #flingCapturedView(int, int, int, int)}. If the Callback invokes
191c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * one of these methods, the ViewDragHelper will enter {@link #STATE_SETTLING}
192c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * and the view capture will not fully end until it comes to a complete stop.
193c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * If neither of these methods is invoked before <code>onViewReleased</code> returns,
194c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * the view will stop in place and the ViewDragHelper will return to
195c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * {@link #STATE_IDLE}.</p>
196c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
197c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param releasedChild The captured child view now being released
198c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param xvel X velocity of the pointer as it left the screen in pixels per second.
199c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param yvel Y velocity of the pointer as it left the screen in pixels per second.
200c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
201c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public void onViewReleased(View releasedChild, float xvel, float yvel) {}
202c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
203c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
204c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called when one of the subscribed edges in the parent view has been touched
205c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * by the user while no child view is currently captured.
206c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
207c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param edgeFlags A combination of edge flags describing the edge(s) currently touched
208c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param pointerId ID of the pointer touching the described edge(s)
209c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_LEFT
210c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_TOP
211c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_RIGHT
212c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_BOTTOM
213c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
214c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public void onEdgeTouched(int edgeFlags, int pointerId) {}
215c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
216c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
217ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell         * Called when the given edge may become locked. This can happen if an edge drag
218acc82321ad119706485db342eaa12b225fa9b667Adam Powell         * was preliminarily rejected before beginning, but after {@link #onEdgeTouched(int, int)}
219ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell         * was called. This method should return true to lock this edge or false to leave it
220ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell         * unlocked. The default behavior is to leave edges unlocked.
221acc82321ad119706485db342eaa12b225fa9b667Adam Powell         *
222acc82321ad119706485db342eaa12b225fa9b667Adam Powell         * @param edgeFlags A combination of edge flags describing the edge(s) locked
223ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell         * @return true to lock the edge, false to leave it unlocked
224acc82321ad119706485db342eaa12b225fa9b667Adam Powell         */
225ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        public boolean onEdgeLock(int edgeFlags) {
226ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            return false;
227ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        }
228acc82321ad119706485db342eaa12b225fa9b667Adam Powell
229acc82321ad119706485db342eaa12b225fa9b667Adam Powell        /**
230c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called when the user has started a deliberate drag away from one
231c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * of the subscribed edges in the parent view while no child view is currently captured.
232c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
233c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param edgeFlags A combination of edge flags describing the edge(s) dragged
234c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param pointerId ID of the pointer touching the described edge(s)
235c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_LEFT
236c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_TOP
237c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_RIGHT
238c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @see #EDGE_BOTTOM
239c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
240c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public void onEdgeDragStarted(int edgeFlags, int pointerId) {}
241c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
242c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
243c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called to determine the Z-order of child views.
244c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
245c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param index the ordered position to query for
246c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @return index of the view that should be ordered at position <code>index</code>
247c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
248c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public int getOrderedChildIndex(int index) {
249c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return index;
250c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
251c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
252c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
253c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Return the magnitude of a draggable child view's horizontal range of motion in pixels.
254c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * This method should return 0 for views that cannot move horizontally.
255c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
256c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param child Child view to check
257c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @return range of horizontal motion in pixels
258c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
259c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public int getViewHorizontalDragRange(View child) {
260c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return 0;
261c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
262c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
263c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
264c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Return the magnitude of a draggable child view's vertical range of motion in pixels.
265c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * This method should return 0 for views that cannot move vertically.
266c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
267c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param child Child view to check
268c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @return range of vertical motion in pixels
269c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
270c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public int getViewVerticalDragRange(View child) {
271c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return 0;
272c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
273c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
274c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
275c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Called when the user's input indicates that they want to capture the given child view
276c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * with the pointer indicated by pointerId. The callback should return true if the user
277c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * is permitted to drag the given view with the indicated pointer.
278c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
279c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * <p>ViewDragHelper may call this method multiple times for the same view even if
280c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * the view is already captured; this indicates that a new pointer is trying to take
281c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * control of the view.</p>
282c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
283c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * <p>If this method returns true, a call to {@link #onViewCaptured(android.view.View, int)}
284c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * will follow if the capture is successful.</p>
285c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
286c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param child Child the user is attempting to capture
287c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param pointerId ID of the pointer attempting the capture
288c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @return true if capture should be allowed, false otherwise
289c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
290c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public abstract boolean tryCaptureView(View child, int pointerId);
291c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
292c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
293c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Restrict the motion of the dragged child view along the horizontal axis.
294c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * The default implementation does not allow horizontal motion; the extending
295c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * class must override this method and provide the desired clamping.
296c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
2971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         *
298c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param child Child view being dragged
2991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param left Attempted motion along the X axis
3001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param dx Proposed change in position for left
3011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @return The new clamped position for left
302c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
3031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public int clampViewPositionHorizontal(View child, int left, int dx) {
304c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return 0;
305c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
306c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
307c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        /**
308c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * Restrict the motion of the dragged child view along the vertical axis.
309c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * The default implementation does not allow vertical motion; the extending
310c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * class must override this method and provide the desired clamping.
311c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         *
3121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         *
313c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         * @param child Child view being dragged
3141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param top Attempted motion along the Y axis
3151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param dy Proposed change in position for top
3161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @return The new clamped position for top
317c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell         */
3181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public int clampViewPositionVertical(View child, int top, int dy) {
319c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return 0;
320c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
321c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
322c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
323c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
324c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Interpolator defining the animation curve for mScroller
325c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
326c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private static final Interpolator sInterpolator = new Interpolator() {
327c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public float getInterpolation(float t) {
328c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            t -= 1.0f;
329c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return t * t * t * t * t + 1.0f;
330c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
331c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    };
332c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
333c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private final Runnable mSetIdleRunnable = new Runnable() {
334c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        public void run() {
335c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            setDragState(STATE_IDLE);
336c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
337c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    };
338c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
339c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
340c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Factory method to create a new ViewDragHelper.
341c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
342c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param forParent Parent view to monitor
343c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param cb Callback to provide information and receive events
344c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return a new ViewDragHelper instance
345c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
346c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public static ViewDragHelper create(ViewGroup forParent, Callback cb) {
347c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return new ViewDragHelper(forParent.getContext(), forParent, cb);
348c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
349c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
350c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
3511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Factory method to create a new ViewDragHelper.
3521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
3531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param forParent Parent view to monitor
3541d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param sensitivity Multiplier for how sensitive the helper should be about detecting
3551d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *                    the start of a drag. Larger values are more sensitive. 1.0f is normal.
3561d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param cb Callback to provide information and receive events
3571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @return a new ViewDragHelper instance
3581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
3591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public static ViewDragHelper create(ViewGroup forParent, float sensitivity, Callback cb) {
3601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final ViewDragHelper helper = create(forParent, cb);
3611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        helper.mTouchSlop = (int) (helper.mTouchSlop * (1 / sensitivity));
3621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return helper;
3631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
3641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
3651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
366c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Apps should use ViewDragHelper.create() to get a new instance.
367c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * This will allow VDH to use internal compatibility implementations for different
368c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * platform versions.
369c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
370c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param context Context to initialize config-dependent params from
371c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param forParent Parent view to monitor
372c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
373c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) {
374c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (forParent == null) {
375c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalArgumentException("Parent view may not be null");
376c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
377c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (cb == null) {
378c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalArgumentException("Callback may not be null");
379c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
380c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
381c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mParentView = forParent;
382c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCallback = cb;
383c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
384c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final ViewConfiguration vc = ViewConfiguration.get(context);
385c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float density = context.getResources().getDisplayMetrics().density;
386c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mEdgeSize = (int) (EDGE_SIZE * density + 0.5f);
387c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
388c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mTouchSlop = vc.getScaledTouchSlop();
389c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mMaxVelocity = vc.getScaledMaximumFlingVelocity();
390c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mMinVelocity = vc.getScaledMinimumFlingVelocity();
391c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mScroller = ScrollerCompat.create(context, sInterpolator);
392c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
393c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
394c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
3951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Retrieve the current drag state of this helper. This will return one of
3961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}.
3971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @return The current drag state
3981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
3991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public int getViewDragState() {
4001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return mDragState;
4011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
404c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Enable edge tracking for the selected edges of the parent view.
405c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The callback's {@link Callback#onEdgeTouched(int, int)} and
406c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * {@link Callback#onEdgeDragStarted(int, int)} methods will only be invoked
407c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * for edges for which edge tracking has been enabled.
408c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
409c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param edgeFlags Combination of edge flags describing the edges to watch
410c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_LEFT
411c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_TOP
412c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_RIGHT
413c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_BOTTOM
414c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
415c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void setEdgeTrackingEnabled(int edgeFlags) {
416c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mTrackingEdges = edgeFlags;
417c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
418c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
419c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
420ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * Return the size of an edge. This is the range in pixels along the edges of this view
421ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * that will actively detect edge touches or drags if edge tracking is enabled.
422ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     *
423ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * @return The size of an edge in pixels
424ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * @see #setEdgeTrackingEnabled(int)
425ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     */
426ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    public int getEdgeSize() {
427ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        return mEdgeSize;
428ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    }
429ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell
430ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    /**
431c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Capture a specific child view for dragging within the parent. The callback will be notified
432c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * but {@link Callback#tryCaptureView(android.view.View, int)} will not be asked permission to
433c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * capture this view.
434c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
435c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param childView Child view to capture
436c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param activePointerId ID of the pointer that is dragging the captured child view
437c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
438c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void captureChildView(View childView, int activePointerId) {
439c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (childView.getParent() != mParentView) {
440c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalArgumentException("captureChildView: parameter must be a descendant " +
441c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    "of the ViewDragHelper's tracked parent view (" + mParentView + ")");
442c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
443c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
444c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCapturedView = childView;
445c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mActivePointerId = activePointerId;
446c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCallback.onViewCaptured(childView, activePointerId);
4476580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        setDragState(STATE_DRAGGING);
448c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
449c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
450c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
451c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The currently captured view, or null if no view has been captured.
452c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
453c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public View getCapturedView() {
454c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mCapturedView;
455c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
456c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
457c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
458c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The ID of the pointer currently dragging the captured view,
459c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *         or {@link #INVALID_POINTER}.
460c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
461c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public int getActivePointerId() {
462c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mActivePointerId;
463c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
464c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
465c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
4661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @return The minimum distance in pixels that the user must travel to initiate a drag
4671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
4681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public int getTouchSlop() {
4691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return mTouchSlop;
4701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
473c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The result of a call to this method is equivalent to
474c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)} receiving an ACTION_CANCEL event.
475c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
476c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void cancel() {
477c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mActivePointerId = INVALID_POINTER;
478c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        clearMotionHistory();
479c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
480c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mVelocityTracker != null) {
481c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker.recycle();
482c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker = null;
483c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
484c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
485c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
486c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
487c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * {@link #cancel()}, but also abort all motion in progress and snap to the end of any
488c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * animation.
489c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
490c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void abort() {
491c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        cancel();
492c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState == STATE_SETTLING) {
493c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int oldX = mScroller.getCurrX();
494c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int oldY = mScroller.getCurrY();
495c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mScroller.abortAnimation();
496c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int newX = mScroller.getCurrX();
497c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int newY = mScroller.getCurrY();
4981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCallback.onViewPositionChanged(mCapturedView, newX, newY, newX - oldX, newY - oldY);
499c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
500c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        setDragState(STATE_IDLE);
501c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
502c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
503c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
504c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Animate the view <code>child</code> to the given (left, top) position.
505c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
506c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * on each subsequent frame to continue the motion until it returns false. If this method
507c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * returns false there is no further work to do to complete the movement.
508c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
5091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * <p>This operation does not count as a capture event, though {@link #getCapturedView()}
5101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * will still report the sliding view while the slide is in progress.</p>
5111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
512c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param child Child view to capture and animate
513c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalLeft Final left position of child
514c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalTop Final top position of child
515c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if animation should continue through {@link #continueSettling(boolean)} calls
516c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
517c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean smoothSlideViewTo(View child, int finalLeft, int finalTop) {
518c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCapturedView = child;
519c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mActivePointerId = INVALID_POINTER;
520c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
521c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return forceSettleCapturedViewAt(finalLeft, finalTop, 0, 0);
522c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
523c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
524c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
525c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Settle the captured view at the given (left, top) position.
526c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The appropriate velocity from prior motion will be taken into account.
527c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
528c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * on each subsequent frame to continue the motion until it returns false. If this method
529c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * returns false there is no further work to do to complete the movement.
530c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
531c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalLeft Settled left edge position for the captured view
532c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalTop Settled top edge position for the captured view
533c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if animation should continue through {@link #continueSettling(boolean)} calls
534c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
535c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean settleCapturedViewAt(int finalLeft, int finalTop) {
536c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (!mReleaseInProgress) {
537c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalStateException("Cannot settleCapturedViewAt outside of a call to " +
538c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    "Callback#onViewReleased");
539c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
540c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
541c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return forceSettleCapturedViewAt(finalLeft, finalTop,
542c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
543c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId));
544c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
545c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
546c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
547c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Settle the captured view at the given (left, top) position.
548c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
549c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalLeft Target left position for the captured view
550c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalTop Target top position for the captured view
551c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param xvel Horizontal velocity
552c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param yvel Vertical velocity
553c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if animation should continue through {@link #continueSettling(boolean)} calls
554c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
555c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private boolean forceSettleCapturedViewAt(int finalLeft, int finalTop, int xvel, int yvel) {
556c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int startLeft = mCapturedView.getLeft();
557c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int startTop = mCapturedView.getTop();
558c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int dx = finalLeft - startLeft;
559c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int dy = finalTop - startTop;
560c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
561c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (dx == 0 && dy == 0) {
562c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // Nothing to do. Send callbacks, be done.
5631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mScroller.abortAnimation();
564c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            setDragState(STATE_IDLE);
565c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return false;
566c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
567c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
568c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int duration = computeSettleDuration(mCapturedView, dx, dy, xvel, yvel);
569c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mScroller.startScroll(startLeft, startTop, dx, dy, duration);
570c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
571c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        setDragState(STATE_SETTLING);
572c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return true;
573c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
574c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
575c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int computeSettleDuration(View child, int dx, int dy, int xvel, int yvel) {
576c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        xvel = clampMag(xvel, (int) mMinVelocity, (int) mMaxVelocity);
577c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        yvel = clampMag(yvel, (int) mMinVelocity, (int) mMaxVelocity);
578c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absDx = Math.abs(dx);
579c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absDy = Math.abs(dy);
580c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absXVel = Math.abs(xvel);
581c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absYVel = Math.abs(yvel);
582c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int addedVel = absXVel + absYVel;
583c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int addedDistance = absDx + absDy;
584c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
585c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float xweight = xvel != 0 ? (float) absXVel / addedVel :
586c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (float) absDx / addedDistance;
587c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float yweight = yvel != 0 ? (float) absYVel / addedVel :
588c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (float) absDy / addedDistance;
589c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
590c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int xduration = computeAxisDuration(dx, xvel, mCallback.getViewHorizontalDragRange(child));
591c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int yduration = computeAxisDuration(dy, yvel, mCallback.getViewVerticalDragRange(child));
592c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
593c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return (int) (xduration * xweight + yduration * yweight);
594c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
595c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
596c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int computeAxisDuration(int delta, int velocity, int motionRange) {
597c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (delta == 0) {
598c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return 0;
599c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
600c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
601c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int width = mParentView.getWidth();
602c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int halfWidth = width / 2;
603c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float distanceRatio = Math.min(1f, (float) Math.abs(delta) / width);
604c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float distance = halfWidth + halfWidth *
605c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                distanceInfluenceForSnapDuration(distanceRatio);
606c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
607c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int duration;
608c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        velocity = Math.abs(velocity);
609c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (velocity > 0) {
610c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
611c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        } else {
612c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final float range = (float) Math.abs(delta) / motionRange;
613c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            duration = (int) ((range + 1) * BASE_SETTLE_DURATION);
614c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
615c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return Math.min(duration, MAX_SETTLE_DURATION);
616c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
617c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
618c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
619c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Clamp the magnitude of value for absMin and absMax.
620c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If the value is below the minimum, it will be clamped to zero.
621c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If the value is above the maximum, it will be clamped to the maximum.
622c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
623c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param value Value to clamp
624c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param absMin Absolute value of the minimum significant value to return
625c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param absMax Absolute value of the maximum value to return
626c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The clamped value with the same sign as <code>value</code>
627c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
628c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int clampMag(int value, int absMin, int absMax) {
629c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absValue = Math.abs(value);
630c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (absValue < absMin) return 0;
631c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (absValue > absMax) return value > 0 ? absMax : -absMax;
632c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return value;
633c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
634c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
635c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float distanceInfluenceForSnapDuration(float f) {
636c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        f -= 0.5f; // center the values about 0.
637c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        f *= 0.3f * Math.PI / 2.0f;
638c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return (float) Math.sin(f);
639c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
640c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
641c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
642c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Settle the captured view based on standard free-moving fling behavior.
643c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The caller should invoke {@link #continueSettling(boolean)} on each subsequent frame
644c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * to continue the motion until it returns false.
645c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
646c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param minLeft Minimum X position for the view's left edge
647c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param minTop Minimum Y position for the view's top edge
648c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param maxLeft Maximum X position for the view's left edge
649c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param maxTop Maximum Y position for the view's top edge
650c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
651c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop) {
652c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (!mReleaseInProgress) {
653c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalStateException("Cannot flingCapturedView outside of a call to " +
654c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    "Callback#onViewReleased");
655c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
656c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
657c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mScroller.fling(mCapturedView.getLeft(), mCapturedView.getTop(),
658c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
659c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
660c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                minLeft, maxLeft, minTop, maxTop);
661c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
662c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        setDragState(STATE_SETTLING);
663c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
664c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
665c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
666c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Move the captured settling view by the appropriate amount for the current time.
667c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If <code>continueSettling</code> returns true, the caller should call it again
668c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * on the next frame to continue.
669c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
670c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param deferCallbacks true if state callbacks should be deferred via posted message.
671c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *                       Set this to true if you are calling this method from
672c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *                       {@link android.view.View#computeScroll()} or similar methods
673c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *                       invoked as part of layout or drawing.
674c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if settle is still in progress
675c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
676c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean continueSettling(boolean deferCallbacks) {
677c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState == STATE_SETTLING) {
6786580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            boolean keepGoing = mScroller.computeScrollOffset();
679c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int x = mScroller.getCurrX();
680c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int y = mScroller.getCurrY();
681c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int dx = x - mCapturedView.getLeft();
682c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int dy = y - mCapturedView.getTop();
683c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
684c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (dx != 0) {
685c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                mCapturedView.offsetLeftAndRight(dx);
686c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
687c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (dy != 0) {
688c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                mCapturedView.offsetTopAndBottom(dy);
689c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
690c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
691c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (dx != 0 || dy != 0) {
6921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mCallback.onViewPositionChanged(mCapturedView, x, y, dx, dy);
693c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
694c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
6956580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            if (keepGoing && x == mScroller.getFinalX() && y == mScroller.getFinalY()) {
6966580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                // Close enough. The interpolator/scroller might think we're still moving
6976580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                // but the user sure doesn't.
6986580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                mScroller.abortAnimation();
6996580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                keepGoing = mScroller.isFinished();
7006580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            }
7016580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
702c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (!keepGoing) {
703c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (deferCallbacks) {
704c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    mParentView.post(mSetIdleRunnable);
705c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else {
706c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    setDragState(STATE_IDLE);
707c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
708c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
709c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
710c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
711c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mDragState == STATE_SETTLING;
712c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
713c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
714c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
715c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Like all callback events this must happen on the UI thread, but release
716c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * involves some extra semantics. During a release (mReleaseInProgress)
717c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * is the only time it is valid to call {@link #settleCapturedViewAt(int, int)}
718c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * or {@link #flingCapturedView(int, int, int, int)}.
719c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
720c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void dispatchViewReleased(float xvel, float yvel) {
721c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mReleaseInProgress = true;
722c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCallback.onViewReleased(mCapturedView, xvel, yvel);
723c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mReleaseInProgress = false;
724c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
725c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState == STATE_DRAGGING) {
726c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // onViewReleased didn't call a method that would have changed this. Go idle.
727c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            setDragState(STATE_IDLE);
728c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
729c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
730c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
731c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void clearMotionHistory() {
732c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mInitialMotionX == null) {
733c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return;
734c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
735c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mInitialMotionX, 0);
736c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mInitialMotionY, 0);
737c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mLastMotionX, 0);
738c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mLastMotionY, 0);
739c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mInitialEdgesTouched, 0);
740c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mEdgeDragsInProgress, 0);
741acc82321ad119706485db342eaa12b225fa9b667Adam Powell        Arrays.fill(mEdgeDragsLocked, 0);
7421732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mPointersDown = 0;
743c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
744c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
745c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void clearMotionHistory(int pointerId) {
746c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mInitialMotionX == null) {
747c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return;
748c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
749c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionX[pointerId] = 0;
750c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionY[pointerId] = 0;
751c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mLastMotionX[pointerId] = 0;
752c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mLastMotionY[pointerId] = 0;
753c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialEdgesTouched[pointerId] = 0;
754c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mEdgeDragsInProgress[pointerId] = 0;
755acc82321ad119706485db342eaa12b225fa9b667Adam Powell        mEdgeDragsLocked[pointerId] = 0;
7561732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mPointersDown &= ~(1 << pointerId);
757c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
758c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
759c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void ensureMotionHistorySizeForId(int pointerId) {
760c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mInitialMotionX == null || mInitialMotionX.length <= pointerId) {
761c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] imx = new float[pointerId + 1];
762c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] imy = new float[pointerId + 1];
763c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] lmx = new float[pointerId + 1];
764c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] lmy = new float[pointerId + 1];
765c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            int[] iit = new int[pointerId + 1];
766c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            int[] edip = new int[pointerId + 1];
767acc82321ad119706485db342eaa12b225fa9b667Adam Powell            int[] edl = new int[pointerId + 1];
768c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
769c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (mInitialMotionX != null) {
770c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mInitialMotionX, 0, imx, 0, mInitialMotionX.length);
771c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mInitialMotionY, 0, imy, 0, mInitialMotionY.length);
772c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mLastMotionX, 0, lmx, 0, mLastMotionX.length);
773c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mLastMotionY, 0, lmy, 0, mLastMotionY.length);
774c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mInitialEdgesTouched, 0, iit, 0, mInitialEdgesTouched.length);
775c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mEdgeDragsInProgress, 0, edip, 0, mEdgeDragsInProgress.length);
776acc82321ad119706485db342eaa12b225fa9b667Adam Powell                System.arraycopy(mEdgeDragsLocked, 0, edl, 0, mEdgeDragsLocked.length);
777c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
778c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
779c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mInitialMotionX = imx;
780c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mInitialMotionY = imy;
781c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionX = lmx;
782c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionY = lmy;
783c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mInitialEdgesTouched = iit;
784c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mEdgeDragsInProgress = edip;
785acc82321ad119706485db342eaa12b225fa9b667Adam Powell            mEdgeDragsLocked = edl;
786c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
787c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
788c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
789c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void saveInitialMotion(float x, float y, int pointerId) {
790c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        ensureMotionHistorySizeForId(pointerId);
791c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionX[pointerId] = mLastMotionX[pointerId] = x;
792c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionY[pointerId] = mLastMotionY[pointerId] = y;
793c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialEdgesTouched[pointerId] = getEdgesTouched((int) x, (int) y);
7941732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mPointersDown |= 1 << pointerId;
795c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
796c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
797c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void saveLastMotion(MotionEvent ev) {
798c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int pointerCount = MotionEventCompat.getPointerCount(ev);
799c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        for (int i = 0; i < pointerCount; i++) {
800c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int pointerId = MotionEventCompat.getPointerId(ev, i);
801c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final float x = MotionEventCompat.getX(ev, i);
802c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final float y = MotionEventCompat.getY(ev, i);
803c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionX[pointerId] = x;
804c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionY[pointerId] = y;
805c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
806c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
807c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
8081732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
8091732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if the given pointer ID represents a pointer that is currently down (to the best
8101732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * of the ViewDragHelper's knowledge).
8111732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
8121732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * <p>The state used to report this information is populated by the methods
8131732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or
8141732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)}. If one of these methods has not
8151732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * been called for all relevant MotionEvents to track, the information reported
8161732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * by this method may be stale or incorrect.</p>
8171732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
8181732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param pointerId pointer ID to check; corresponds to IDs provided by MotionEvent
8191732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if the pointer with the given ID is still down
8201732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
8211732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean isPointerDown(int pointerId) {
8221732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return (mPointersDown & 1 << pointerId) != 0;
8231732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
8241732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
825c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    void setDragState(int state) {
826c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState != state) {
827c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mDragState = state;
828c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mCallback.onViewDragStateChanged(state);
8296580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            if (state == STATE_IDLE) {
8306580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                mCapturedView = null;
8316580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            }
832c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
833c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
834c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
835c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
836c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Attempt to capture the view with the given pointer ID. The callback will be involved.
837c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * This will put us into the "dragging" state. If we've already captured this view with
838c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * this pointer this method will immediately return true without consulting the callback.
839c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
840c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param toCapture View to capture
841c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param pointerId Pointer to capture with
842c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if capture was successful
843c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
844c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    boolean tryCaptureViewForDrag(View toCapture, int pointerId) {
845c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (toCapture == mCapturedView && mActivePointerId == pointerId) {
846c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // Already done!
847c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return true;
848c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
849c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (toCapture != null && mCallback.tryCaptureView(toCapture, pointerId)) {
850c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mActivePointerId = pointerId;
851c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            captureChildView(toCapture, pointerId);
852c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return true;
853c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
854c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return false;
855c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
856c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
857c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
858c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Tests scrollability within child views of v given a delta of dx.
859c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
860c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param v View to test for horizontal scrollability
861c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param checkV Whether the view v passed should itself be checked for scrollability (true),
862c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *               or just its children (false).
863c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dx Delta scrolled in pixels along the X axis
864c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dy Delta scrolled in pixels along the Y axis
865c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param x X coordinate of the active touch point
866c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param y Y coordinate of the active touch point
867c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if child views of v can be scrolled by delta of dx.
868c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
869c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) {
870c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (v instanceof ViewGroup) {
871c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final ViewGroup group = (ViewGroup) v;
872c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int scrollX = v.getScrollX();
873c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int scrollY = v.getScrollY();
874c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int count = group.getChildCount();
875c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // Count backwards - let topmost views consume scroll distance first.
876c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            for (int i = count - 1; i >= 0; i--) {
877c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // TODO: Add versioned support here for transformed views.
878c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // This will not work for transformed views in Honeycomb+
879c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final View child = group.getChildAt(i);
880c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
881c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
882c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        canScroll(child, true, dx, dy, x + scrollX - child.getLeft(),
883c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                                y + scrollY - child.getTop())) {
884c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    return true;
885c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
886c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
887c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
888c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
889c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return checkV && (ViewCompat.canScrollHorizontally(v, -dx) ||
890c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                ViewCompat.canScrollVertically(v, -dy));
891c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
892c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
893c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
894c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Check if this event as provided to the parent view's onInterceptTouchEvent should
895c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * cause the parent to intercept the touch event stream.
896c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
897c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param ev MotionEvent provided to onInterceptTouchEvent
898c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if the parent view should return true from onInterceptTouchEvent
899c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
900c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean shouldInterceptTouchEvent(MotionEvent ev) {
9011fbad11af8f178d9fcee85dabe7cd8f24d2bc9a2Adam Powell        final int action = MotionEventCompat.getActionMasked(ev);
9021fbad11af8f178d9fcee85dabe7cd8f24d2bc9a2Adam Powell        final int actionIndex = MotionEventCompat.getActionIndex(ev);
9036580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
9046580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        if (action == MotionEvent.ACTION_DOWN) {
9056580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // Reset things for a new event stream, just in case we didn't get
9066580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // the whole previous stream.
9076580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            cancel();
9086580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        }
9096580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
910c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mVelocityTracker == null) {
911c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker = VelocityTracker.obtain();
912c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
913c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mVelocityTracker.addMovement(ev);
914c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
915c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        switch (action) {
916c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_DOWN: {
917c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = ev.getX();
918c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = ev.getY();
919c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, 0);
920c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
921c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
922c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final View toCapture = findTopChildUnder((int) x, (int) y);
923c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
924c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // Catch a settling view if possible.
925c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
926c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    tryCaptureViewForDrag(toCapture, pointerId);
927c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
928c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
929c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int edgesTouched = mInitialEdgesTouched[pointerId];
930c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if ((edgesTouched & mTrackingEdges) != 0) {
931c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
932c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
933c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
934c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
935c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
936c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_DOWN: {
937c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
938c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = MotionEventCompat.getX(ev, actionIndex);
939c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = MotionEventCompat.getY(ev, actionIndex);
940c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
941c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
942c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
943c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // A ViewDragHelper can only manipulate one view at a time.
944c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_IDLE) {
945c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int edgesTouched = mInitialEdgesTouched[pointerId];
946c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if ((edgesTouched & mTrackingEdges) != 0) {
947c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
948c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
949c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else if (mDragState == STATE_SETTLING) {
950c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // Catch a settling view if possible.
951c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final View toCapture = findTopChildUnder((int) x, (int) y);
952c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if (toCapture == mCapturedView) {
953c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        tryCaptureViewForDrag(toCapture, pointerId);
954c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
955c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
956c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
957c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
958c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
959c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_MOVE: {
960c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // First to cross a touch slop over a draggable view wins. Also report edge drags.
961c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerCount = MotionEventCompat.getPointerCount(ev);
962c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                for (int i = 0; i < pointerCount; i++) {
963c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int pointerId = MotionEventCompat.getPointerId(ev, i);
964c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float x = MotionEventCompat.getX(ev, i);
965c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float y = MotionEventCompat.getY(ev, i);
966c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float dx = x - mInitialMotionX[pointerId];
967c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float dy = y - mInitialMotionY[pointerId];
968c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
969c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    reportNewEdgeDrags(dx, dy, pointerId);
970c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if (mDragState == STATE_DRAGGING) {
971c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        // Callback might have started an edge drag
972c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        break;
973c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
974c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
975c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final View toCapture = findTopChildUnder((int) x, (int) y);
9761732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                    if (toCapture != null && checkTouchSlop(toCapture, dx, dy) &&
977c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            tryCaptureViewForDrag(toCapture, pointerId)) {
978c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        break;
979c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
980c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
981c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveLastMotion(ev);
982c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
983c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
984c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
985c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_UP: {
986c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
987c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                clearMotionHistory(pointerId);
988c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
989c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
990c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
991c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_UP:
992c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_CANCEL: {
993c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                cancel();
994c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
995c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
996c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
997c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
998c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mDragState == STATE_DRAGGING;
999c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1000c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1001c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1002c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Process a touch event received by the parent view. This method will dispatch callback events
1003c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * as needed before returning. The parent view's onTouchEvent implementation should call this.
1004c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1005c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param ev The touch event received by the parent view
1006c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
1007c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void processTouchEvent(MotionEvent ev) {
10086580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        final int action = MotionEventCompat.getActionMasked(ev);
10096580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        final int actionIndex = MotionEventCompat.getActionIndex(ev);
10106580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
10116580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        if (action == MotionEvent.ACTION_DOWN) {
10126580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // Reset things for a new event stream, just in case we didn't get
10136580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // the whole previous stream.
10146580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            cancel();
10156580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        }
10166580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
1017c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mVelocityTracker == null) {
1018c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker = VelocityTracker.obtain();
1019c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1020c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mVelocityTracker.addMovement(ev);
1021c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1022c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        switch (action) {
1023c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_DOWN: {
1024c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = ev.getX();
1025c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = ev.getY();
1026c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, 0);
1027c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final View toCapture = findTopChildUnder((int) x, (int) y);
1028c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1029c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
1030c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1031c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // Since the parent is already directly processing this touch event,
1032c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // there is no reason to delay for a slop before dragging.
1033c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // Start immediately if possible.
1034c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                tryCaptureViewForDrag(toCapture, pointerId);
1035c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1036c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int edgesTouched = mInitialEdgesTouched[pointerId];
1037c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if ((edgesTouched & mTrackingEdges) != 0) {
1038c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
1039c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1040c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1041c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1042c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1043c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_DOWN: {
1044c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1045c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = MotionEventCompat.getX(ev, actionIndex);
1046c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = MotionEventCompat.getY(ev, actionIndex);
1047c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1048c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
1049c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1050c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // A ViewDragHelper can only manipulate one view at a time.
1051c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_IDLE) {
1052c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // If we're idle we can do anything! Treat it like a normal down event.
1053c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1054c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final View toCapture = findTopChildUnder((int) x, (int) y);
1055c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    tryCaptureViewForDrag(toCapture, pointerId);
1056c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1057c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int edgesTouched = mInitialEdgesTouched[pointerId];
1058c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if ((edgesTouched & mTrackingEdges) != 0) {
1059c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
1060c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1061c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else if (isCapturedViewUnder((int) x, (int) y)) {
1062c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // We're still tracking a captured view. If the same view is under this
1063c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // point, we'll swap to controlling it with this pointer instead.
1064c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // (This will still work if we're "catching" a settling view.)
1065c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1066c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    tryCaptureViewForDrag(mCapturedView, pointerId);
1067c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1068c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1069c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1070c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1071c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_MOVE: {
1072c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING) {
1073c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
1074c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float x = MotionEventCompat.getX(ev, index);
1075c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float y = MotionEventCompat.getY(ev, index);
1076c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int idx = (int) (x - mLastMotionX[mActivePointerId]);
1077c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int idy = (int) (y - mLastMotionY[mActivePointerId]);
1078c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1079c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);
1080c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1081c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    saveLastMotion(ev);
1082c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else {
1083c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // Check to see if any pointer is now over a draggable view.
1084c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int pointerCount = MotionEventCompat.getPointerCount(ev);
1085c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    for (int i = 0; i < pointerCount; i++) {
1086c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final int pointerId = MotionEventCompat.getPointerId(ev, i);
1087c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float x = MotionEventCompat.getX(ev, i);
1088c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float y = MotionEventCompat.getY(ev, i);
1089c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float dx = x - mInitialMotionX[pointerId];
1090c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float dy = y - mInitialMotionY[pointerId];
1091c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1092c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        reportNewEdgeDrags(dx, dy, pointerId);
1093c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        if (mDragState == STATE_DRAGGING) {
1094c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            // Callback might have started an edge drag.
1095c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            break;
1096c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1097c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1098c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final View toCapture = findTopChildUnder((int) x, (int) y);
10991732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                        if (checkTouchSlop(toCapture, dx, dy) &&
1100c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                                tryCaptureViewForDrag(toCapture, pointerId)) {
1101c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            break;
1102c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1103c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1104c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    saveLastMotion(ev);
1105c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1106c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1107c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1108c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1109c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_UP: {
1110c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1111c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
1112c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // Try to find another pointer that's still holding on to the captured view.
1113c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    int newActivePointer = INVALID_POINTER;
1114c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int pointerCount = MotionEventCompat.getPointerCount(ev);
1115c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    for (int i = 0; i < pointerCount; i++) {
1116c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final int id = MotionEventCompat.getPointerId(ev, i);
1117c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        if (id == mActivePointerId) {
1118c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            // This one's going away, skip.
1119c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            continue;
1120c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1121c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1122c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float x = MotionEventCompat.getX(ev, i);
1123c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float y = MotionEventCompat.getY(ev, i);
1124c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        if (findTopChildUnder((int) x, (int) y) == mCapturedView &&
1125c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                                tryCaptureViewForDrag(mCapturedView, id)) {
1126c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            newActivePointer = mActivePointerId;
1127c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            break;
1128c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1129c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1130c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1131c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if (newActivePointer == INVALID_POINTER) {
1132c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        // We didn't find another pointer still touching the view, release it.
1133c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        releaseViewForPointerUp();
1134c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1135c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1136c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                clearMotionHistory(pointerId);
1137c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1138c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1139c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1140c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_UP: {
1141c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING) {
1142c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    releaseViewForPointerUp();
1143c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1144c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                cancel();
1145c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1146c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1147c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1148c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_CANCEL: {
1149c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING) {
1150c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    dispatchViewReleased(0, 0);
1151c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1152c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                cancel();
1153c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1154c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1155c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1156c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1157c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1158c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void reportNewEdgeDrags(float dx, float dy, int pointerId) {
1159c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int dragsStarted = 0;
1160acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_LEFT)) {
1161c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_LEFT;
1162c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1163acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_TOP)) {
1164c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_TOP;
1165c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1166acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_RIGHT)) {
1167c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_RIGHT;
1168c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1169acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_BOTTOM)) {
1170c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_BOTTOM;
1171c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1172c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1173c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (dragsStarted != 0) {
1174c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mEdgeDragsInProgress[pointerId] |= dragsStarted;
1175c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mCallback.onEdgeDragStarted(dragsStarted, pointerId);
1176c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1177c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1178c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1179acc82321ad119706485db342eaa12b225fa9b667Adam Powell    private boolean checkNewEdgeDrag(float delta, float odelta, int pointerId, int edge) {
1180acc82321ad119706485db342eaa12b225fa9b667Adam Powell        final float absDelta = Math.abs(delta);
1181acc82321ad119706485db342eaa12b225fa9b667Adam Powell        final float absODelta = Math.abs(odelta);
1182acc82321ad119706485db342eaa12b225fa9b667Adam Powell
1183fd5162a69e607f9199a502574c7486eb4e695e09Adam Powell        if ((mInitialEdgesTouched[pointerId] & edge) != edge  || (mTrackingEdges & edge) == 0 ||
1184fd5162a69e607f9199a502574c7486eb4e695e09Adam Powell                (mEdgeDragsLocked[pointerId] & edge) == edge ||
1185acc82321ad119706485db342eaa12b225fa9b667Adam Powell                (mEdgeDragsInProgress[pointerId] & edge) == edge ||
11861732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                (absDelta <= mTouchSlop && absODelta <= mTouchSlop)) {
1187acc82321ad119706485db342eaa12b225fa9b667Adam Powell            return false;
1188acc82321ad119706485db342eaa12b225fa9b667Adam Powell        }
1189ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        if (absDelta < absODelta * 0.5f && mCallback.onEdgeLock(edge)) {
1190acc82321ad119706485db342eaa12b225fa9b667Adam Powell            mEdgeDragsLocked[pointerId] |= edge;
1191acc82321ad119706485db342eaa12b225fa9b667Adam Powell            return false;
1192acc82321ad119706485db342eaa12b225fa9b667Adam Powell        }
11931732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return (mEdgeDragsInProgress[pointerId] & edge) == 0 && absDelta > mTouchSlop;
11946580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    }
11956580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
1196c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1197c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Check if we've crossed a reasonable touch slop for the given child view.
1198c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If the child cannot be dragged along the horizontal or vertical axis, motion
1199c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * along that axis will not count toward the slop check.
1200c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1201c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param child Child to check
1202c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dx Motion since initial position along X axis
1203c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dy Motion since initial position along Y axis
1204c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if the touch slop has been crossed
1205c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
12061732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    private boolean checkTouchSlop(View child, float dx, float dy) {
120789e17886e6149bddfdb08a242c9e88889596419cAdam Powell        if (child == null) {
120889e17886e6149bddfdb08a242c9e88889596419cAdam Powell            return false;
120989e17886e6149bddfdb08a242c9e88889596419cAdam Powell        }
1210c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final boolean checkHorizontal = mCallback.getViewHorizontalDragRange(child) > 0;
1211c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final boolean checkVertical = mCallback.getViewVerticalDragRange(child) > 0;
1212c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1213c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (checkHorizontal && checkVertical) {
1214c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return dx * dx + dy * dy > mTouchSlop * mTouchSlop;
1215c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        } else if (checkHorizontal) {
1216c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return Math.abs(dx) > mTouchSlop;
1217c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        } else if (checkVertical) {
1218c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return Math.abs(dy) > mTouchSlop;
1219c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1220c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return false;
1221c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1222c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
12231732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
12241732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if any pointer tracked in the current gesture has crossed
12251732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the required slop threshold.
12261732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12271732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * <p>This depends on internal state populated by
12281732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or
12291732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)}. You should only rely on
12301732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the results of this method after all currently available touch data
12311732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * has been provided to one of these two methods.</p>
12321732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12331732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL},
12341732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *                   {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL}
12351732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if the slop threshold has been crossed, false otherwise
12361732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
12371732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean checkTouchSlop(int directions) {
12381732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final int count = mInitialMotionX.length;
12391732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        for (int i = 0; i < count; i++) {
12401732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            if (checkTouchSlop(directions, i)) {
12411732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                return true;
12421732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            }
12431732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
12441732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return false;
12451732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
12461732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12471732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
12481732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if the specified pointer tracked in the current gesture has crossed
12491732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the required slop threshold.
12501732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12511732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * <p>This depends on internal state populated by
12521732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or
12531732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)}. You should only rely on
12541732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the results of this method after all currently available touch data
12551732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * has been provided to one of these two methods.</p>
12561732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12571732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL},
12581732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *                   {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL}
12591732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param pointerId ID of the pointer to slop check as specified by MotionEvent
12601732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if the slop threshold has been crossed, false otherwise
12611732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
12621732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean checkTouchSlop(int directions, int pointerId) {
12631732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        if (!isPointerDown(pointerId)) {
12641732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return false;
12651732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
12661732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12671732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final boolean checkHorizontal = (directions & DIRECTION_HORIZONTAL) == DIRECTION_HORIZONTAL;
12681732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final boolean checkVertical = (directions & DIRECTION_VERTICAL) == DIRECTION_VERTICAL;
12691732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12701732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final float dx = mLastMotionX[pointerId] - mInitialMotionX[pointerId];
12711732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final float dy = mLastMotionY[pointerId] - mInitialMotionY[pointerId];
12721732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12731732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        if (checkHorizontal && checkVertical) {
12741732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return dx * dx + dy * dy > mTouchSlop * mTouchSlop;
12751732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        } else if (checkHorizontal) {
12761732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return Math.abs(dx) > mTouchSlop;
12771732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        } else if (checkVertical) {
12781732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return Math.abs(dy) > mTouchSlop;
12791732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
12801732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return false;
12811732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
12821732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12831732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
12841732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if any of the edges specified were initially touched in the currently active gesture.
12851732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * If there is no currently active gesture this method will return false.
12861732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12871732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT},
12881732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and
12891732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_ALL}
12901732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if any of the edges specified were initially touched in the current gesture
12911732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
12921732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean isEdgeTouched(int edges) {
12931732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final int count = mInitialEdgesTouched.length;
12941732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        for (int i = 0; i < count; i++) {
12951732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            if (isEdgeTouched(edges, i)) {
12961732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                return true;
12971732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            }
12981732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
12991732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return false;
13001732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
13011732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
13021732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
13031732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if any of the edges specified were initially touched by the pointer with
13041732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the specified ID. If there is no currently active gesture or if there is no pointer with
13051732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the given ID currently down this method will return false.
13061732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
13071732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT},
13081732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and
13091732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_ALL}
13101732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if any of the edges specified were initially touched in the current gesture
13111732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
13121732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean isEdgeTouched(int edges, int pointerId) {
13131732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return isPointerDown(pointerId) && (mInitialEdgesTouched[pointerId] & edges) != 0;
13141732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
13151732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
1316c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void releaseViewForPointerUp() {
1317c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
1318c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        dispatchViewReleased(VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
1319c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId));
1320c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1321c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
13221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private void dragTo(int left, int top, int dx, int dy) {
13231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        int clampedX = left;
13241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        int clampedY = top;
13251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int oldLeft = mCapturedView.getLeft();
13261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int oldTop = mCapturedView.getTop();
13271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (dx != 0) {
13281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            clampedX = mCallback.clampViewPositionHorizontal(mCapturedView, left, dx);
13291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCapturedView.offsetLeftAndRight(clampedX - oldLeft);
1330c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
13311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (dy != 0) {
13321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            clampedY = mCallback.clampViewPositionVertical(mCapturedView, top, dy);
13331d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCapturedView.offsetTopAndBottom(clampedY - oldTop);
1334c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1335c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
13361d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (dx != 0 || dy != 0) {
13371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int clampedDx = clampedX - oldLeft;
13381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int clampedDy = clampedY - oldTop;
13391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCallback.onViewPositionChanged(mCapturedView, clampedX, clampedY,
13401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    clampedDx, clampedDy);
1341c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1342c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1343c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1344c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1345c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Determine if the currently captured view is under the given point in the
1346c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * parent view's coordinate system. If there is no captured view this method
1347c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * will return false.
1348c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1349c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param x X position to test in the parent's coordinate system
1350c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param y Y position to test in the parent's coordinate system
1351c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if the captured view is under the given point, false otherwise
1352c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
1353c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean isCapturedViewUnder(int x, int y) {
13546580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        return isViewUnder(mCapturedView, x, y);
13556580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    }
13566580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
13576580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    /**
13586580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * Determine if the supplied view is under the given point in the
13596580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * parent view's coordinate system.
13606580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     *
13616580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @param view Child view of the parent to hit test
13626580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @param x X position to test in the parent's coordinate system
13636580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @param y Y position to test in the parent's coordinate system
13646580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @return true if the supplied view is under the given point, false otherwise
13656580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     */
13666580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    public boolean isViewUnder(View view, int x, int y) {
13676580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        if (view == null) {
1368c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return false;
1369c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
13706580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        return x >= view.getLeft() &&
13716580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                x < view.getRight() &&
13726580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                y >= view.getTop() &&
1373471f0f4e84c7d1f96e48fb26713f246d69c3241fAdam Powell                y < view.getBottom();
1374c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1375c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1376c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1377c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Find the topmost child under the given point within the parent view's coordinate system.
1378c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The child order is determined using {@link Callback#getOrderedChildIndex(int)}.
1379c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1380c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param x X position to test in the parent's coordinate system
1381c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param y Y position to test in the parent's coordinate system
1382c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The topmost child view under (x, y) or null if none found.
1383c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
1384c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public View findTopChildUnder(int x, int y) {
1385c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int childCount = mParentView.getChildCount();
1386c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        for (int i = childCount - 1; i >= 0; i--) {
1387c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i));
1388c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (x >= child.getLeft() && x < child.getRight() &&
1389c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    y >= child.getTop() && y < child.getBottom()) {
1390c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                return child;
1391c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1392c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1393c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return null;
1394c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1395c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1396c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int getEdgesTouched(int x, int y) {
1397c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int result = 0;
1398c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1399c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (x < mParentView.getLeft() + mEdgeSize) result |= EDGE_LEFT;
1400c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (y < mParentView.getTop() + mEdgeSize) result |= EDGE_TOP;
1401c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (x > mParentView.getRight() - mEdgeSize) result |= EDGE_RIGHT;
1402c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (y > mParentView.getBottom() - mEdgeSize) result |= EDGE_BOTTOM;
1403c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1404c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return result;
1405c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1406c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell}
1407