1 /* 2 * Copyright (C) 2016 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.launcher3.dragndrop; 18 19 import android.graphics.PointF; 20 import android.view.MotionEvent; 21 import android.view.VelocityTracker; 22 import android.view.ViewConfiguration; 23 24 import com.android.launcher3.ButtonDropTarget; 25 import com.android.launcher3.DeviceProfile; 26 import com.android.launcher3.DropTarget; 27 import com.android.launcher3.Launcher; 28 import com.android.launcher3.R; 29 import com.android.launcher3.util.FlingAnimation; 30 31 /** 32 * Utility class to manage fling to delete action during drag and drop. 33 */ 34 public class FlingToDeleteHelper { 35 36 private static final float MAX_FLING_DEGREES = 35f; 37 38 private final Launcher mLauncher; 39 40 private ButtonDropTarget mDropTarget; 41 private VelocityTracker mVelocityTracker; 42 FlingToDeleteHelper(Launcher launcher)43 public FlingToDeleteHelper(Launcher launcher) { 44 mLauncher = launcher; 45 } 46 recordMotionEvent(MotionEvent ev)47 public void recordMotionEvent(MotionEvent ev) { 48 if (mVelocityTracker == null) { 49 mVelocityTracker = VelocityTracker.obtain(); 50 } 51 mVelocityTracker.addMovement(ev); 52 } 53 releaseVelocityTracker()54 public void releaseVelocityTracker() { 55 if (mVelocityTracker != null) { 56 mVelocityTracker.recycle(); 57 mVelocityTracker = null; 58 } 59 } 60 getDropTarget()61 public DropTarget getDropTarget() { 62 return mDropTarget; 63 } 64 getFlingAnimation(DropTarget.DragObject dragObject, DragOptions options)65 public Runnable getFlingAnimation(DropTarget.DragObject dragObject, DragOptions options) { 66 if (options == null) { 67 return null; 68 } 69 PointF vel = isFlingingToDelete(); 70 options.isFlingToDelete = vel != null; 71 if (!options.isFlingToDelete) { 72 return null; 73 } 74 return new FlingAnimation(dragObject, vel, mDropTarget, mLauncher, options); 75 } 76 77 /** 78 * Determines whether the user flung the current item to delete it. 79 * 80 * @return the vector at which the item was flung, or null if no fling was detected. 81 */ isFlingingToDelete()82 private PointF isFlingingToDelete() { 83 if (mVelocityTracker == null) return null; 84 if (mDropTarget == null) { 85 mDropTarget = (ButtonDropTarget) mLauncher.findViewById(R.id.delete_target_text); 86 } 87 if (mDropTarget == null || !mDropTarget.isDropEnabled()) return null; 88 ViewConfiguration config = ViewConfiguration.get(mLauncher); 89 mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); 90 PointF vel = new PointF(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); 91 float theta = MAX_FLING_DEGREES + 1; 92 DeviceProfile deviceProfile = mLauncher.getDeviceProfile(); 93 if (mVelocityTracker.getYVelocity() < deviceProfile.flingToDeleteThresholdVelocity) { 94 // Do a quick dot product test to ensure that we are flinging upwards 95 PointF upVec = new PointF(0f, -1f); 96 theta = getAngleBetweenVectors(vel, upVec); 97 } else if (mLauncher.getDeviceProfile().isVerticalBarLayout() && 98 mVelocityTracker.getXVelocity() < deviceProfile.flingToDeleteThresholdVelocity) { 99 // Remove icon is on left side instead of top, so check if we are flinging to the left. 100 PointF leftVec = new PointF(-1f, 0f); 101 theta = getAngleBetweenVectors(vel, leftVec); 102 } 103 if (theta <= Math.toRadians(MAX_FLING_DEGREES)) { 104 return vel; 105 } 106 return null; 107 } 108 getAngleBetweenVectors(PointF vec1, PointF vec2)109 private float getAngleBetweenVectors(PointF vec1, PointF vec2) { 110 return (float) Math.acos(((vec1.x * vec2.x) + (vec1.y * vec2.y)) / 111 (vec1.length() * vec2.length())); 112 } 113 } 114