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.os.Bundle;
20 import android.support.v17.leanback.app.DetailsFragment;
21 import android.support.v17.leanback.widget.ClassPresenterSelector;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.TextView;
26 
27 import com.android.tv.ApplicationSingletons;
28 import com.android.tv.R;
29 import com.android.tv.TvApplication;
30 import com.android.tv.dvr.DvrDataManager;
31 import com.android.tv.dvr.DvrScheduleManager;
32 import com.android.tv.dvr.ScheduledRecording;
33 
34 /**
35  * A  base fragment to show the list of schedule recordings.
36  */
37 public abstract class BaseDvrSchedulesFragment extends DetailsFragment
38         implements DvrDataManager.ScheduledRecordingListener,
39         DvrScheduleManager.OnConflictStateChangeListener {
40     /**
41      * The key for scheduled recording which has be selected in the list.
42      */
43     public static String SCHEDULES_KEY_SCHEDULED_RECORDING = "schedules_key_scheduled_recording";
44 
45     private ScheduleRowAdapter mRowsAdapter;
46     private TextView mEmptyInfoScreenView;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     public void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         ClassPresenterSelector presenterSelector = new ClassPresenterSelector();
52         presenterSelector.addClassPresenter(SchedulesHeaderRow.class, onCreateHeaderRowPresenter());
53         presenterSelector.addClassPresenter(ScheduleRow.class, onCreateRowPresenter());
54         mRowsAdapter = onCreateRowsAdapter(presenterSelector);
55         setAdapter(mRowsAdapter);
56         mRowsAdapter.start();
57         ApplicationSingletons singletons = TvApplication.getSingletons(getContext());
58         singletons.getDvrDataManager().addScheduledRecordingListener(this);
59         singletons.getDvrScheduleManager().addOnConflictStateChangeListener(this);
60         mEmptyInfoScreenView = (TextView) getActivity().findViewById(R.id.empty_info_screen);
61     }
62 
63     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)64     public View onCreateView(LayoutInflater inflater, ViewGroup container,
65             Bundle savedInstanceState) {
66         View view = super.onCreateView(inflater, container, savedInstanceState);
67         int firstItemPosition = getFirstItemPosition();
68         if (firstItemPosition != -1) {
69             getRowsFragment().setSelectedPosition(firstItemPosition, false);
70         }
71         return view;
72     }
73 
74     /**
75      * Returns rows adapter.
76      */
getRowsAdapter()77     protected ScheduleRowAdapter getRowsAdapter() {
78         return mRowsAdapter;
79     }
80 
81     /**
82      * Shows the empty message.
83      */
showEmptyMessage(int messageId)84     void showEmptyMessage(int messageId) {
85         mEmptyInfoScreenView.setText(messageId);
86         if (mEmptyInfoScreenView.getVisibility() != View.VISIBLE) {
87             mEmptyInfoScreenView.setVisibility(View.VISIBLE);
88         }
89     }
90 
91     /**
92      * Hides the empty message.
93      */
hideEmptyMessage()94     void hideEmptyMessage() {
95         if (mEmptyInfoScreenView.getVisibility() == View.VISIBLE) {
96             mEmptyInfoScreenView.setVisibility(View.GONE);
97         }
98     }
99 
100     @Override
onInflateTitleView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)101     public View onInflateTitleView(LayoutInflater inflater, ViewGroup parent,
102             Bundle savedInstanceState) {
103         // Workaround of b/31046014
104         return null;
105     }
106 
107     @Override
onDestroy()108     public void onDestroy() {
109         ApplicationSingletons singletons = TvApplication.getSingletons(getContext());
110         singletons.getDvrScheduleManager().removeOnConflictStateChangeListener(this);
111         singletons.getDvrDataManager().removeScheduledRecordingListener(this);
112         mRowsAdapter.stop();
113         super.onDestroy();
114     }
115 
116     /**
117      * Creates header row presenter.
118      */
onCreateHeaderRowPresenter()119     public abstract SchedulesHeaderRowPresenter onCreateHeaderRowPresenter();
120 
121     /**
122      * Creates rows presenter.
123      */
onCreateRowPresenter()124     public abstract ScheduleRowPresenter onCreateRowPresenter();
125 
126     /**
127      * Creates rows adapter.
128      */
onCreateRowsAdapter(ClassPresenterSelector presenterSelecor)129     public abstract ScheduleRowAdapter onCreateRowsAdapter(ClassPresenterSelector presenterSelecor);
130 
131     /**
132      * Gets the first focus position in schedules list.
133      */
getFirstItemPosition()134     protected int getFirstItemPosition() {
135         for (int i = 0; i < mRowsAdapter.size(); i++) {
136             if (mRowsAdapter.get(i) instanceof ScheduleRow) {
137                 return i;
138             }
139         }
140         return -1;
141     }
142 
143     @Override
onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings)144     public void onScheduledRecordingAdded(ScheduledRecording... scheduledRecordings) {
145         if (mRowsAdapter != null) {
146             for (ScheduledRecording recording : scheduledRecordings) {
147                 mRowsAdapter.onScheduledRecordingAdded(recording);
148             }
149         }
150     }
151 
152     @Override
onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings)153     public void onScheduledRecordingRemoved(ScheduledRecording... scheduledRecordings) {
154         if (mRowsAdapter != null) {
155             for (ScheduledRecording recording : scheduledRecordings) {
156                 mRowsAdapter.onScheduledRecordingRemoved(recording);
157             }
158         }
159     }
160 
161     @Override
onScheduledRecordingStatusChanged(ScheduledRecording... scheduledRecordings)162     public void onScheduledRecordingStatusChanged(ScheduledRecording... scheduledRecordings) {
163         if (mRowsAdapter != null) {
164             for (ScheduledRecording recording : scheduledRecordings) {
165                 mRowsAdapter.onScheduledRecordingUpdated(recording, false);
166             }
167         }
168     }
169 
170     @Override
onConflictStateChange(boolean conflict, ScheduledRecording... schedules)171     public void onConflictStateChange(boolean conflict, ScheduledRecording... schedules) {
172         if (mRowsAdapter != null) {
173             for (ScheduledRecording recording : schedules) {
174                 mRowsAdapter.onScheduledRecordingUpdated(recording, true);
175             }
176         }
177     }
178 }