TimeListAdapter.java revision 816a4be1a0f34f6a48877c8afd3dbbca19eac435
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
17package com.android.tv.guide;
18
19import android.content.res.Resources;
20import android.support.v7.widget.RecyclerView;
21import android.text.format.DateFormat;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.TextView;
26
27import com.android.tv.R;
28
29import java.util.Date;
30import 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 */
36public 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