RecentsViewTouchHandler.java revision 9a74290a95e27fc6478758f32a681ccdf495f1e4
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.content.res.Configuration;
20import android.graphics.Point;
21import android.view.MotionEvent;
22import com.android.systemui.recents.Recents;
23import com.android.systemui.recents.RecentsConfiguration;
24import com.android.systemui.recents.events.EventBus;
25import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
26import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
27import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
28import com.android.systemui.recents.events.ui.dragndrop.DragStartInitializeDropTargetsEvent;
29import com.android.systemui.recents.misc.ReferenceCountedTrigger;
30import com.android.systemui.recents.misc.SystemServicesProxy;
31import com.android.systemui.recents.model.Task;
32import com.android.systemui.recents.model.TaskStack;
33
34import java.util.ArrayList;
35
36
37/**
38 * Represents the dock regions for each orientation.
39 */
40class DockRegion {
41    public static TaskStack.DockState[] PHONE_LANDSCAPE = {
42            // We only allow docking to the left for now on small devices
43            TaskStack.DockState.LEFT
44    };
45    public static TaskStack.DockState[] PHONE_PORTRAIT = {
46            // We only allow docking to the top for now on small devices
47            TaskStack.DockState.TOP
48    };
49    public static TaskStack.DockState[] TABLET_LANDSCAPE = {
50            TaskStack.DockState.LEFT,
51            TaskStack.DockState.RIGHT
52    };
53    public static TaskStack.DockState[] TABLET_PORTRAIT = PHONE_PORTRAIT;
54}
55
56/**
57 * Handles touch events for a RecentsView.
58 */
59public class RecentsViewTouchHandler {
60
61    private static final String TAG = "RecentsViewTouchHandler";
62    private static final boolean DEBUG = false;
63
64    private RecentsView mRv;
65
66    private Task mDragTask;
67    private TaskView mTaskView;
68
69    private Point mTaskViewOffset = new Point();
70    private Point mDownPos = new Point();
71    private boolean mDragging;
72
73    private DropTarget mLastDropTarget;
74    private ArrayList<DropTarget> mDropTargets = new ArrayList<>();
75
76    public RecentsViewTouchHandler(RecentsView rv) {
77        mRv = rv;
78    }
79
80    /**
81     * Registers a new drop target for the current drag only.
82     */
83    public void registerDropTargetForCurrentDrag(DropTarget target) {
84        mDropTargets.add(target);
85    }
86
87    /**
88     * Returns the preferred dock states for the current orientation.
89     */
90    public TaskStack.DockState[] getDockStatesForCurrentOrientation() {
91        boolean isLandscape = mRv.getResources().getConfiguration().orientation ==
92                Configuration.ORIENTATION_LANDSCAPE;
93        RecentsConfiguration config = Recents.getConfiguration();
94        TaskStack.DockState[] dockStates = isLandscape ?
95                (config.isLargeScreen ? DockRegion.TABLET_LANDSCAPE : DockRegion.PHONE_LANDSCAPE) :
96                (config.isLargeScreen ? DockRegion.TABLET_PORTRAIT : DockRegion.PHONE_PORTRAIT);
97        return dockStates;
98    }
99
100    /** Touch preprocessing for handling below */
101    public boolean onInterceptTouchEvent(MotionEvent ev) {
102        handleTouchEvent(ev);
103        return mDragging;
104    }
105
106    /** Handles touch events once we have intercepted them */
107    public boolean onTouchEvent(MotionEvent ev) {
108        handleTouchEvent(ev);
109        return mDragging;
110    }
111
112    /**** Events ****/
113
114    public final void onBusEvent(DragStartEvent event) {
115        SystemServicesProxy ssp = Recents.getSystemServices();
116        mRv.getParent().requestDisallowInterceptTouchEvent(true);
117        mDragging = true;
118        mDragTask = event.task;
119        mTaskView = event.taskView;
120        mDropTargets.clear();
121
122        int[] recentsViewLocation = new int[2];
123        mRv.getLocationInWindow(recentsViewLocation);
124        mTaskViewOffset.set(mTaskView.getLeft() - recentsViewLocation[0] + event.tlOffset.x,
125                mTaskView.getTop() - recentsViewLocation[1] + event.tlOffset.y);
126        float x = mDownPos.x - mTaskViewOffset.x;
127        float y = mDownPos.y - mTaskViewOffset.y;
128        mTaskView.setTranslationX(x);
129        mTaskView.setTranslationY(y);
130
131        RecentsConfiguration config = Recents.getConfiguration();
132        if (!ssp.hasDockedTask()) {
133            // Add the dock state drop targets (these take priority)
134            TaskStack.DockState[] dockStates = getDockStatesForCurrentOrientation();
135            for (TaskStack.DockState dockState : dockStates) {
136                registerDropTargetForCurrentDrag(dockState);
137            }
138        }
139
140        // Request other drop targets to register themselves
141        EventBus.getDefault().send(new DragStartInitializeDropTargetsEvent(event.task, this));
142    }
143
144    public final void onBusEvent(DragEndEvent event) {
145        mDragging = false;
146        mDragTask = null;
147        mTaskView = null;
148        mLastDropTarget = null;
149    }
150
151    /**
152     * Handles dragging touch events
153     * @param ev
154     */
155    private void handleTouchEvent(MotionEvent ev) {
156        int action = ev.getAction();
157        switch (action & MotionEvent.ACTION_MASK) {
158            case MotionEvent.ACTION_DOWN:
159                mDownPos.set((int) ev.getX(), (int) ev.getY());
160                break;
161            case MotionEvent.ACTION_MOVE: {
162                if (mDragging) {
163                    int width = mRv.getMeasuredWidth();
164                    int height = mRv.getMeasuredHeight();
165                    float evX = ev.getX();
166                    float evY = ev.getY();
167                    float x = evX - mTaskViewOffset.x;
168                    float y = evY - mTaskViewOffset.y;
169
170                    DropTarget currentDropTarget = null;
171                    for (DropTarget target : mDropTargets) {
172                        if (target.acceptsDrop((int) evX, (int) evY, width, height)) {
173                            currentDropTarget = target;
174                            break;
175                        }
176                    }
177                    if (mLastDropTarget != currentDropTarget) {
178                        mLastDropTarget = currentDropTarget;
179                        EventBus.getDefault().send(new DragDropTargetChangedEvent(mDragTask,
180                                currentDropTarget));
181                    }
182
183                    mTaskView.setTranslationX(x);
184                    mTaskView.setTranslationY(y);
185                }
186                break;
187            }
188            case MotionEvent.ACTION_UP:
189            case MotionEvent.ACTION_CANCEL: {
190                if (mDragging) {
191                    ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger(
192                            mRv.getContext());
193                    postAnimationTrigger.increment();
194                    EventBus.getDefault().send(new DragEndEvent(mDragTask, mTaskView,
195                            mLastDropTarget, postAnimationTrigger));
196                    postAnimationTrigger.decrement();
197                    break;
198                }
199            }
200        }
201    }
202}
203