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