blob: 3d0e75a8ef700ba9bcf0a489f0979d2f3cd586a7 [file] [log] [blame]
Sid Soundararajanb58c46a2016-01-26 15:39:27 -08001/*
2 * Copyright (C) 2016 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 */
16package com.android.systemui.recents.tv.views;
17
Winsonc0d70582016-01-29 10:24:39 -080018import android.content.Context;
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080019import android.support.v17.leanback.widget.HorizontalGridView;
20import android.util.AttributeSet;
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080021import android.view.View;
Winsonc0d70582016-01-29 10:24:39 -080022
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080023import com.android.systemui.R;
24import com.android.systemui.recents.RecentsActivity;
25import com.android.systemui.recents.events.EventBus;
26import com.android.systemui.recents.events.ui.AllTaskViewsDismissedEvent;
27import com.android.systemui.recents.model.Task;
28import com.android.systemui.recents.model.TaskStack;
29import com.android.systemui.recents.model.TaskStack.TaskStackCallbacks;
Winsonbe8e6962016-02-01 14:27:52 -080030import com.android.systemui.recents.views.AnimationProps;
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080031
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * Horizontal Grid View Implementation to show the Task Stack for TV.
37 */
Sid Soundararajan1008cc22016-02-01 11:11:14 -080038public class TaskStackHorizontalGridView extends HorizontalGridView implements TaskStackCallbacks {
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080039
40 private TaskStack mStack;
41 private ArrayList<TaskCardView> mTaskViews = new ArrayList<>();
42 private Task mFocusedTask;
43
Sid Soundararajan1008cc22016-02-01 11:11:14 -080044 public TaskStackHorizontalGridView(Context context) {
45 this(context, null);
46 }
47
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080048 public TaskStackHorizontalGridView(Context context, AttributeSet attrs) {
49 super(context, attrs);
50 }
51
52 @Override
53 protected void onAttachedToWindow() {
54 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
Sid Soundararajan4bdb6872016-03-18 13:42:10 -070055 setWindowAlignment(WINDOW_ALIGN_NO_EDGE);
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080056 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
57 super.onAttachedToWindow();
58 }
59
60 @Override
61 protected void onDetachedFromWindow() {
62 super.onDetachedFromWindow();
63 EventBus.getDefault().unregister(this);
64 }
65 /**
66 * Resets this view for reuse.
67 */
68 public void reset() {
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080069 requestLayout();
70 }
71
72 /**
73 * @param task - Task to reset
74 */
75 private void resetFocusedTask(Task task) {
Sid Soundararajanb58c46a2016-01-26 15:39:27 -080076 mFocusedTask = null;
77 }
78
79 /**
80 * Sets the task stack.
81 * @param stack
82 */
83 public void setStack(TaskStack stack) {
84 //Set new stack
85 mStack = stack;
86 if (mStack != null) {
87 mStack.setCallbacks(this);
88 }
89 //Layout with new stack
90 requestLayout();
91 }
92
93 /**
94 * @return Returns the task stack.
95 */
96 public TaskStack getStack() {
97 return mStack;
98 }
99
100 /**
101 * @return - The focused task.
102 */
103 public Task getFocusedTask() {
Sid Soundararajan1008cc22016-02-01 11:11:14 -0800104 if (findFocus() != null) {
105 mFocusedTask = ((TaskCardView)findFocus()).getTask();
106 }
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800107 return mFocusedTask;
108 }
109
110 /**
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700111 * @return - The focused task card view.
112 */
113 public TaskCardView getFocusedTaskCardView() {
114 return ((TaskCardView)findFocus());
115 }
116
117 /**
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800118 * @param task
119 * @return Child view for given task
120 */
121 public TaskCardView getChildViewForTask(Task task) {
122 List<TaskCardView> taskViews = getTaskViews();
123 int taskViewCount = taskViews.size();
124 for (int i = 0; i < taskViewCount; i++) {
125 TaskCardView tv = taskViews.get(i);
126 if (tv.getTask() == task) {
127 return tv;
128 }
129 }
130 return null;
131 }
132
133 public List<TaskCardView> getTaskViews() {
134 return mTaskViews;
135 }
136
137 @Override
138 public void onStackTaskAdded(TaskStack stack, Task newTask){
139 getAdapter().notifyItemInserted(stack.getStackTasks().indexOf(newTask));
140 }
141
142 @Override
143 public void onStackTaskRemoved(TaskStack stack, Task removedTask, boolean wasFrontMostTask,
Winson20684082016-03-16 17:13:34 -0700144 Task newFrontMostTask, AnimationProps animation, boolean fromDockGesture) {
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800145 getAdapter().notifyItemRemoved(stack.getStackTasks().indexOf(removedTask));
146 if (mFocusedTask == removedTask) {
147 resetFocusedTask(removedTask);
148 }
149 // If there are no remaining tasks, then just close recents
150 if (mStack.getStackTaskCount() == 0) {
151 boolean shouldFinishActivity = (mStack.getStackTaskCount() == 0);
152 if (shouldFinishActivity) {
Winson20684082016-03-16 17:13:34 -0700153 EventBus.getDefault().send(new AllTaskViewsDismissedEvent(fromDockGesture
154 ? R.string.recents_empty_message
155 : R.string.recents_empty_message_dismissed_all));
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800156 }
157 }
158 }
Winsona1ededd2016-03-25 12:23:12 -0700159
160 @Override
161 public void onStackTasksUpdated(TaskStack stack) {
162 // Do nothing
163 }
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800164}