Home | History | Annotate | Download | only in ui
      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;
     18 
     19 import android.support.v17.leanback.widget.PresenterSelector;
     20 
     21 import com.android.tv.dvr.DvrDataManager;
     22 import com.android.tv.dvr.ScheduledRecording;
     23 
     24 /**
     25  * Adapter for {@link ScheduledRecording} filtered by
     26  * {@link com.android.tv.dvr.ScheduledRecording.RecordingState}.
     27  */
     28 final class ScheduledRecordingsAdapter extends SortedArrayAdapter<ScheduledRecording>
     29         implements DvrDataManager.ScheduledRecordingListener {
     30     private final int mState;
     31     private final DvrDataManager mDataManager;
     32 
     33     ScheduledRecordingsAdapter(DvrDataManager dataManager, int state,
     34             PresenterSelector presenterSelector) {
     35         super(presenterSelector, ScheduledRecording.START_TIME_THEN_PRIORITY_COMPARATOR);
     36         mDataManager = dataManager;
     37         mState = state;
     38     }
     39 
     40     public void start() {
     41         clear();
     42         switch (mState) {
     43             case ScheduledRecording.STATE_RECORDING_NOT_STARTED:
     44                 addAll(mDataManager.getNonStartedScheduledRecordings());
     45                 break;
     46             case ScheduledRecording.STATE_RECORDING_IN_PROGRESS:
     47                 addAll(mDataManager.getStartedRecordings());
     48                 break;
     49             default:
     50                 throw new IllegalStateException("Unknown recording state " + mState);
     51 
     52         }
     53         mDataManager.addScheduledRecordingListener(this);
     54     }
     55 
     56     public void stop() {
     57         mDataManager.removeScheduledRecordingListener(this);
     58     }
     59 
     60     @Override
     61     long getId(ScheduledRecording item) {
     62         return item.getId();
     63     }
     64 
     65     @Override  //DvrDataManager.ScheduledRecordingListener
     66     public void onScheduledRecordingAdded(ScheduledRecording scheduledRecording) {
     67         if (scheduledRecording.getState() == mState) {
     68             add(scheduledRecording);
     69         }
     70     }
     71 
     72     @Override  //DvrDataManager.ScheduledRecordingListener
     73     public void onScheduledRecordingRemoved(ScheduledRecording scheduledRecording) {
     74         remove(scheduledRecording);
     75     }
     76 
     77     @Override  //DvrDataManager.ScheduledRecordingListener
     78     public void onScheduledRecordingStatusChanged(ScheduledRecording scheduledRecording) {
     79         if (scheduledRecording.getState() == mState) {
     80             change(scheduledRecording);
     81         } else {
     82             remove(scheduledRecording);
     83         }
     84     }
     85 }
     86