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 
18 import android.animation.Animator;
19 import android.support.v7.widget.RecyclerView;
20 import android.util.Log;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 import com.android.systemui.R;
26 import com.android.systemui.recents.Recents;
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.RecentsTaskLoader;
31 import com.android.systemui.recents.model.Task;
32 import com.android.systemui.recents.views.AnimationProps;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
38 
39 public class TaskStackHorizontalViewAdapter extends
40         RecyclerView.Adapter<TaskStackHorizontalViewAdapter.ViewHolder> {
41 
42     //Full class name is 30 characters
43     private static final String TAG = "TaskStackViewAdapter";
44     private List<Task> mTaskList;
45     private TaskStackHorizontalGridView mGridView;
46 
47     public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
48         private TaskCardView mTaskCardView;
49         private Task mTask;
ViewHolder(View v)50         public ViewHolder(View v) {
51             super(v);
52             mTaskCardView = (TaskCardView) v;
53         }
54 
init(Task task)55         public void init(Task task) {
56             mTaskCardView.init(task);
57             mTask = task;
58             mTaskCardView.setOnClickListener(this);
59         }
60 
61         @Override
onClick(View v)62         public void onClick(View v) {
63             try {
64                 if (mTaskCardView.isInDismissState()) {
65                     mTaskCardView.startDismissTaskAnimation(
66                             getRemoveAtListener(getAdapterPosition(), mTaskCardView.getTask()));
67                 } else {
68                     EventBus.getDefault().send(new LaunchTvTaskEvent(mTaskCardView, mTask,
69                             null, INVALID_STACK_ID));
70                 }
71             } catch (Exception e) {
72                 Log.e(TAG, v.getContext()
73                         .getString(R.string.recents_launch_error_message, mTask.title), e);
74             }
75 
76         }
77 
getRemoveAtListener(final int position, final Task task)78         private Animator.AnimatorListener getRemoveAtListener(final int position,
79                                                               final Task task) {
80             return new Animator.AnimatorListener() {
81 
82                 @Override
83                 public void onAnimationStart(Animator animation) { }
84 
85                 @Override
86                 public void onAnimationEnd(Animator animation) {
87                     removeTask(task);
88                     EventBus.getDefault().send(new DeleteTaskDataEvent(task));
89                 }
90 
91                 @Override
92                 public void onAnimationCancel(Animator animation) { }
93 
94                 @Override
95                 public void onAnimationRepeat(Animator animation) { }
96             };
97 
98         }
99     }
100 
101     public TaskStackHorizontalViewAdapter(List tasks) {
102         mTaskList = new ArrayList<Task>(tasks);
103     }
104 
105     public void setNewStackTasks(List tasks) {
106         mTaskList.clear();
107         mTaskList.addAll(tasks);
108         notifyDataSetChanged();
109     }
110 
111     @Override
112     public TaskStackHorizontalViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
113             int viewType) {
114         LayoutInflater inflater = LayoutInflater.from(parent.getContext());
115         ViewHolder viewHolder = new ViewHolder(
116                         inflater.inflate(R.layout.recents_tv_task_card_view, parent, false));
117         return viewHolder;
118     }
119 
120     @Override
121     public void onBindViewHolder(ViewHolder holder, int position) {
122         Task task = mTaskList.get(position);
123         // Retrives from caches, loading only if necessary
124         Recents.getTaskLoader().loadTaskData(task);
125         holder.init(task);
126     }
127 
128     @Override
129     public int getItemCount() {
130         return mTaskList.size();
131     }
132 
133     public void removeTask(Task task) {
134         int position = mTaskList.indexOf(task);
135         if (position >= 0) {
136             mTaskList.remove(position);
137             notifyItemRemoved(position);
138             if (mGridView != null) {
139                 mGridView.getStack().removeTask(task, AnimationProps.IMMEDIATE,
140                         false);
141             }
142         }
143     }
144 
145     public int getPositionOfTask(Task task) {
146         int position = mTaskList.indexOf(task);
147         return (position >= 0) ? position : 0;
148     }
149 
150 
151     public void setTaskStackHorizontalGridView(TaskStackHorizontalGridView gridView) {
152         mGridView = gridView;
153     }
154 
155     public void addTaskAt(Task task, int position) {
156         mTaskList.add(position, task);
157         notifyItemInserted(position);
158     }
159 }
160