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 
17 package com.android.tv.dvr.ui;
18 
19 import android.app.Activity;
20 import android.support.annotation.CallSuper;
21 import android.support.v17.leanback.widget.Presenter;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 
25 import com.android.tv.dvr.DvrUiHelper;
26 
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Set;
30 
31 /**
32  * An abstract class to present DVR items in {@link RecordingCardView}, which is mainly used in
33  * {@link DvrBrowseFragment}. DVR items might include: {@link ScheduledRecording},
34  * {@link RecordedProgram}, and {@link SeriesRecording}.
35  */
36 public abstract class DvrItemPresenter extends Presenter {
37     private final Set<ViewHolder> mBoundViewHolders = new HashSet<>();
38     private final OnClickListener mOnClickListener = onCreateOnClickListener();
39 
40     @Override
41     @CallSuper
onBindViewHolder(ViewHolder viewHolder, Object o)42     public void onBindViewHolder(ViewHolder viewHolder, Object o) {
43         viewHolder.view.setTag(o);
44         viewHolder.view.setOnClickListener(mOnClickListener);
45         mBoundViewHolders.add(viewHolder);
46     }
47 
48     @Override
49     @CallSuper
onUnbindViewHolder(ViewHolder viewHolder)50     public void onUnbindViewHolder(ViewHolder viewHolder) {
51         mBoundViewHolders.remove(viewHolder);
52     }
53 
54     /**
55      * Unbinds all bound view holders.
56      */
unbindAllViewHolders()57     public void unbindAllViewHolders() {
58         // When browse fragments are destroyed, RecyclerView would not call presenters'
59         // onUnbindViewHolder(). We should handle it by ourselves to prevent resources leaks.
60         for (ViewHolder viewHolder : new HashSet<>(mBoundViewHolders)) {
61             onUnbindViewHolder(viewHolder);
62         }
63     }
64 
65     /**
66      * Creates {@link OnClickListener} for DVR library's card views.
67      */
onCreateOnClickListener()68     protected OnClickListener onCreateOnClickListener() {
69         return new OnClickListener() {
70             @Override
71             public void onClick(View view) {
72                 if (view instanceof RecordingCardView) {
73                     RecordingCardView v = (RecordingCardView) view;
74                     DvrUiHelper.startDetailsActivity((Activity) v.getContext(),
75                             v.getTag(), v.getImageView(), false);
76                 }
77             }
78         };
79     }
80 }