aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/DvrUiHelper.java
blob: c0d3b0c5e2a23c99dd32df7362c7eb5511558868 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 * Copyright (C) 2015 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.dvr;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.tv.TvInputManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.MainThread;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.tv.MainActivity;
import com.android.tv.R;
import com.android.tv.TvApplication;
import com.android.tv.common.SoftPreconditions;
import com.android.tv.data.Channel;
import com.android.tv.data.Program;
import com.android.tv.dvr.ui.DvrDetailsActivity;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrAlreadyRecordedDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrAlreadyScheduledDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrChannelRecordDurationOptionDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrChannelWatchConflictDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrInsufficientSpaceErrorDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrMissingStorageErrorDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrProgramConflictDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrScheduleDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrSmallSizedStorageErrorDialogFragment;
import com.android.tv.dvr.ui.DvrHalfSizedDialogFragment.DvrStopRecordingDialogFragment;
import com.android.tv.dvr.ui.DvrSchedulesActivity;
import com.android.tv.dvr.ui.DvrSeriesDeletionActivity;
import com.android.tv.dvr.ui.DvrSeriesScheduledDialogActivity;
import com.android.tv.dvr.ui.DvrSeriesSettingsActivity;
import com.android.tv.dvr.ui.DvrStopRecordingFragment;
import com.android.tv.dvr.ui.DvrStopSeriesRecordingDialogFragment;
import com.android.tv.dvr.ui.DvrStopSeriesRecordingFragment;
import com.android.tv.dvr.ui.HalfSizedDialogFragment;
import com.android.tv.dvr.ui.list.DvrSchedulesFragment;
import com.android.tv.dvr.ui.list.DvrSeriesSchedulesFragment;
import com.android.tv.util.Utils;

import java.util.Collections;
import java.util.List;

/**
 * A helper class for DVR UI.
 */
@MainThread
@TargetApi(Build.VERSION_CODES.N)
public class DvrUiHelper {
    /**
     * Handles the action to create the new schedule. It returns {@code true} if the schedule is
     * added and there's no additional UI, otherwise {@code false}.
     */
    public static boolean handleCreateSchedule(MainActivity activity, Program program) {
        if (program == null) {
            return false;
        }
        DvrManager dvrManager = TvApplication.getSingletons(activity).getDvrManager();
        if (!program.isEpisodic()) {
            // One time recording.
            dvrManager.addSchedule(program);
            if (!dvrManager.getConflictingSchedules(program).isEmpty()) {
                DvrUiHelper.showScheduleConflictDialog(activity, program);
                return false;
            }
        } else {
            SeriesRecording seriesRecording = dvrManager.getSeriesRecording(program);
            if (seriesRecording == null || seriesRecording.isStopped()) {
                DvrUiHelper.showScheduleDialog(activity, program);
                return false;
            } else {
                // Show recorded program rather than the schedule.
                RecordedProgram recordedProgram = dvrManager.getRecordedProgram(program.getTitle(),
                        program.getSeasonNumber(), program.getEpisodeNumber());
                if (recordedProgram != null) {
                    DvrUiHelper.showAlreadyRecordedDialog(activity, program);
                    return false;
                }
                ScheduledRecording duplicate = dvrManager.getScheduledRecording(program.getTitle(),
                        program.getSeasonNumber(), program.getEpisodeNumber());
                if (duplicate != null
                        && (duplicate.getState() == ScheduledRecording.STATE_RECORDING_NOT_STARTED
                        || duplicate.getState()
                        == ScheduledRecording.STATE_RECORDING_IN_PROGRESS)) {
                    DvrUiHelper.showAlreadyScheduleDialog(activity, program);
                    return false;
                }
                // Just add the schedule.
                dvrManager.addSchedule(program);
            }
        }
        return true;

    }

    /**
     * Checks if the storage status is good for recording and shows error messages if needed.
     *
     * @return true if the storage status is fine to be recorded for {@code inputId}.
     */
    public static boolean checkStorageStatusAndShowErrorMessage(Activity activity, String inputId) {
        if (!Utils.isBundledInput(inputId)) {
            return true;
        }
        DvrStorageStatusManager dvrStorageStatusManager =
                TvApplication.getSingletons(activity).getDvrStorageStatusManager();
        int status = dvrStorageStatusManager.getDvrStorageStatus();
        if (status == DvrStorageStatusManager.STORAGE_STATUS_TOTAL_CAPACITY_TOO_SMALL) {
            showDvrSmallSizedStorageErrorDialog(activity);
            return false;
        } else if (status == DvrStorageStatusManager.STORAGE_STATUS_MISSING) {
            showDvrMissingStorageErrorDialog(activity, inputId);
            return false;
        } else if (status == DvrStorageStatusManager.STORAGE_STATUS_FREE_SPACE_INSUFFICIENT) {
            // TODO: handle insufficient storage case.
            return true;
        } else {
            return true;
        }
    }

