ScanResultFragment.java revision d41f0075a7d2ea826204e81fcec57d0aa57171a9
1/*
2 * Copyright (C) 2015 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.tuner.setup;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.os.Bundle;
22import android.support.annotation.NonNull;
23import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
24import android.support.v17.leanback.widget.GuidedAction;
25
26import com.android.tv.common.ui.setup.SetupGuidedStepFragment;
27import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
28import com.android.tv.tuner.R;
29import com.android.tv.tuner.TunerPreferences;
30import com.android.tv.tuner.util.TunerInputInfoUtils;
31
32import java.util.List;
33
34/**
35 * A fragment for initial screen.
36 */
37public class ScanResultFragment extends SetupMultiPaneFragment {
38    public static final String ACTION_CATEGORY =
39            "com.android.tv.tuner.setup.ScanResultFragment";
40
41    @Override
42    protected SetupGuidedStepFragment onCreateContentFragment() {
43        return new ContentFragment();
44    }
45
46    @Override
47    protected String getActionCategory() {
48        return ACTION_CATEGORY;
49    }
50
51    @Override
52    protected boolean needsDoneButton() {
53        return false;
54    }
55
56    public static class ContentFragment extends SetupGuidedStepFragment {
57        private int mChannelCountOnPreference;
58
59        @Override
60        public void onAttach(Context context) {
61            super.onAttach(context);
62            mChannelCountOnPreference = TunerPreferences.getScannedChannelCount(context);
63        }
64
65        @NonNull
66        @Override
67        public Guidance onCreateGuidance(Bundle savedInstanceState) {
68            String title;
69            String description;
70            String breadcrumb;
71            if (mChannelCountOnPreference > 0) {
72                Resources res = getResources();
73                title = res.getQuantityString(R.plurals.ut_result_found_title,
74                        mChannelCountOnPreference, mChannelCountOnPreference);
75                description = res.getQuantityString(R.plurals.ut_result_found_description,
76                        mChannelCountOnPreference, mChannelCountOnPreference);
77                breadcrumb = null;
78            } else {
79                title = getString(R.string.ut_result_not_found_title);
80                if (TunerInputInfoUtils.isBuiltInTuner(getActivity())) {
81                    description = getString(R.string.bt_result_not_found_description);
82                } else {
83                    description = getString(R.string.ut_result_not_found_description);
84                }
85                breadcrumb = getString(R.string.ut_setup_breadcrumb);
86            }
87            return new Guidance(title, description, breadcrumb, null);
88        }
89
90        @Override
91        public void onCreateActions(@NonNull List<GuidedAction> actions,
92                Bundle savedInstanceState) {
93            String[] choices;
94            int doneActionIndex;
95            if (mChannelCountOnPreference > 0) {
96                choices = getResources().getStringArray(R.array.ut_result_found_choices);
97                doneActionIndex = 0;
98            } else {
99                choices = getResources().getStringArray(R.array.ut_result_not_found_choices);
100                doneActionIndex = 1;
101            }
102            for (int i = 0; i < choices.length; ++i) {
103                if (i == doneActionIndex) {
104                    actions.add(new GuidedAction.Builder(getActivity()).id(ACTION_DONE)
105                            .title(choices[i]).build());
106                } else {
107                    actions.add(new GuidedAction.Builder(getActivity()).id(i).title(choices[i])
108                            .build());
109                }
110            }
111        }
112
113        @Override
114        protected String getActionCategory() {
115            return ACTION_CATEGORY;
116        }
117    }
118}
119