RecentsViewTouchHandler.java revision b7035f3a4738efdf40dc0ad06f19303ffcd784f7
1/*
2 * Copyright (C) 2014 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.systemui.recents.views;
18
19import android.app.ActivityManager;
20import android.content.res.Configuration;
21import android.graphics.Point;
22import android.graphics.Rect;
23import android.view.InputDevice;
24import android.view.MotionEvent;
25import android.view.PointerIcon;
26import android.view.ViewConfiguration;
27import android.view.ViewDebug;
28
29import com.android.internal.policy.DividerSnapAlgorithm;
30import com.android.systemui.recents.Recents;
31import com.android.systemui.recents.events.EventBus;
32import com.android.systemui.recents.events.activity.ConfigurationChangedEvent;
33import com.android.systemui.recents.events.activity.HideRecentsEvent;
34import com.android.systemui.recents.events.ui.HideIncompatibleAppOverlayEvent;
35import com.android.systemui.recents.events.ui.ShowIncompatibleAppOverlayEvent;
36import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
37import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
38import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
39import com.android.systemui.recents.events.ui.dragndrop.DragStartInitializeDropTargetsEvent;
40import com.android.systemui.recents.misc.SystemServicesProxy;
41import com.android.systemui.recents.model.Task;
42import com.android.systemui.recents.model.TaskStack;
43
44import java.util.ArrayList;
45
46/**
47 * Handles touch events for a RecentsView.
48 */
49public class RecentsViewTouchHandler {
50
51    private RecentsView mRv;
52
53    @ViewDebug.ExportedProperty(deepExport=true, prefix="drag_task")
54    private Task mDragTask;
55    @ViewDebug.ExportedProperty(deepExport=true, prefix="drag_task_view_")
56    private TaskView mTaskView;
57
58    @ViewDebug.ExportedProperty(category="recents")
59    private Point mTaskViewOffset = new Point();
60    @ViewDebug.ExportedProperty(category="recents")
61    private Point mDownPos = new Point();
62    @ViewDebug.ExportedProperty(category="recents")
63    private boolean mDragRequested;
64    @ViewDebug.ExportedProperty(category="recents")
65    private boolean mIsDragging;
66    private float mDragSlop;
67    private int mDeviceId = -1;
68
69    private DropTarget mLastDropTarget;
70    private DividerSnapAlgorithm mDividerSnapAlgorithm;
71    private ArrayList<DropTarget> mDropTargets = new ArrayList<>();
72    private ArrayList<TaskStack.DockState> mVisibleDockStates = new ArrayList<>();
73
74    public RecentsViewTouchHandler(RecentsView rv) {
75        mRv = rv;
76        mDragSlop = ViewConfiguration.get(rv.getContext()).getScaledTouchSlop();
77        updateSnapAlgorithm();
78    }
79
80    private void updateSnapAlgorithm() {
81        Rect insets = new Rect();
82        SystemServicesProxy.getInstance(mRv.getContext()).getStableInsets(insets);
83        mDividerSnapAlgorithm = DividerSnapAlgorithm.create(mRv.getContext(), insets);
84    }
85
86    /**
87     * Registers a new drop target for the current drag only.
88     */
89    public void registerDropTargetForCurrentDrag(DropTarget target) {
90        mDropTargets.add(target);
91    }
92
93    /**
94     * Returns the set of visible dock states for this current drag.
95     */
96    public ArrayList<TaskStack.DockState> getVisibleDockStates() {
97        return mVisibleDockStates;
98    }
99
100    /** Touch preprocessing for handling below */
101    public boolean onInterceptTouchEvent(MotionEvent ev) {
102        handleTouchEvent(ev);
103        return mDragRequested;
104    }
105
106    /** Handles touch events once we have intercepted them */
107    public boolean onTouchEvent(MotionEvent ev) {
108        handleTouchEvent(ev);
109        if (ev.getAction() == MotionEvent.ACTION_UP && mRv.getStack().getStackTaskCount() == 0) {
110            EventBus.getDefault().send(new HideRecentsEvent(false, true));
111        }
112        return true;
113    }
114
115    /**** Events ****/
116
117    public final void onBusEvent(DragStartEvent event) {
118        SystemServicesProxy ssp = Recents.getSystemServices();
119        mRv.getParent().requestDisallowInterceptTouchEvent(true);
120        mDragRequested = true;
121        // We defer starting the actual drag handling until the user moves past the drag slop
122        mIsDragging = false;
123        mDragTask = event.task;
124        mTaskView = event.taskView;
125        mDropTargets.clear();
126
127        int[] recentsViewLocation = new int[2];
128        mRv.getLocationInWindow(recentsViewLocation);
129        mTaskViewOffset.set(mTaskView.getLeft() - recentsViewLocation[0] + event.tlOffset.x,
130                mTaskView.getTop() - recentsViewLocation[1] + event.tlOffset.y);
131
132        // Change space coordinates relative to the view to RecentsView when user initiates a touch
133        if (event.isUserTouchInitiated) {
134            float x = mDownPos.x - mTaskViewOffset.x;
135            float y = mDownPos.y - mTaskViewOffset.y;
136            mTaskView.setTranslationX(x);
137            mTaskView.setTranslationY(y);
138        }
139
140        mVisibleDockStates.clear();
141        if (ActivityManager.supportsMultiWindow() && !ssp.hasDockedTask()
142                && mDividerSnapAlgorithm.isSplitScreenFeasible()) {
143            Recents.logDockAttempt(mRv.getContext(), event.task.getTopComponent(),
144                    event.task.resizeMode);
145            if (!event.task.isDockable) {
146                EventBus.getDefault().send(new ShowIncompatibleAppOverlayEvent());
147            } else {
148                // Add the dock state drop targets (these take priority)
149                TaskStack.DockState[] dockStates = Recents.getConfiguration()
150                        .getDockStatesForCurrentOrientation();
151                for (TaskStack.DockState dockState : dockStates) {
152                    registerDropTargetForCurrentDrag(dockState);
153                    dockState.update(mRv.getContext());
154                    mVisibleDockStates.add(dockState);
155                }
156            }
157        }
158
159        // Request other drop targets to register themselves
160        EventBus.getDefault().send(new DragStartInitializeDropTargetsEvent(event.task,
161                event.taskView, this));
162        if (mDeviceId != -1) {
163            InputDevice.getDevice(mDeviceId).setPointerType(PointerIcon.TYPE_GRABBING);
164        }
165    }
166
167    public final void onBusEvent(DragEndEvent event) {
168        if (!mDragTask.isDockable) {
169            EventBus.getDefault().send(new HideIncompatibleAppOverlayEvent());
170        }
171        mDragRequested = false;
172        mDragTask = null;
173        mTaskView = null;
174        mLastDropTarget = null;
175    }
176
177    public final void onBusEvent(ConfigurationChangedEvent event) {
178        if (event.fromDisplayDensityChange || event.fromDeviceOrientationChange) {
179            updateSnapAlgorithm();
180        }
181    }
182
183    /**
184     * Handles dragging touch events
185     */
186    private void handleTouchEvent(MotionEvent ev) {
187        int action = ev.getActionMasked();
188        switch (action) {
189            case MotionEvent.ACTION_DOWN:
190                mDownPos.set((int) ev.getX(), (int) ev.getY());
191                mDeviceId = ev.getDeviceId();
192                break;
193            case MotionEvent.ACTION_MOVE: {
194                float evX = ev.getX();
195                float evY = ev.getY();
196                float x = evX - mTaskViewOffset.x;
197                float y = evY - mTaskViewOffset.y;
198
199                if (mDragRequested) {
200                    if (!mIsDragging) {
201                        mIsDragging = Math.hypot(evX - mDownPos.x, evY - mDownPos.y) > mDragSlop;
202                    }
203                    if (mIsDragging) {
204                        int width = mRv.getMeasuredWidth();
205                        int height = mRv.getMeasuredHeight();
206
207                        DropTarget currentDropTarget = null;
208
209                        // Give priority to the current drop target to retain the touch handling
210                        if (mLastDropTarget != null) {
211                            if (mLastDropTarget.acceptsDrop((int) evX, (int) evY, width, height,
212                                    mRv.mSystemInsets, true /* isCurrentTarget */)) {
213                                currentDropTarget = mLastDropTarget;
214                            }
215                        }
216
217                        // Otherwise, find the next target to handle this event
218                        if (currentDropTarget == null) {
219                            for (DropTarget target : mDropTargets) {
220                                if (target.acceptsDrop((int) evX, (int) evY, width, height,
221                                        mRv.mSystemInsets, false /* isCurrentTarget */)) {
222                                    currentDropTarget = target;
223                                    break;
224                                }
225                            }
226                        }
227                        if (mLastDropTarget != currentDropTarget) {
228                            mLastDropTarget = currentDropTarget;
229                            EventBus.getDefault().send(new DragDropTargetChangedEvent(mDragTask,
230                                    currentDropTarget));
231                        }
232
233                    }
234
235                    mTaskView.setTranslationX(x);
236                    mTaskView.setTranslationY(y);
237                }
238                break;
239            }
240            case MotionEvent.ACTION_UP:
241            case MotionEvent.ACTION_CANCEL: {
242                if (mDragRequested) {
243                    boolean cancelled = action == MotionEvent.ACTION_CANCEL;
244                    if (cancelled) {
245                        EventBus.getDefault().send(new DragDropTargetChangedEvent(mDragTask, null));
246                    }
247                    EventBus.getDefault().send(new DragEndEvent(mDragTask, mTaskView,
248                            !cancelled ? mLastDropTarget : null));
249                    break;
250                }
251                mDeviceId = -1;
252            }
253        }
254    }
255}
256