1 /*
2  * Copyright (C) 2015 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.os.Bundle;
20 import android.support.annotation.IntDef;
21 import android.support.v17.leanback.app.BrowseFragment;
22 import android.support.v17.leanback.widget.ArrayObjectAdapter;
23 import android.support.v17.leanback.widget.ClassPresenterSelector;
24 import android.support.v17.leanback.widget.HeaderItem;
25 import android.support.v17.leanback.widget.ListRow;
26 import android.support.v17.leanback.widget.ListRowPresenter;
27 import android.support.v17.leanback.widget.ObjectAdapter;
28 import android.util.Log;
29 
30 import com.android.tv.R;
31 import com.android.tv.TvApplication;
32 import com.android.tv.common.recording.RecordedProgram;
33 import com.android.tv.dvr.DvrDataManager;
34 import com.android.tv.dvr.ScheduledRecording;
35 
36 import java.lang.annotation.Retention;
37 import java.lang.annotation.RetentionPolicy;
38 import java.util.LinkedHashMap;
39 
40 /**
41  * {@link BrowseFragment} for DVR functions.
42  */
43 public class DvrBrowseFragment extends BrowseFragment {
44     private static final String TAG = "DvrBrowseFragment";
45     private static final boolean DEBUG = false;
46 
47     private ScheduledRecordingsAdapter mRecordingsInProgressAdapter;
48     private ScheduledRecordingsAdapter mRecordingsNotStatedAdapter;
49     private RecordedProgramsAdapter mRecordedProgramsAdapter;
50 
51     @IntDef({DVR_CURRENT_RECORDINGS, DVR_SCHEDULED_RECORDINGS, DVR_RECORDED_PROGRAMS, DVR_SETTINGS})
52     @Retention(RetentionPolicy.SOURCE)
53     public @interface DVR_HEADERS_MODE {}
54     public static final int DVR_CURRENT_RECORDINGS = 0;
55     public static final int DVR_SCHEDULED_RECORDINGS = 1;
56     public static final int DVR_RECORDED_PROGRAMS = 2;
57     public static final int DVR_SETTINGS = 3;
58 
59     private static final LinkedHashMap<Integer, Integer> sHeaders =
60             new LinkedHashMap<Integer, Integer>() {{
61         put(DVR_CURRENT_RECORDINGS, R.string.dvr_main_current_recordings);
62         put(DVR_SCHEDULED_RECORDINGS, R.string.dvr_main_scheduled_recordings);
63         put(DVR_RECORDED_PROGRAMS, R.string.dvr_main_recorded_programs);
64         /* put(DVR_SETTINGS, R.string.dvr_main_settings); */ // TODO: Temporarily remove it for DP.
65     }};
66 
67     private DvrDataManager mDvrDataManager;
68     private ArrayObjectAdapter mRowsAdapter;
69 
70     @Override
onCreate(Bundle savedInstanceState)71     public void onCreate(Bundle savedInstanceState) {
72         if (DEBUG) Log.d(TAG, "onCreate");
73         super.onCreate(savedInstanceState);
74         mDvrDataManager = TvApplication.getSingletons(getContext()).getDvrDataManager();
75         setupUiElements();
76         setupAdapters();
77         mRecordingsInProgressAdapter.start();
78         mRecordingsNotStatedAdapter.start();
79         mRecordedProgramsAdapter.start();
80         initRows();
81         prepareEntranceTransition();
82         startEntranceTransition();
83     }
84 
85     @Override
onStart()86     public void onStart() {
87         if (DEBUG) Log.d(TAG, "onStart");
88         super.onStart();
89         // TODO: It's a workaround for a bug that a progress bar isn't hidden.
90         // We need to remove it later.
91         getProgressBarManager().disableProgressBar();
92     }
93 
94     @Override
onDestroy()95     public void onDestroy() {
96         if (DEBUG) Log.d(TAG, "onDestroy");
97         mRecordingsInProgressAdapter.stop();
98         mRecordingsNotStatedAdapter.stop();
99         mRecordedProgramsAdapter.stop();
100         super.onDestroy();
101     }
102 
setupUiElements()103     private void setupUiElements() {
104         setHeadersState(HEADERS_ENABLED);
105         setHeadersTransitionOnBackEnabled(false);
106     }
107 
setupAdapters()108     private void setupAdapters() {
109         mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
110         setAdapter(mRowsAdapter);
111         ClassPresenterSelector presenterSelector = new ClassPresenterSelector();
112         EmptyItemPresenter emptyItemPresenter = new EmptyItemPresenter(this);
113         ScheduledRecordingPresenter scheduledRecordingPresenter = new ScheduledRecordingPresenter(
114                 getContext());
115         RecordedProgramPresenter recordedProgramPresenter = new RecordedProgramPresenter(
116                 getContext());
117         presenterSelector.addClassPresenter(ScheduledRecording.class, scheduledRecordingPresenter);
118         presenterSelector.addClassPresenter(RecordedProgram.class, recordedProgramPresenter);
119         presenterSelector.addClassPresenter(EmptyHolder.class, emptyItemPresenter);
120         mRecordingsInProgressAdapter = new ScheduledRecordingsAdapter(mDvrDataManager,
121                 ScheduledRecording.STATE_RECORDING_IN_PROGRESS, presenterSelector);
122         mRecordingsNotStatedAdapter = new ScheduledRecordingsAdapter(mDvrDataManager,
123                 ScheduledRecording.STATE_RECORDING_NOT_STARTED, presenterSelector);
124         mRecordedProgramsAdapter = new RecordedProgramsAdapter(mDvrDataManager, presenterSelector);
125     }
126 
initRows()127     private void initRows() {
128         mRowsAdapter.clear();
129         for (@DVR_HEADERS_MODE int i : sHeaders.keySet()) {
130             HeaderItem gridHeader = new HeaderItem(i, getContext().getString(sHeaders.get(i)));
131             ObjectAdapter gridRowAdapter = null;
132             switch (i) {
133                 case DVR_CURRENT_RECORDINGS: {
134                     gridRowAdapter = mRecordingsInProgressAdapter;
135                     break;
136                 }
137                 case DVR_SCHEDULED_RECORDINGS: {
138                     gridRowAdapter = mRecordingsNotStatedAdapter;
139                 }
140                     break;
141                 case DVR_RECORDED_PROGRAMS: {
142                     gridRowAdapter = mRecordedProgramsAdapter;
143                 }
144                     break;
145                 case DVR_SETTINGS:
146                     gridRowAdapter = new ArrayObjectAdapter(new EmptyItemPresenter(this));
147                     // TODO: provide setup rows.
148                     break;
149             }
150             if (gridRowAdapter != null) {
151                 mRowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));
152             }
153         }
154     }
155 }
156