PagedViewWithDraggableItems.java revision 94569f47c2fe3c2e04e993379d36e0597925e19f
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.MotionEvent;
22import android.view.View;
23
24
25/* Class that does most of the work of enabling dragging items out of a PagedView by performing a
26 * vertical drag. Used by both CustomizePagedView and AllAppsPagedView.
27 * Subclasses must do the following:
28 *   * call setDragSlopeThreshold after making an instance of the PagedViewWithDraggableItems
29 *   * call child.setOnLongClickListener(this) and child.setOnTouchListener(this) on all children
30 *       (good place to do it is in syncPageItems)
31 *   * override beginDragging(View) (but be careful to call super.beginDragging(View)
32 *
33 */
34public abstract class PagedViewWithDraggableItems extends PagedView
35    implements View.OnLongClickListener, View.OnTouchListener {
36    private View mLastTouchedItem;
37    private boolean mIsDragging;
38    private boolean mIsDragEnabled;
39    private float mDragSlopeThreshold;
40
41    public PagedViewWithDraggableItems(Context context) {
42        super(context, null);
43    }
44
45    public PagedViewWithDraggableItems(Context context, AttributeSet attrs) {
46        super(context, attrs, 0);
47    }
48
49    public PagedViewWithDraggableItems(Context context, AttributeSet attrs, int defStyle) {
50        super(context, attrs, defStyle);
51    }
52
53    protected boolean beginDragging(View v) {
54        boolean wasDragging = mIsDragging;
55        mIsDragging = true;
56        return !wasDragging;
57    }
58
59    protected void cancelDragging() {
60        mIsDragging = false;
61        mLastTouchedItem = null;
62        mIsDragEnabled = false;
63    }
64
65    private void handleTouchEvent(MotionEvent ev) {
66        final int action = ev.getAction();
67        switch (action & MotionEvent.ACTION_MASK) {
68            case MotionEvent.ACTION_DOWN:
69                cancelDragging();
70                mIsDragEnabled = true;
71                break;
72            case MotionEvent.ACTION_MOVE:
73                if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging && mIsDragEnabled) {
74                    determineDraggingStart(ev);
75                }
76                break;
77        }
78    }
79
80    @Override
81    public boolean onInterceptTouchEvent(MotionEvent ev) {
82        handleTouchEvent(ev);
83        return super.onInterceptTouchEvent(ev);
84    }
85
86    @Override
87    public boolean onTouchEvent(MotionEvent ev) {
88        handleTouchEvent(ev);
89        return super.onTouchEvent(ev);
90    }
91
92    @Override
93    public boolean onTouch(View v, MotionEvent event) {
94        mLastTouchedItem = v;
95        mIsDragEnabled = true;
96        return false;
97    }
98
99    @Override
100    public boolean onLongClick(View v) {
101        // Return early if this is not initiated from a touch
102        if (!v.isInTouchMode()) return false;
103        // Return early if we are still animating the pages
104        if (mNextPage != INVALID_PAGE) return false;
105        return beginDragging(v);
106    }
107
108
109    /*
110     * Determines if we should change the touch state to start scrolling after the
111     * user moves their touch point too far.
112     */
113    protected void determineScrollingStart(MotionEvent ev) {
114        if (!mIsDragging) super.determineScrollingStart(ev);
115    }
116
117    /*
118     * Determines if we should change the touch state to start dragging after the
119     * user moves their touch point far enough.
120     */
121    protected void determineDraggingStart(MotionEvent ev) {
122        /*
123         * Locally do absolute value. mLastMotionX is set to the y value
124         * of the down event.
125         */
126        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
127        final float x = ev.getX(pointerIndex);
128        final float y = ev.getY(pointerIndex);
129        final int xDiff = (int) Math.abs(x - mLastMotionX);
130        final int yDiff = (int) Math.abs(y - mLastMotionY);
131
132        final int touchSlop = mTouchSlop;
133        boolean yMoved = yDiff > touchSlop;
134        boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
135
136        if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
137            // Drag if the user moved far enough along the Y axis
138            beginDragging(mLastTouchedItem);
139
140            // Cancel any pending long press
141            if (mAllowLongPress) {
142                mAllowLongPress = false;
143                // Try canceling the long press. It could also have been scheduled
144                // by a distant descendant, so use the mAllowLongPress flag to block
145                // everything
146                final View currentPage = getPageAt(mCurrentPage);
147                if (currentPage != null) {
148                    currentPage.cancelLongPress();
149                }
150            }
151        }
152    }
153
154    public void setDragSlopeThreshold(float dragSlopeThreshold) {
155        mDragSlopeThreshold = dragSlopeThreshold;
156    }
157
158    @Override
159    protected void onDetachedFromWindow() {
160        cancelDragging();
161        super.onDetachedFromWindow();
162    }
163}
164