• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.tuner.setup;
18 
19 import android.content.res.Resources;
20 import android.os.Bundle;
21 import android.support.annotation.NonNull;
22 import android.support.annotation.Nullable;
23 import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
24 import android.support.v17.leanback.widget.GuidedAction;
25 import android.util.Log;
26 import android.view.View;
27 import com.android.tv.common.ui.setup.SetupGuidedStepFragment;
28 import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
29 import com.android.tv.tuner.R;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /** Lineup Fragment shows available lineups and lets users select one of them. */
34 public class LineupFragment extends SetupMultiPaneFragment {
35     public static final String TAG = "LineupFragment";
36     public static final boolean DEBUG = false;
37 
38     public static final String ACTION_CATEGORY = "com.android.tv.tuner.setup.LineupFragment";
39     public static final String KEY_LINEUP_NAMES = "lineup_names";
40     public static final String KEY_MATCH_NUMBERS = "match_numbers";
41     public static final String KEY_DEFAULT_LINEUP = "default_lineup";
42     public static final String KEY_LINEUP_NOT_FOUND = "lineup_not_found";
43     public static final int ACTION_ID_RETRY = SetupMultiPaneFragment.MAX_SUBCLASSES_ID - 1;
44 
45     private ContentFragment contentFragment;
46     private Bundle args;
47     private ArrayList<String> lineups;
48     private boolean lineupNotFound;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         if (savedInstanceState == null) {
53             lineups = getArguments().getStringArrayList(KEY_LINEUP_NAMES);
54         }
55         super.onCreate(savedInstanceState);
56     }
57 
58     @Override
onCreateContentFragment()59     protected SetupGuidedStepFragment onCreateContentFragment() {
60         contentFragment = new LineupFragment.ContentFragment();
61         Bundle args = new Bundle();
62         Bundle arguments = this.args != null ? this.args : getArguments();
63         args.putStringArrayList(KEY_LINEUP_NAMES, lineups);
64         args.putIntegerArrayList(
65                 KEY_MATCH_NUMBERS, arguments.getIntegerArrayList(KEY_MATCH_NUMBERS));
66         args.putInt(KEY_DEFAULT_LINEUP, arguments.getInt(KEY_DEFAULT_LINEUP));
67         args.putBoolean(KEY_LINEUP_NOT_FOUND, lineupNotFound);
68         contentFragment.setArguments(args);
69         return contentFragment;
70     }
71 
72     @Override
getActionCategory()73     protected String getActionCategory() {
74         return ACTION_CATEGORY;
75     }
76 
onLineupFound(Bundle args)77     public void onLineupFound(Bundle args) {
78         if (DEBUG) {
79             Log.d(TAG, "onLineupFound");
80         }
81         this.args = args;
82         lineups = args.getStringArrayList(KEY_LINEUP_NAMES);
83         lineupNotFound = false;
84         if (contentFragment != null) {
85             updateContentFragment();
86         }
87     }
88 
onLineupNotFound()89     public void onLineupNotFound() {
90         if (DEBUG) {
91             Log.d(TAG, "onLineupNotFound");
92         }
93         lineupNotFound = true;
94         if (contentFragment != null) {
95             updateContentFragment();
96         }
97     }
98 
onRetry()99     public void onRetry() {
100         if (DEBUG) {
101             Log.d(TAG, "onRetry");
102         }
103         if (contentFragment != null) {
104             lineupNotFound = false;
105             lineups = null;
106             updateContentFragment();
107         } else {
108             // onRetry() can be called only when retry button is clicked.
109             // This should never happen.
110             throw new RuntimeException(
111                     "ContentFragment hasn't been created when onRetry() is called");
112         }
113     }
114 
115     @Override
needsDoneButton()116     protected boolean needsDoneButton() {
117         return false;
118     }
119 
updateContentFragment()120     private void updateContentFragment() {
121         contentFragment = (ContentFragment) onCreateContentFragment();
122         getChildFragmentManager()
123                 .beginTransaction()
124                 .replace(
125                         com.android.tv.common.R.id.guided_step_fragment_container,
126                         contentFragment,
127                         SetupMultiPaneFragment.CONTENT_FRAGMENT_TAG)
128                 .commit();
129     }
130 
131     /** The content fragment of {@link LineupFragment}. */
132     public static class ContentFragment extends SetupGuidedStepFragment {
133 
134         private ArrayList<String> lineups;
135         private ArrayList<Integer> matchNumbers;
136         private int defaultLineup;
137         private boolean lineupNotFound;
138 
139         @Override
onCreate(Bundle savedInstanceState)140         public void onCreate(Bundle savedInstanceState) {
141             if (savedInstanceState == null) {
142                 lineups = getArguments().getStringArrayList(KEY_LINEUP_NAMES);
143                 matchNumbers = getArguments().getIntegerArrayList(KEY_MATCH_NUMBERS);
144                 defaultLineup = getArguments().getInt(KEY_DEFAULT_LINEUP);
145                 this.lineupNotFound = getArguments().getBoolean(KEY_LINEUP_NOT_FOUND);
146             }
147             super.onCreate(savedInstanceState);
148         }
149 
150         @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)151         public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
152             super.onViewCreated(view, savedInstanceState);
153             int position = findActionPositionById(defaultLineup);
154             if (position >= 0 && position < getActions().size()) {
155                 setSelectedActionPosition(position);
156             }
157         }
158 
159         @NonNull
160         @Override
onCreateGuidance(Bundle savedInstanceState)161         public Guidance onCreateGuidance(Bundle savedInstanceState) {
162             if ((lineups != null && lineups.isEmpty()) || this.lineupNotFound) {
163                 return new Guidance(
164                         getString(R.string.ut_lineup_title_lineups_not_found),
165                         getString(R.string.ut_lineup_description_lineups_not_found),
166                         getString(R.string.ut_setup_breadcrumb),
167                         null);
168             } else if (lineups == null) {
169                 return new Guidance(
170                         getString(R.string.ut_lineup_title_fetching_lineups),
171                         getString(R.string.ut_lineup_description_fetching_lineups),
172                         getString(R.string.ut_setup_breadcrumb),
173                         null);
174             }
175             return new Guidance(
176                     getString(R.string.ut_lineup_title_lineups_found),
177                     getString(R.string.ut_lineup_description_lineups_found),
178                     getString(R.string.ut_setup_breadcrumb),
179                     null);
180         }
181 
182         @Override
onCreateActions( @onNull List<GuidedAction> actions, Bundle savedInstanceState)183         public void onCreateActions(
184                 @NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
185             actions.addAll(buildActions());
186         }
187 
188         @Override
getActionCategory()189         protected String getActionCategory() {
190             return ACTION_CATEGORY;
191         }
192 
buildActions()193         private List<GuidedAction> buildActions() {
194             List<GuidedAction> actions = new ArrayList<>();
195 
196             if ((lineups != null && lineups.isEmpty()) || this.lineupNotFound) {
197                 actions.add(
198                         new GuidedAction.Builder(getActivity())
199                                 .id(ACTION_ID_RETRY)
200                                 .title(com.android.tv.common.R.string.action_text_retry)
201                                 .build());
202                 actions.add(
203                         new GuidedAction.Builder(getActivity())
204                                 .id(ACTION_SKIP)
205                                 .title(com.android.tv.common.R.string.action_text_skip)
206                                 .build());
207             } else if (lineups == null) {
208                 actions.add(
209                         new GuidedAction.Builder(getActivity())
210                                 .id(ACTION_SKIP)
211                                 .title(com.android.tv.common.R.string.action_text_skip)
212                                 .build());
213             } else {
214                 Resources res = getResources();
215                 for (int i = 0; i < lineups.size(); ++i) {
216                     int matchNumber = matchNumbers.get(i);
217                     String description =
218                             matchNumber == 0
219                                     ? res.getString(R.string.ut_lineup_no_channels_matched)
220                                     : res.getQuantityString(
221                                             R.plurals.ut_lineup_channels_matched,
222                                             matchNumber,
223                                             matchNumber);
224                     actions.add(
225                             new GuidedAction.Builder(getActivity())
226                                     .id(i)
227                                     .title(lineups.get(i))
228                                     .description(description)
229                                     .build());
230                 }
231             }
232             return actions;
233         }
234     }
235 }
236