aboutsummaryrefslogtreecommitdiff
path: root/tuner/src/com/android/tv/tuner/setup/LineupFragment.java
blob: 41f755df1e29f9eb46689711ef78bf88b0cbfd3c (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
/*
 * Copyright (C) 2017 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.tuner.setup;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
import android.support.v17.leanback.widget.GuidedAction;
import android.util.Log;
import android.view.View;
import com.android.tv.common.ui.setup.SetupGuidedStepFragment;
import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
import com.android.tv.tuner.R;
import java.util.ArrayList;
import java.util.List;

/** Lineup Fragment shows available lineups and lets users select one of them. */
public class LineupFragment extends SetupMultiPaneFragment {
    public static final String TAG = "LineupFragment";
    public static final boolean DEBUG = false;

    public static final String ACTION_CATEGORY = "com.android.tv.tuner.setup.LineupFragment";
    public static final String KEY_LINEUP_NAMES = "lineup_names";
    public static final String KEY_MATCH_NUMBERS = "match_numbers";
    public static final String KEY_DEFAULT_LINEUP = "default_lineup";
    public static final String KEY_LINEUP_NOT_FOUND = "lineup_not_found";
    public static final int ACTION_ID_RETRY = SetupMultiPaneFragment.MAX_SUBCLASSES_ID - 1;

    private ContentFragment contentFragment;
    private Bundle args;
    private ArrayList<String> lineups;
    private boolean lineupNotFound;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            lineups = getArguments().getStringArrayList(KEY_LINEUP_NAMES);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected SetupGuidedStepFragment onCreateContentFragment() {
        contentFragment = new LineupFragment.ContentFragment();
        Bundle args = new Bundle();
        Bundle arguments = this.args != null ? this.args : getArguments();
        args.putStringArrayList(KEY_LINEUP_NAMES, lineups);
        args.putIntegerArrayList(
                KEY_MATCH_NUMBERS, arguments.getIntegerArrayList(KEY_MATCH_NUMBERS));
        args.putInt(KEY_DEFAULT_LINEUP, arguments.getInt(KEY_DEFAULT_LINEUP));
        args.putBoolean(KEY_LINEUP_NOT_FOUND, lineupNotFound);
        contentFragment.setArguments(args);
        return contentFragment;
    }

    @Override
    protected String getActionCategory() {
        return ACTION_CATEGORY;
    }

    public void onLineupFound(Bundle args) {
        if (DEBUG) {
            Log.d(TAG, "onLineupFound");
        }
        this.args = args;
        lineups = args.getStringArrayList(KEY_LINEUP_NAMES);
        lineupNotFound = false;
        if (contentFragment != null) {
            updateContentFragment();
        }
    }

    public void onLineupNotFound() {
        if (DEBUG) {
            Log.d(TAG, "onLineupNotFound");
        }
        lineupNotFound = true;
        if (contentFragment != null) {
            updateContentFragment();
        }
    }

    public void onRetry() {
        if (DEBUG) {
            Log.d(TAG, "onRetry");
        }
        if (contentFragment != null) {
            lineupNotFound = false;
            lineups = null;
            updateContentFragment();
        } else {
            // onRetry() can be called only when retry button is clicked.
            // This should never happen.
            throw new RuntimeException(
                    "ContentFragment hasn't been created when onRetry() is called");
        }
    }

    @Override
    protected boolean needsDoneButton() {
        return false;
    }

    private void updateContentFragment() {
        contentFragment = (ContentFragment) onCreateContentFragment();
        getChildFragmentManager()
                .beginTransaction()
                .replace(
                        com.android.tv.common.R.id.guided_step_fragment_container,
                        contentFragment,
                        SetupMultiPaneFragment.CONTENT_FRAGMENT_TAG)
                .commit();
    }

    /** The content fragment of {@link LineupFragment}. */
    public static class ContentFragment extends SetupGuidedStepFragment {

        private ArrayList<String> lineups;
        private ArrayList<Integer> matchNumbers;
        private int defaultLineup;
        private boolean lineupNotFound;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            if (savedInstanceState == null) {
                lineups = getArguments().getStringArrayList(KEY_LINEUP_NAMES);
                matchNumbers = getArguments().getIntegerArrayList(KEY_MATCH_NUMBERS);
                defaultLineup = getArguments().getInt(KEY_DEFAULT_LINEUP);
                this.lineupNotFound = getArguments().getBoolean(KEY_LINEUP_NOT_FOUND);
            }
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            int position = findActionPositionById(defaultLineup);
            if (position >= 0 && position < getActions().size()) {
                setSelectedActionPosition(position);
            }
        }

        @NonNull
        @Override
        public Guidance onCreateGuidance(Bundle savedInstanceState) {
            if ((lineups != null && lineups.isEmpty()) || this.lineupNotFound) {
                return new Guidance(
                        getString(R.string.ut_lineup_title_lineups_not_found),
                        getString(R.string.ut_lineup_description_lineups_not_found),
                        getString(R.string.ut_setup_breadcrumb),
                        null);
            } else if (lineups == null) {
                return new Guidance(
                        getString(R.string.ut_lineup_title_fetching_lineups),
                        getString(R.string.ut_lineup_description_fetching_lineups),
                        getString(R.string.ut_setup_breadcrumb),
                        null);
            }
            return new Guidance(
                    getString(R.string.ut_lineup_title_lineups_found),
                    getString(R.string.ut_lineup_description_lineups_found),
                    getString(R.string.ut_setup_breadcrumb),
                    null);
        }

        @Override
        public void onCreateActions(
                @NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
            actions.addAll(buildActions());
        }

        @Override
        protected String getActionCategory() {
            return ACTION_CATEGORY;
        }

        private List<GuidedAction> buildActions() {
            List<GuidedAction> actions = new ArrayList<>();

            if ((lineups != null && lineups.isEmpty()) || this.lineupNotFound) {
                actions.add(
                        new GuidedAction.Builder(getActivity())
                                .id(ACTION_ID_RETRY)
                                .title(com.android.tv.common.R.string.action_text_retry)
                                .build());
                actions.add(
                        new GuidedAction.Builder(getActivity())
                                .id(ACTION_SKIP)
                                .title(com.android.tv.common.R.string.action_text_skip)
                                .build());
            } else if (lineups == null) {
                actions.add(
                        new GuidedAction.Builder(getActivity())
                                .id(ACTION_SKIP)
                                .title(com.android.tv.common.R.string.action_text_skip)
                                .build());
            } else {
                Resources res = getResources();
                for (int i = 0; i < lineups.size(); ++i) {
                    int matchNumber = matchNumbers.get(i);
                    String description =
                            matchNumber == 0
                                    ? res.getString(R.string.ut_lineup_no_channels_matched)
                                    : res.getQuantityString(
                                            R.plurals.ut_lineup_channels_matched,
                                            matchNumber,
                                            matchNumber);
                    actions.add(
                            new GuidedAction.Builder(getActivity())
                                    .id(i)
                                    .title(lineups.get(i))
                                    .description(description)
                                    .build());
                }
            }
            return actions;
        }
    }
}