aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dialog/RecentlyWatchedDialogFragment.java
blob: eb6940fb2c49e997b5fad0924688095f102de163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.media.tv.TvContract;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.android.tv.MainActivity;
import com.android.tv.R;
import com.android.tv.data.ChannelDataManager;
import com.android.tv.data.api.Channel;

/** Displays the watch history */
public class RecentlyWatchedDialogFragment extends SafeDismissDialogFragment
        implements LoaderManager.LoaderCallbacks<Cursor> {
    public static final String DIALOG_TAG = RecentlyWatchedDialogFragment.class.getSimpleName();

    private static final String EMPTY_STRING = "";
    private static final String TRACKER_LABEL = "Recently watched history";

    private SimpleCursorAdapter mAdapter;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        getLoaderManager().initLoader(0, null, this);

        final ChannelDataManager dataChannelManager =
                ((MainActivity) getActivity()).getChannelDataManager();

        String[] from = {
            TvContract.WatchedPrograms._ID,
            TvContract.WatchedPrograms.COLUMN_CHANNEL_ID,
            TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS,
            TvContract.WatchedPrograms.COLUMN_TITLE
        };
        int[] to = {
            R.id.watched_program_id,
            R.id.watched_program_channel_id,
            R.id.watched_program_watch_time,
            R.id.watched_program_title
        };
        mAdapter =
                new SimpleCursorAdapter(
                        getActivity(), R.layout.list_item_watched_program, null, from, to, 0);
        mAdapter.setViewBinder(
                new SimpleCursorAdapter.ViewBinder() {
                    @Override
                    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                        String name = cursor.getColumnName(columnIndex);
                        if (TvContract.WatchedPrograms.COLUMN_CHANNEL_ID.equals(name)) {
                            long channelId = cursor.getLong(columnIndex);
                            ((TextView) view).setText(String.valueOf(channelId));
                            // Update display number
                            String displayNumber;
                            Channel channel = dataChannelManager.getChannel(channelId);
                            if (channel == null) {
                                displayNumber = EMPTY_STRING;
                            } else {
                                displayNumber = channel.getDisplayNumber();
                            }
                            TextView displayNumberView =
                                    ((TextView)
                                            ((View) view.getParent())
                                                    .findViewById(
                                                            R.id.watched_program_channel_display_number));
                            displayNumberView.setText(displayNumber);
                            return true;
                        } else if (TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS
                                .equals(name)) {
                            long time = cursor.getLong(columnIndex);
                            CharSequence timeString =
                                    DateUtils.getRelativeTimeSpanString(
                                            time,
                                            System.currentTimeMillis(),
                                            DateUtils.SECOND_IN_MILLIS);
                            ((TextView) view).setText(String.valueOf(timeString));
                            return true;
                        }
                        return false;
                    }
                });

        ListView listView = new ListView(getActivity());
        listView.setAdapter(mAdapter);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        return builder.setTitle(R.string.recently_watched).setView(listView).create();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // If we have an adapter we should close its cursor, which we do by assigning a null cursor.
        if (mAdapter != null) {
            mAdapter.changeCursor(null);
        }
    }

    @Override
    public String getTrackerLabel() {
        return TRACKER_LABEL;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {
            TvContract.WatchedPrograms._ID,
            TvContract.WatchedPrograms.COLUMN_CHANNEL_ID,
            TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS,
            TvContract.WatchedPrograms.COLUMN_TITLE
        };
        return new CursorLoader(
                getActivity(),
                TvContract.WatchedPrograms.CONTENT_URI,
                projection,
                null,
                null,
                TvContract.WatchedPrograms._ID + " DESC");
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.changeCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursor) {
        mAdapter.changeCursor(null);
    }
}