Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.systemui.recents.tv.views; |
| 17 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 18 | import android.content.Context; |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 19 | import android.support.v17.leanback.widget.HorizontalGridView; |
| 20 | import android.util.AttributeSet; |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 21 | import android.view.View; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 22 | |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 23 | import com.android.systemui.R; |
| 24 | import com.android.systemui.recents.RecentsActivity; |
| 25 | import com.android.systemui.recents.events.EventBus; |
| 26 | import com.android.systemui.recents.events.ui.AllTaskViewsDismissedEvent; |
| 27 | import com.android.systemui.recents.model.Task; |
| 28 | import com.android.systemui.recents.model.TaskStack; |
| 29 | import com.android.systemui.recents.model.TaskStack.TaskStackCallbacks; |
Winson | be8e696 | 2016-02-01 14:27:52 -0800 | [diff] [blame] | 30 | import com.android.systemui.recents.views.AnimationProps; |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
| 36 | * Horizontal Grid View Implementation to show the Task Stack for TV. |
| 37 | */ |
Sid Soundararajan | 1008cc2 | 2016-02-01 11:11:14 -0800 | [diff] [blame] | 38 | public class TaskStackHorizontalGridView extends HorizontalGridView implements TaskStackCallbacks { |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 39 | |
| 40 | private TaskStack mStack; |
| 41 | private ArrayList<TaskCardView> mTaskViews = new ArrayList<>(); |
| 42 | private Task mFocusedTask; |
| 43 | |
Sid Soundararajan | 1008cc2 | 2016-02-01 11:11:14 -0800 | [diff] [blame] | 44 | public TaskStackHorizontalGridView(Context context) { |
| 45 | this(context, null); |
| 46 | } |
| 47 | |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 48 | 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 Soundararajan | 4bdb687 | 2016-03-18 13:42:10 -0700 | [diff] [blame] | 55 | setWindowAlignment(WINDOW_ALIGN_NO_EDGE); |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 56 | 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 Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 69 | requestLayout(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param task - Task to reset |
| 74 | */ |
| 75 | private void resetFocusedTask(Task task) { |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 76 | 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 Soundararajan | 1008cc2 | 2016-02-01 11:11:14 -0800 | [diff] [blame] | 104 | if (findFocus() != null) { |
| 105 | mFocusedTask = ((TaskCardView)findFocus()).getTask(); |
| 106 | } |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 107 | return mFocusedTask; |
| 108 | } |
| 109 | |
| 110 | /** |
Sid Soundararajan | 4bdb687 | 2016-03-18 13:42:10 -0700 | [diff] [blame] | 111 | * @return - The focused task card view. |
| 112 | */ |
| 113 | public TaskCardView getFocusedTaskCardView() { |
| 114 | return ((TaskCardView)findFocus()); |
| 115 | } |
| 116 | |
| 117 | /** |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 118 | * @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, |
Winson | 2068408 | 2016-03-16 17:13:34 -0700 | [diff] [blame] | 144 | Task newFrontMostTask, AnimationProps animation, boolean fromDockGesture) { |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 145 | 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) { |
Winson | 2068408 | 2016-03-16 17:13:34 -0700 | [diff] [blame] | 153 | EventBus.getDefault().send(new AllTaskViewsDismissedEvent(fromDockGesture |
| 154 | ? R.string.recents_empty_message |
| 155 | : R.string.recents_empty_message_dismissed_all)); |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
Winson | a1ededd | 2016-03-25 12:23:12 -0700 | [diff] [blame] | 159 | |
| 160 | @Override |
| 161 | public void onStackTasksUpdated(TaskStack stack) { |
| 162 | // Do nothing |
| 163 | } |
Sid Soundararajan | b58c46a | 2016-01-26 15:39:27 -0800 | [diff] [blame] | 164 | } |