1 /* 2 * Copyright (C) 2018 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.dvr.ui.list; 18 19 import android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.TextView; 24 import androidx.leanback.app.DetailsFragment; 25 import androidx.leanback.widget.ClassPresenterSelector; 26 import com.android.tv.R; 27 import com.android.tv.TvSingletons; 28 import com.android.tv.dvr.DvrDataManager; 29 import com.android.tv.dvr.data.RecordedProgram; 30 import com.android.tv.dvr.data.ScheduledRecording; 31 import com.android.tv.dvr.ui.list.SchedulesHeaderRowPresenter.DateHeaderRowPresenter; 32 import com.android.tv.common.flags.UiFlags; 33 34 /** A fragment to show the DVR history. */ 35 public class DvrHistoryFragment extends DetailsFragment 36 implements DvrDataManager.ScheduledRecordingListener, 37 DvrDataManager.RecordedProgramListener { 38 39 private DvrHistoryRowAdapter mRowsAdapter; 40 private TextView mEmptyInfoScreenView; 41 private DvrDataManager mDvrDataManager; 42 43 @Override onCreate(Bundle savedInstanceState)44 public void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 ClassPresenterSelector presenterSelector = new ClassPresenterSelector(); 47 presenterSelector.addClassPresenter( 48 SchedulesHeaderRow.class, new DateHeaderRowPresenter(getContext())); 49 presenterSelector.addClassPresenter( 50 ScheduleRow.class, new ScheduleRowPresenter(getContext())); 51 TvSingletons singletons = TvSingletons.getSingletons(getContext()); 52 UiFlags uiFlags = singletons.getUiFlags(); 53 mDvrDataManager = singletons.getDvrDataManager(); 54 mRowsAdapter = 55 new DvrHistoryRowAdapter( 56 getContext(), 57 presenterSelector, 58 singletons.getClock(), 59 mDvrDataManager, 60 uiFlags); 61 setAdapter(mRowsAdapter); 62 mRowsAdapter.start(); 63 mDvrDataManager.addScheduledRecordingListener(this); 64 mDvrDataManager.addRecordedProgramListener(this); 65 mEmptyInfoScreenView = (TextView) getActivity().findViewById(R.id.empty_info_screen); 66 } 67 68 @Override onDestroy()69 public void onDestroy() { 70 mDvrDataManager.removeScheduledRecordingListener(this); 71 mDvrDataManager.removeRecordedProgramListener(this); 72 super.onDestroy(); 73 } 74 75 /** Shows the empty message. */ showEmptyMessage()76 void showEmptyMessage() { 77 mEmptyInfoScreenView.setText(R.string.dvr_history_empty_state); 78 if (mEmptyInfoScreenView.getVisibility() != View.VISIBLE) { 79 mEmptyInfoScreenView.setVisibility(View.VISIBLE); 80 } 81 } 82 83 /** Hides the empty message. */ hideEmptyMessage()84 void hideEmptyMessage() { 85 if (mEmptyInfoScreenView.getVisibility() == View.VISIBLE) { 86 mEmptyInfoScreenView.setVisibility(View.GONE); 87 } 88 } 89 90 @Override onInflateTitleView( LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)91 public View onInflateTitleView( 92 LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 93 // Workaround of b/31046014 94 return null; 95 } 96 97 @Override onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings)98 public void onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) { 99 if (mRowsAdapter != null) { 100 for (ScheduledRecording recording : scheduledRecordings) { 101 mRowsAdapter.onScheduledRecordingAdded(recording); 102 } 103 if (mRowsAdapter.size() > 0) { 104 hideEmptyMessage(); 105 } 106 } 107 } 108 109 @Override onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings)110 public void onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) { 111 if (mRowsAdapter != null) { 112 for (ScheduledRecording recording : scheduledRecordings) { 113 mRowsAdapter.onScheduledRecordingRemoved(recording); 114 } 115 if (mRowsAdapter.size() == 0) { 116 showEmptyMessage(); 117 } 118 } 119 } 120 121 @Override onScheduledRecordingStatusChanged(ScheduledRecording... scheduledRecordings)122 public void onScheduledRecordingStatusChanged(ScheduledRecording... scheduledRecordings) { 123 if (mRowsAdapter != null) { 124 for (ScheduledRecording recording : scheduledRecordings) { 125 mRowsAdapter.onScheduledRecordingUpdated(recording); 126 } 127 if (mRowsAdapter.size() == 0) { 128 showEmptyMessage(); 129 } else { 130 hideEmptyMessage(); 131 } 132 } 133 } 134 135 @Override onRecordedProgramsAdded(RecordedProgram... recordedPrograms)136 public void onRecordedProgramsAdded(RecordedProgram... recordedPrograms) { 137 if (mRowsAdapter != null) { 138 for (RecordedProgram p : recordedPrograms) { 139 mRowsAdapter.onScheduledRecordingAdded(p); 140 } 141 if (mRowsAdapter.size() > 0) { 142 hideEmptyMessage(); 143 } 144 } 145 } 146 147 @Override onRecordedProgramsChanged(RecordedProgram... recordedPrograms)148 public void onRecordedProgramsChanged(RecordedProgram... recordedPrograms) { 149 if (mRowsAdapter != null) { 150 for (RecordedProgram program : recordedPrograms) { 151 mRowsAdapter.onScheduledRecordingUpdated(program); 152 } 153 if (mRowsAdapter.size() == 0) { 154 showEmptyMessage(); 155 } else { 156 hideEmptyMessage(); 157 } 158 } 159 } 160 161 @Override onRecordedProgramsRemoved(RecordedProgram... recordedPrograms)162 public void onRecordedProgramsRemoved(RecordedProgram... recordedPrograms) { 163 if (mRowsAdapter != null) { 164 for (RecordedProgram p : recordedPrograms) { 165 mRowsAdapter.onScheduledRecordingRemoved(p); 166 } 167 if (mRowsAdapter.size() == 0) { 168 showEmptyMessage(); 169 } 170 } 171 } 172 } 173