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.usbtuner.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;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28
29import com.android.tv.common.ui.setup.SetupGuidedStepFragment;
30import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
31import com.android.usbtuner.R;
32import com.android.usbtuner.UsbTunerPreferences;
33
34import java.util.List;
35
36/**
37 * A fragment for initial screen.
38 */
39public class ScanResultFragment extends SetupMultiPaneFragment {
40    public static final String ACTION_CATEGORY =
41            "com.android.usbtuner.setup.ScanResultFragment";
42
43    /**
44     * An action which moves to previous page when the user presses BACK button.
45     * In some cases, more than one page can be popped out.
46     */
47    public static final int ACTION_BACK_TO_CONNECTION_TYPE = ACTION_DONE - 1;
48
49    @Override
50    protected SetupGuidedStepFragment onCreateContentFragment() {
51        return new ContentFragment();
52    }
53
54    @Override
55    protected String getActionCategory() {
56        return ACTION_CATEGORY;
57    }
58
59    @Override
60    protected boolean needsDoneButton() {
61        return false;
62    }
63
64    public static class ContentFragment extends SetupGuidedStepFragment {
65        private int mChannelCountOnPreference;
66
67        @Override
68        public void onAttach(Context context) {
69            super.onAttach(context);
70            mChannelCountOnPreference = UsbTunerPreferences.getScannedChannelCount(context);
71        }
72
73        @NonNull
74        @Override
75        public Guidance onCreateGuidance(Bundle savedInstanceState) {
76            String title;
77            String description;
78            String breadcrumb;
79            if (mChannelCountOnPreference > 0) {
80                Resources res = getResources();
81                title = res.getQuantityString(R.plurals.ut_result_found_title,
82                        mChannelCountOnPreference, mChannelCountOnPreference);
83                description = res.getQuantityString(R.plurals.ut_result_found_description,
84                        mChannelCountOnPreference, mChannelCountOnPreference);
85                breadcrumb = null;
86            } else {
87                title = getString(R.string.ut_result_not_found_title);
88                description = getString(R.string.ut_result_not_found_description);
89                breadcrumb = getString(R.string.ut_setup_breadcrumb);
90            }
91            return new Guidance(title, description, breadcrumb, null);
92        }
93
94        @Override
95        public void onCreateActions(@NonNull List<GuidedAction> actions,
96                Bundle savedInstanceState) {
97            String[] choices;
98            int doneActionIndex;
99            if (mChannelCountOnPreference > 0) {
100                choices = getResources().getStringArray(R.array.ut_result_found_choices);
101                doneActionIndex = 0;
102            } else {
103                choices = getResources().getStringArray(R.array.ut_result_not_found_choices);
104                doneActionIndex = 1;
105            }
106            for (int i = 0; i < choices.length; ++i) {
107                if (i == doneActionIndex) {
108                    actions.add(new GuidedAction.Builder(getActivity()).id(ACTION_DONE)
109                            .title(choices[i]).build());
110                } else {
111                    actions.add(new GuidedAction.Builder(getActivity()).id(i).title(choices[i])
112                            .build());
113                }
114            }
115        }
116
117        @Override
118        protected String getActionCategory() {
119            return ACTION_CATEGORY;
120        }
121    }
122}
123