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