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.dialog;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.LoaderManager;
22 import android.content.CursorLoader;
23 import android.content.Loader;
24 import android.database.Cursor;
25 import android.media.tv.TvContract;
26 import android.os.Bundle;
27 import android.text.format.DateUtils;
28 import android.view.View;
29 import android.widget.ListView;
30 import android.widget.SimpleCursorAdapter;
31 import android.widget.TextView;
32 import com.android.tv.MainActivity;
33 import com.android.tv.R;
34 import com.android.tv.data.ChannelDataManager;
35 import com.android.tv.data.api.Channel;
36 
37 /** Displays the watch history */
38 public class RecentlyWatchedDialogFragment extends SafeDismissDialogFragment
39         implements LoaderManager.LoaderCallbacks<Cursor> {
40     public static final String DIALOG_TAG = RecentlyWatchedDialogFragment.class.getSimpleName();
41 
42     private static final String EMPTY_STRING = "";
43     private static final String TRACKER_LABEL = "Recently watched history";
44 
45     private SimpleCursorAdapter mAdapter;
46 
47     @Override
onCreateDialog(Bundle savedInstanceState)48     public Dialog onCreateDialog(Bundle savedInstanceState) {
49         getLoaderManager().initLoader(0, null, this);
50 
51         final ChannelDataManager dataChannelManager =
52                 ((MainActivity) getActivity()).getChannelDataManager();
53 
54         String[] from = {
55             TvContract.WatchedPrograms._ID,
56             TvContract.WatchedPrograms.COLUMN_CHANNEL_ID,
57             TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS,
58             TvContract.WatchedPrograms.COLUMN_TITLE
59         };
60         int[] to = {
61             R.id.watched_program_id,
62             R.id.watched_program_channel_id,
63             R.id.watched_program_watch_time,
64             R.id.watched_program_title
65         };
66         mAdapter =
67                 new SimpleCursorAdapter(
68                         getActivity(), R.layout.list_item_watched_program, null, from, to, 0);
69         mAdapter.setViewBinder(
70                 new SimpleCursorAdapter.ViewBinder() {
71                     @Override
72                     public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
73                         String name = cursor.getColumnName(columnIndex);
74                         if (TvContract.WatchedPrograms.COLUMN_CHANNEL_ID.equals(name)) {
75                             long channelId = cursor.getLong(columnIndex);
76                             ((TextView) view).setText(String.valueOf(channelId));
77                             // Update display number
78                             String displayNumber;
79                             Channel channel = dataChannelManager.getChannel(channelId);
80                             if (channel == null) {
81                                 displayNumber = EMPTY_STRING;
82                             } else {
83                                 displayNumber = channel.getDisplayNumber();
84                             }
85                             TextView displayNumberView =
86                                     ((TextView)
87                                             ((View) view.getParent())
88                                                     .findViewById(
89                                                             R.id.watched_program_channel_display_number));
90                             displayNumberView.setText(displayNumber);
91                             return true;
92                         } else if (TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS
93                                 .equals(name)) {
94                             long time = cursor.getLong(columnIndex);
95                             CharSequence timeString =
96                                     DateUtils.getRelativeTimeSpanString(
97                                             time,
98                                             System.currentTimeMillis(),
99                                             DateUtils.SECOND_IN_MILLIS);
100                             ((TextView) view).setText(String.valueOf(timeString));
101                             return true;
102                         }
103                         return false;
104                     }
105                 });
106 
107         ListView listView = new ListView(getActivity());
108         listView.setAdapter(mAdapter);
109 
110         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
111         return builder.setTitle(R.string.recently_watched).setView(listView).create();
112     }
113 
114     @Override
onDestroy()115     public void onDestroy() {
116         super.onDestroy();
117         // If we have an adapter we should close its cursor, which we do by assigning a null cursor.
118         if (mAdapter != null) {
119             mAdapter.changeCursor(null);
120         }
121     }
122 
123     @Override
getTrackerLabel()124     public String getTrackerLabel() {
125         return TRACKER_LABEL;
126     }
127 
128     @Override
onCreateLoader(int id, Bundle args)129     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
130         String[] projection = {
131             TvContract.WatchedPrograms._ID,
132             TvContract.WatchedPrograms.COLUMN_CHANNEL_ID,
133             TvContract.WatchedPrograms.COLUMN_WATCH_START_TIME_UTC_MILLIS,
134             TvContract.WatchedPrograms.COLUMN_TITLE
135         };
136         return new CursorLoader(
137                 getActivity(),
138                 TvContract.WatchedPrograms.CONTENT_URI,
139                 projection,
140                 null,
141                 null,
142                 TvContract.WatchedPrograms._ID + " DESC");
143     }
144 
145     @Override
onLoadFinished(Loader<Cursor> loader, Cursor cursor)146     public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
147         mAdapter.changeCursor(cursor);
148     }
149 
150     @Override
onLoaderReset(Loader<Cursor> cursor)151     public void onLoaderReset(Loader<Cursor> cursor) {
152         mAdapter.changeCursor(null);
153     }
154 }
155