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    /**
39579f95ce3e660d267831067e514ff455156c4381fAdam Powell     * Set the minimum velocity that will be detected as having a magnitude greater than zero
39679f95ce3e660d267831067e514ff455156c4381fAdam Powell     * in pixels per second. Callback methods accepting a velocity will be clamped appropriately.
39779f95ce3e660d267831067e514ff455156c4381fAdam Powell     *
39879f95ce3e660d267831067e514ff455156c4381fAdam Powell     * @param minVel Minimum velocity to detect
39979f95ce3e660d267831067e514ff455156c4381fAdam Powell     */
40079f95ce3e660d267831067e514ff455156c4381fAdam Powell    public void setMinVelocity(float minVel) {
40179f95ce3e660d267831067e514ff455156c4381fAdam Powell        mMinVelocity = minVel;
40279f95ce3e660d267831067e514ff455156c4381fAdam Powell    }
40379f95ce3e660d267831067e514ff455156c4381fAdam Powell
40479f95ce3e660d267831067e514ff455156c4381fAdam Powell    /**
40579f95ce3e660d267831067e514ff455156c4381fAdam Powell     * Return the currently configured minimum velocity. Any flings with a magnitude less
40679f95ce3e660d267831067e514ff455156c4381fAdam Powell     * than this value in pixels per second. Callback methods accepting a velocity will receive
40779f95ce3e660d267831067e514ff455156c4381fAdam Powell     * zero as a velocity value if the real detected velocity was below this threshold.
40879f95ce3e660d267831067e514ff455156c4381fAdam Powell     *
40979f95ce3e660d267831067e514ff455156c4381fAdam Powell     * @return the minimum velocity that will be detected
41079f95ce3e660d267831067e514ff455156c4381fAdam Powell     */
41179f95ce3e660d267831067e514ff455156c4381fAdam Powell    public float getMinVelocity() {
41279f95ce3e660d267831067e514ff455156c4381fAdam Powell        return mMinVelocity;
41379f95ce3e660d267831067e514ff455156c4381fAdam Powell    }
41479f95ce3e660d267831067e514ff455156c4381fAdam Powell
41579f95ce3e660d267831067e514ff455156c4381fAdam Powell    /**
4161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Retrieve the current drag state of this helper. This will return one of
4171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}.
4181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @return The current drag state
4191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
4201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public int getViewDragState() {
4211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return mDragState;
4221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
425c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Enable edge tracking for the selected edges of the parent view.
426c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The callback's {@link Callback#onEdgeTouched(int, int)} and
427c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * {@link Callback#onEdgeDragStarted(int, int)} methods will only be invoked
428c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * for edges for which edge tracking has been enabled.
429c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
430c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param edgeFlags Combination of edge flags describing the edges to watch
431c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_LEFT
432c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_TOP
433c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_RIGHT
434c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @see #EDGE_BOTTOM
435c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
436c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void setEdgeTrackingEnabled(int edgeFlags) {
437c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mTrackingEdges = edgeFlags;
438c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
439c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
440c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
441ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * Return the size of an edge. This is the range in pixels along the edges of this view
442ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * that will actively detect edge touches or drags if edge tracking is enabled.
443ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     *
444ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * @return The size of an edge in pixels
445ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * @see #setEdgeTrackingEnabled(int)
446ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     */
447ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    public int getEdgeSize() {
448ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        return mEdgeSize;
449ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    }
450ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell
451ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    /**
452c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Capture a specific child view for dragging within the parent. The callback will be notified
453c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * but {@link Callback#tryCaptureView(android.view.View, int)} will not be asked permission to
454c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * capture this view.
455c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
456c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param childView Child view to capture
457c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param activePointerId ID of the pointer that is dragging the captured child view
458c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
459c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void captureChildView(View childView, int activePointerId) {
460c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (childView.getParent() != mParentView) {
461c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalArgumentException("captureChildView: parameter must be a descendant " +
462c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    "of the ViewDragHelper's tracked parent view (" + mParentView + ")");
463c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
464c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
465c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCapturedView = childView;
466c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mActivePointerId = activePointerId;
467c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCallback.onViewCaptured(childView, activePointerId);
4686580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        setDragState(STATE_DRAGGING);
469c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
470c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
471c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
472c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The currently captured view, or null if no view has been captured.
473c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
474c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public View getCapturedView() {
475c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mCapturedView;
476c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
477c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
478c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
479c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The ID of the pointer currently dragging the captured view,
480c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *         or {@link #INVALID_POINTER}.
481c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
482c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public int getActivePointerId() {
483c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mActivePointerId;
484c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
485c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
486c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
4871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @return The minimum distance in pixels that the user must travel to initiate a drag
4881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
4891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public int getTouchSlop() {
4901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return mTouchSlop;
4911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
494c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The result of a call to this method is equivalent to
495c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)} receiving an ACTION_CANCEL event.
496c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
497c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void cancel() {
498c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mActivePointerId = INVALID_POINTER;
499c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        clearMotionHistory();
500c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
501c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mVelocityTracker != null) {
502c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker.recycle();
503c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker = null;
504c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
505c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
506c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
507c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
508c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * {@link #cancel()}, but also abort all motion in progress and snap to the end of any
509c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * animation.
510c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
511c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void abort() {
512c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        cancel();
513c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState == STATE_SETTLING) {
514c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int oldX = mScroller.getCurrX();
515c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int oldY = mScroller.getCurrY();
516c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mScroller.abortAnimation();
517c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int newX = mScroller.getCurrX();
518c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int newY = mScroller.getCurrY();
5191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCallback.onViewPositionChanged(mCapturedView, newX, newY, newX - oldX, newY - oldY);
520c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
521c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        setDragState(STATE_IDLE);
522c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
523c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
524c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
525c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Animate the view <code>child</code> to the given (left, top) position.
526c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
527c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * on each subsequent frame to continue the motion until it returns false. If this method
528c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * returns false there is no further work to do to complete the movement.
529c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
5301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * <p>This operation does not count as a capture event, though {@link #getCapturedView()}
5311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * will still report the sliding view while the slide is in progress.</p>
5321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
533c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param child Child view to capture and animate
534c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalLeft Final left position of child
535c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalTop Final top position of child
536c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if animation should continue through {@link #continueSettling(boolean)} calls
537c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
538c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean smoothSlideViewTo(View child, int finalLeft, int finalTop) {
539c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCapturedView = child;
540c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mActivePointerId = INVALID_POINTER;
541c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
542c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return forceSettleCapturedViewAt(finalLeft, finalTop, 0, 0);
543c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
544c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
545c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
546c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Settle the captured view at the given (left, top) position.
547c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The appropriate velocity from prior motion will be taken into account.
548c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
549c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * on each subsequent frame to continue the motion until it returns false. If this method
550c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * returns false there is no further work to do to complete the movement.
551c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
552c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalLeft Settled left edge position for the captured view
553c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalTop Settled top edge position for the captured view
554c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if animation should continue through {@link #continueSettling(boolean)} calls
555c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
556c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean settleCapturedViewAt(int finalLeft, int finalTop) {
557c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (!mReleaseInProgress) {
558c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalStateException("Cannot settleCapturedViewAt outside of a call to " +
559c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    "Callback#onViewReleased");
560c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
561c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
562c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return forceSettleCapturedViewAt(finalLeft, finalTop,
563c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
564c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId));
565c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
566c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
567c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
568c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Settle the captured view at the given (left, top) position.
569c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
570c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalLeft Target left position for the captured view
571c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param finalTop Target top position for the captured view
572c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param xvel Horizontal velocity
573c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param yvel Vertical velocity
574c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if animation should continue through {@link #continueSettling(boolean)} calls
575c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
576c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private boolean forceSettleCapturedViewAt(int finalLeft, int finalTop, int xvel, int yvel) {
577c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int startLeft = mCapturedView.getLeft();
578c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int startTop = mCapturedView.getTop();
579c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int dx = finalLeft - startLeft;
580c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int dy = finalTop - startTop;
581c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
582c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (dx == 0 && dy == 0) {
583c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // Nothing to do. Send callbacks, be done.
5841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mScroller.abortAnimation();
585c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            setDragState(STATE_IDLE);
586c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return false;
587c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
588c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
589c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int duration = computeSettleDuration(mCapturedView, dx, dy, xvel, yvel);
590c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mScroller.startScroll(startLeft, startTop, dx, dy, duration);
591c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
592c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        setDragState(STATE_SETTLING);
593c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return true;
594c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
595c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
596c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int computeSettleDuration(View child, int dx, int dy, int xvel, int yvel) {
597c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        xvel = clampMag(xvel, (int) mMinVelocity, (int) mMaxVelocity);
598c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        yvel = clampMag(yvel, (int) mMinVelocity, (int) mMaxVelocity);
599c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absDx = Math.abs(dx);
600c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absDy = Math.abs(dy);
601c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absXVel = Math.abs(xvel);
602c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absYVel = Math.abs(yvel);
603c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int addedVel = absXVel + absYVel;
604c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int addedDistance = absDx + absDy;
605c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
606c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float xweight = xvel != 0 ? (float) absXVel / addedVel :
607c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (float) absDx / addedDistance;
608c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float yweight = yvel != 0 ? (float) absYVel / addedVel :
609c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (float) absDy / addedDistance;
610c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
611c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int xduration = computeAxisDuration(dx, xvel, mCallback.getViewHorizontalDragRange(child));
612c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int yduration = computeAxisDuration(dy, yvel, mCallback.getViewVerticalDragRange(child));
613c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
614c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return (int) (xduration * xweight + yduration * yweight);
615c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
616c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
617c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int computeAxisDuration(int delta, int velocity, int motionRange) {
618c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (delta == 0) {
619c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return 0;
620c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
621c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
622c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int width = mParentView.getWidth();
623c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int halfWidth = width / 2;
624c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float distanceRatio = Math.min(1f, (float) Math.abs(delta) / width);
625c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final float distance = halfWidth + halfWidth *
626c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                distanceInfluenceForSnapDuration(distanceRatio);
627c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
628c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int duration;
629c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        velocity = Math.abs(velocity);
630c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (velocity > 0) {
631c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
632c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        } else {
633c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final float range = (float) Math.abs(delta) / motionRange;
634c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            duration = (int) ((range + 1) * BASE_SETTLE_DURATION);
635c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
636c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return Math.min(duration, MAX_SETTLE_DURATION);
637c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
638c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
639c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
640c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Clamp the magnitude of value for absMin and absMax.
641c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If the value is below the minimum, it will be clamped to zero.
642c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If the value is above the maximum, it will be clamped to the maximum.
643c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
644c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param value Value to clamp
645c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param absMin Absolute value of the minimum significant value to return
646c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param absMax Absolute value of the maximum value to return
647c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The clamped value with the same sign as <code>value</code>
648c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
649c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int clampMag(int value, int absMin, int absMax) {
650c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int absValue = Math.abs(value);
651c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (absValue < absMin) return 0;
652c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (absValue > absMax) return value > 0 ? absMax : -absMax;
653c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return value;
654c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
655c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
65679f95ce3e660d267831067e514ff455156c4381fAdam Powell    /**
65779f95ce3e660d267831067e514ff455156c4381fAdam Powell     * Clamp the magnitude of value for absMin and absMax.
65879f95ce3e660d267831067e514ff455156c4381fAdam Powell     * If the value is below the minimum, it will be clamped to zero.
65979f95ce3e660d267831067e514ff455156c4381fAdam Powell     * If the value is above the maximum, it will be clamped to the maximum.
66079f95ce3e660d267831067e514ff455156c4381fAdam Powell     *
66179f95ce3e660d267831067e514ff455156c4381fAdam Powell     * @param value Value to clamp
66279f95ce3e660d267831067e514ff455156c4381fAdam Powell     * @param absMin Absolute value of the minimum significant value to return
66379f95ce3e660d267831067e514ff455156c4381fAdam Powell     * @param absMax Absolute value of the maximum value to return
66479f95ce3e660d267831067e514ff455156c4381fAdam Powell     * @return The clamped value with the same sign as <code>value</code>
66579f95ce3e660d267831067e514ff455156c4381fAdam Powell     */
66679f95ce3e660d267831067e514ff455156c4381fAdam Powell    private float clampMag(float value, float absMin, float absMax) {
66779f95ce3e660d267831067e514ff455156c4381fAdam Powell        final float absValue = Math.abs(value);
66879f95ce3e660d267831067e514ff455156c4381fAdam Powell        if (absValue < absMin) return 0;
66979f95ce3e660d267831067e514ff455156c4381fAdam Powell        if (absValue > absMax) return value > 0 ? absMax : -absMax;
67079f95ce3e660d267831067e514ff455156c4381fAdam Powell        return value;
67179f95ce3e660d267831067e514ff455156c4381fAdam Powell    }
67279f95ce3e660d267831067e514ff455156c4381fAdam Powell
673c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private float distanceInfluenceForSnapDuration(float f) {
674c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        f -= 0.5f; // center the values about 0.
675c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        f *= 0.3f * Math.PI / 2.0f;
676c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return (float) Math.sin(f);
677c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
678c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
679c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
680c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Settle the captured view based on standard free-moving fling behavior.
681c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The caller should invoke {@link #continueSettling(boolean)} on each subsequent frame
682c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * to continue the motion until it returns false.
683c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
684c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param minLeft Minimum X position for the view's left edge
685c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param minTop Minimum Y position for the view's top edge
686c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param maxLeft Maximum X position for the view's left edge
687c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param maxTop Maximum Y position for the view's top edge
688c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
689c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop) {
690c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (!mReleaseInProgress) {
691c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            throw new IllegalStateException("Cannot flingCapturedView outside of a call to " +
692c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    "Callback#onViewReleased");
693c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
694c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
695c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mScroller.fling(mCapturedView.getLeft(), mCapturedView.getTop(),
696c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
697c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
698c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                minLeft, maxLeft, minTop, maxTop);
699c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
700c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        setDragState(STATE_SETTLING);
701c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
702c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
703c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
704c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Move the captured settling view by the appropriate amount for the current time.
705c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If <code>continueSettling</code> returns true, the caller should call it again
706c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * on the next frame to continue.
707c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
708c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param deferCallbacks true if state callbacks should be deferred via posted message.
709c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *                       Set this to true if you are calling this method from
710c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *                       {@link android.view.View#computeScroll()} or similar methods
711c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *                       invoked as part of layout or drawing.
712c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if settle is still in progress
713c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
714c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean continueSettling(boolean deferCallbacks) {
715c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState == STATE_SETTLING) {
7166580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            boolean keepGoing = mScroller.computeScrollOffset();
717c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int x = mScroller.getCurrX();
718c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int y = mScroller.getCurrY();
719c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int dx = x - mCapturedView.getLeft();
720c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int dy = y - mCapturedView.getTop();
721c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
722c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (dx != 0) {
723c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                mCapturedView.offsetLeftAndRight(dx);
724c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
725c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (dy != 0) {
726c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                mCapturedView.offsetTopAndBottom(dy);
727c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
728c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
729c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (dx != 0 || dy != 0) {
7301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mCallback.onViewPositionChanged(mCapturedView, x, y, dx, dy);
731c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
732c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
7336580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            if (keepGoing && x == mScroller.getFinalX() && y == mScroller.getFinalY()) {
7346580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                // Close enough. The interpolator/scroller might think we're still moving
7356580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                // but the user sure doesn't.
7366580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                mScroller.abortAnimation();
7376580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                keepGoing = mScroller.isFinished();
7386580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            }
7396580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
740c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (!keepGoing) {
741c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (deferCallbacks) {
742c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    mParentView.post(mSetIdleRunnable);
743c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else {
744c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    setDragState(STATE_IDLE);
745c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
746c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
747c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
748c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
749c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mDragState == STATE_SETTLING;
750c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
751c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
752c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
753c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Like all callback events this must happen on the UI thread, but release
754c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * involves some extra semantics. During a release (mReleaseInProgress)
755c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * is the only time it is valid to call {@link #settleCapturedViewAt(int, int)}
756c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * or {@link #flingCapturedView(int, int, int, int)}.
757c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
758c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void dispatchViewReleased(float xvel, float yvel) {
759c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mReleaseInProgress = true;
760c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mCallback.onViewReleased(mCapturedView, xvel, yvel);
761c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mReleaseInProgress = false;
762c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
763c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState == STATE_DRAGGING) {
764c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // onViewReleased didn't call a method that would have changed this. Go idle.
765c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            setDragState(STATE_IDLE);
766c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
767c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
768c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
769c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void clearMotionHistory() {
770c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mInitialMotionX == null) {
771c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return;
772c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
773c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mInitialMotionX, 0);
774c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mInitialMotionY, 0);
775c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mLastMotionX, 0);
776c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mLastMotionY, 0);
777c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mInitialEdgesTouched, 0);
778c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        Arrays.fill(mEdgeDragsInProgress, 0);
779acc82321ad119706485db342eaa12b225fa9b667Adam Powell        Arrays.fill(mEdgeDragsLocked, 0);
7801732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mPointersDown = 0;
781c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
782c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
783c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void clearMotionHistory(int pointerId) {
784c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mInitialMotionX == null) {
785c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return;
786c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
787c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionX[pointerId] = 0;
788c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionY[pointerId] = 0;
789c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mLastMotionX[pointerId] = 0;
790c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mLastMotionY[pointerId] = 0;
791c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialEdgesTouched[pointerId] = 0;
792c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mEdgeDragsInProgress[pointerId] = 0;
793acc82321ad119706485db342eaa12b225fa9b667Adam Powell        mEdgeDragsLocked[pointerId] = 0;
7941732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mPointersDown &= ~(1 << pointerId);
795c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
796c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
797c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void ensureMotionHistorySizeForId(int pointerId) {
798c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mInitialMotionX == null || mInitialMotionX.length <= pointerId) {
799c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] imx = new float[pointerId + 1];
800c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] imy = new float[pointerId + 1];
801c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] lmx = new float[pointerId + 1];
802c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            float[] lmy = new float[pointerId + 1];
803c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            int[] iit = new int[pointerId + 1];
804c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            int[] edip = new int[pointerId + 1];
805acc82321ad119706485db342eaa12b225fa9b667Adam Powell            int[] edl = new int[pointerId + 1];
806c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
807c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (mInitialMotionX != null) {
808c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mInitialMotionX, 0, imx, 0, mInitialMotionX.length);
809c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mInitialMotionY, 0, imy, 0, mInitialMotionY.length);
810c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mLastMotionX, 0, lmx, 0, mLastMotionX.length);
811c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mLastMotionY, 0, lmy, 0, mLastMotionY.length);
812c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mInitialEdgesTouched, 0, iit, 0, mInitialEdgesTouched.length);
813c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                System.arraycopy(mEdgeDragsInProgress, 0, edip, 0, mEdgeDragsInProgress.length);
814acc82321ad119706485db342eaa12b225fa9b667Adam Powell                System.arraycopy(mEdgeDragsLocked, 0, edl, 0, mEdgeDragsLocked.length);
815c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
816c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
817c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mInitialMotionX = imx;
818c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mInitialMotionY = imy;
819c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionX = lmx;
820c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionY = lmy;
821c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mInitialEdgesTouched = iit;
822c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mEdgeDragsInProgress = edip;
823acc82321ad119706485db342eaa12b225fa9b667Adam Powell            mEdgeDragsLocked = edl;
824c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
825c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
826c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
827c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void saveInitialMotion(float x, float y, int pointerId) {
828c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        ensureMotionHistorySizeForId(pointerId);
829c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionX[pointerId] = mLastMotionX[pointerId] = x;
830c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialMotionY[pointerId] = mLastMotionY[pointerId] = y;
831c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mInitialEdgesTouched[pointerId] = getEdgesTouched((int) x, (int) y);
8321732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mPointersDown |= 1 << pointerId;
833c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
834c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
835c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void saveLastMotion(MotionEvent ev) {
836c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int pointerCount = MotionEventCompat.getPointerCount(ev);
837c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        for (int i = 0; i < pointerCount; i++) {
838c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int pointerId = MotionEventCompat.getPointerId(ev, i);
839c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final float x = MotionEventCompat.getX(ev, i);
840c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final float y = MotionEventCompat.getY(ev, i);
841c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionX[pointerId] = x;
842c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mLastMotionY[pointerId] = y;
843c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
844c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
845c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
8461732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
8471732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if the given pointer ID represents a pointer that is currently down (to the best
8481732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * of the ViewDragHelper's knowledge).
8491732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
8501732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * <p>The state used to report this information is populated by the methods
8511732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or
8521732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)}. If one of these methods has not
8531732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * been called for all relevant MotionEvents to track, the information reported
8541732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * by this method may be stale or incorrect.</p>
8551732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
8561732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param pointerId pointer ID to check; corresponds to IDs provided by MotionEvent
8571732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if the pointer with the given ID is still down
8581732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
8591732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean isPointerDown(int pointerId) {
8601732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return (mPointersDown & 1 << pointerId) != 0;
8611732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
8621732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
863c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    void setDragState(int state) {
864c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mDragState != state) {
865c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mDragState = state;
866c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mCallback.onViewDragStateChanged(state);
8676580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            if (state == STATE_IDLE) {
8686580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                mCapturedView = null;
8696580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            }
870c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
871c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
872c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
873c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
874c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Attempt to capture the view with the given pointer ID. The callback will be involved.
875c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * This will put us into the "dragging" state. If we've already captured this view with
876c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * this pointer this method will immediately return true without consulting the callback.
877c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
878c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param toCapture View to capture
879c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param pointerId Pointer to capture with
880c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if capture was successful
881c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
882c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    boolean tryCaptureViewForDrag(View toCapture, int pointerId) {
883c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (toCapture == mCapturedView && mActivePointerId == pointerId) {
884c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // Already done!
885c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return true;
886c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
887c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (toCapture != null && mCallback.tryCaptureView(toCapture, pointerId)) {
888c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mActivePointerId = pointerId;
889c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            captureChildView(toCapture, pointerId);
890c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return true;
891c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
892c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return false;
893c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
894c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
895c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
896c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Tests scrollability within child views of v given a delta of dx.
897c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
898c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param v View to test for horizontal scrollability
899c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param checkV Whether the view v passed should itself be checked for scrollability (true),
900c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *               or just its children (false).
901c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dx Delta scrolled in pixels along the X axis
902c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dy Delta scrolled in pixels along the Y axis
903c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param x X coordinate of the active touch point
904c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param y Y coordinate of the active touch point
905c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if child views of v can be scrolled by delta of dx.
906c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
907c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) {
908c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (v instanceof ViewGroup) {
909c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final ViewGroup group = (ViewGroup) v;
910c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int scrollX = v.getScrollX();
911c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int scrollY = v.getScrollY();
912c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final int count = group.getChildCount();
913c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            // Count backwards - let topmost views consume scroll distance first.
914c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            for (int i = count - 1; i >= 0; i--) {
915c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // TODO: Add versioned support here for transformed views.
916c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // This will not work for transformed views in Honeycomb+
917c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final View child = group.getChildAt(i);
918c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
919c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
920c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        canScroll(child, true, dx, dy, x + scrollX - child.getLeft(),
921c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                                y + scrollY - child.getTop())) {
922c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    return true;
923c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
924c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
925c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
926c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
927c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return checkV && (ViewCompat.canScrollHorizontally(v, -dx) ||
928c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                ViewCompat.canScrollVertically(v, -dy));
929c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
930c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
931c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
932c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Check if this event as provided to the parent view's onInterceptTouchEvent should
933c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * cause the parent to intercept the touch event stream.
934c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
935c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param ev MotionEvent provided to onInterceptTouchEvent
936c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if the parent view should return true from onInterceptTouchEvent
937c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
938c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean shouldInterceptTouchEvent(MotionEvent ev) {
9391fbad11af8f178d9fcee85dabe7cd8f24d2bc9a2Adam Powell        final int action = MotionEventCompat.getActionMasked(ev);
9401fbad11af8f178d9fcee85dabe7cd8f24d2bc9a2Adam Powell        final int actionIndex = MotionEventCompat.getActionIndex(ev);
9416580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
9426580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        if (action == MotionEvent.ACTION_DOWN) {
9436580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // Reset things for a new event stream, just in case we didn't get
9446580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // the whole previous stream.
9456580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            cancel();
9466580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        }
9476580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
948c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mVelocityTracker == null) {
949c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker = VelocityTracker.obtain();
950c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
951c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mVelocityTracker.addMovement(ev);
952c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
953c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        switch (action) {
954c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_DOWN: {
955c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = ev.getX();
956c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = ev.getY();
957c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, 0);
958c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
959c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
960c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final View toCapture = findTopChildUnder((int) x, (int) y);
961c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
962c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // Catch a settling view if possible.
963c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
964c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    tryCaptureViewForDrag(toCapture, pointerId);
965c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
966c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
967c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int edgesTouched = mInitialEdgesTouched[pointerId];
968c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if ((edgesTouched & mTrackingEdges) != 0) {
969c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
970c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
971c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
972c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
973c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
974c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_DOWN: {
975c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
976c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = MotionEventCompat.getX(ev, actionIndex);
977c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = MotionEventCompat.getY(ev, actionIndex);
978c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
979c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
980c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
981c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // A ViewDragHelper can only manipulate one view at a time.
982c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_IDLE) {
983c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int edgesTouched = mInitialEdgesTouched[pointerId];
984c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if ((edgesTouched & mTrackingEdges) != 0) {
985c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
986c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
987c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else if (mDragState == STATE_SETTLING) {
988c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // Catch a settling view if possible.
989c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final View toCapture = findTopChildUnder((int) x, (int) y);
990c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if (toCapture == mCapturedView) {
991c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        tryCaptureViewForDrag(toCapture, pointerId);
992c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
993c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
994c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
995c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
996c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
997c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_MOVE: {
998c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // First to cross a touch slop over a draggable view wins. Also report edge drags.
999c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerCount = MotionEventCompat.getPointerCount(ev);
1000c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                for (int i = 0; i < pointerCount; i++) {
1001c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int pointerId = MotionEventCompat.getPointerId(ev, i);
1002c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float x = MotionEventCompat.getX(ev, i);
1003c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float y = MotionEventCompat.getY(ev, i);
1004c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float dx = x - mInitialMotionX[pointerId];
1005c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float dy = y - mInitialMotionY[pointerId];
1006c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1007c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    reportNewEdgeDrags(dx, dy, pointerId);
1008c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if (mDragState == STATE_DRAGGING) {
1009c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        // Callback might have started an edge drag
1010c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        break;
1011c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1012c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1013c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final View toCapture = findTopChildUnder((int) x, (int) y);
10141732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                    if (toCapture != null && checkTouchSlop(toCapture, dx, dy) &&
1015c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            tryCaptureViewForDrag(toCapture, pointerId)) {
1016c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        break;
1017c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1018c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1019c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveLastMotion(ev);
1020c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1021c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1022c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1023c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_UP: {
1024c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1025c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                clearMotionHistory(pointerId);
1026c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1027c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1028c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1029c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_UP:
1030c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_CANCEL: {
1031c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                cancel();
1032c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1033c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1034c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1035c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1036c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return mDragState == STATE_DRAGGING;
1037c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1038c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1039c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1040c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Process a touch event received by the parent view. This method will dispatch callback events
1041c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * as needed before returning. The parent view's onTouchEvent implementation should call this.
1042c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1043c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param ev The touch event received by the parent view
1044c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
1045c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public void processTouchEvent(MotionEvent ev) {
10466580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        final int action = MotionEventCompat.getActionMasked(ev);
10476580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        final int actionIndex = MotionEventCompat.getActionIndex(ev);
10486580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
10496580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        if (action == MotionEvent.ACTION_DOWN) {
10506580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // Reset things for a new event stream, just in case we didn't get
10516580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            // the whole previous stream.
10526580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell            cancel();
10536580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        }
10546580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
1055c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (mVelocityTracker == null) {
1056c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mVelocityTracker = VelocityTracker.obtain();
1057c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1058c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mVelocityTracker.addMovement(ev);
1059c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1060c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        switch (action) {
1061c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_DOWN: {
1062c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = ev.getX();
1063c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = ev.getY();
1064c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, 0);
1065c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final View toCapture = findTopChildUnder((int) x, (int) y);
1066c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1067c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
1068c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1069c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // Since the parent is already directly processing this touch event,
1070c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // there is no reason to delay for a slop before dragging.
1071c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // Start immediately if possible.
1072c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                tryCaptureViewForDrag(toCapture, pointerId);
1073c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1074c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int edgesTouched = mInitialEdgesTouched[pointerId];
1075c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if ((edgesTouched & mTrackingEdges) != 0) {
1076c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
1077c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1078c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1079c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1080c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1081c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_DOWN: {
1082c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1083c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float x = MotionEventCompat.getX(ev, actionIndex);
1084c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final float y = MotionEventCompat.getY(ev, actionIndex);
1085c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1086c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                saveInitialMotion(x, y, pointerId);
1087c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1088c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                // A ViewDragHelper can only manipulate one view at a time.
1089c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_IDLE) {
1090c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // If we're idle we can do anything! Treat it like a normal down event.
1091c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1092c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final View toCapture = findTopChildUnder((int) x, (int) y);
1093c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    tryCaptureViewForDrag(toCapture, pointerId);
1094c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1095c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int edgesTouched = mInitialEdgesTouched[pointerId];
1096c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if ((edgesTouched & mTrackingEdges) != 0) {
1097c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
1098c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1099c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else if (isCapturedViewUnder((int) x, (int) y)) {
1100c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // We're still tracking a captured view. If the same view is under this
1101c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // point, we'll swap to controlling it with this pointer instead.
1102c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // (This will still work if we're "catching" a settling view.)
1103c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1104c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    tryCaptureViewForDrag(mCapturedView, pointerId);
1105c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1106c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1107c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1108c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1109c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_MOVE: {
1110c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING) {
1111c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
1112c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float x = MotionEventCompat.getX(ev, index);
1113c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final float y = MotionEventCompat.getY(ev, index);
1114c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int idx = (int) (x - mLastMotionX[mActivePointerId]);
1115c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int idy = (int) (y - mLastMotionY[mActivePointerId]);
1116c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1117c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);
1118c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1119c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    saveLastMotion(ev);
1120c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                } else {
1121c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // Check to see if any pointer is now over a draggable view.
1122c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int pointerCount = MotionEventCompat.getPointerCount(ev);
1123c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    for (int i = 0; i < pointerCount; i++) {
1124c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final int pointerId = MotionEventCompat.getPointerId(ev, i);
1125c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float x = MotionEventCompat.getX(ev, i);
1126c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float y = MotionEventCompat.getY(ev, i);
1127c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float dx = x - mInitialMotionX[pointerId];
1128c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float dy = y - mInitialMotionY[pointerId];
1129c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1130c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        reportNewEdgeDrags(dx, dy, pointerId);
1131c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        if (mDragState == STATE_DRAGGING) {
1132c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            // Callback might have started an edge drag.
1133c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            break;
1134c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1135c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1136c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final View toCapture = findTopChildUnder((int) x, (int) y);
11371732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                        if (checkTouchSlop(toCapture, dx, dy) &&
1138c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                                tryCaptureViewForDrag(toCapture, pointerId)) {
1139c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            break;
1140c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1141c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1142c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    saveLastMotion(ev);
1143c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1144c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1145c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1146c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1147c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEventCompat.ACTION_POINTER_UP: {
1148c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
1149c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
1150c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    // Try to find another pointer that's still holding on to the captured view.
1151c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    int newActivePointer = INVALID_POINTER;
1152c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    final int pointerCount = MotionEventCompat.getPointerCount(ev);
1153c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    for (int i = 0; i < pointerCount; i++) {
1154c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final int id = MotionEventCompat.getPointerId(ev, i);
1155c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        if (id == mActivePointerId) {
1156c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            // This one's going away, skip.
1157c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            continue;
1158c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1159c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1160c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float x = MotionEventCompat.getX(ev, i);
1161c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        final float y = MotionEventCompat.getY(ev, i);
1162c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        if (findTopChildUnder((int) x, (int) y) == mCapturedView &&
1163c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                                tryCaptureViewForDrag(mCapturedView, id)) {
1164c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            newActivePointer = mActivePointerId;
1165c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                            break;
1166c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        }
1167c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1168c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1169c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    if (newActivePointer == INVALID_POINTER) {
1170c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        // We didn't find another pointer still touching the view, release it.
1171c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                        releaseViewForPointerUp();
1172c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    }
1173c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1174c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                clearMotionHistory(pointerId);
1175c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1176c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1177c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1178c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_UP: {
1179c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING) {
1180c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    releaseViewForPointerUp();
1181c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1182c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                cancel();
1183c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1184c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1185c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1186c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            case MotionEvent.ACTION_CANCEL: {
1187c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                if (mDragState == STATE_DRAGGING) {
1188c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    dispatchViewReleased(0, 0);
1189c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                }
1190c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                cancel();
1191c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                break;
1192c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1193c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1194c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1195c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1196c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void reportNewEdgeDrags(float dx, float dy, int pointerId) {
1197c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int dragsStarted = 0;
1198acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_LEFT)) {
1199c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_LEFT;
1200c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1201acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_TOP)) {
1202c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_TOP;
1203c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1204acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_RIGHT)) {
1205c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_RIGHT;
1206c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1207acc82321ad119706485db342eaa12b225fa9b667Adam Powell        if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_BOTTOM)) {
1208c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            dragsStarted |= EDGE_BOTTOM;
1209c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1210c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1211c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (dragsStarted != 0) {
1212c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mEdgeDragsInProgress[pointerId] |= dragsStarted;
1213c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            mCallback.onEdgeDragStarted(dragsStarted, pointerId);
1214c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1215c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1216c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1217acc82321ad119706485db342eaa12b225fa9b667Adam Powell    private boolean checkNewEdgeDrag(float delta, float odelta, int pointerId, int edge) {
1218acc82321ad119706485db342eaa12b225fa9b667Adam Powell        final float absDelta = Math.abs(delta);
1219acc82321ad119706485db342eaa12b225fa9b667Adam Powell        final float absODelta = Math.abs(odelta);
1220acc82321ad119706485db342eaa12b225fa9b667Adam Powell
1221fd5162a69e607f9199a502574c7486eb4e695e09Adam Powell        if ((mInitialEdgesTouched[pointerId] & edge) != edge  || (mTrackingEdges & edge) == 0 ||
1222fd5162a69e607f9199a502574c7486eb4e695e09Adam Powell                (mEdgeDragsLocked[pointerId] & edge) == edge ||
1223acc82321ad119706485db342eaa12b225fa9b667Adam Powell                (mEdgeDragsInProgress[pointerId] & edge) == edge ||
12241732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                (absDelta <= mTouchSlop && absODelta <= mTouchSlop)) {
1225acc82321ad119706485db342eaa12b225fa9b667Adam Powell            return false;
1226acc82321ad119706485db342eaa12b225fa9b667Adam Powell        }
1227ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        if (absDelta < absODelta * 0.5f && mCallback.onEdgeLock(edge)) {
1228acc82321ad119706485db342eaa12b225fa9b667Adam Powell            mEdgeDragsLocked[pointerId] |= edge;
1229acc82321ad119706485db342eaa12b225fa9b667Adam Powell            return false;
1230acc82321ad119706485db342eaa12b225fa9b667Adam Powell        }
12311732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return (mEdgeDragsInProgress[pointerId] & edge) == 0 && absDelta > mTouchSlop;
12326580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    }
12336580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
1234c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1235c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Check if we've crossed a reasonable touch slop for the given child view.
1236c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * If the child cannot be dragged along the horizontal or vertical axis, motion
1237c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * along that axis will not count toward the slop check.
1238c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1239c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param child Child to check
1240c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dx Motion since initial position along X axis
1241c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param dy Motion since initial position along Y axis
1242c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if the touch slop has been crossed
1243c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
12441732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    private boolean checkTouchSlop(View child, float dx, float dy) {
124589e17886e6149bddfdb08a242c9e88889596419cAdam Powell        if (child == null) {
124689e17886e6149bddfdb08a242c9e88889596419cAdam Powell            return false;
124789e17886e6149bddfdb08a242c9e88889596419cAdam Powell        }
1248c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final boolean checkHorizontal = mCallback.getViewHorizontalDragRange(child) > 0;
1249c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final boolean checkVertical = mCallback.getViewVerticalDragRange(child) > 0;
1250c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1251c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (checkHorizontal && checkVertical) {
1252c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return dx * dx + dy * dy > mTouchSlop * mTouchSlop;
1253c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        } else if (checkHorizontal) {
1254c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return Math.abs(dx) > mTouchSlop;
1255c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        } else if (checkVertical) {
1256c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return Math.abs(dy) > mTouchSlop;
1257c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1258c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return false;
1259c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1260c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
12611732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
12621732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if any pointer tracked in the current gesture has crossed
12631732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the required slop threshold.
12641732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12651732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * <p>This depends on internal state populated by
12661732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or
12671732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)}. You should only rely on
12681732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the results of this method after all currently available touch data
12691732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * has been provided to one of these two methods.</p>
12701732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12711732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL},
12721732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *                   {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL}
12731732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if the slop threshold has been crossed, false otherwise
12741732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
12751732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean checkTouchSlop(int directions) {
12761732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final int count = mInitialMotionX.length;
12771732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        for (int i = 0; i < count; i++) {
12781732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            if (checkTouchSlop(directions, i)) {
12791732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                return true;
12801732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            }
12811732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
12821732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return false;
12831732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
12841732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12851732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
12861732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if the specified pointer tracked in the current gesture has crossed
12871732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the required slop threshold.
12881732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12891732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * <p>This depends on internal state populated by
12901732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or
12911732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * {@link #processTouchEvent(android.view.MotionEvent)}. You should only rely on
12921732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the results of this method after all currently available touch data
12931732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * has been provided to one of these two methods.</p>
12941732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
12951732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL},
12961732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *                   {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL}
12971732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param pointerId ID of the pointer to slop check as specified by MotionEvent
12981732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if the slop threshold has been crossed, false otherwise
12991732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
13001732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean checkTouchSlop(int directions, int pointerId) {
13011732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        if (!isPointerDown(pointerId)) {
13021732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return false;
13031732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
13041732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
13051732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final boolean checkHorizontal = (directions & DIRECTION_HORIZONTAL) == DIRECTION_HORIZONTAL;
13061732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final boolean checkVertical = (directions & DIRECTION_VERTICAL) == DIRECTION_VERTICAL;
13071732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
13081732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final float dx = mLastMotionX[pointerId] - mInitialMotionX[pointerId];
13091732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final float dy = mLastMotionY[pointerId] - mInitialMotionY[pointerId];
13101732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
13111732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        if (checkHorizontal && checkVertical) {
13121732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return dx * dx + dy * dy > mTouchSlop * mTouchSlop;
13131732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        } else if (checkHorizontal) {
13141732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return Math.abs(dx) > mTouchSlop;
13151732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        } else if (checkVertical) {
13161732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            return Math.abs(dy) > mTouchSlop;
13171732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
13181732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return false;
13191732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
13201732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
13211732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
13221732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if any of the edges specified were initially touched in the currently active gesture.
13231732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * If there is no currently active gesture this method will return false.
13241732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
13251732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT},
13261732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and
13271732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_ALL}
13281732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if any of the edges specified were initially touched in the current gesture
13291732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
13301732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean isEdgeTouched(int edges) {
13311732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        final int count = mInitialEdgesTouched.length;
13321732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        for (int i = 0; i < count; i++) {
13331732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            if (isEdgeTouched(edges, i)) {
13341732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                return true;
13351732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            }
13361732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
13371732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return false;
13381732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
13391732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
13401732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
13411732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Check if any of the edges specified were initially touched by the pointer with
13421732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the specified ID. If there is no currently active gesture or if there is no pointer with
13431732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * the given ID currently down this method will return false.
13441732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *
13451732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT},
13461732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and
13471732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     *              {@link #EDGE_ALL}
13481732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * @return true if any of the edges specified were initially touched in the current gesture
13491732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
13501732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    public boolean isEdgeTouched(int edges, int pointerId) {
13511732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        return isPointerDown(pointerId) && (mInitialEdgesTouched[pointerId] & edges) != 0;
13521732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    }
13531732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
1354c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private void releaseViewForPointerUp() {
1355c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
135679f95ce3e660d267831067e514ff455156c4381fAdam Powell        final float xvel = clampMag(
135779f95ce3e660d267831067e514ff455156c4381fAdam Powell                VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
135879f95ce3e660d267831067e514ff455156c4381fAdam Powell                mMinVelocity, mMaxVelocity);
135979f95ce3e660d267831067e514ff455156c4381fAdam Powell        final float yvel = clampMag(
136079f95ce3e660d267831067e514ff455156c4381fAdam Powell                VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
136179f95ce3e660d267831067e514ff455156c4381fAdam Powell                mMinVelocity, mMaxVelocity);
136279f95ce3e660d267831067e514ff455156c4381fAdam Powell        dispatchViewReleased(xvel, yvel);
1363c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1364c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
13651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private void dragTo(int left, int top, int dx, int dy) {
13661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        int clampedX = left;
13671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        int clampedY = top;
13681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int oldLeft = mCapturedView.getLeft();
13691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int oldTop = mCapturedView.getTop();
13701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (dx != 0) {
13711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            clampedX = mCallback.clampViewPositionHorizontal(mCapturedView, left, dx);
13721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCapturedView.offsetLeftAndRight(clampedX - oldLeft);
1373c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
13741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (dy != 0) {
13751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            clampedY = mCallback.clampViewPositionVertical(mCapturedView, top, dy);
13761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCapturedView.offsetTopAndBottom(clampedY - oldTop);
1377c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1378c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
13791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (dx != 0 || dy != 0) {
13801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int clampedDx = clampedX - oldLeft;
13811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int clampedDy = clampedY - oldTop;
13821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mCallback.onViewPositionChanged(mCapturedView, clampedX, clampedY,
13831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    clampedDx, clampedDy);
1384c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1385c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1386c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1387c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1388c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Determine if the currently captured view is under the given point in the
1389c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * parent view's coordinate system. If there is no captured view this method
1390c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * will return false.
1391c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1392c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param x X position to test in the parent's coordinate system
1393c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param y Y position to test in the parent's coordinate system
1394c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return true if the captured view is under the given point, false otherwise
1395c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
1396c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public boolean isCapturedViewUnder(int x, int y) {
13976580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        return isViewUnder(mCapturedView, x, y);
13986580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    }
13996580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell
14006580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    /**
14016580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * Determine if the supplied view is under the given point in the
14026580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * parent view's coordinate system.
14036580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     *
14046580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @param view Child view of the parent to hit test
14056580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @param x X position to test in the parent's coordinate system
14066580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @param y Y position to test in the parent's coordinate system
14076580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     * @return true if the supplied view is under the given point, false otherwise
14086580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell     */
14096580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell    public boolean isViewUnder(View view, int x, int y) {
14106580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        if (view == null) {
1411c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            return false;
1412c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
14136580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell        return x >= view.getLeft() &&
14146580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                x < view.getRight() &&
14156580cf4b7e74a2a017ed95b0dc50155b9995edebAdam Powell                y >= view.getTop() &&
1416471f0f4e84c7d1f96e48fb26713f246d69c3241fAdam Powell                y < view.getBottom();
1417c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1418c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1419c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    /**
1420c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * Find the topmost child under the given point within the parent view's coordinate system.
1421c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * The child order is determined using {@link Callback#getOrderedChildIndex(int)}.
1422c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     *
1423c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param x X position to test in the parent's coordinate system
1424c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @param y Y position to test in the parent's coordinate system
1425c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     * @return The topmost child view under (x, y) or null if none found.
1426c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell     */
1427c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    public View findTopChildUnder(int x, int y) {
1428c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        final int childCount = mParentView.getChildCount();
1429c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        for (int i = childCount - 1; i >= 0; i--) {
1430c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i));
1431c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            if (x >= child.getLeft() && x < child.getRight() &&
1432c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                    y >= child.getTop() && y < child.getBottom()) {
1433c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell                return child;
1434c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell            }
1435c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        }
1436c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return null;
1437c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1438c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1439c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    private int getEdgesTouched(int x, int y) {
1440c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        int result = 0;
1441c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1442c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (x < mParentView.getLeft() + mEdgeSize) result |= EDGE_LEFT;
1443c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (y < mParentView.getTop() + mEdgeSize) result |= EDGE_TOP;
1444c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (x > mParentView.getRight() - mEdgeSize) result |= EDGE_RIGHT;
1445c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        if (y > mParentView.getBottom() - mEdgeSize) result |= EDGE_BOTTOM;
1446c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell
1447c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell        return result;
1448c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell    }
1449c56ba65d20be8742ff717907a3a2cd81dd0e5f3cAdam Powell}
1450