1 /*
2  * Copyright (C) 2015 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.guide;
18 
19 import android.content.Context;
20 import android.support.annotation.MainThread;
21 import androidx.recyclerview.widget.RecyclerView;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.TextView;
27 import com.android.tv.R;
28 import com.android.tv.data.GenreItems;
29 import java.util.List;
30 
31 /** Adapts the genre items obtained from {@link GenreItems} to the program guide side panel. */
32 class GenreListAdapter extends RecyclerView.Adapter<GenreListAdapter.GenreRowHolder> {
33     private static final String TAG = "GenreListAdapter";
34     private static final boolean DEBUG = false;
35 
36     private final Context mContext;
37     private final ProgramManager mProgramManager;
38     private final ProgramGuide mProgramGuide;
39     private String[] mGenreLabels;
40 
GenreListAdapter(Context context, ProgramManager programManager, ProgramGuide guide)41     GenreListAdapter(Context context, ProgramManager programManager, ProgramGuide guide) {
42         mContext = context;
43         mProgramManager = programManager;
44         mProgramManager.addListener(
45                 new ProgramManager.ListenerAdapter() {
46                     @Override
47                     public void onGenresUpdated() {
48                         mGenreLabels = GenreItems.getLabels(mContext);
49                         notifyDataSetChanged();
50                     }
51                 });
52         mProgramGuide = guide;
53     }
54 
55     @Override
getItemCount()56     public int getItemCount() {
57         List<Integer> filteredGenreIds = mProgramManager.getFilteredGenreIds();
58         if (filteredGenreIds == null) {
59             // Genre item would be available after program manager builds genre filter.
60             return 0;
61         }
62         return filteredGenreIds.size();
63     }
64 
65     @Override
getItemViewType(int position)66     public int getItemViewType(int position) {
67         return R.layout.program_guide_side_panel_row;
68     }
69 
70     @Override
onBindViewHolder(GenreRowHolder holder, int position)71     public void onBindViewHolder(GenreRowHolder holder, int position) {
72         List<Integer> filteredGenreIds = mProgramManager.getFilteredGenreIds();
73         int genreId = filteredGenreIds.get(position);
74         holder.onBind(genreId, mGenreLabels[genreId]);
75     }
76 
77     @Override
onCreateViewHolder(ViewGroup parent, int viewType)78     public GenreRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
79         View itemView = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
80         itemView.addOnAttachStateChangeListener(
81                 new View.OnAttachStateChangeListener() {
82                     @Override
83                     public void onViewAttachedToWindow(View view) {
84                         // Animation is not meaningful now, skip it.
85                         view.getStateListAnimator().jumpToCurrentState();
86                     }
87 
88                     @Override
89                     public void onViewDetachedFromWindow(View view) {
90                         // Do nothing
91                     }
92                 });
93         return new GenreRowHolder(itemView, mProgramGuide);
94     }
95 
96     static class GenreRowHolder extends RecyclerView.ViewHolder
97             implements View.OnFocusChangeListener {
98         private final ProgramGuide mProgramGuide;
99         private int mGenreId;
100 
101         @MainThread
GenreRowHolder(View itemView, ProgramGuide programGuide)102         GenreRowHolder(View itemView, ProgramGuide programGuide) {
103             super(itemView);
104             mProgramGuide = programGuide;
105         }
106 
onBind(int genreId, String genreLabel)107         public void onBind(int genreId, String genreLabel) {
108             mGenreId = genreId;
109 
110             TextView textView = (TextView) itemView;
111             textView.setText(genreLabel);
112 
113             itemView.setOnFocusChangeListener(this);
114         }
115 
116         @Override
onFocusChange(View view, boolean hasFocus)117         public void onFocusChange(View view, boolean hasFocus) {
118             if (hasFocus) {
119                 if (DEBUG) {
120                     Log.d(
121                             TAG,
122                             "onFocusChanged "
123                                     + ((TextView) view).getText()
124                                     + "("
125                                     + mGenreId
126                                     + ") hasFocus");
127                 }
128                 mProgramGuide.requestGenreChange(mGenreId);
129             }
130         }
131     }
132 }
133