RecentsViewTouchHandler.java revision 9be3a060827154617eed9132c64431af56d98eb4
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(mRv.getContext()) && !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 device = InputDevice.getDevice(mDeviceId);
164            if (device != null) {
165                device.setPointerType(PointerIcon.TYPE_GRABBING);
166            }
167        }
168    }
169
170    public final void onBusEvent(DragEndEvent event) {
171        if (!mDragTask.isDockable) {
172            EventBus.getDefault().send(new HideIncompatibleAppOverlayEvent());
173        }
174        mDragRequested = false;
175        mDragTask = null;
176        mTaskView = null;
177        mLastDropTarget = null;
178    }
179
180    public final void onBusEvent(ConfigurationChangedEvent event) {
181        if (event.fromDisplayDensityChange || event.fromDeviceOrientationChange) {
182            updateSnapAlgorithm();
183        }
184    }
185
186    /**
187     * Handles dragging touch events
188     */
189    private void handleTouchEvent(MotionEvent ev) {
190        int action = ev.getActionMasked();
191        switch (action) {
192            case MotionEvent.ACTION_DOWN:
193                mDownPos.set((int) ev.getX(), (int) ev.getY());
194                mDeviceId = ev.getDeviceId();
195                break;
196            case MotionEvent.ACTION_MOVE: {
197                float evX = ev.getX();
198                float evY = ev.getY();
199                float x = evX - mTaskViewOffset.x;
200                float y = evY - mTaskViewOffset.y;
201
202                if (mDragRequested) {
203                    if (!mIsDragging) {
204                        mIsDragging = Math.hypot(evX - mDownPos.x, evY - mDownPos.y) > mDragSlop;
205                    }
206                    if (mIsDragging) {
207                        int width = mRv.getMeasuredWidth();
208                        int height = mRv.getMeasuredHeight();
209
210                        DropTarget currentDropTarget = null;
211
212                        // Give priority to the current drop target to retain the touch handling
213                        if (mLastDropTarget != null) {
214                            if (mLastDropTarget.acceptsDrop((int) evX, (int) evY, width, height,
215                                    mRv.mSystemInsets, true /* isCurrentTarget */)) {
216                                currentDropTarget = mLastDropTarget;
217                            }
218                        }
219
220                        // Otherwise, find the next target to handle this event
221                        if (currentDropTarget == null) {
222                            for (DropTarget target : mDropTargets) {
223                                if (target.acceptsDrop((int) evX, (int) evY, width, height,
224                                        mRv.mSystemInsets, false /* isCurrentTarget */)) {
225                                    currentDropTarget = target;
226                                    break;
227                                }
228                            }
229                        }
230                        if (mLastDropTarget != currentDropTarget) {
231                            mLastDropTarget = currentDropTarget;
232                            EventBus.getDefault().send(new DragDropTargetChangedEvent(mDragTask,
233                                    currentDropTarget));
234                        }
235
236                    }
237
238                    mTaskView.setTranslationX(x);
239                    mTaskView.setTranslationY(y);
240                }
241                break;
242            }
243            case MotionEvent.ACTION_UP:
244            case MotionEvent.ACTION_CANCEL: {
245                if (mDragRequested) {
246                    boolean cancelled = action == MotionEvent.ACTION_CANCEL;
247                    if (cancelled) {
248                        EventBus.getDefault().send(new DragDropTargetChangedEvent(mDragTask, null));
249                    }
250                    EventBus.getDefault().send(new DragEndEvent(mDragTask, mTaskView,
251                            !cancelled ? mLastDropTarget : null));
252                    break;
253                }
254                mDeviceId = -1;
255            }
256        }
257    }
258}
259