Home | History | Annotate | Download | only in guide
      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.res.Resources;
     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;
     25 
     26 import com.android.tv.R;
     27 import com.android.tv.data.Channel;
     28 import com.android.tv.guide.ProgramManager.TableEntriesUpdatedListener;
     29 import com.android.tv.guide.ProgramManager.TableEntry;
     30 
     31 /**
     32  * Adapts a program list for a specific channel from {@link ProgramManager} to a row of the program
     33  * guide table.
     34  */
     35 public class ProgramListAdapter extends RecyclerView.Adapter<ProgramListAdapter.ProgramViewHolder>
     36         implements TableEntriesUpdatedListener {
     37     private static final String TAG = "ProgramListAdapter";
     38     private static final boolean DEBUG = false;
     39 
     40     private final ProgramManager mProgramManager;
     41     private final int mChannelIndex;
     42     private final String mNoInfoProgramTitle;
     43     private final String mBlockedProgramTitle;
     44 
     45     private long mChannelId;
     46 
     47     public ProgramListAdapter(Resources res, ProgramManager programManager, int channelIndex) {
     48         setHasStableIds(true);
     49         mProgramManager = programManager;
     50         mChannelIndex = channelIndex;
     51         mNoInfoProgramTitle = res.getString(R.string.program_title_for_no_information);
     52         mBlockedProgramTitle = res.getString(R.string.program_title_for_blocked_channel);
     53         onTableEntriesUpdated();
     54     }
     55 
     56     @Override
     57     public void onTableEntriesUpdated() {
     58         Channel channel = mProgramManager.getChannel(mChannelIndex);
     59         if (channel == null) {
     60             // The channel has just been removed. Do nothing.
     61         } else {
     62             mChannelId = channel.getId();
     63             if (DEBUG) Log.d(TAG, "update for channel " + mChannelId);
     64             notifyDataSetChanged();
     65         }
     66     }
     67 
     68     public ProgramManager getProgramManager() {
     69         return mProgramManager;
     70     }
     71 
     72     @Override
     73     public int getItemCount() {
     74         return mProgramManager.getTableEntryCount(mChannelId);
     75     }
     76 
     77     @Override
     78     public int getItemViewType(int position) {
     79         return R.layout.program_guide_table_item;
     80     }
     81 
     82     @Override
     83     public long getItemId(int position) {
     84         return mProgramManager.getTableEntry(mChannelId, position).getId();
     85     }
     86 
     87     @Override
     88     public void onBindViewHolder(ProgramViewHolder holder, int position) {
     89         TableEntry tableEntry = mProgramManager.getTableEntry(mChannelId, position);
     90         String gapTitle = tableEntry.isBlocked() ? mBlockedProgramTitle : mNoInfoProgramTitle;
     91         holder.onBind(tableEntry, this.getProgramManager(), gapTitle);
     92     }
     93 
     94     @Override
     95     public void onViewRecycled(ProgramViewHolder holder) {
     96         holder.onUnbind();
     97     }
     98 
     99     @Override
    100     public ProgramViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    101         View itemView = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
    102         return new ProgramViewHolder(itemView);
    103     }
    104 
    105     public static class ProgramViewHolder extends RecyclerView.ViewHolder {
    106         // Should be called from main thread.
    107         public ProgramViewHolder(View itemView) {
    108             super(itemView);
    109         }
    110 
    111         public void onBind(TableEntry entry, ProgramManager programManager, String gapTitle) {
    112             if (DEBUG) {
    113                 Log.d(TAG, "onBind. View = " + itemView + ", Entry = " + entry);
    114             }
    115             ((ProgramItemView) itemView).setValues(entry, programManager.getSelectedGenreId(),
    116                     programManager.getFromUtcMillis(), programManager.getToUtcMillis(), gapTitle);
    117         }
    118 
    119         public void onUnbind() {
    120             ((ProgramItemView) itemView).clearValues();
    121         }
    122     }
    123 }
    124