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