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