blob: 473334b40d8dcaef4ed25398ece3f3972c9ce953 [file] [log] [blame]
Winsonbe7607a2015-10-01 17:24:51 -07001/*
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;
Winson53ec42c2015-10-28 15:55:35 -070022import com.android.systemui.recents.Recents;
Winson882072b2015-10-12 11:26:33 -070023import com.android.systemui.recents.RecentsConfiguration;
Winsonbe7607a2015-10-01 17:24:51 -070024import com.android.systemui.recents.events.EventBus;
Winsoneca4ab62015-11-04 10:50:28 -080025import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070026import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
27import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winsoneca4ab62015-11-04 10:50:28 -080028import com.android.systemui.recents.events.ui.dragndrop.DragStartInitializeDropTargetsEvent;
Winsond759a712015-11-30 18:43:43 -080029import com.android.systemui.recents.misc.SystemServicesProxy;
Winsonbe7607a2015-10-01 17:24:51 -070030import com.android.systemui.recents.model.Task;
31import com.android.systemui.recents.model.TaskStack;
32
Winsoneca4ab62015-11-04 10:50:28 -080033import java.util.ArrayList;
34
Winsonbe7607a2015-10-01 17:24:51 -070035
36/**
37 * Represents the dock regions for each orientation.
38 */
39class DockRegion {
Winson882072b2015-10-12 11:26:33 -070040 public static TaskStack.DockState[] PHONE_LANDSCAPE = {
Winsone92440e2015-10-28 09:48:47 -070041 // We only allow docking to the left for now on small devices
Winson2364f262015-10-26 10:56:59 -070042 TaskStack.DockState.LEFT
Winsonbe7607a2015-10-01 17:24:51 -070043 };
Winson882072b2015-10-12 11:26:33 -070044 public static TaskStack.DockState[] PHONE_PORTRAIT = {
Winsone92440e2015-10-28 09:48:47 -070045 // We only allow docking to the top for now on small devices
Winson4165d3362015-10-10 14:40:35 -070046 TaskStack.DockState.TOP
Winsonbe7607a2015-10-01 17:24:51 -070047 };
Winson882072b2015-10-12 11:26:33 -070048 public static TaskStack.DockState[] TABLET_LANDSCAPE = {
Winsone92440e2015-10-28 09:48:47 -070049 TaskStack.DockState.LEFT,
50 TaskStack.DockState.RIGHT
Winson882072b2015-10-12 11:26:33 -070051 };
52 public static TaskStack.DockState[] TABLET_PORTRAIT = PHONE_PORTRAIT;
Winsonbe7607a2015-10-01 17:24:51 -070053}
54
55/**
56 * Handles touch events for a RecentsView.
57 */
Winsoneca4ab62015-11-04 10:50:28 -080058public class RecentsViewTouchHandler {
59
Winsonbe7607a2015-10-01 17:24:51 -070060 private RecentsView mRv;
61
62 private Task mDragTask;
63 private TaskView mTaskView;
Winsonbe7607a2015-10-01 17:24:51 -070064
Winson479f7442015-11-25 15:16:27 -080065 private Point mTaskViewOffset = new Point();
Winsonbe7607a2015-10-01 17:24:51 -070066 private Point mDownPos = new Point();
67 private boolean mDragging;
Winsoneca4ab62015-11-04 10:50:28 -080068
69 private DropTarget mLastDropTarget;
70 private ArrayList<DropTarget> mDropTargets = new ArrayList<>();
Winsonbe7607a2015-10-01 17:24:51 -070071
72 public RecentsViewTouchHandler(RecentsView rv) {
73 mRv = rv;
74 }
75
Winson882072b2015-10-12 11:26:33 -070076 /**
Winsoneca4ab62015-11-04 10:50:28 -080077 * Registers a new drop target for the current drag only.
78 */
79 public void registerDropTargetForCurrentDrag(DropTarget target) {
80 mDropTargets.add(target);
81 }
82
83 /**
Winson882072b2015-10-12 11:26:33 -070084 * Returns the preferred dock states for the current orientation.
85 */
86 public TaskStack.DockState[] getDockStatesForCurrentOrientation() {
Winson4165d3362015-10-10 14:40:35 -070087 boolean isLandscape = mRv.getResources().getConfiguration().orientation ==
88 Configuration.ORIENTATION_LANDSCAPE;
Winson53ec42c2015-10-28 15:55:35 -070089 RecentsConfiguration config = Recents.getConfiguration();
Winson4165d3362015-10-10 14:40:35 -070090 TaskStack.DockState[] dockStates = isLandscape ?
Winson882072b2015-10-12 11:26:33 -070091 (config.isLargeScreen ? DockRegion.TABLET_LANDSCAPE : DockRegion.PHONE_LANDSCAPE) :
92 (config.isLargeScreen ? DockRegion.TABLET_PORTRAIT : DockRegion.PHONE_PORTRAIT);
93 return dockStates;
Winson4165d3362015-10-10 14:40:35 -070094 }
95
Winsonbe7607a2015-10-01 17:24:51 -070096 /** Touch preprocessing for handling below */
97 public boolean onInterceptTouchEvent(MotionEvent ev) {
Winson4165d3362015-10-10 14:40:35 -070098 handleTouchEvent(ev);
Winsonbe7607a2015-10-01 17:24:51 -070099 return mDragging;
100 }
101
102 /** Handles touch events once we have intercepted them */
103 public boolean onTouchEvent(MotionEvent ev) {
Winson4165d3362015-10-10 14:40:35 -0700104 handleTouchEvent(ev);
105 return mDragging;
Winsonbe7607a2015-10-01 17:24:51 -0700106 }
107
108 /**** Events ****/
109
110 public final void onBusEvent(DragStartEvent event) {
Winsond759a712015-11-30 18:43:43 -0800111 SystemServicesProxy ssp = Recents.getSystemServices();
Winsonbe7607a2015-10-01 17:24:51 -0700112 mRv.getParent().requestDisallowInterceptTouchEvent(true);
113 mDragging = true;
114 mDragTask = event.task;
115 mTaskView = event.taskView;
Winsoneca4ab62015-11-04 10:50:28 -0800116 mDropTargets.clear();
Winsonbe7607a2015-10-01 17:24:51 -0700117
Winson479f7442015-11-25 15:16:27 -0800118 int[] recentsViewLocation = new int[2];
119 mRv.getLocationInWindow(recentsViewLocation);
120 mTaskViewOffset.set(mTaskView.getLeft() - recentsViewLocation[0] + event.tlOffset.x,
121 mTaskView.getTop() - recentsViewLocation[1] + event.tlOffset.y);
122 float x = mDownPos.x - mTaskViewOffset.x;
123 float y = mDownPos.y - mTaskViewOffset.y;
124 mTaskView.setTranslationX(x);
125 mTaskView.setTranslationY(y);
Winsoneca4ab62015-11-04 10:50:28 -0800126
Winsond759a712015-11-30 18:43:43 -0800127 if (!ssp.hasDockedTask()) {
Winsoneca4ab62015-11-04 10:50:28 -0800128 // 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));
Winsonbe7607a2015-10-01 17:24:51 -0700137 }
138
139 public final void onBusEvent(DragEndEvent event) {
140 mDragging = false;
141 mDragTask = null;
142 mTaskView = null;
Winsoneca4ab62015-11-04 10:50:28 -0800143 mLastDropTarget = null;
Winson4165d3362015-10-10 14:40:35 -0700144 }
145
146 /**
147 * Handles dragging touch events
Winson4165d3362015-10-10 14:40:35 -0700148 */
149 private void handleTouchEvent(MotionEvent ev) {
Winson4165d3362015-10-10 14:40:35 -0700150 int action = ev.getAction();
151 switch (action & MotionEvent.ACTION_MASK) {
152 case MotionEvent.ACTION_DOWN:
153 mDownPos.set((int) ev.getX(), (int) ev.getY());
154 break;
155 case MotionEvent.ACTION_MOVE: {
156 if (mDragging) {
157 int width = mRv.getMeasuredWidth();
158 int height = mRv.getMeasuredHeight();
159 float evX = ev.getX();
160 float evY = ev.getY();
Winson479f7442015-11-25 15:16:27 -0800161 float x = evX - mTaskViewOffset.x;
162 float y = evY - mTaskViewOffset.y;
Winson4165d3362015-10-10 14:40:35 -0700163
Winsoneca4ab62015-11-04 10:50:28 -0800164 DropTarget currentDropTarget = null;
165 for (DropTarget target : mDropTargets) {
166 if (target.acceptsDrop((int) evX, (int) evY, width, height)) {
167 currentDropTarget = target;
Winson4165d3362015-10-10 14:40:35 -0700168 break;
169 }
170 }
Winsoneca4ab62015-11-04 10:50:28 -0800171 if (mLastDropTarget != currentDropTarget) {
172 mLastDropTarget = currentDropTarget;
173 EventBus.getDefault().send(new DragDropTargetChangedEvent(mDragTask,
174 currentDropTarget));
Winson4165d3362015-10-10 14:40:35 -0700175 }
176
Winson479f7442015-11-25 15:16:27 -0800177 mTaskView.setTranslationX(x);
178 mTaskView.setTranslationY(y);
Winson4165d3362015-10-10 14:40:35 -0700179 }
180 break;
181 }
182 case MotionEvent.ACTION_UP:
183 case MotionEvent.ACTION_CANCEL: {
Winson09d8a182015-10-10 14:50:23 -0700184 if (mDragging) {
Winson479f7442015-11-25 15:16:27 -0800185 EventBus.getDefault().send(new DragEndEvent(mDragTask, mTaskView,
Winson Chungaaeaac12015-12-16 16:49:36 -0500186 mLastDropTarget));
Winson09d8a182015-10-10 14:50:23 -0700187 break;
188 }
Winson4165d3362015-10-10 14:40:35 -0700189 }
190 }
Winsonbe7607a2015-10-01 17:24:51 -0700191 }
192}