aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/ui/SetupView.java
blob: ba90dcfe786ffc1a83873da19f554a42b0082901 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * 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.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.media.tv.TvInputInfo;
import android.support.annotation.VisibleForTesting;
import android.support.v17.leanback.widget.VerticalGridView;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.tv.MainActivity;
import com.android.tv.R;
import com.android.tv.data.ChannelDataManager;
import com.android.tv.util.SetupUtils;
import com.android.tv.util.TvInputManagerHelper;

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

public class SetupView extends FullscreenDialogView {
    private static final int FINISH_ACTIVITY_DELAY_MS = 200;
    private static final int REFRESH_DELAY_MS_AFTER_WINDOW_FOCUS_GAINED = 200;

    private VerticalGridView mInputView;
    private ChannelDataManager mChannelDataManager;
    private List<TvInputInfo> mInputList;
    // mInputList[0:mKnownInputStartIndex - 1] are new inputs.
    // And mInputList[mKnownInputStartIndex:end] are inputs which have been shown in SetupView.
    private int mKnownInputStartIndex;
    private boolean mShowDivider;
    private SetupAdapter mAdapter;
    private boolean mClosing;
    private boolean mInitialized;
    private SetupUtils mSetupUtils;
    private boolean mNeedIntroDialog;

    private final ChannelDataManager.Listener mChannelDataListener = new ChannelDataManager.Listener() {
        @Override
        public void onLoadFinished() { }

        @Override
        public void onChannelListUpdated() {
            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onChannelBrowsableChanged() {
            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }
        }
    };

    public SetupView(Context context) {
        this(context, null, 0);
    }

