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.dvr.ui.list;
18
19import android.app.Activity;
20import android.app.ProgressDialog;
21import android.os.Bundle;
22import android.support.annotation.IntDef;
23
24import com.android.tv.R;
25import com.android.tv.TvApplication;
26import com.android.tv.data.Program;
27import com.android.tv.dvr.data.SeriesRecording;
28import com.android.tv.dvr.provider.EpisodicProgramLoadTask;
29import com.android.tv.dvr.recorder.SeriesRecordingScheduler;
30import com.android.tv.dvr.ui.BigArguments;
31
32import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
34import java.util.Collections;
35import java.util.List;
36
37/**
38 * Activity to show the list of recording schedules.
39 */
40public class DvrSchedulesActivity extends Activity {
41    /**
42     * The key for the type of the schedules which will be listed in the list. The type of the value
43     * should be {@link ScheduleListType}.
44     */
45    public static final String KEY_SCHEDULES_TYPE = "schedules_type";
46
47    @Retention(RetentionPolicy.SOURCE)
48    @IntDef({TYPE_FULL_SCHEDULE, TYPE_SERIES_SCHEDULE})
49    public @interface ScheduleListType {}
50    /**
51     * A type which means the activity will display the full scheduled recordings.
52     */
53    public static final int TYPE_FULL_SCHEDULE = 0;
54    /**
55     * A type which means the activity will display a scheduled recording list of a series
56     * recording.
57     */
58    public static final int TYPE_SERIES_SCHEDULE = 1;
59
60    @Override
61    public void onCreate(final Bundle savedInstanceState) {
62        TvApplication.setCurrentRunningProcess(this, true);
63        // Pass null to prevent automatically re-creating fragments
64        super.onCreate(null);
65        setContentView(R.layout.activity_dvr_schedules);
66        int scheduleType = getIntent().getIntExtra(KEY_SCHEDULES_TYPE, TYPE_FULL_SCHEDULE);
67        if (scheduleType == TYPE_FULL_SCHEDULE) {
68            DvrSchedulesFragment schedulesFragment = new DvrSchedulesFragment();
69            schedulesFragment.setArguments(getIntent().getExtras());
70            getFragmentManager().beginTransaction().add(
71                    R.id.fragment_container, schedulesFragment).commit();
72        } else if (scheduleType == TYPE_SERIES_SCHEDULE) {
73            if (BigArguments.getArgument(DvrSeriesSchedulesFragment
74                    .SERIES_SCHEDULES_KEY_SERIES_PROGRAMS) != null) {
75                // The programs will be passed to the DvrSeriesSchedulesFragment, so don't need
76                // to reset the BigArguments.
77                showDvrSeriesSchedulesFragment(getIntent().getExtras());
78            } else {
79                final ProgressDialog dialog = ProgressDialog.show(this, null, getString(
80                        R.string.dvr_series_progress_message_reading_programs));
81                SeriesRecording seriesRecording = getIntent().getExtras()
82                        .getParcelable(DvrSeriesSchedulesFragment
83                                .SERIES_SCHEDULES_KEY_SERIES_RECORDING);
84                // To get programs faster, hold the update of the series schedules.
85                SeriesRecordingScheduler.getInstance(this).pauseUpdate();
86                new EpisodicProgramLoadTask(this, Collections.singletonList(seriesRecording)) {
87                    @Override
88                    protected void onPostExecute(List<Program> programs) {
89                        SeriesRecordingScheduler.getInstance(DvrSchedulesActivity.this)
90                                .resumeUpdate();
91                        dialog.dismiss();
92                        Bundle args = getIntent().getExtras();
93                        BigArguments.reset();
94                        BigArguments.setArgument(
95                                DvrSeriesSchedulesFragment.SERIES_SCHEDULES_KEY_SERIES_PROGRAMS,
96                                programs == null ? Collections.EMPTY_LIST : programs);
97                        showDvrSeriesSchedulesFragment(args);
98                    }
99                }.setLoadCurrentProgram(true)
100                        .setLoadDisallowedProgram(true)
101                        .setLoadScheduledEpisode(true)
102                        .setIgnoreChannelOption(true)
103                        .execute();
104            }
105        } else {
106            finish();
107        }
108    }
109
110    private void showDvrSeriesSchedulesFragment(Bundle args) {
111        DvrSeriesSchedulesFragment schedulesFragment = new DvrSeriesSchedulesFragment();
112        schedulesFragment.setArguments(args);
113        getFragmentManager().beginTransaction().add(
114                R.id.fragment_container, schedulesFragment).commit();
115    }
116}
117