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