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 17 package com.android.launcher2; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.MotionEvent; 22 import 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 */ 34 public 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 private Launcher mLauncher; 41 42 public PagedViewWithDraggableItems(Context context) { 43 this(context, null); 44 } 45 46 public PagedViewWithDraggableItems(Context context, AttributeSet attrs) { 47 this(context, attrs, 0); 48 } 49 50 public PagedViewWithDraggableItems(Context context, AttributeSet attrs, int defStyle) { 51 super(context, attrs, defStyle); 52 mLauncher = (Launcher) context; 53 } 54 55 protected boolean beginDragging(View v) { 56 boolean wasDragging = mIsDragging; 57 mIsDragging = true; 58 return !wasDragging; 59 } 60 61 protected void cancelDragging() { 62 mIsDragging = false; 63 mLastTouchedItem = null; 64 mIsDragEnabled = false; 65 } 66 67 private void handleTouchEvent(MotionEvent ev) { 68 final int action = ev.getAction(); 69 switch (action & MotionEvent.ACTION_MASK) { 70 case MotionEvent.ACTION_DOWN: 71 cancelDragging(); 72 mIsDragEnabled = true; 73 break; 74 case MotionEvent.ACTION_MOVE: 75 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging && mIsDragEnabled) { 76 determineDraggingStart(ev); 77 } 78 break; 79 } 80 } 81 82 @Override 83 public boolean onInterceptTouchEvent(MotionEvent ev) { 84 handleTouchEvent(ev); 85 return super.onInterceptTouchEvent(ev); 86 } 87 88 @Override 89 public boolean onTouchEvent(MotionEvent ev) { 90 handleTouchEvent(ev); 91 return super.onTouchEvent(ev); 92 } 93 94 @Override 95 public boolean onTouch(View v, MotionEvent event) { 96 mLastTouchedItem = v; 97 mIsDragEnabled = true; 98 return false; 99 } 100 101 @Override 102 public boolean onLongClick(View v) { 103 // Return early if this is not initiated from a touch 104 if (!v.isInTouchMode()) return false; 105 // Return early if we are still animating the pages 106 if (mNextPage != INVALID_PAGE) return false; 107 // When we have exited all apps or are in transition, disregard long clicks 108 if (!mLauncher.isAllAppsCustomizeOpen() || 109 mLauncher.getWorkspace().isSwitchingState()) return false; 110 111 return beginDragging(v); 112 } 113 114 /* 115 * Determines if we should change the touch state to start scrolling after the 116 * user moves their touch point too far. 117 */ 118 protected void determineScrollingStart(MotionEvent ev) { 119 if (!mIsDragging) super.determineScrollingStart(ev); 120 } 121 122 /* 123 * Determines if we should change the touch state to start dragging after the 124 * user moves their touch point far enough. 125 */ 126 protected void determineDraggingStart(MotionEvent ev) { 127 /* 128 * Locally do absolute value. mLastMotionX is set to the y value 129 * of the down event. 130 */ 131 final int pointerIndex = ev.findPointerIndex(mActivePointerId); 132 final float x = ev.getX(pointerIndex); 133 final float y = ev.getY(pointerIndex); 134 final int xDiff = (int) Math.abs(x - mLastMotionX); 135 final int yDiff = (int) Math.abs(y - mLastMotionY); 136 137 final int touchSlop = mTouchSlop; 138 boolean yMoved = yDiff > touchSlop; 139 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold; 140 141 if (isUpwardMotion && yMoved && mLastTouchedItem != null) { 142 // Drag if the user moved far enough along the Y axis 143 beginDragging(mLastTouchedItem); 144 145 // Cancel any pending long press 146 if (mAllowLongPress) { 147 mAllowLongPress = false; 148 // Try canceling the long press. It could also have been scheduled 149 // by a distant descendant, so use the mAllowLongPress flag to block 150 // everything 151 final View currentPage = getPageAt(mCurrentPage); 152 if (currentPage != null) { 153 currentPage.cancelLongPress(); 154 } 155 } 156 } 157 } 158 159 public void setDragSlopeThreshold(float dragSlopeThreshold) { 160 mDragSlopeThreshold = dragSlopeThreshold; 161 } 162 163 @Override 164 protected void onDetachedFromWindow() { 165 cancelDragging(); 166 super.onDetachedFromWindow(); 167 } 168 } 169