blob: 22ade9f43fe50ce3255fa387e485b2400a52effc [file] [log] [blame]
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.recents.tv.views;
import android.content.Context;
import android.support.v17.leanback.widget.HorizontalGridView;
import android.util.AttributeSet;
import android.view.View;
import com.android.systemui.R;
import com.android.systemui.recents.RecentsActivity;
import com.android.systemui.recents.events.EventBus;
import com.android.systemui.recents.events.ui.AllTaskViewsDismissedEvent;
import com.android.systemui.recents.model.Task;
import com.android.systemui.recents.model.TaskStack;
import com.android.systemui.recents.model.TaskStack.TaskStackCallbacks;
import com.android.systemui.recents.views.AnimationProps;
import java.util.ArrayList;
import java.util.List;
/**
* Horizontal Grid View Implementation to show the Task Stack for TV.
*/
public class TaskStackHorizontalGridView extends HorizontalGridView implements TaskStackCallbacks {
private TaskStack mStack;
private ArrayList<TaskCardView> mTaskViews = new ArrayList<>();
private Task mFocusedTask;
public TaskStackHorizontalGridView(Context context) {
this(context, null);
}
public TaskStackHorizontalGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onAttachedToWindow() {
EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
setWindowAlignment(WINDOW_ALIGN_NO_EDGE);
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
EventBus.getDefault().unregister(this);
}
/**
* Resets this view for reuse.
*/
public void reset() {
requestLayout();
}
/**
* @param task - Task to reset
*/
private void resetFocusedTask(Task task) {
mFocusedTask = null;
}
/**
* Sets the task stack.
* @param stack
*/
public void setStack(TaskStack stack) {
//Set new stack
mStack = stack;
if (mStack != null) {
mStack.setCallbacks(this);
}
//Layout with new stack
requestLayout();
}
/**
* @return Returns the task stack.
*/
public TaskStack getStack() {
return mStack;
}
/**
* @return - The focused task.
*/
public Task getFocusedTask() {
if (findFocus() != null) {
mFocusedTask = ((TaskCardView)findFocus()).getTask();
}
return mFocusedTask;
}
/**
* @return - The focused task card view.
*/
public TaskCardView getFocusedTaskCardView() {
return ((TaskCardView)findFocus());
}
/**
* @param task
* @return Child view for given task
*/
public TaskCardView getChildViewForTask(Task task) {
List<TaskCardView> taskViews = getTaskViews();
int taskViewCount = taskViews.size();
for (int i = 0; i < taskViewCount; i++) {
TaskCardView tv = taskViews.get(i);
if (tv.getTask() == task) {
return tv;
}
}
return null;
}
public List<TaskCardView> getTaskViews() {
return mTaskViews;
}
@Override
public void onStackTaskAdded(TaskStack stack, Task newTask){
getAdapter().notifyItemInserted(stack.getStackTasks().indexOf(newTask));
}
@Override
public void onStackTaskRemoved(TaskStack stack, Task removedTask, boolean wasFrontMostTask,
Task newFrontMostTask, AnimationProps animation, boolean fromDockGesture) {
getAdapter().notifyItemRemoved(stack.getStackTasks().indexOf(removedTask));
if (mFocusedTask == removedTask) {
resetFocusedTask(removedTask);
}
// If there are no remaining tasks, then just close recents
if (mStack.getStackTaskCount() == 0) {
boolean shouldFinishActivity = (mStack.getStackTaskCount() == 0);
if (shouldFinishActivity) {
EventBus.getDefault().send(new AllTaskViewsDismissedEvent(fromDockGesture
? R.string.recents_empty_message
: R.string.recents_empty_message_dismissed_all));
}
}
}
}