    public SetupView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SetupView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTransitionAnimationEnabled(true);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        TextView titleView = (TextView) findViewById(R.id.setup_title);
        titleView.setText(R.string.setup_title);
        TextView descriptionView = (TextView) findViewById(R.id.setup_description);
        descriptionView.setText(R.string.setup_description);
        mInputView = (VerticalGridView) findViewById(R.id.input_list);
        TypedValue outValue = new TypedValue();
        getResources().getValue(R.dimen.setup_item_window_alignment_offset_percent, outValue, true);
        mInputView.setWindowAlignmentOffsetPercent(outValue.getFloat());
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return mClosing || super.dispatchKeyEvent(event);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        if (hasWindowFocus && mAdapter != null) {
            // Without the following delay, the channel count description is sometimes
            // changed twice by this method and mChannelDataListener.
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    // When channel count is still 0 after setup, the description should be changed
                    // from "Not set up" to "No channels".
                    if (mAdapter.getItemCount() != 0) {
                        mAdapter.notifyItemRangeChanged(0, mAdapter.getItemCount());
                    }
                }
            }, REFRESH_DELAY_MS_AFTER_WINDOW_FOCUS_GAINED);
        }
    }

    /**
     * Initializes SetupView.
     */
    @Override
    public void initialize(MainActivity activity, Dialog dialog) {
        super.initialize(activity, dialog);
        if (mInitialized) {
            throw new IllegalStateException("initialize() is called more than once");
        }
        mInitialized = true;
        final TvInputManagerHelper inputManager = getActivity().getTvInputManagerHelper();
        mChannelDataManager = getActivity().getChannelDataManager();
        mSetupUtils = SetupUtils.getInstance(activity);
        mKnownInputStartIndex = 0;
        mInputList = inputManager.getTvInputInfos(true, true);
        Collections.sort(mInputList, new TvInputInfoComparator(mSetupUtils, inputManager));
        for (TvInputInfo input : mInputList) {
            if (mSetupUtils.isNewInput(input.getId())) {
                mSetupUtils.markAsKnownInput(input.getId());
                ++mKnownInputStartIndex;
            }
        }
        mShowDivider = mKnownInputStartIndex != 0 && mKnownInputStartIndex != mInputList.size();
        mAdapter = new SetupAdapter();
        mInputView.setAdapter(mAdapter);
        mChannelDataManager.addListener(mChannelDataListener);
        mNeedIntroDialog = mSetupUtils.isFirstTune();
    }

    /**
     * Called when the DialogFragment including this view is destroyed.
     */
    @Override
    public void onDestroy() {
        mChannelDataManager.removeListener(mChannelDataListener);
    }

    @Override
    protected void dismiss() {
        mClosing = true;
        if (mNeedIntroDialog) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            IntroView v = (IntroView) inflater.inflate(R.layout.intro_dialog, null);
            transitionTo(v);
        } else {
            super.dismiss();
        }
    }
    /**
     * Called when the back key is pressed.
     */
    @Override
    public void onBackPressed() {
        if (mChannelDataManager.getChannelCount() == 0) {
            // If there is no channel, we finish the activity rather than closing just the view.
            getActivity().finish();
        }
        dismiss();
    }

    private class SetupAdapter extends RecyclerView.Adapter<MyViewHolder> {
        @Override
        public int getItemViewType(int position) {
            if (mShowDivider && position == mKnownInputStartIndex) {
                return R.layout.setup_item_divider;
            } else if (position == getItemCount() - 1) {
                return R.layout.setup_item_action;
            } else {
                return R.layout.setup_item_input;
            }
        }

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

        @Override
        public void onBindViewHolder(final MyViewHolder viewHolder, int position) {
            if (position == getItemCount() - 1) {
                final boolean closeActivity = mChannelDataManager.getChannelCount() == 0;
                viewHolder.mTitle.setText(R.string.setup_done_button_label);
                viewHolder.itemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mClosing = true;
                        if (closeActivity) {
                            // To wait completing ripple animation, finish() is called
                            // FINISH_ACTIVITY_DELAY_MS later.
                            mNeedIntroDialog = false;
                            postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    getActivity().finish();
                                }
                            }, FINISH_ACTIVITY_DELAY_MS);
                        } else {
                            dismiss();
                        }
                    }
                });
            } else {
                if (mShowDivider) {
                    if (position == mKnownInputStartIndex) {
                        // This view is a divider.
                        return;
                    } else if (position > mKnownInputStartIndex) {
                        --position;
                    }
                }
                final TvInputInfo input = mInputList.get(position);
                viewHolder.mTitle.setText(input.loadLabel(getContext()));
                int channelCount = mChannelDataManager.getChannelCountForInput(input.getId());
                if (mSetupUtils.hasSetupLaunched(input.getId())) {
                    if (channelCount == 0) {
                        viewHolder.mDescription.setText(R.string.setup_input_no_channels);
                    } else {
                        viewHolder.mDescription.setText(getResources().getQuantityString(
                                R.plurals.setup_input_channels, channelCount, channelCount));
                    }
                } else if (position >= mKnownInputStartIndex) {
                    viewHolder.mDescription.setText(R.string.channel_description_setup_now);
                } else {
                    viewHolder.mDescription.setText(R.string.setup_input_new);
                }
                viewHolder.itemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getActivity().startSetupActivity(input, true);
                    }
                });
            }
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(viewType, parent,
                    false);
            return new MyViewHolder(itemView);
        }
    }

    private static class MyViewHolder extends RecyclerView.ViewHolder {
        final TextView mTitle;
        final TextView mDescription;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mDescription = (TextView) itemView.findViewById(R.id.description);
        }
    }

    @VisibleForTesting
    static class TvInputInfoComparator implements Comparator<TvInputInfo> {
        private SetupUtils mSetupUtils;
        private TvInputManagerHelper mInputManager;

        public TvInputInfoComparator(SetupUtils setupUtils, TvInputManagerHelper inputManager) {
            mSetupUtils = setupUtils;
            mInputManager = inputManager;
        }

        @Override
        public int compare(TvInputInfo lhs, TvInputInfo rhs) {
            boolean lhsIsNewInput = mSetupUtils.isNewInput(lhs.getId());
            boolean rhsIsNewInput = mSetupUtils.isNewInput(rhs.getId());
            if (lhsIsNewInput != rhsIsNewInput) {
                return lhsIsNewInput ? -1 : 1;
            }
            return mInputManager.getDefaultTvInputInfoComparator().compare(lhs, rhs);
        }
    };
}