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.dvr.ui;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.graphics.drawable.Drawable;
22 import android.os.Bundle;
23 import android.support.v17.leanback.widget.GuidanceStylist;
24 import android.support.v17.leanback.widget.GuidedAction;
25 
26 import com.android.tv.R;
27 import com.android.tv.TvApplication;
28 import com.android.tv.dvr.DvrDataManager;
29 import com.android.tv.dvr.DvrScheduleManager;
30 import com.android.tv.dvr.ScheduledRecording;
31 import com.android.tv.dvr.SeriesRecording;
32 import com.android.tv.dvr.ui.list.DvrSeriesSchedulesFragment;
33 
34 import java.util.List;
35 
36 public class DvrSeriesScheduledFragment extends DvrGuidedStepFragment {
37     private final static long SERIES_RECORDING_ID_NOT_SET = -1;
38 
39     private final static int ACTION_VIEW_SCHEDULES = 1;
40 
41     private DvrScheduleManager mDvrScheduleManager;
42     private SeriesRecording mSeriesRecording;
43     private boolean mShowViewScheduleOption;
44 
45     private int mSchedulesAddedCount = 0;
46     private boolean mHasConflict = false;
47     private int mInThisSeriesConflictCount = 0;
48     private int mOutThisSeriesConflictCount = 0;
49 
50     @Override
onAttach(Context context)51     public void onAttach(Context context) {
52         super.onAttach(context);
53         long seriesRecordingId = getArguments().getLong(
54                 DvrSeriesScheduledDialogActivity.SERIES_RECORDING_ID, SERIES_RECORDING_ID_NOT_SET);
55         if (seriesRecordingId == SERIES_RECORDING_ID_NOT_SET) {
56             getActivity().finish();
57             return;
58         }
59         mShowViewScheduleOption = getArguments().getBoolean(
60                 DvrSeriesScheduledDialogActivity.SHOW_VIEW_SCHEDULE_OPTION);
61         mDvrScheduleManager = TvApplication.getSingletons(context).getDvrScheduleManager();
62         mSeriesRecording = TvApplication.getSingletons(context).getDvrDataManager()
63                 .getSeriesRecording(seriesRecordingId);
64         if (mSeriesRecording == null) {
65             getActivity().finish();
66             return;
67         }
68         mSchedulesAddedCount = TvApplication.getSingletons(getContext()).getDvrManager()
69                 .getAvailableScheduledRecording(mSeriesRecording.getId()).size();
70         List<ScheduledRecording> conflictingRecordings =
71                 mDvrScheduleManager.getConflictingSchedules(mSeriesRecording);
72         mHasConflict = !conflictingRecordings.isEmpty();
73         for (ScheduledRecording recording : conflictingRecordings) {
74             if (recording.getSeriesRecordingId() == mSeriesRecording.getId()) {
75                 ++mInThisSeriesConflictCount;
76             } else {
77                 ++mOutThisSeriesConflictCount;
78             }
79         }
80      }
81 
82     @Override
onCreateGuidance(Bundle savedInstanceState)83     public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
84         String title = getString(R.string.dvr_series_recording_dialog_title);
85         Drawable icon;
86         if (!mHasConflict) {
87             icon = getResources().getDrawable(R.drawable.ic_check_circle_white_48dp, null);
88         } else {
89             icon = getResources().getDrawable(R.drawable.ic_error_white_48dp, null);
90         }
91         return new GuidanceStylist.Guidance(title, getDescription(), null, icon);
92     }
93 
94     @Override
onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState)95     public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
96         Context context = getContext();
97         actions.add(new GuidedAction.Builder(context)
98                 .clickAction(GuidedAction.ACTION_ID_OK)
99                 .build());
100         if (mShowViewScheduleOption) {
101             actions.add(new GuidedAction.Builder(context)
102                     .id(ACTION_VIEW_SCHEDULES)
103                     .title(R.string.dvr_action_view_schedules)
104                     .build());
105         }
106     }
107 
108     @Override
onGuidedActionClicked(GuidedAction action)109     public void onGuidedActionClicked(GuidedAction action) {
110         if (action.getId() == ACTION_VIEW_SCHEDULES) {
111             Intent intent = new Intent(getActivity(), DvrSchedulesActivity.class);
112             intent.putExtra(DvrSchedulesActivity.KEY_SCHEDULES_TYPE, DvrSchedulesActivity
113                     .TYPE_SERIES_SCHEDULE);
114             intent.putExtra(DvrSeriesSchedulesFragment.SERIES_SCHEDULES_KEY_SERIES_RECORDING,
115                     mSeriesRecording);
116             startActivity(intent);
117         }
118         getActivity().finish();
119     }
120 
getDescription()121     private String getDescription() {
122         if (!mHasConflict) {
123             return getResources().getQuantityString(
124                     R.plurals.dvr_series_recording_scheduled_no_conflict, mSchedulesAddedCount,
125                     mSchedulesAddedCount, mSeriesRecording.getTitle());
126         } else {
127             // mInThisSeriesConflictCount equals 0 and mOutThisSeriesConflictCount equals 0 means
128             // mHasConflict is false. So we don't need to check that case.
129             if (mInThisSeriesConflictCount != 0 && mOutThisSeriesConflictCount != 0) {
130                 return getResources().getQuantityString(R.plurals
131                         .dvr_series_recording_scheduled_this_and_other_series_conflict,
132                         mSchedulesAddedCount, mSchedulesAddedCount, mSeriesRecording.getTitle(),
133                         mInThisSeriesConflictCount + mOutThisSeriesConflictCount);
134             } else if (mInThisSeriesConflictCount != 0) {
135                 return getResources().getQuantityString(R.plurals
136                         .dvr_series_recording_scheduled_only_this_series_conflict,
137                         mSchedulesAddedCount, mSchedulesAddedCount, mSeriesRecording.getTitle(),
138                         mInThisSeriesConflictCount);
139             } else {
140                 if (mOutThisSeriesConflictCount == 1) {
141                     return getResources().getQuantityString(R.plurals
142                             .dvr_series_recording_scheduled_only_other_series_one_conflict,
143                             mSchedulesAddedCount, mSchedulesAddedCount,
144                             mSeriesRecording.getTitle());
145                 } else {
146                     return getResources().getQuantityString(R.plurals
147                             .dvr_series_recording_scheduled_only_other_series_conflict,
148                             mSchedulesAddedCount, mSchedulesAddedCount, mSeriesRecording.getTitle(),
149                             mOutThisSeriesConflictCount);
150                 }
151             }
152         }
153     }
154 }
155