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
17package com.android.tv.dialog;
18
19import android.annotation.SuppressLint;
20import android.annotation.TargetApi;
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.os.Build.VERSION_CODES;
24import android.os.Bundle;
25import android.support.annotation.NonNull;
26import android.text.TextUtils;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.ArrayAdapter;
31import android.widget.ListView;
32import android.widget.TextView;
33import com.android.tv.R;
34import com.android.tv.TvSingletons;
35import com.android.tv.data.ChannelDataManager;
36import com.android.tv.data.api.Channel;
37import com.android.tv.dvr.DvrDataManager;
38import com.android.tv.dvr.data.ScheduledRecording;
39import com.android.tv.dvr.data.ScheduledRecording.RecordingState;
40import com.android.tv.dvr.ui.DvrUiHelper;
41import com.android.tv.util.Utils;
42import java.util.ArrayList;
43import 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
48public 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
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
141    public String getTrackerLabel() {
142        return TRACKER_LABEL;
143    }
144}
145