    /**
     * Shows the schedule dialog.
     */
    public static void showScheduleDialog(MainActivity activity, Program program) {
        if (SoftPreconditions.checkNotNull(program) == null) {
            return;
        }
        Bundle args = new Bundle();
        args.putParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM, program);
        showDialogFragment(activity, new DvrScheduleDialogFragment(), args, true, true);
    }

    /**
     * Shows the recording duration options dialog.
     */
    public static void showChannelRecordDurationOptions(MainActivity activity, Channel channel) {
        if (SoftPreconditions.checkNotNull(channel) == null) {
            return;
        }
        Bundle args = new Bundle();
        args.putLong(DvrHalfSizedDialogFragment.KEY_CHANNEL_ID, channel.getId());
        showDialogFragment(activity, new DvrChannelRecordDurationOptionDialogFragment(), args);
    }

    /**
     * Shows the dialog which says that the new schedule conflicts with others.
     */
    public static void showScheduleConflictDialog(MainActivity activity, Program program) {
        if (program == null) {
            return;
        }
        Bundle args = new Bundle();
        args.putParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM, program);
        showDialogFragment(activity, new DvrProgramConflictDialogFragment(), args, false, true);
    }

    /**
     * Shows the conflict dialog for the channel watching.
     */
    public static void showChannelWatchConflictDialog(MainActivity activity, Channel channel) {
        if (channel == null) {
            return;
        }
        Bundle args = new Bundle();
        args.putLong(DvrHalfSizedDialogFragment.KEY_CHANNEL_ID, channel.getId());
        showDialogFragment(activity, new DvrChannelWatchConflictDialogFragment(), args);
    }

    /**
     * Shows DVR insufficient space error dialog.
     */
    public static void showDvrInsufficientSpaceErrorDialog(MainActivity activity) {
        showDialogFragment(activity, new DvrInsufficientSpaceErrorDialogFragment(), null);
        Utils.clearRecordingFailedReason(activity,
                TvInputManager.RECORDING_ERROR_INSUFFICIENT_SPACE);
    }

    /**
     * Shows DVR missing storage error dialog.
     */
    private static void showDvrMissingStorageErrorDialog(Activity activity, String inputId) {
        SoftPreconditions.checkArgument(!TextUtils.isEmpty(inputId));
        Bundle args = new Bundle();
        args.putString(DvrHalfSizedDialogFragment.KEY_INPUT_ID, inputId);
        showDialogFragment(activity, new DvrMissingStorageErrorDialogFragment(), args);
    }

    /**
     * Shows DVR small sized storage error dialog.
     */
    public static void showDvrSmallSizedStorageErrorDialog(Activity activity) {
        showDialogFragment(activity, new DvrSmallSizedStorageErrorDialogFragment(), null);
    }

    /**
     * Shows stop recording dialog.
     */
    public static void showStopRecordingDialog(Activity activity, long channelId, int reason,
            HalfSizedDialogFragment.OnActionClickListener listener) {
        Bundle args = new Bundle();
        args.putLong(DvrHalfSizedDialogFragment.KEY_CHANNEL_ID, channelId);
        args.putInt(DvrStopRecordingFragment.KEY_REASON, reason);
        DvrHalfSizedDialogFragment fragment = new DvrStopRecordingDialogFragment();
        fragment.setOnActionClickListener(listener);
        showDialogFragment(activity, fragment, args);
    }

    /**
     * Shows "already scheduled" dialog.
     */
    public static void showAlreadyScheduleDialog(MainActivity activity, Program program) {
        if (program == null) {
            return;
        }
        Bundle args = new Bundle();
        args.putParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM, program);
        showDialogFragment(activity, new DvrAlreadyScheduledDialogFragment(), args, false, true);
    }

    /**
     * Shows "already recorded" dialog.
     */
    public static void showAlreadyRecordedDialog(MainActivity activity, Program program) {
        if (program == null) {
            return;
        }
        Bundle args = new Bundle();
        args.putParcelable(DvrHalfSizedDialogFragment.KEY_PROGRAM, program);
        showDialogFragment(activity, new DvrAlreadyRecordedDialogFragment(), args, false, true);
    }

    private static void showDialogFragment(Activity activity,
            DvrHalfSizedDialogFragment dialogFragment, Bundle args) {
        showDialogFragment(activity, dialogFragment, args, false, false);
    }

    private static void showDialogFragment(Activity activity,
            DvrHalfSizedDialogFragment dialogFragment, Bundle args, boolean keepSidePanelHistory,
            boolean keepProgramGuide) {
        dialogFragment.setArguments(args);
        if (activity instanceof MainActivity) {
            ((MainActivity) activity).getOverlayManager()
                    .showDialogFragment(DvrHalfSizedDialogFragment.DIALOG_TAG, dialogFragment,
                            keepSidePanelHistory, keepProgramGuide);
        } else {
            dialogFragment.show(activity.getFragmentManager(),
                    DvrHalfSizedDialogFragment.DIALOG_TAG);
        }
    }

    /**
     * Checks whether channel watch conflict dialog is open or not.
     */
    public static boolean isChannelWatchConflictDialogShown(MainActivity activity) {
        return activity.getOverlayManager().getCurrentDialog() instanceof
                DvrChannelWatchConflictDialogFragment;
    }

    private static ScheduledRecording getEarliestScheduledRecording(List<ScheduledRecording>
            recordings) {
        ScheduledRecording earlistScheduledRecording = null;
        if (!recordings.isEmpty()) {
            Collections.sort(recordings,
                    ScheduledRecording.START_TIME_THEN_PRIORITY_THEN_ID_COMPARATOR);
            earlistScheduledRecording = recordings.get(0);
        }
        return earlistScheduledRecording;
    }

    /**
     * Shows the schedules activity to resolve the tune conflict.
     */
    public static void startSchedulesActivityForTuneConflict(Context context, Channel channel) {
        if (channel == null) {
            return;
        }
        List<ScheduledRecording> conflicts = TvApplication.getSingletons(context).getDvrManager()
                .getConflictingSchedulesForTune(channel.getId());
        startSchedulesActivity(context, getEarliestScheduledRecording(conflicts));
    }

    /**
     * Shows the schedules activity to resolve the one time recording conflict.
     */
    public static void startSchedulesActivityForOneTimeRecordingConflict(Context context,
            List<ScheduledRecording> conflicts) {
        startSchedulesActivity(context, getEarliestScheduledRecording(conflicts));
    }

    /**
     * Shows the schedules activity with full schedule.
     */
    public static void startSchedulesActivity(Context context, ScheduledRecording
            focusedScheduledRecording) {
        Intent intent = new Intent(context, DvrSchedulesActivity.class);
        intent.putExtra(DvrSchedulesActivity.KEY_SCHEDULES_TYPE,
                DvrSchedulesActivity.TYPE_FULL_SCHEDULE);
        if (focusedScheduledRecording != null) {
            intent.putExtra(DvrSchedulesFragment.SCHEDULES_KEY_SCHEDULED_RECORDING,
                    focusedScheduledRecording);
        }
        context.startActivity(intent);
    }

    /**
     * Shows the schedules activity for series recording.
     */
    public static void startSchedulesActivityForSeries(Context context,
            SeriesRecording seriesRecording) {
        Intent intent = new Intent(context, DvrSchedulesActivity.class);
        intent.putExtra(DvrSchedulesActivity.KEY_SCHEDULES_TYPE,
                DvrSchedulesActivity.TYPE_SERIES_SCHEDULE);
        intent.putExtra(DvrSeriesSchedulesFragment.SERIES_SCHEDULES_KEY_SERIES_RECORDING,
                seriesRecording);
        context.startActivity(intent);
    }

    /**
     * Shows the series settings activity.
     *
     * @param channelIds Channel ID list which has programs belonging to the series.
     */
    public static void startSeriesSettingsActivity(Context context, long seriesRecordingId,
            @Nullable long[] channelIds, boolean removeEmptySeriesSchedule,
            boolean isWindowTranslucent, boolean showViewScheduleOptionInDialog) {
        Intent intent = new Intent(context, DvrSeriesSettingsActivity.class);
        intent.putExtra(DvrSeriesSettingsActivity.SERIES_RECORDING_ID, seriesRecordingId);
        intent.putExtra(DvrSeriesSettingsActivity.CHANNEL_ID_LIST, channelIds);
        intent.putExtra(DvrSeriesSettingsActivity.REMOVE_EMPTY_SERIES_RECORDING,
                removeEmptySeriesSchedule);
        intent.putExtra(DvrSeriesSettingsActivity.IS_WINDOW_TRANSLUCENT, isWindowTranslucent);
        intent.putExtra(DvrSeriesSettingsActivity.SHOW_VIEW_SCHEDULE_OPTION_IN_DIALOG,
                showViewScheduleOptionInDialog);
        context.startActivity(intent);
    }

    /**
     * Shows "series recording scheduled" dialog activity.
     */
    public static void StartSeriesScheduledDialogActivity(Context context,
            SeriesRecording seriesRecording, boolean showViewScheduleOptionInDialog) {
        if (seriesRecording == null) {
            return;
        }
        Intent intent = new Intent(context, DvrSeriesScheduledDialogActivity.class);
        intent.putExtra(DvrSeriesScheduledDialogActivity.SERIES_RECORDING_ID,
                seriesRecording.getId());
        intent.putExtra(DvrSeriesScheduledDialogActivity.SHOW_VIEW_SCHEDULE_OPTION,
                showViewScheduleOptionInDialog);
        context.startActivity(intent);
    }

    /**
     * Shows the details activity for the DVR items. The type of DVR items may be
     * {@link ScheduledRecording}, {@link RecordedProgram}, or {@link SeriesRecording}.
     */
    public static void startDetailsActivity(Activity activity, Object dvrItem,
            @Nullable ImageView imageView, boolean hideViewSchedule) {
        if (dvrItem == null) {
            return;
        }
        Intent intent = new Intent(activity, DvrDetailsActivity.class);
        long recordingId;
        int viewType;
        if (dvrItem instanceof ScheduledRecording) {
            ScheduledRecording schedule = (ScheduledRecording) dvrItem;
            recordingId = schedule.getId();
            if (schedule.getState() == ScheduledRecording.STATE_RECORDING_NOT_STARTED) {
                viewType = DvrDetailsActivity.SCHEDULED_RECORDING_VIEW;
            } else if (schedule.getState() == ScheduledRecording.STATE_RECORDING_IN_PROGRESS) {
                viewType = DvrDetailsActivity.CURRENT_RECORDING_VIEW;
            } else {
                return;
            }
        } else if (dvrItem instanceof RecordedProgram) {
            recordingId = ((RecordedProgram) dvrItem).getId();
            viewType = DvrDetailsActivity.RECORDED_PROGRAM_VIEW;
        } else if (dvrItem instanceof SeriesRecording) {
            recordingId = ((SeriesRecording) dvrItem).getId();
            viewType = DvrDetailsActivity.SERIES_RECORDING_VIEW;
        } else {
            return;
        }
        intent.putExtra(DvrDetailsActivity.RECORDING_ID, recordingId);
        intent.putExtra(DvrDetailsActivity.DETAILS_VIEW_TYPE, viewType);
        intent.putExtra(DvrDetailsActivity.HIDE_VIEW_SCHEDULE, hideViewSchedule);
        Bundle bundle = null;
        if (imageView != null) {
            bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, imageView,
                    DvrDetailsActivity.SHARED_ELEMENT_NAME).toBundle();
        }
        activity.startActivity(intent, bundle);
    }

    /**
     * Shows the cancel all dialog for series schedules list.
     */
    public static void showCancelAllSeriesRecordingDialog(DvrSchedulesActivity activity,
            SeriesRecording seriesRecording) {
        DvrStopSeriesRecordingDialogFragment dvrStopSeriesRecordingDialogFragment =
                new DvrStopSeriesRecordingDialogFragment();
        Bundle arguments = new Bundle();
        arguments.putParcelable(DvrStopSeriesRecordingFragment.KEY_SERIES_RECORDING,
                seriesRecording);
        dvrStopSeriesRecordingDialogFragment.setArguments(arguments);
        dvrStopSeriesRecordingDialogFragment.show(activity.getFragmentManager(),
                DvrStopSeriesRecordingDialogFragment.DIALOG_TAG);
    }

    /**
     * Shows the series deletion activity.
     */
    public static void startSeriesDeletionActivity(Context context, long seriesRecordingId) {
        Intent intent = new Intent(context, DvrSeriesDeletionActivity.class);
        intent.putExtra(DvrSeriesDeletionActivity.SERIES_RECORDING_ID, seriesRecordingId);
        context.startActivity(intent);
    }

    public static void showAddScheduleToast(Context context,
            String title, long startTimeMs, long endTimeMs) {
        String msg = (startTimeMs > System.currentTimeMillis()) ?
            context.getString(R.string.dvr_msg_program_scheduled, title)
            : context.getString(R.string.dvr_msg_current_program_scheduled, title,
                    Utils.toTimeString(endTimeMs, false));
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}