aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/ui/KeypadChannelSwitchView.java
blob: e26258111f9512aeb9977f353c51f3c56b138b09 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * 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.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.android.tv.MainActivity;
import com.android.tv.R;
import com.android.tv.TvSingletons;
import com.android.tv.analytics.Tracker;
import com.android.tv.common.SoftPreconditions;
import com.android.tv.common.util.DurationTimer;
import com.android.tv.data.ChannelNumber;
import com.android.tv.data.api.Channel;
import java.util.ArrayList;
import java.util.List;

public class KeypadChannelSwitchView extends LinearLayout
        implements TvTransitionManager.TransitionLayout {
    private static final String TAG = "KeypadChannelSwitchView";

    private static final int MAX_CHANNEL_NUMBER_DIGIT = 4;
    private static final int MAX_MINOR_CHANNEL_NUMBER_DIGIT = 3;
    private static final int MAX_CHANNEL_ITEM = 8;
    private static final String CHANNEL_DELIMITERS_REGEX = "[-\\.\\s]";
    public static final String SCREEN_NAME = "Channel switch";

    private final MainActivity mMainActivity;
    private final Tracker mTracker;
    private final DurationTimer mViewDurationTimer = new DurationTimer();
    private boolean mNavigated = false;
    @Nullable // Once mChannels is set to null it should not be used again.
    private List<Channel> mChannels;
    private TextView mChannelNumberView;
    private ListView mChannelItemListView;
    private final ChannelNumber mTypedChannelNumber = new ChannelNumber();
    private final ArrayList<Channel> mChannelCandidates = new ArrayList<>();
    protected final ChannelItemAdapter mAdapter = new ChannelItemAdapter();
    private final LayoutInflater mLayoutInflater;
    private Channel mSelectedChannel;

    private final Runnable mHideRunnable =
            new Runnable() {
                @Override
                public void run() {
                    mCurrentHeight = 0;
                    if (mSelectedChannel != null) {
                        mMainActivity.tuneToChannel(mSelectedChannel);
                        mTracker.sendChannelNumberItemChosenByTimeout();
                    } else {
                        mMainActivity
                                .getOverlayManager()
                                .hideOverlays(
                                        TvOverlayManager.FLAG_HIDE_OVERLAYS_KEEP_DIALOG
                                                | TvOverlayManager
                                                        .FLAG_HIDE_OVERLAYS_KEEP_SIDE_PANELS
                                                | TvOverlayManager
                                                        .FLAG_HIDE_OVERLAYS_KEEP_PROGRAM_GUIDE
                                                | TvOverlayManager.FLAG_HIDE_OVERLAYS_KEEP_MENU
                                                | TvOverlayManager
                                                        .FLAG_HIDE_OVERLAYS_KEEP_FRAGMENT);
                    }
                }
            };
    private final long mShowDurationMillis;
    private final long mRippleAnimDurationMillis;
    private final int mBaseViewHeight;
    private final int mItemHeight;
    private final int mResizeAnimDuration;
    private Animator mResizeAnimator;
    private final Interpolator mResizeInterpolator;
    // NOTE: getHeight() will be updated after layout() is called. mCurrentHeight is needed for
    // getting the latest updated value of the view height before layout().
    private int mCurrentHeight;

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

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

    public KeypadChannelSwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mMainActivity = (MainActivity) context;
        mTracker = TvSingletons.getSingletons(context).getTracker();
        Resources resources = getResources();
        mLayoutInflater = LayoutInflater.from(context);
        mShowDurationMillis = resources.getInteger(R.integer.keypad_channel_switch_show_duration);
        mRippleAnimDurationMillis =
                resources.getInteger(R.integer.keypad_channel_switch_ripple_anim_duration);
        mBaseViewHeight =
                resources.getDimensionPixelSize(R.dimen.keypad_channel_switch_base_height);
        mItemHeight = resources.getDimensionPixelSize(R.dimen.keypad_channel_switch_item_height);
        mResizeAnimDuration = resources.getInteger(R.integer.keypad_channel_switch_anim_duration);
        mResizeInterpolator =
                AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mChannelNumberView = (TextView) findViewById(R.id.channel_number);
        mChannelItemListView = (ListView) findViewById(R.id.channel_list);
        mChannelItemListView.setAdapter(mAdapter);
        mChannelItemListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(
                            AdapterView<?> parent, View view, int position, long id) {
                        if (position >= mAdapter.getCount()) {
                            // It can happen during closing.
                            return;
                        }
                        mChannelItemListView.setFocusable(false);
                        final Channel channel = ((Channel) mAdapter.getItem(position));
                        postDelayed(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        mChannelItemListView.setFocusable(true);
                                        mMainActivity.tuneToChannel(channel);
                                        mTracker.sendChannelNumberItemClicked();
                                    }
                                },
                                mRippleAnimDurationMillis);
                    }
                });
        mChannelItemListView.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        if (position >= mAdapter.getCount()) {
                            // It can happen during closing.
                            mSelectedChannel = null;
                        } else {
                            mSelectedChannel = (Channel) mAdapter.getItem(position);
                        }
                        if (position != 0 && !mNavigated) {
                            mNavigated = true;
                            mTracker.sendChannelInputNavigated();
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                        mSelectedChannel = null;
                    }
                });
    }

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

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        SoftPreconditions.checkNotNull(mChannels, TAG, "mChannels");
        if (isChannelNumberKey(keyCode)) {
            onNumberKeyUp(keyCode - KeyEvent.KEYCODE_0);
            return true;
        }
        if (ChannelNumber.isChannelNumberDelimiterKey(keyCode)) {
            onDelimiterKeyUp();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onEnterAction(boolean fromEmptyScene) {
        reset();
        if (fromEmptyScene) {
            ViewUtils.setTransitionAlpha(mChannelItemListView, 1f);
        }
        mNavigated = false;
        mViewDurationTimer.start();
        mTracker.sendShowChannelSwitch();
        mTracker.sendScreenView(SCREEN_NAME);
        updateView();
        scheduleHide();
    }

    @Override
    public void onExitAction() {
        mCurrentHeight = 0;
        mTracker.sendHideChannelSwitch(mViewDurationTimer.reset());
        cancelHide();
    }

    private void scheduleHide() {
        cancelHide();
        postDelayed(mHideRunnable, mShowDurationMillis);
    }

    private void cancelHide() {
        removeCallbacks(mHideRunnable);
    }

    private void reset() {
        mTypedChannelNumber.reset();
        mSelectedChannel = null;
        mChannelCandidates.clear();
        mAdapter.notifyDataSetChanged();
    }

    public void setChannels(@Nullable List<Channel> channels) {
        mChannels = channels;
    }

    public static boolean isChannelNumberKey(int keyCode) {
        return keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9;
    }

    public void onNumberKeyUp(int num) {
        // Reset typed channel number in some cases.
        if (mTypedChannelNumber.majorNumber == null) {
            mTypedChannelNumber.reset();
        } else if (!mTypedChannelNumber.hasDelimiter
                && mTypedChannelNumber.majorNumber.length() >= MAX_CHANNEL_NUMBER_DIGIT) {
            mTypedChannelNumber.reset();
        } else if (mTypedChannelNumber.hasDelimiter
                && mTypedChannelNumber.minorNumber != null
                && mTypedChannelNumber.minorNumber.length() >= MAX_MINOR_CHANNEL_NUMBER_DIGIT) {
            mTypedChannelNumber.reset();
        }

        if (!mTypedChannelNumber.hasDelimiter) {
            mTypedChannelNumber.majorNumber += String.valueOf(num);
        } else {
            mTypedChannelNumber.minorNumber += String.valueOf(num);
        }
        mTracker.sendChannelNumberInput();
        updateView();
    }

    private void onDelimiterKeyUp() {
        if (mTypedChannelNumber.hasDelimiter || mTypedChannelNumber.majorNumber.length() == 0) {
            return;
        }
        mTypedChannelNumber.hasDelimiter = true;
        mTracker.sendChannelNumberInput();
        updateView();
    }

    private void updateView() {
        mChannelNumberView.setText(mTypedChannelNumber.toString() + "_");
        mChannelCandidates.clear();
        ArrayList<Channel> secondaryChannelCandidates = new ArrayList<>();
        for (Channel channel : mChannels) {
            ChannelNumber chNumber = ChannelNumber.parseChannelNumber(channel.getDisplayNumber());
            if (chNumber == null) {
                Log.i(
                        TAG,
                        "Malformed channel number (name="
                                + channel.getDisplayName()
                                + ", number="
                                + channel.getDisplayNumber()
                                + ")");
                continue;
            }
            if (matchChannelNumber(mTypedChannelNumber, chNumber)) {
                mChannelCandidates.add(channel);
            } else if (!mTypedChannelNumber.hasDelimiter) {
                // Even if a user doesn't type '-', we need to match the typed number to not only
                // the major number but also the minor number. For example, when a user types '111'
                // without delimiter, it should be matched to '111', '1-11' and '11-1'.
                if (channel.getDisplayNumber()
                        .replaceAll(CHANNEL_DELIMITERS_REGEX, "")
                        .startsWith(mTypedChannelNumber.majorNumber)) {
                    secondaryChannelCandidates.add(channel);
                }
            }
        }
        mChannelCandidates.addAll(secondaryChannelCandidates);
        mAdapter.notifyDataSetChanged();
        if (mAdapter.getCount() > 0) {
            mChannelItemListView.requestFocus();
            mChannelItemListView.setSelection(0);
            mSelectedChannel = mChannelCandidates.get(0);
        }

        updateViewHeight();
    }

    private void updateViewHeight() {
        int itemListHeight = mItemHeight * Math.min(MAX_CHANNEL_ITEM, mAdapter.getCount());
        int targetHeight = mBaseViewHeight + itemListHeight;
        if (mResizeAnimator != null) {
            mResizeAnimator.cancel();
            mResizeAnimator = null;
        }

        if (mCurrentHeight == 0) {
            // Do not add the resize animation when the banner has not been shown before.
            mCurrentHeight = targetHeight;
            setViewHeight(this, targetHeight);
        } else if (mCurrentHeight != targetHeight) {
            mResizeAnimator = createResizeAnimator(targetHeight);
            mResizeAnimator.start();
        }
    }

    private Animator createResizeAnimator(int targetHeight) {
        ValueAnimator animator = ValueAnimator.ofInt(mCurrentHeight, targetHeight);
        animator.addUpdateListener(
                new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int value = (Integer) animation.getAnimatedValue();
                        setViewHeight(KeypadChannelSwitchView.this, value);
                        mCurrentHeight = value;
                    }
                });
        animator.setDuration(mResizeAnimDuration);
        animator.addListener(
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animator) {
                        mResizeAnimator = null;
                    }
                });
        animator.setInterpolator(mResizeInterpolator);
        return animator;
    }

    private void setViewHeight(View view, int height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (height != layoutParams.height) {
            layoutParams.height = height;
            view.setLayoutParams(layoutParams);
        }
    }

    private static boolean matchChannelNumber(ChannelNumber typedChNumber, ChannelNumber chNumber) {
        if (!chNumber.majorNumber.equals(typedChNumber.majorNumber)) {
            return false;
        }
        if (typedChNumber.hasDelimiter) {
            if (!chNumber.hasDelimiter) {
                return false;
            }
            if (!chNumber.minorNumber.startsWith(typedChNumber.minorNumber)) {
                return false;
            }
        }
        return true;
    }

    class ChannelItemAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mChannelCandidates.size();
        }

        @Override
        public Object getItem(int position) {
            return mChannelCandidates.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final Channel channel = mChannelCandidates.get(position);
            View v = convertView;
            if (v == null) {
                v = mLayoutInflater.inflate(R.layout.keypad_channel_switch_item, parent, false);
            }

            TextView channelNumberView = (TextView) v.findViewById(R.id.number);
            channelNumberView.setText(channel.getDisplayNumber());

            TextView channelNameView = (TextView) v.findViewById(R.id.name);
            channelNameView.setText(channel.getDisplayName());
            return v;
        }
    }
}