OSDN Git Service

Removing unused code
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / recents / views / RecentsView.java
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
17 package com.android.systemui.recents.views;
18
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;
52
53 import java.util.ArrayList;
54 import java.util.List;
55
56 import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
57
58 /**
59  * This view is the the top level layout that contains TaskStacks (which are laid out according
60  * to their SpaceNode bounds.
61  */
62 public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
63
64     private static final String TAG = "RecentsView";
65     private static final boolean DEBUG = false;
66
67     private int mStackViewVisibility = View.VISIBLE;
68
69     LayoutInflater mInflater;
70     Handler mHandler;
71
72     ArrayList<TaskStack> mStacks;
73     TaskStackView mTaskStackView;
74     RecentsAppWidgetHostView mSearchBar;
75
76     RecentsTransitionHelper mTransitionHelper;
77     RecentsViewTouchHandler mTouchHandler;
78     DragView mDragView;
79     TaskStack.DockState[] mVisibleDockStates = {
80             TaskStack.DockState.LEFT,
81             TaskStack.DockState.TOP,
82             TaskStack.DockState.RIGHT,
83             TaskStack.DockState.BOTTOM,
84     };
85
86     Interpolator mFastOutSlowInInterpolator;
87
88     Rect mSystemInsets = new Rect();
89
90     public RecentsView(Context context) {
91         super(context);
92     }
93
94     public RecentsView(Context context, AttributeSet attrs) {
95         this(context, attrs, 0);
96     }
97
98     public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
99         this(context, attrs, defStyleAttr, 0);
100     }
101
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);
111     }
112
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);
121             } else {
122                 mTaskStackView = new TaskStackView(getContext(), stack);
123                 mTaskStackView.setCallbacks(this);
124                 addView(mTaskStackView);
125             }
126         } else {
127             if (mTaskStackView != null) {
128                 removeView(mTaskStackView);
129             }
130             mTaskStackView = new TaskStackView(getContext(), stack);
131             mTaskStackView.setCallbacks(this);
132             addView(mTaskStackView);
133         }
134         mTaskStackView.setVisibility(mStackViewVisibility);
135
136         // Trigger a new layout
137         requestLayout();
138     }
139
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.
151                 if (found)
152                     return task;
153                 // Remember the first possible task as the top task.
154                 if (returnTask == null)
155                     returnTask = task;
156                 if (task == taskToSearch)
157                     found = true;
158             }
159         }
160         return returnTask;
161     }
162
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();
168             if (task != null) {
169                 TaskView taskView = mTaskStackView.getChildViewForTask(task);
170                 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
171                         INVALID_STACK_ID);
172                 return true;
173             }
174         }
175         return false;
176     }
177
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);
190                     return true;
191                 }
192             }
193         }
194         return false;
195     }
196
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);
204         }
205         ctx.postAnimationTrigger.decrement();
206     }
207
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);
215         }
216         ctx.postAnimationTrigger.decrement();
217
218         // If we are going home, cancel the previous task's window transition
219         EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
220
221         // Notify of the exit animation
222         EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
223     }
224
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);
230         }
231         // Add the new search bar
232         if (searchBar != null) {
233             mSearchBar = searchBar;
234             addView(mSearchBar);
235         }
236     }
237
238     /** Returns whether there is currently a search bar */
239     public boolean hasValidSearchBar() {
240         return mSearchBar != null && !mSearchBar.isReinflateRequired();
241     }
242
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();
249         }
250     }
251
252     @Override
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();
257     }
258
259     @Override
260     protected void onDetachedFromWindow() {
261         super.onDetachedFromWindow();
262         EventBus.getDefault().unregister(this);
263         EventBus.getDefault().unregister(mTouchHandler);
264     }
265
266     /**
267      * This is called with the full size of the window since we are handling our own insets.
268      */
269     @Override
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);
274
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);
280             mSearchBar.measure(
281                     MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
282                     MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
283         }
284
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);
291         }
292
293         if (mDragView != null) {
294             Rect taskRect = mTaskStackView.mLayoutAlgorithm.mTaskRect;
295             mDragView.measure(
296                     MeasureSpec.makeMeasureSpec(taskRect.width(), MeasureSpec.AT_MOST),
297                     MeasureSpec.makeMeasureSpec(taskRect.height(), MeasureSpec.AT_MOST));
298         }
299
300         setMeasuredDimension(width, height);
301     }
302
303     /**
304      * This is called with the full size of the window since we are handling our own insets.
305      */
306     @Override
307     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
308         RecentsConfiguration config = Recents.getConfiguration();
309
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);
318         }
319
320         if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
321             mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
322         }
323
324         if (mDragView != null) {
325             mDragView.layout(left, top, left + mDragView.getMeasuredWidth(),
326                     top + mDragView.getMeasuredHeight());
327         }
328     }
329
330     @Override
331     public WindowInsets onApplyWindowInsets(WindowInsets insets) {
332         mSystemInsets.set(insets.getSystemWindowInsets());
333         requestLayout();
334         return insets.consumeSystemWindowInsets();
335     }
336
337     @Override
338     public boolean onInterceptTouchEvent(MotionEvent ev) {
339         return mTouchHandler.onInterceptTouchEvent(ev);
340     }
341
342     @Override
343     public boolean onTouchEvent(MotionEvent ev) {
344         return mTouchHandler.onTouchEvent(ev);
345     }
346
347     @Override
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) {
353                 d.draw(canvas);
354             }
355         }
356     }
357
358     @Override
359     protected boolean verifyDrawable(Drawable who) {
360         for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
361             Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
362             if (d == who) {
363                 return true;
364             }
365         }
366         return super.verifyDrawable(who);
367     }
368
369     public void disableLayersForOneFrame() {
370         if (mTaskStackView != null) {
371             mTaskStackView.disableLayersForOneFrame();
372         }
373     }
374
375     /**** TaskStackView.TaskStackCallbacks Implementation ****/
376
377     @Override
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,
382                 destinationStack);
383     }
384
385     /**** EventBus Events ****/
386
387     public final void onBusEvent(DragStartEvent event) {
388         // Add the drag view
389         mDragView = event.dragView;
390         addView(mDragView);
391
392         updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
393                 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
394     }
395
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);
400         } else {
401             final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
402             updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
403         }
404     }
405
406     public final void onBusEvent(final DragEndEvent event) {
407         final Runnable cleanUpRunnable = new Runnable() {
408             @Override
409             public void run() {
410                 // Remove the drag view
411                 removeView(mDragView);
412                 mDragView = null;
413             }
414         };
415
416         // Animate the overlay alpha back to 0
417         updateVisibleDockRegions(null, -1);
418
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() {
423                 @Override
424                 public void run() {
425                     cleanUpRunnable.run();
426                 }
427             });
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)
440                     .setDuration(175)
441                     .setInterpolator(mFastOutSlowInInterpolator)
442                     .withEndAction(event.postAnimationTrigger.decrementAsRunnable())
443                     .start();
444
445         } else if (event.dropTarget instanceof TaskStack.DockState) {
446             final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
447
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();
451
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);
456
457         } else {
458             // We dropped on another drop target, so just add the cleanup to the post animation
459             // trigger
460             event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
461                 @Override
462                 public void run() {
463                     cleanUpRunnable.run();
464                 }
465             });
466         }
467     }
468
469     public final void onBusEvent(DraggingInRecentsEvent event) {
470         setStackViewVisibility(View.VISIBLE);
471         setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
472     }
473
474     public final void onBusEvent(DraggingInRecentsEndedEvent event) {
475         animate().translationY(0f);
476     }
477
478     /**
479      * Updates the dock region to match the specified dock state.
480      */
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);
486             }
487         }
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);
493             } else {
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);
500             }
501         }
502     }
503
504     public void setStackViewVisibility(int stackViewVisibility) {
505         mStackViewVisibility = stackViewVisibility;
506         if (mTaskStackView != null) {
507             mTaskStackView.setVisibility(stackViewVisibility);
508             invalidate();
509         }
510     }
511 }