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