• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.dialog;
18 
19 import android.annotation.SuppressLint;
20 import android.annotation.TargetApi;
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.os.Build.VERSION_CODES;
24 import android.os.Bundle;
25 import android.support.annotation.NonNull;
26 import android.text.TextUtils;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ArrayAdapter;
31 import android.widget.ListView;
32 import android.widget.TextView;
33 import com.android.tv.R;
34 import com.android.tv.TvSingletons;
35 import com.android.tv.data.ChannelDataManager;
36 import com.android.tv.data.api.Channel;
37 import com.android.tv.dvr.DvrDataManager;
38 import com.android.tv.dvr.data.ScheduledRecording;
39 import com.android.tv.dvr.data.ScheduledRecording.RecordingState;
40 import com.android.tv.dvr.ui.DvrUiHelper;
41 import com.android.tv.util.Utils;
42 import java.util.ArrayList;
43 import java.util.List;
44 
45 /** Displays the DVR history. */
46 @TargetApi(VERSION_CODES.N)
47 @SuppressWarnings("AndroidApiChecker") // TODO(b/32513850) remove when error prone is updated
48 public class DvrHistoryDialogFragment extends SafeDismissDialogFragment {
49     public static final String DIALOG_TAG = DvrHistoryDialogFragment.class.getSimpleName();
50 
51     private static final String TRACKER_LABEL = "DVR history";
52     private final List<ScheduledRecording> mSchedules = new ArrayList<>();
53 
54     @Override
onCreateDialog(Bundle savedInstanceState)55     public Dialog onCreateDialog(Bundle savedInstanceState) {
56         TvSingletons singletons = TvSingletons.getSingletons(getContext());
57         DvrDataManager dataManager = singletons.getDvrDataManager();
58         ChannelDataManager channelDataManager = singletons.getChannelDataManager();
59         for (ScheduledRecording schedule : dataManager.getAllScheduledRecordings()) {
60             if (!schedule.isInProgress() && !schedule.isNotStarted()) {
61                 mSchedules.add(schedule);
62             }
63         }
64         mSchedules.sort(ScheduledRecording.START_TIME_COMPARATOR.reversed());
65         LayoutInflater inflater = LayoutInflater.from(getContext());
66         ArrayAdapter adapter =
67                 new ArrayAdapter<ScheduledRecording>(
68                         getContext(),
69                         R.layout.list_item_dvr_history,
70                         ScheduledRecording.toArray(mSchedules)) {
71                     @NonNull
72                     @Override
73                     public View getView(int position, View convertView, ViewGroup parent) {
74                         View view = inflater.inflate(R.layout.list_item_dvr_history, parent, false);
75                         ScheduledRecording schedule = mSchedules.get(position);
76                         setText(view, R.id.state, getStateString(schedule.getState()));
77                         setText(view, R.id.schedule_time, getRecordingTimeText(schedule));
78                         setText(
79                                 view,
80                                 R.id.program_title,
81                                 DvrUiHelper.getStyledTitleWithEpisodeNumber(
82                                         getContext(), schedule, 0));
83                         setText(view, R.id.channel_name, getChannelNameText(schedule));
84                         return view;
85                     }
86 
87                     private void setText(View view, int id, CharSequence text) {
88                         ((TextView) view.findViewById(id)).setText(text);
89                     }
90 
91                     private void setText(View view, int id, int text) {
92                         ((TextView) view.findViewById(id)).setText(text);
93                     }
94 
95                     @SuppressLint("SwitchIntDef")
96                     private int getStateString(@RecordingState int state) {
97                         switch (state) {
98                             case ScheduledRecording.STATE_RECORDING_CLIPPED:
99                                 return R.string.dvr_history_dialog_state_clip;
100                             case ScheduledRecording.STATE_RECORDING_FAILED:
101                                 return R.string.dvr_history_dialog_state_fail;
102                             case ScheduledRecording.STATE_RECORDING_FINISHED:
103                                 return R.string.dvr_history_dialog_state_success;
104                             default:
105                                 break;
106                         }
107                         return 0;
108                     }
109 
110                     private String getChannelNameText(ScheduledRecording schedule) {
111                         Channel channel = channelDataManager.getChannel(schedule.getChannelId());
112                         return channel == null
113                                 ? null
114                                 : TextUtils.isEmpty(channel.getDisplayName())
115                                         ? channel.getDisplayNumber()
116                                         : channel.getDisplayName().trim()
117                                                 + " "
118                                                 + channel.getDisplayNumber();
119                     }
120 
121                     private String getRecordingTimeText(ScheduledRecording schedule) {
122                         return Utils.getDurationString(
123                                 getContext(),
124                                 schedule.getStartTimeMs(),
125                                 schedule.getEndTimeMs(),
126                                 true,
127                                 true,
128                                 true,
129                                 0);
130                     }
131                 };
132         ListView listView = new ListView(getActivity());
133         listView.setAdapter(adapter);
134         return new AlertDialog.Builder(getActivity())
135                 .setTitle(R.string.dvr_history_dialog_title)
136                 .setView(listView)
137                 .create();
138     }
139 
140     @Override
getTrackerLabel()141     public String getTrackerLabel() {
142         return TRACKER_LABEL;
143     }
144 }
145