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.dvr.ui.list; 18 19 import android.content.Context; 20 import android.support.annotation.Nullable; 21 import com.android.tv.common.SoftPreconditions; 22 import com.android.tv.dvr.data.ScheduledRecording; 23 import com.android.tv.dvr.ui.DvrUiHelper; 24 25 /** A class for schedule recording row. */ 26 class ScheduleRow { 27 private final SchedulesHeaderRow mHeaderRow; 28 @Nullable private ScheduledRecording mSchedule; 29 private boolean mStopRecordingRequested; 30 private boolean mStartRecordingRequested; 31 ScheduleRow(@ullable ScheduledRecording recording, SchedulesHeaderRow headerRow)32 public ScheduleRow(@Nullable ScheduledRecording recording, SchedulesHeaderRow headerRow) { 33 mSchedule = recording; 34 mHeaderRow = headerRow; 35 } 36 37 /** Gets which {@link SchedulesHeaderRow} this schedule row belongs to. */ getHeaderRow()38 public SchedulesHeaderRow getHeaderRow() { 39 return mHeaderRow; 40 } 41 42 /** Returns the recording schedule. */ 43 @Nullable getSchedule()44 public ScheduledRecording getSchedule() { 45 return mSchedule; 46 } 47 48 /** Checks if the stop recording has been requested or not. */ isStopRecordingRequested()49 public boolean isStopRecordingRequested() { 50 return mStopRecordingRequested; 51 } 52 53 /** Sets the flag of stop recording request. */ setStopRecordingRequested(boolean stopRecordingRequested)54 public void setStopRecordingRequested(boolean stopRecordingRequested) { 55 SoftPreconditions.checkState(!mStartRecordingRequested); 56 mStopRecordingRequested = stopRecordingRequested; 57 } 58 59 /** Checks if the start recording has been requested or not. */ isStartRecordingRequested()60 public boolean isStartRecordingRequested() { 61 return mStartRecordingRequested; 62 } 63 64 /** Sets the flag of start recording request. */ setStartRecordingRequested(boolean startRecordingRequested)65 public void setStartRecordingRequested(boolean startRecordingRequested) { 66 SoftPreconditions.checkState(!mStopRecordingRequested); 67 mStartRecordingRequested = startRecordingRequested; 68 } 69 70 /** Sets the recording schedule. */ setSchedule(@ullable ScheduledRecording schedule)71 public void setSchedule(@Nullable ScheduledRecording schedule) { 72 mSchedule = schedule; 73 } 74 75 /** Returns the channel ID. */ getChannelId()76 public long getChannelId() { 77 return mSchedule != null ? mSchedule.getChannelId() : -1; 78 } 79 80 /** Returns the start time. */ getStartTimeMs()81 public long getStartTimeMs() { 82 return mSchedule != null ? mSchedule.getStartTimeMs() : -1; 83 } 84 85 /** Returns the end time. */ getEndTimeMs()86 public long getEndTimeMs() { 87 return mSchedule != null ? mSchedule.getEndTimeMs() : -1; 88 } 89 90 /** Returns the duration. */ getDuration()91 public final long getDuration() { 92 return getEndTimeMs() - getStartTimeMs(); 93 } 94 95 /** Checks if the program is on air. */ isOnAir()96 public final boolean isOnAir() { 97 long currentTimeMs = System.currentTimeMillis(); 98 return getStartTimeMs() <= currentTimeMs && getEndTimeMs() > currentTimeMs; 99 } 100 101 /** Checks if the schedule is not started. */ isRecordingNotStarted()102 public final boolean isRecordingNotStarted() { 103 return mSchedule != null 104 && mSchedule.getState() == ScheduledRecording.STATE_RECORDING_NOT_STARTED; 105 } 106 107 /** Checks if the schedule is in progress. */ isRecordingInProgress()108 public final boolean isRecordingInProgress() { 109 return mSchedule != null 110 && mSchedule.getState() == ScheduledRecording.STATE_RECORDING_IN_PROGRESS; 111 } 112 113 /** Checks if the schedule is failed. */ isRecordingFailed()114 public final boolean isRecordingFailed() { 115 return mSchedule != null 116 && mSchedule.getState() == ScheduledRecording.STATE_RECORDING_FAILED; 117 } 118 119 /** Checks if the schedule has been canceled or not. */ isScheduleCanceled()120 public final boolean isScheduleCanceled() { 121 return mSchedule != null 122 && mSchedule.getState() == ScheduledRecording.STATE_RECORDING_CANCELED; 123 } 124 isRecordingFinished()125 public boolean isRecordingFinished() { 126 return mSchedule != null 127 && (mSchedule.getState() == ScheduledRecording.STATE_RECORDING_FAILED 128 || mSchedule.getState() == ScheduledRecording.STATE_RECORDING_CLIPPED 129 || mSchedule.getState() == ScheduledRecording.STATE_RECORDING_FINISHED); 130 } 131 hasRecordedProgram()132 public boolean hasRecordedProgram() { 133 return mSchedule != null 134 && mSchedule.getRecordedProgramId() != null 135 && mSchedule.getState() == ScheduledRecording.STATE_RECORDING_FINISHED; 136 } 137 138 /** Creates and returns the new schedule with the existing information. */ createNewScheduleBuilder()139 public ScheduledRecording.Builder createNewScheduleBuilder() { 140 return mSchedule != null ? ScheduledRecording.buildFrom(mSchedule) : null; 141 } 142 143 /** Returns the program title with episode number. */ getProgramTitleWithEpisodeNumber(Context context)144 public String getProgramTitleWithEpisodeNumber(Context context) { 145 return mSchedule != null 146 ? DvrUiHelper.getStyledTitleWithEpisodeNumber(context, mSchedule, 0).toString() 147 : null; 148 } 149 150 /** Returns the program title including the season/episode number. */ getEpisodeDisplayTitle(Context context)151 public String getEpisodeDisplayTitle(Context context) { 152 return mSchedule != null ? mSchedule.getEpisodeDisplayTitle(context) : null; 153 } 154 155 @Override toString()156 public String toString() { 157 return getClass().getSimpleName() 158 + "(schedule=" 159 + mSchedule 160 + ",stopRecordingRequested=" 161 + mStopRecordingRequested 162 + ",startRecordingRequested=" 163 + mStartRecordingRequested 164 + ")"; 165 } 166 167 /** Checks if the {@code schedule} is for the program or channel. */ matchSchedule(ScheduledRecording schedule)168 public boolean matchSchedule(ScheduledRecording schedule) { 169 if (mSchedule == null) { 170 return false; 171 } 172 if (mSchedule.getType() == ScheduledRecording.TYPE_TIMED) { 173 return mSchedule.getChannelId() == schedule.getChannelId() 174 && mSchedule.getStartTimeMs() == schedule.getStartTimeMs() 175 && mSchedule.getEndTimeMs() == schedule.getEndTimeMs(); 176 } else { 177 return mSchedule.getProgramId() == schedule.getProgramId(); 178 } 179 } 180 } 181