RecentsViewTouchHandler.java revision 29379ec859bf7118dec9f3248c63ee369218ad6b
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.topActivity, event.task.resizeMode);
166            if (!event.task.isDockable) {
167                EventBus.getDefault().send(new ShowIncompatibleAppOverlayEvent());
168            } else {
169                // Add the dock state drop targets (these take priority)
170                TaskStack.DockState[] dockStates = getDockStatesForCurrentOrientation();
171                for (TaskStack.DockState dockState : dockStates) {
172                    registerDropTargetForCurrentDrag(dockState);
173                    mVisibleDockStates.add(dockState);
174                }
175            }
176        }
177
178        // Request other drop targets to register themselves
179        EventBus.getDefault().send(new DragStartInitializeDropTargetsEvent(event.task,
180                event.taskView, this));
181    }
182
183    public final void onBusEvent(DragEndEvent event) {
184        if (!mDragTask.isDockable) {
185            EventBus.getDefault().send(new HideIncompatibleAppOverlayEvent());
186        }
187        mDragRequested = false;
188        mDragTask = null;
189        mTaskView = null;
190        mLastDropTarget = null;
191    }
192
193    public final void onBusEvent(ConfigurationChangedEvent event) {
194        updateSnapAlgorithm();
195    }
196
197    /**
198     * Handles dragging touch events
199     */
200    private void handleTouchEvent(MotionEvent ev) {
201        int action = ev.getAction();
202        switch (action & MotionEvent.ACTION_MASK) {
203            case MotionEvent.ACTION_DOWN:
204                mDownPos.set((int) ev.getX(), (int) ev.getY());
205                break;
206            case MotionEvent.ACTION_MOVE: {
207                float evX = ev.getX();
208                float evY = ev.getY();
209                float x = evX - mTaskViewOffset.x;
210                float y = evY - mTaskViewOffset.y;
211
212                if (mDragRequested) {
213                    if (!mIsDragging) {
214                        mIsDragging = Math.hypot(evX - mDownPos.x, evY - mDownPos.y) > mDragSlop;
215                    }
216                    if (mIsDragging) {
217                        int width = mRv.getMeasuredWidth();
218                        int height = mRv.getMeasuredHeight();
219
220                        DropTarget currentDropTarget = null;
221
222                        // Give priority to the current drop target to retain the touch handling
223                        if (mLastDropTarget != null) {
224                            if (mLastDropTarget.acceptsDrop((int) evX, (int) evY, width, height,
225                                    true /* isCurrentTarget */)) {
226                                currentDropTarget = mLastDropTarget;
227                            }
228                        }
229
230                        // Otherwise, find the next target to handle this event
231                        if (currentDropTarget == null) {
232                            for (DropTarget target : mDropTargets) {
233                                if (target.acceptsDrop((int) evX, (int) evY, width, height,
234                                        false /* isCurrentTarget */)) {
235                                    currentDropTarget = target;
236                                    break;
237                                }
238                            }
239                        }
240                        if (mLastDropTarget != currentDropTarget) {
241                            mLastDropTarget = currentDropTarget;
242                            EventBus.getDefault().send(new DragDropTargetChangedEvent(mDragTask,
243                                    currentDropTarget));
244                        }
245
246                    }
247
248                    mTaskView.setTranslationX(x);
249                    mTaskView.setTranslationY(y);
250                }
251                break;
252            }
253            case MotionEvent.ACTION_UP:
254            case MotionEvent.ACTION_CANCEL: {
255                if (mDragRequested) {
256                    EventBus.getDefault().send(new DragEndEvent(mDragTask, mTaskView,
257                            mLastDropTarget));
258                    break;
259                }
260            }
261        }
262    }
263}
264