aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dialog/DvrHistoryDialogFragment.java
blob: 2ed98b871a05fa3e529304a4a136f6de96c568d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.dialog;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.tv.ApplicationSingletons;
import com.android.tv.R;
import com.android.tv.TvApplication;
import com.android.tv.data.Channel;
import com.android.tv.data.ChannelDataManager;
import com.android.tv.dvr.DvrDataManager;
import com.android.tv.dvr.data.ScheduledRecording;
import com.android.tv.dvr.data.ScheduledRecording.RecordingState;
import com.android.tv.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Displays the DVR history.
 */
@TargetApi(VERSION_CODES.N)
public class DvrHistoryDialogFragment extends SafeDismissDialogFragment {
    public static final String DIALOG_TAG = DvrHistoryDialogFragment.class.getSimpleName();

    private static final String TRACKER_LABEL = "DVR history";
    private final List<ScheduledRecording> mSchedules = new ArrayList<>();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ApplicationSingletons singletons = TvApplication.getSingletons(getContext());
        DvrDataManager dataManager = singletons.getDvrDataManager();
        ChannelDataManager channelDataManager = singletons.getChannelDataManager();
        for (ScheduledRecording schedule : dataManager.getAllScheduledRecordings()) {
            if (!schedule.isInProgress() && !schedule.isNotStarted()) {
                mSchedules.add(schedule);
            }
        }
        mSchedules.sort(ScheduledRecording.START_TIME_COMPARATOR.reversed());
        LayoutInflater inflater = LayoutInflater.from(getContext());
        ArrayAdapter adapter = new ArrayAdapter<ScheduledRecording>(getContext(),
                R.layout.list_item_dvr_history, ScheduledRecording.toArray(mSchedules)) {
            @NonNull
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = inflater.inflate(R.layout.list_item_dvr_history, parent, false);
                ScheduledRecording schedule = mSchedules.get(position);
                setText(view, R.id.state, getStateString(schedule.getState()));
                setText(view, R.id.schedule_time, getRecordingTimeText(schedule));
                setText(view, R.id.program_title,
                        schedule.getProgramTitleWithEpisodeNumber(getContext()));
                setText(view, R.id.channel_name, getChannelNameText(schedule));
                return view;
            }

            private void setText(View view, int id, String text) {
                ((TextView) view.findViewById(id)).setText(text);
            }

            private void setText(View view, int id, int text) {
                ((TextView) view.findViewById(id)).setText(text);
            }

            @SuppressLint("SwitchIntDef")
            private int getStateString(@RecordingState int state) {
                switch (state) {
                    case ScheduledRecording.STATE_RECORDING_CLIPPED:
                        return R.string.dvr_history_dialog_state_clip;
                    case ScheduledRecording.STATE_RECORDING_FAILED:
                        return R.string.dvr_history_dialog_state_fail;
                    case ScheduledRecording.STATE_RECORDING_FINISHED:
                        return R.string.dvr_history_dialog_state_success;
                    default:
                        break;
                }
                return 0;
            }

            private String getChannelNameText(ScheduledRecording schedule) {
                Channel channel = channelDataManager.getChannel(schedule.getChannelId());
                return channel == null ? null :
                        TextUtils.isEmpty(channel.getDisplayName()) ? channel.getDisplayNumber() :
                                channel.getDisplayName().trim() + " " + channel.getDisplayNumber();
            }

            private String getRecordingTimeText(ScheduledRecording schedule) {
                return Utils.getDurationString(getContext(), schedule.getStartTimeMs(),
                        schedule.getEndTimeMs(), true, true, true, 0);
            }
        };
        ListView listView = new ListView(getActivity());
        listView.setAdapter(adapter);
        return new AlertDialog.Builder(getActivity()).setTitle(R.string.dvr_history_dialog_title)
                .setView(listView).create();
    }

    @Override
    public String getTrackerLabel() {
        return TRACKER_LABEL;
    }
}