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
17package com.android.launcher3.dragndrop;
18
19import android.graphics.PointF;
20import android.os.SystemClock;
21import android.view.DragEvent;
22import android.view.MotionEvent;
23import android.view.VelocityTracker;
24import android.view.ViewConfiguration;
25
26import com.android.launcher3.ButtonDropTarget;
27import com.android.launcher3.DropTarget;
28import com.android.launcher3.Launcher;
29import com.android.launcher3.R;
30import com.android.launcher3.util.FlingAnimation;
31
32/**
33 * Utility class to manage fling to delete action during drag and drop.
34 */
35public class FlingToDeleteHelper {
36
37    private static final float MAX_FLING_DEGREES = 35f;
38
39    private final Launcher mLauncher;
40    private final int mFlingToDeleteThresholdVelocity;
41
42    private ButtonDropTarget mDropTarget;
43    private VelocityTracker mVelocityTracker;
44
45    public FlingToDeleteHelper(Launcher launcher) {
46        mLauncher = launcher;
47        mFlingToDeleteThresholdVelocity = launcher.getResources()
48                .getDimensionPixelSize(R.dimen.drag_flingToDeleteMinVelocity);
49    }
50
51    public void recordMotionEvent(MotionEvent ev) {
52        if (mVelocityTracker == null) {
53            mVelocityTracker = VelocityTracker.obtain();
54        }
55        mVelocityTracker.addMovement(ev);
56    }
57
58    /**
59     * Same as {@link #recordMotionEvent}. It creates a temporary {@link MotionEvent} object
60     * using {@param event} for tracking velocity.
61     */
62    public void recordDragEvent(long dragStartTime, DragEvent event) {
63        final int motionAction;
64        switch (event.getAction()) {
65            case DragEvent.ACTION_DRAG_STARTED:
66                motionAction = MotionEvent.ACTION_DOWN;
67                break;
68            case DragEvent.ACTION_DRAG_LOCATION:
69                motionAction = MotionEvent.ACTION_MOVE;
70                break;
71            case DragEvent.ACTION_DRAG_ENDED:
72                motionAction = MotionEvent.ACTION_UP;
73                break;
74            default:
75                return;
76        }
77        MotionEvent emulatedEvent = MotionEvent.obtain(dragStartTime, SystemClock.uptimeMillis(),
78                motionAction, event.getX(), event.getY(), 0);
79        recordMotionEvent(emulatedEvent);
80        emulatedEvent.recycle();
81    }
82
83    public void releaseVelocityTracker() {
84        if (mVelocityTracker != null) {
85            mVelocityTracker.recycle();
86            mVelocityTracker = null;
87        }
88    }
89
90    public DropTarget getDropTarget() {
91        return mDropTarget;
92    }
93
94    public Runnable getFlingAnimation(DropTarget.DragObject dragObject) {
95        PointF vel = isFlingingToDelete();
96        if (vel == null) {
97            return null;
98        }
99        return new FlingAnimation(dragObject, vel, mDropTarget, mLauncher);
100    }
101
102    /**
103     * Determines whether the user flung the current item to delete it.
104     *
105     * @return the vector at which the item was flung, or null if no fling was detected.
106     */
107    private PointF isFlingingToDelete() {
108        if (mDropTarget == null) {
109            mDropTarget = (ButtonDropTarget) mLauncher.findViewById(R.id.delete_target_text);
110        }
111        if (mDropTarget == null || !mDropTarget.isDropEnabled()) return null;
112        ViewConfiguration config = ViewConfiguration.get(mLauncher);
113        mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());
114        PointF vel = new PointF(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
115        float theta = MAX_FLING_DEGREES + 1;
116        if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
117            // Do a quick dot product test to ensure that we are flinging upwards
118            PointF upVec = new PointF(0f, -1f);
119            theta = getAngleBetweenVectors(vel, upVec);
120        } else if (mLauncher.getDeviceProfile().isVerticalBarLayout() &&
121                mVelocityTracker.getXVelocity() < mFlingToDeleteThresholdVelocity) {
122            // Remove icon is on left side instead of top, so check if we are flinging to the left.
123            PointF leftVec = new PointF(-1f, 0f);
124            theta = getAngleBetweenVectors(vel, leftVec);
125        }
126        if (theta <= Math.toRadians(MAX_FLING_DEGREES)) {
127            return vel;
128        }
129        return null;
130    }
131
132    private float getAngleBetweenVectors(PointF vec1, PointF vec2) {
133        return (float) Math.acos(((vec1.x * vec2.x) + (vec1.y * vec2.y)) /
134                (vec1.length() * vec2.length()));
135    }
136}
137