2 * Copyright (C) 2016 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.
16 package com.android.systemui.recents.tv.views;
18 import android.animation.Animator;
19 import android.app.Activity;
20 import android.support.v7.widget.RecyclerView;
21 import android.util.Log;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
26 import com.android.systemui.R;
27 import com.android.systemui.recents.events.EventBus;
28 import com.android.systemui.recents.events.activity.LaunchTvTaskEvent;
29 import com.android.systemui.recents.events.ui.DeleteTaskDataEvent;
30 import com.android.systemui.recents.model.Task;
31 import com.android.systemui.recents.views.AnimationProps;
33 import java.util.ArrayList;
34 import java.util.List;
36 import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
38 public class TaskStackHorizontalViewAdapter extends
39 RecyclerView.Adapter<TaskStackHorizontalViewAdapter.ViewHolder> {
41 //Full class name is 30 characters
42 private static final String TAG = "TaskStackViewAdapter";
43 private List<Task> mTaskList;
44 private TaskStackHorizontalGridView mGridView;
45 private boolean mResetAddedCards;
47 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
48 private TaskCardView mTaskCardView;
50 private boolean mShouldReset;
51 public ViewHolder(View v) {
53 if(v instanceof TaskCardView) {
54 mTaskCardView = (TaskCardView) v;
58 public void init(Task task) {
59 mTaskCardView.init(task);
61 mTaskCardView.setOnClickListener(this);
65 public void onClick(View v) {
67 if (mTaskCardView.isInDismissState()) {
68 mTaskCardView.startDismissTaskAnimation(
69 getRemoveAtListener(getAdapterPosition(), mTaskCardView.getTask()));
71 EventBus.getDefault().send(new LaunchTvTaskEvent(mTaskCardView, mTask,
72 null, INVALID_STACK_ID));
74 } catch (Exception e) {
75 Log.e(TAG, v.getContext()
76 .getString(R.string.recents_launch_error_message, mTask.title), e);
81 private Animator.AnimatorListener getRemoveAtListener(final int position,
83 return new Animator.AnimatorListener() {
86 public void onAnimationStart(Animator animation) { }
89 public void onAnimationEnd(Animator animation) {
91 EventBus.getDefault().send(new DeleteTaskDataEvent(task));
96 public void onAnimationCancel(Animator animation) { }
99 public void onAnimationRepeat(Animator animation) { }
105 public TaskStackHorizontalViewAdapter(List tasks) {
106 mTaskList = new ArrayList<Task>(tasks);
109 public void setNewStackTasks(List tasks) {
111 mTaskList.addAll(tasks);
112 notifyDataSetChanged();
116 public TaskStackHorizontalViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
118 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
119 ViewHolder viewHolder = new ViewHolder(
120 inflater.inflate(R.layout.recents_tv_task_card_view, parent, false));
125 public void onBindViewHolder(ViewHolder holder, int position) {
126 holder.init(mTaskList.get(position));
130 public void onViewAttachedToWindow(ViewHolder holder) {
131 if (mResetAddedCards) {
132 holder.mTaskCardView.reset();
137 public void onViewDetachedFromWindow(ViewHolder holder) {
138 // We only want to reset on view detach if this is the last task being dismissed.
139 // This is so that we do not reset when shifting to apps etc, as it is not needed.
140 if (holder.mShouldReset) {
141 holder.mTaskCardView.reset();
142 holder.mShouldReset = false;
147 public int getItemCount() {
148 return mTaskList.size();
151 private void removeAt(int position) {
152 Task removedTask = mTaskList.remove(position);
153 if (mGridView != null) {
154 mGridView.getStack().removeTask(removedTask, AnimationProps.IMMEDIATE,
157 notifyItemRemoved(position);
160 public void removeTask(Task task) {
161 int position = mTaskList.indexOf(task);
163 mTaskList.remove(position);
164 notifyItemRemoved(position);
168 public int getPositionOfTask(Task task) {
169 int position = mTaskList.indexOf(task);
170 return (position >= 0) ? position : 0;
174 public void setTaskStackHorizontalGridView(TaskStackHorizontalGridView gridView) {
175 mGridView = gridView;
178 public void addTaskAt(Task task, int position) {
179 mTaskList.add(position, task);
180 notifyItemInserted(position);
183 public void setResetAddedCards(boolean reset) {
184 mResetAddedCards = reset;