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.text.format.DateFormat; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.TextView; 26 27 import com.android.tv.R; 28 29 import java.util.Date; 30 import java.util.concurrent.TimeUnit; 31 32 /** 33 * Adapts the time range from {@link ProgramManager} to the timeline header row of the program 34 * guide table. 35 */ 36 public class TimeListAdapter extends RecyclerView.Adapter<TimeListAdapter.TimeViewHolder> { 37 private static final long TIME_UNIT_MS = TimeUnit.MINUTES.toMillis(30); 38 private static int sRowHeaderOverlapping; 39 40 // Nearest half hour at or before the start time. 41 private long mStartUtcMs; 42 43 public TimeListAdapter(Resources res) { 44 if (sRowHeaderOverlapping == 0) { 45 sRowHeaderOverlapping = Math.abs(res.getDimensionPixelOffset( 46 R.dimen.program_guide_table_header_row_overlap)); 47 } 48 } 49 50 public void update(long startTimeMs) { 51 mStartUtcMs = startTimeMs; 52 notifyDataSetChanged(); 53 } 54 55 @Override 56 public int getItemCount() { 57 return Integer.MAX_VALUE; 58 } 59 60 @Override 61 public int getItemViewType(int position) { 62 return R.layout.program_guide_table_header_row_item; 63 } 64 65 @Override 66 public void onBindViewHolder(TimeViewHolder holder, int position) { 67 long startTime = mStartUtcMs + position * TIME_UNIT_MS; 68 long endTime = startTime + TIME_UNIT_MS; 69 70 View itemView = holder.itemView; 71 72 TextView textView = (TextView) itemView.findViewById(R.id.time); 73 String time = DateFormat.getTimeFormat(itemView.getContext()).format(new Date(startTime)); 74 textView.setText(time); 75 76 RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) itemView.getLayoutParams(); 77 lp.width = GuideUtils.convertMillisToPixel(startTime, endTime); 78 if (position == 0) { 79 // Adjust width for the first entry to make the item starts from the fading edge. 80 lp.setMarginStart(sRowHeaderOverlapping - lp.width / 2); 81 } else { 82 lp.setMarginStart(0); 83 } 84 itemView.setLayoutParams(lp); 85 } 86 87 @Override 88 public TimeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 89 View itemView = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false); 90 return new TimeViewHolder(itemView); 91 } 92 93 public static class TimeViewHolder extends RecyclerView.ViewHolder { 94 public TimeViewHolder(View itemView) { 95 super(itemView); 96 } 97 } 98 } 99