2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.recents.views;
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Rect;
22 import android.graphics.drawable.Drawable;
23 import android.os.Handler;
24 import android.util.ArraySet;
25 import android.util.AttributeSet;
26 import android.view.AppTransitionAnimationSpec;
27 import android.view.LayoutInflater;
28 import android.view.MotionEvent;
29 import android.view.View;
30 import android.view.WindowInsets;
31 import android.view.animation.AnimationUtils;
32 import android.view.animation.Interpolator;
33 import android.widget.FrameLayout;
34 import com.android.internal.annotations.GuardedBy;
35 import com.android.internal.logging.MetricsLogger;
36 import com.android.systemui.R;
37 import com.android.systemui.recents.Recents;
38 import com.android.systemui.recents.RecentsActivity;
39 import com.android.systemui.recents.RecentsAppWidgetHostView;
40 import com.android.systemui.recents.RecentsConfiguration;
41 import com.android.systemui.recents.events.EventBus;
42 import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
43 import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
44 import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
45 import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
46 import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
47 import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
48 import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
49 import com.android.systemui.recents.misc.SystemServicesProxy;
50 import com.android.systemui.recents.model.Task;
51 import com.android.systemui.recents.model.TaskStack;
53 import java.util.ArrayList;
54 import java.util.List;
56 import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
59 * This view is the the top level layout that contains TaskStacks (which are laid out according
60 * to their SpaceNode bounds.
62 public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
64 private static final String TAG = "RecentsView";
65 private static final boolean DEBUG = false;
67 private int mStackViewVisibility = View.VISIBLE;
69 LayoutInflater mInflater;
72 ArrayList<TaskStack> mStacks;
73 TaskStackView mTaskStackView;
74 RecentsAppWidgetHostView mSearchBar;
76 RecentsTransitionHelper mTransitionHelper;
77 RecentsViewTouchHandler mTouchHandler;
79 TaskStack.DockState[] mVisibleDockStates = {
80 TaskStack.DockState.LEFT,
81 TaskStack.DockState.TOP,
82 TaskStack.DockState.RIGHT,
83 TaskStack.DockState.BOTTOM,
86 Interpolator mFastOutSlowInInterpolator;
88 Rect mSystemInsets = new Rect();
90 public RecentsView(Context context) {
94 public RecentsView(Context context, AttributeSet attrs) {
95 this(context, attrs, 0);
98 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
99 this(context, attrs, defStyleAttr, 0);
102 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
103 super(context, attrs, defStyleAttr, defStyleRes);
104 setWillNotDraw(false);
105 mInflater = LayoutInflater.from(context);
106 mHandler = new Handler();
107 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
108 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
109 com.android.internal.R.interpolator.fast_out_slow_in);
110 mTouchHandler = new RecentsViewTouchHandler(this);
113 /** Set/get the bsp root node */
114 public void setTaskStack(TaskStack stack) {
115 RecentsConfiguration config = Recents.getConfiguration();
116 if (config.getLaunchState().launchedReuseTaskStackViews) {
117 if (mTaskStackView != null) {
118 // If onRecentsHidden is not triggered, we need to the stack view again here
119 mTaskStackView.reset();
120 mTaskStackView.setStack(stack);
122 mTaskStackView = new TaskStackView(getContext(), stack);
123 mTaskStackView.setCallbacks(this);
124 addView(mTaskStackView);
127 if (mTaskStackView != null) {
128 removeView(mTaskStackView);
130 mTaskStackView = new TaskStackView(getContext(), stack);
131 mTaskStackView.setCallbacks(this);
132 addView(mTaskStackView);
134 mTaskStackView.setVisibility(mStackViewVisibility);
136 // Trigger a new layout
140 /** Gets the next task in the stack - or if the last - the top task */
141 public Task getNextTaskOrTopTask(Task taskToSearch) {
142 Task returnTask = null;
143 boolean found = false;
144 if (mTaskStackView != null) {
145 TaskStack stack = mTaskStackView.getStack();
146 ArrayList<Task> taskList = stack.getTasks();
147 // Iterate the stack views and try and find the focused task
148 for (int j = taskList.size() - 1; j >= 0; --j) {
149 Task task = taskList.get(j);
150 // Return the next task in the line.
153 // Remember the first possible task as the top task.
154 if (returnTask == null)
156 if (task == taskToSearch)
163 /** Launches the focused task from the first stack if possible */
164 public boolean launchFocusedTask() {
165 if (mTaskStackView != null) {
166 TaskStack stack = mTaskStackView.getStack();
167 Task task = mTaskStackView.getFocusedTask();
169 TaskView taskView = mTaskStackView.getChildViewForTask(task);
170 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
178 /** Launches a given task. */
179 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
180 if (mTaskStackView != null) {
181 TaskStack stack = mTaskStackView.getStack();
182 // Iterate the stack views and try and find the given task.
183 List<TaskView> taskViews = mTaskStackView.getTaskViews();
184 int taskViewCount = taskViews.size();
185 for (int j = 0; j < taskViewCount; j++) {
186 TaskView tv = taskViews.get(j);
187 if (tv.getTask() == task) {
188 onTaskViewClicked(mTaskStackView, tv, stack, task, false,
189 taskBounds, destinationStack);
197 /** Requests all task stacks to start their enter-recents animation */
198 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
199 // We have to increment/decrement the post animation trigger in case there are no children
200 // to ensure that it runs
201 ctx.postAnimationTrigger.increment();
202 if (mTaskStackView != null) {
203 mTaskStackView.startEnterRecentsAnimation(ctx);
205 ctx.postAnimationTrigger.decrement();
208 /** Requests all task stacks to start their exit-recents animation */
209 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
210 // We have to increment/decrement the post animation trigger in case there are no children
211 // to ensure that it runs
212 ctx.postAnimationTrigger.increment();
213 if (mTaskStackView != null) {
214 mTaskStackView.startExitToHomeAnimation(ctx);
216 ctx.postAnimationTrigger.decrement();
218 // If we are going home, cancel the previous task's window transition
219 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
221 // Notify of the exit animation
222 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
225 /** Adds the search bar */
226 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
227 // Remove the previous search bar if one exists
228 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
229 removeView(mSearchBar);
231 // Add the new search bar
232 if (searchBar != null) {
233 mSearchBar = searchBar;
238 /** Returns whether there is currently a search bar */
239 public boolean hasValidSearchBar() {
240 return mSearchBar != null && !mSearchBar.isReinflateRequired();
243 /** Sets the visibility of the search bar */
244 public void setSearchBarVisibility(int visibility) {
245 if (mSearchBar != null) {
246 mSearchBar.setVisibility(visibility);
247 // Always bring the search bar to the top
248 mSearchBar.bringToFront();
253 protected void onAttachedToWindow() {
254 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
255 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
256 super.onAttachedToWindow();
260 protected void onDetachedFromWindow() {
261 super.onDetachedFromWindow();
262 EventBus.getDefault().unregister(this);
263 EventBus.getDefault().unregister(mTouchHandler);
267 * This is called with the full size of the window since we are handling our own insets.
270 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
271 RecentsConfiguration config = Recents.getConfiguration();
272 int width = MeasureSpec.getSize(widthMeasureSpec);
273 int height = MeasureSpec.getSize(heightMeasureSpec);
275 // Get the search bar bounds and measure the search bar layout
276 Rect searchBarSpaceBounds = new Rect();
277 if (mSearchBar != null) {
278 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
279 searchBarSpaceBounds);
281 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
282 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
285 Rect taskStackBounds = new Rect();
286 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
287 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
288 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
289 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
290 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
293 if (mDragView != null) {
294 Rect taskRect = mTaskStackView.mLayoutAlgorithm.mTaskRect;
296 MeasureSpec.makeMeasureSpec(taskRect.width(), MeasureSpec.AT_MOST),
297 MeasureSpec.makeMeasureSpec(taskRect.height(), MeasureSpec.AT_MOST));
300 setMeasuredDimension(width, height);
304 * This is called with the full size of the window since we are handling our own insets.
307 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
308 RecentsConfiguration config = Recents.getConfiguration();
310 // Get the search bar bounds so that we lay it out
311 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
312 Rect searchBarSpaceBounds = new Rect();
313 if (mSearchBar != null) {
314 config.getSearchBarBounds(measuredRect,
315 mSystemInsets.top, searchBarSpaceBounds);
316 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
317 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
320 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
321 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
324 if (mDragView != null) {
325 mDragView.layout(left, top, left + mDragView.getMeasuredWidth(),
326 top + mDragView.getMeasuredHeight());
331 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
332 mSystemInsets.set(insets.getSystemWindowInsets());
334 return insets.consumeSystemWindowInsets();
338 public boolean onInterceptTouchEvent(MotionEvent ev) {
339 return mTouchHandler.onInterceptTouchEvent(ev);
343 public boolean onTouchEvent(MotionEvent ev) {
344 return mTouchHandler.onTouchEvent(ev);
348 protected void dispatchDraw(Canvas canvas) {
349 super.dispatchDraw(canvas);
350 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
351 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
352 if (d.getAlpha() > 0) {
359 protected boolean verifyDrawable(Drawable who) {
360 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
361 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
366 return super.verifyDrawable(who);
369 public void disableLayersForOneFrame() {
370 if (mTaskStackView != null) {
371 mTaskStackView.disableLayersForOneFrame();
375 /**** TaskStackView.TaskStackCallbacks Implementation ****/
378 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
379 final TaskStack stack, final Task task, final boolean lockToTask,
380 final Rect bounds, int destinationStack) {
381 mTransitionHelper.launchTaskFromRecents(stack, task, stackView, tv, lockToTask, bounds,
385 /**** EventBus Events ****/
387 public final void onBusEvent(DragStartEvent event) {
389 mDragView = event.dragView;
392 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
393 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
396 public final void onBusEvent(DragDropTargetChangedEvent event) {
397 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
398 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
399 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
401 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
402 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
406 public final void onBusEvent(final DragEndEvent event) {
407 final Runnable cleanUpRunnable = new Runnable() {
410 // Remove the drag view
411 removeView(mDragView);
416 // Animate the overlay alpha back to 0
417 updateVisibleDockRegions(null, -1);
419 if (event.dropTarget == null) {
420 // No drop targets for hit, so just animate the task back to its place
421 event.postAnimationTrigger.increment();
422 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
425 cleanUpRunnable.run();
428 // Animate the task back to where it was before then clean up afterwards
429 TaskViewTransform taskTransform = new TaskViewTransform();
430 TaskStackLayoutAlgorithm layoutAlgorithm = mTaskStackView.getStackAlgorithm();
431 layoutAlgorithm.getStackTransform(event.task,
432 mTaskStackView.getScroller().getStackScroll(), taskTransform, null);
433 event.dragView.animate()
434 .scaleX(taskTransform.scale)
435 .scaleY(taskTransform.scale)
436 .translationX((layoutAlgorithm.mTaskRect.left - event.dragView.getLeft())
437 + taskTransform.translationX)
438 .translationY((layoutAlgorithm.mTaskRect.top - event.dragView.getTop())
439 + taskTransform.translationY)
441 .setInterpolator(mFastOutSlowInInterpolator)
442 .withEndAction(event.postAnimationTrigger.decrementAsRunnable())
445 } else if (event.dropTarget instanceof TaskStack.DockState) {
446 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
448 // For now, just remove the drag view and the original task
449 // TODO: Animate the task to the drop target rect before launching it above
450 cleanUpRunnable.run();
452 // Dock the task and launch it
453 SystemServicesProxy ssp = Recents.getSystemServices();
454 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
455 launchTask(event.task, null, INVALID_STACK_ID);
458 // We dropped on another drop target, so just add the cleanup to the post animation
460 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
463 cleanUpRunnable.run();
469 public final void onBusEvent(DraggingInRecentsEvent event) {
470 setStackViewVisibility(View.VISIBLE);
471 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
474 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
475 animate().translationY(0f);
479 * Updates the dock region to match the specified dock state.
481 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
482 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
483 if (newDockStates != null) {
484 for (TaskStack.DockState dockState : newDockStates) {
485 newDockStatesSet.add(dockState);
488 for (TaskStack.DockState dockState : mVisibleDockStates) {
489 TaskStack.DockState.ViewState viewState = dockState.viewState;
490 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
491 // This is no longer visible, so hide it
492 viewState.startAlphaAnimation(0, 150);
494 // This state is now visible, update the bounds and show it
495 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
496 viewState.dockAreaOverlay.setBounds(
497 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
498 viewState.dockAreaOverlay.setCallback(this);
499 viewState.startAlphaAnimation(alpha, 150);
504 public void setStackViewVisibility(int stackViewVisibility) {
505 mStackViewVisibility = stackViewVisibility;
506 if (mTaskStackView != null) {
507 mTaskStackView.setVisibility(stackViewVisibility);