aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/onboarding/SetupSourcesFragment.java
blob: ad49dc23fcf26eea4cbd009a62eb2c17b13bf19d (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2015 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.onboarding;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.tv.TvInputInfo;
import android.os.Bundle;
import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
import android.support.v17.leanback.widget.GuidedAction;
import android.support.v17.leanback.widget.VerticalGridView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.tv.ApplicationSingletons;
import com.android.tv.R;
import com.android.tv.SetupPassthroughActivity;
import com.android.tv.TvApplication;
import com.android.tv.common.TvCommonUtils;
import com.android.tv.common.ui.setup.SetupGuidedStepFragment;
import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
import com.android.tv.data.ChannelDataManager;
import com.android.tv.data.TvInputNewComparator;
import com.android.tv.util.SetupUtils;
import com.android.tv.util.TvInputManagerHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * A fragment for channel source info/setup.
 */
public class SetupSourcesFragment extends SetupMultiPaneFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setOnClickAction(view.findViewById(R.id.button_done), ACTION_DONE);
        return view;
    }

    @Override
    protected SetupGuidedStepFragment onCreateContentFragment() {
        SetupGuidedStepFragment fragment = new ContentFragment(getActivity());
        Bundle arguments = new Bundle();
        arguments.putBoolean(SetupGuidedStepFragment.KEY_THREE_PANE, true);
        fragment.setArguments(arguments);
        return fragment;
    }

    private class ContentFragment extends SetupGuidedStepFragment {
        private static final int REQUEST_CODE_START_SETUP_ACTIVITY = 1;

        private static final int ACTION_DIVIDER = ACTION_DONE + 1;
        private static final int ACTION_INPUT_START = ACTION_DONE + 2;

        private final TvInputManagerHelper mInputManager;
        private final ChannelDataManager mChannelDataManager;
        private final SetupUtils mSetupUtils;
        private List<TvInputInfo> mInputList;
        private SetupSourcesAdapter mAdapter;
        private int mKnownInputStartIndex;
        private boolean mShowDivider;

        ContentFragment(Context context) {
            // TODO: Handle USB TV tuner differently.
            ApplicationSingletons app = TvApplication.getSingletons(context);
            mInputManager = app.getTvInputManagerHelper();
            mChannelDataManager = app.getChannelDataManager();
            mSetupUtils = SetupUtils.getInstance(context);
            mInputList = mInputManager.getTvInputInfos(true, true);
            Collections.sort(mInputList, new TvInputNewComparator(mSetupUtils, mInputManager));
            mKnownInputStartIndex = 0;
            for (TvInputInfo input : mInputList) {
                if (mSetupUtils.isNewInput(input.getId())) {
                    mSetupUtils.markAsKnownInput(input.getId());
                    ++mKnownInputStartIndex;
                }
            }
            mShowDivider = mKnownInputStartIndex != 0 && mKnownInputStartIndex != mInputList.size();
            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }
        }

        @SuppressWarnings("rawtypes")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = super.onCreateView(inflater, container, savedInstanceState);
            VerticalGridView gridView = getGuidedActionsStylist().getActionsGridView();
            RecyclerView.Adapter adapter = gridView.getAdapter();
            mAdapter = new SetupSourcesAdapter(adapter);
            gridView.setAdapter(mAdapter);
            return view;
        }

        @Override
        public Guidance onCreateGuidance(Bundle savedInstanceState) {
            String title = getString(R.string.setup_sources_text);
            String description = getString(R.string.setup_sources_description);
            return new Guidance(title, description, null, null);
        }

        @Override
        public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
            createActionsInternal(actions);
            if (!mChannelDataManager.isDbLoadFinished()) {
                mChannelDataManager.addListener(new ChannelDataManager.Listener() {
                    @Override
                    public void onLoadFinished() {
                        mChannelDataManager.removeListener(this);
                        updateActions();
                    }

                    @Override
                    public void onChannelListUpdated() { }

                    @Override
                    public void onChannelBrowsableChanged() { }
                });
            }
        }

        private void updateActions() {
            List<GuidedAction> actions = new ArrayList<>();
            createActionsInternal(actions);
            setActions(actions);
            mAdapter.notifyDataSetChanged();
        }

        private void createActionsInternal(List<GuidedAction> actions) {
            for (int i = 0; i < mInputList.size(); ++i) {
                if (mShowDivider && i == mKnownInputStartIndex) {
                    actions.add(new GuidedAction.Builder().id(ACTION_DIVIDER).title(null)
                            .description(null).build());
                }
                TvInputInfo input = mInputList.get(i);
                String description;
                int channelCount = mChannelDataManager.getChannelCountForInput(input.getId());
                if (mSetupUtils.isSetupDone(input.getId())) {
                    if (channelCount == 0) {
                        description = getResources().getString(R.string.setup_input_no_channels);
                    } else {
                        description = getResources().getQuantityString(
                                R.plurals.setup_input_channels, channelCount, channelCount);
                    }
                } else if (i >= mKnownInputStartIndex) {
                    description = getResources().getString(R.string.channel_description_setup_now);
                } else {
                    description = getResources().getString(R.string.setup_input_new);
                }
                actions.add(new GuidedAction.Builder().id(ACTION_INPUT_START + i)
                        .title(input.loadLabel(getActivity()).toString()).description(description)
                        .build());
            }
        }

        @Override
        public void onGuidedActionClicked(GuidedAction action) {
            TvInputInfo input = mInputList.get((int) action.getId() - ACTION_INPUT_START);
            Intent intent = TvCommonUtils.createSetupIntent(input);
            if (intent == null) {
                Toast.makeText(getActivity(), R.string.msg_no_setup_activity, Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            // Even though other app can handle the intent, the setup launched by Live channels
            // should go through Live channels SetupPassthroughActivity.
            intent.setComponent(new ComponentName(getActivity(), SetupPassthroughActivity.class));
            try {
                // Now we know that the user intends to set up this input. Grant permission for writing
                // EPG data.
                SetupUtils.grantEpgPermission(getActivity(), input.getServiceInfo().packageName);
                startActivityForResult(intent, REQUEST_CODE_START_SETUP_ACTIVITY);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getActivity(), getString(R.string.msg_unable_to_start_setup_activity,
                        input.loadLabel(getActivity())), Toast.LENGTH_SHORT).show();
                return;
            }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            updateActions();
        }

        @SuppressWarnings("rawtypes")
        private class SetupSourcesAdapter extends RecyclerView.Adapter {
            private static final int VIEW_TYPE_INPUT = 1;
            private static final int VIEW_TYPE_DIVIDER = 2;

            private final RecyclerView.Adapter mGuidedActionAdapter;

            SetupSourcesAdapter(RecyclerView.Adapter adapter) {
                mGuidedActionAdapter = adapter;
            }

            @Override
            public int getItemViewType(int position) {
                if (mShowDivider && position == mKnownInputStartIndex) {
                    return VIEW_TYPE_DIVIDER;
                }
                return VIEW_TYPE_INPUT;
            }

            @Override
            public int getItemCount() {
                if (mInputList == null) {
                    return 0;
                }
                return mInputList.size() + (mShowDivider ? 1 : 0);
            }

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                if (viewType == VIEW_TYPE_INPUT) {
                    return mGuidedActionAdapter.onCreateViewHolder(parent, viewType);
                }
                View itemView = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.onboarding_item_divider, parent, false);
                return new MyViewHolder(itemView);
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onBindViewHolder(ViewHolder viewHolder, int position) {
                if (mShowDivider && position == mKnownInputStartIndex) {
                    return;
                }
                mGuidedActionAdapter.onBindViewHolder(viewHolder, position);
            }

            @Override
            public void onAttachedToRecyclerView(RecyclerView recyclerView) {
                mGuidedActionAdapter.onAttachedToRecyclerView(recyclerView);
            }

            @Override
            public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
                mGuidedActionAdapter.onDetachedFromRecyclerView(recyclerView);
            }
        }
    }

    private static class MyViewHolder extends RecyclerView.ViewHolder {
        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }
}