1 /* 2 * Copyright (C) 2016 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.annotation.SuppressLint; 20 import android.annotation.TargetApi; 21 import android.app.AlertDialog; 22 import android.app.Dialog; 23 import android.os.Build.VERSION_CODES; 24 import android.os.Bundle; 25 import android.support.annotation.NonNull; 26 import android.text.TextUtils; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.widget.ArrayAdapter; 31 import android.widget.ListView; 32 import android.widget.TextView; 33 34 import com.android.tv.ApplicationSingletons; 35 import com.android.tv.R; 36 import com.android.tv.TvApplication; 37 import com.android.tv.data.Channel; 38 import com.android.tv.data.ChannelDataManager; 39 import com.android.tv.dvr.DvrDataManager; 40 import com.android.tv.dvr.data.ScheduledRecording; 41 import com.android.tv.dvr.data.ScheduledRecording.RecordingState; 42 import com.android.tv.dvr.ui.DvrUiHelper; 43 import com.android.tv.util.Utils; 44 45 import java.util.ArrayList; 46 import java.util.List; 47 48 /** 49 * Displays the DVR history. 50 */ 51 @TargetApi(VERSION_CODES.N) 52 public class DvrHistoryDialogFragment extends SafeDismissDialogFragment { 53 public static final String DIALOG_TAG = DvrHistoryDialogFragment.class.getSimpleName(); 54 55 private static final String TRACKER_LABEL = "DVR history"; 56 private final List<ScheduledRecording> mSchedules = new ArrayList<>(); 57 58 @Override onCreateDialog(Bundle savedInstanceState)59 public Dialog onCreateDialog(Bundle savedInstanceState) { 60 ApplicationSingletons singletons = TvApplication.getSingletons(getContext()); 61 DvrDataManager dataManager = singletons.getDvrDataManager(); 62 ChannelDataManager channelDataManager = singletons.getChannelDataManager(); 63 for (ScheduledRecording schedule : dataManager.getAllScheduledRecordings()) { 64 if (!schedule.isInProgress() && !schedule.isNotStarted()) { 65 mSchedules.add(schedule); 66 } 67 } 68 mSchedules.sort(ScheduledRecording.START_TIME_COMPARATOR.reversed()); 69 LayoutInflater inflater = LayoutInflater.from(getContext()); 70 ArrayAdapter adapter = new ArrayAdapter<ScheduledRecording>(getContext(), 71 R.layout.list_item_dvr_history, ScheduledRecording.toArray(mSchedules)) { 72 @NonNull 73 @Override 74 public View getView(int position, View convertView, ViewGroup parent) { 75 View view = inflater.inflate(R.layout.list_item_dvr_history, parent, false); 76 ScheduledRecording schedule = mSchedules.get(position); 77 setText(view, R.id.state, getStateString(schedule.getState())); 78 setText(view, R.id.schedule_time, getRecordingTimeText(schedule)); 79 setText(view, R.id.program_title, DvrUiHelper.getStyledTitleWithEpisodeNumber( 80 getContext(), schedule, 0)); 81 setText(view, R.id.channel_name, getChannelNameText(schedule)); 82 return view; 83 } 84 85 private void setText(View view, int id, CharSequence text) { 86 ((TextView) view.findViewById(id)).setText(text); 87 } 88 89 private void setText(View view, int id, int text) { 90 ((TextView) view.findViewById(id)).setText(text); 91 } 92 93 @SuppressLint("SwitchIntDef") 94 private int getStateString(@RecordingState int state) { 95 switch (state) { 96 case ScheduledRecording.STATE_RECORDING_CLIPPED: 97 return R.string.dvr_history_dialog_state_clip; 98 case ScheduledRecording.STATE_RECORDING_FAILED: 99 return R.string.dvr_history_dialog_state_fail; 100 case ScheduledRecording.STATE_RECORDING_FINISHED: 101 return R.string.dvr_history_dialog_state_success; 102 default: 103 break; 104 } 105 return 0; 106 } 107 108 private String getChannelNameText(ScheduledRecording schedule) { 109 Channel channel = channelDataManager.getChannel(schedule.getChannelId()); 110 return channel == null ? null : 111 TextUtils.isEmpty(channel.getDisplayName()) ? channel.getDisplayNumber() : 112 channel.getDisplayName().trim() + " " + channel.getDisplayNumber(); 113 } 114 115 private String getRecordingTimeText(ScheduledRecording schedule) { 116 return Utils.getDurationString(getContext(), schedule.getStartTimeMs(), 117 schedule.getEndTimeMs(), true, true, true, 0); 118 } 119 }; 120 ListView listView = new ListView(getActivity()); 121 listView.setAdapter(adapter); 122 return new AlertDialog.Builder(getActivity()).setTitle(R.string.dvr_history_dialog_title) 123 .setView(listView).create(); 124 } 125 126 @Override getTrackerLabel()127 public String getTrackerLabel() { 128 return TRACKER_LABEL; 129 } 130 } 131