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