1816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko/*
2816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * Copyright (C) 2015 The Android Open Source Project
3816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko *
4816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * Licensed under the Apache License, Version 2.0 (the "License");
5816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * you may not use this file except in compliance with the License.
6816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * You may obtain a copy of the License at
7816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko *
8816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko *      http://www.apache.org/licenses/LICENSE-2.0
9816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko *
10816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * Unless required by applicable law or agreed to in writing, software
11816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * distributed under the License is distributed on an "AS IS" BASIS,
12816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * See the License for the specific language governing permissions and
14816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko * limitations under the License.
15816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko */
16816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
17816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkopackage com.android.tv.menu;
18816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
19816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport android.content.Context;
20816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport android.content.Intent;
212e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalkoimport android.media.tv.TvInputInfo;
22816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport android.view.View;
23816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport com.android.tv.R;
24944779887775bd950cf1abf348d2df461593f6abLive Channels Teamimport com.android.tv.TvSingletons;
25816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport com.android.tv.analytics.Tracker;
26ba5845f23b8fbc985890f892961abc8b39886611Nick Chalkoimport com.android.tv.common.feature.CommonFeatures;
270cc0713c1bf8027642987b750b80217569d2932aLive Channels Teamimport com.android.tv.data.ChannelImpl;
280cc0713c1bf8027642987b750b80217569d2932aLive Channels Teamimport com.android.tv.data.api.Channel;
2965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalkoimport com.android.tv.dvr.DvrDataManager;
30816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport com.android.tv.recommendation.Recommender;
312e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalkoimport com.android.tv.util.TvInputManagerHelper;
32633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport java.util.ArrayDeque;
33816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport java.util.ArrayList;
34816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalkoimport java.util.List;
35816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
3695961816a768da387f0b5523cf4363ace2044089Nick Chalko/** An adapter of the Channels row. */
37633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkopublic class ChannelsRowAdapter extends ItemListRowView.ItemListAdapter<ChannelsRowItem> {
3865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    // There are four special cards: guide, setup, dvr, applink.
392e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalko    private static final int SIZE_OF_VIEW_TYPE = 5;
40ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko
41816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    private final Context mContext;
42816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    private final Tracker mTracker;
43816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    private final Recommender mRecommender;
4465fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko    private final DvrDataManager mDvrDataManager;
45816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    private final int mMaxCount;
46816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    private final int mMinCount;
47816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
4895961816a768da387f0b5523cf4363ace2044089Nick Chalko    private final View.OnClickListener mGuideOnClickListener =
4995961816a768da387f0b5523cf4363ace2044089Nick Chalko            new View.OnClickListener() {
5095961816a768da387f0b5523cf4363ace2044089Nick Chalko                @Override
5195961816a768da387f0b5523cf4363ace2044089Nick Chalko                public void onClick(View view) {
5295961816a768da387f0b5523cf4363ace2044089Nick Chalko                    mTracker.sendMenuClicked(R.string.channels_item_program_guide);
5395961816a768da387f0b5523cf4363ace2044089Nick Chalko                    getMainActivity().getOverlayManager().showProgramGuide();
5495961816a768da387f0b5523cf4363ace2044089Nick Chalko                }
5595961816a768da387f0b5523cf4363ace2044089Nick Chalko            };
56816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
5795961816a768da387f0b5523cf4363ace2044089Nick Chalko    private final View.OnClickListener mSetupOnClickListener =
5895961816a768da387f0b5523cf4363ace2044089Nick Chalko            new View.OnClickListener() {
5995961816a768da387f0b5523cf4363ace2044089Nick Chalko                @Override
6095961816a768da387f0b5523cf4363ace2044089Nick Chalko                public void onClick(View view) {
6195961816a768da387f0b5523cf4363ace2044089Nick Chalko                    mTracker.sendMenuClicked(R.string.channels_item_setup);
6295961816a768da387f0b5523cf4363ace2044089Nick Chalko                    getMainActivity().getOverlayManager().showSetupFragment();
6395961816a768da387f0b5523cf4363ace2044089Nick Chalko                }
6495961816a768da387f0b5523cf4363ace2044089Nick Chalko            };
65ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko
6695961816a768da387f0b5523cf4363ace2044089Nick Chalko    private final View.OnClickListener mDvrOnClickListener =
6795961816a768da387f0b5523cf4363ace2044089Nick Chalko            new View.OnClickListener() {
6895961816a768da387f0b5523cf4363ace2044089Nick Chalko                @Override
6995961816a768da387f0b5523cf4363ace2044089Nick Chalko                public void onClick(View view) {
7095961816a768da387f0b5523cf4363ace2044089Nick Chalko                    mTracker.sendMenuClicked(R.string.channels_item_dvr);
7195961816a768da387f0b5523cf4363ace2044089Nick Chalko                    getMainActivity().getOverlayManager().showDvrManager();
7295961816a768da387f0b5523cf4363ace2044089Nick Chalko                }
7395961816a768da387f0b5523cf4363ace2044089Nick Chalko            };
74816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
7595961816a768da387f0b5523cf4363ace2044089Nick Chalko    private final View.OnClickListener mAppLinkOnClickListener =
7695961816a768da387f0b5523cf4363ace2044089Nick Chalko            new View.OnClickListener() {
7795961816a768da387f0b5523cf4363ace2044089Nick Chalko                @Override
7895961816a768da387f0b5523cf4363ace2044089Nick Chalko                public void onClick(View view) {
7995961816a768da387f0b5523cf4363ace2044089Nick Chalko                    mTracker.sendMenuClicked(R.string.channels_item_app_link);
8095961816a768da387f0b5523cf4363ace2044089Nick Chalko                    Intent intent = ((AppLinkCardView) view).getIntent();
8195961816a768da387f0b5523cf4363ace2044089Nick Chalko                    if (intent != null) {
8295961816a768da387f0b5523cf4363ace2044089Nick Chalko                        getMainActivity().startActivitySafe(intent);
8395961816a768da387f0b5523cf4363ace2044089Nick Chalko                    }
8495961816a768da387f0b5523cf4363ace2044089Nick Chalko                }
8595961816a768da387f0b5523cf4363ace2044089Nick Chalko            };
86816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
8795961816a768da387f0b5523cf4363ace2044089Nick Chalko    private final View.OnClickListener mChannelOnClickListener =
8895961816a768da387f0b5523cf4363ace2044089Nick Chalko            new View.OnClickListener() {
8995961816a768da387f0b5523cf4363ace2044089Nick Chalko                @Override
9095961816a768da387f0b5523cf4363ace2044089Nick Chalko                public void onClick(View view) {
9195961816a768da387f0b5523cf4363ace2044089Nick Chalko                    // Always send the label "Channels" because the channel ID or name or number
9295961816a768da387f0b5523cf4363ace2044089Nick Chalko                    // might be
9395961816a768da387f0b5523cf4363ace2044089Nick Chalko                    // sensitive.
9495961816a768da387f0b5523cf4363ace2044089Nick Chalko                    mTracker.sendMenuClicked(R.string.menu_title_channels);
9595961816a768da387f0b5523cf4363ace2044089Nick Chalko                    getMainActivity().tuneToChannel((Channel) view.getTag());
9695961816a768da387f0b5523cf4363ace2044089Nick Chalko                    getMainActivity().hideOverlaysForTune();
9795961816a768da387f0b5523cf4363ace2044089Nick Chalko                }
9895961816a768da387f0b5523cf4363ace2044089Nick Chalko            };
99816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
10095961816a768da387f0b5523cf4363ace2044089Nick Chalko    public ChannelsRowAdapter(
10195961816a768da387f0b5523cf4363ace2044089Nick Chalko            Context context, Recommender recommender, int minCount, int maxCount) {
102816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        super(context);
103816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        mContext = context;
104944779887775bd950cf1abf348d2df461593f6abLive Channels Team        TvSingletons tvSingletons = TvSingletons.getSingletons(context);
105944779887775bd950cf1abf348d2df461593f6abLive Channels Team        mTracker = tvSingletons.getTracker();
10665fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        if (CommonFeatures.DVR.isEnabled(context)) {
107944779887775bd950cf1abf348d2df461593f6abLive Channels Team            mDvrDataManager = tvSingletons.getDvrDataManager();
10865fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        } else {
10965fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko            mDvrDataManager = null;
11065fda1eaa94968bb55d5ded10dcb0b3f37fb05f2Nick Chalko        }
111816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        mRecommender = recommender;
112816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        mMinCount = minCount;
113816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        mMaxCount = maxCount;
114633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        setHasStableIds(true);
115816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    }
116816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
117816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    @Override
118816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    public int getItemViewType(int position) {
119633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return getItemList().get(position).getLayoutId();
120816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    }
121816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
122816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    @Override
123816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    protected int getLayoutResId(int viewType) {
124816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        return viewType;
125816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    }
126816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
127816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    @Override
128633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public long getItemId(int position) {
129633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return getItemList().get(position).getItemId();
130633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
131721bd0da688cd552737fbb753a00597f95103b95Adrian Roos
132633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    @Override
133633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public void onBindViewHolder(MyViewHolder viewHolder, int position) {
134816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        int viewType = getItemViewType(position);
135816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        if (viewType == R.layout.menu_card_guide) {
136816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            viewHolder.itemView.setOnClickListener(mGuideOnClickListener);
137816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        } else if (viewType == R.layout.menu_card_setup) {
138816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            viewHolder.itemView.setOnClickListener(mSetupOnClickListener);
139816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        } else if (viewType == R.layout.menu_card_app_link) {
140816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            viewHolder.itemView.setOnClickListener(mAppLinkOnClickListener);
141ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko        } else if (viewType == R.layout.menu_card_dvr) {
142ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko            viewHolder.itemView.setOnClickListener(mDvrOnClickListener);
143d41f0075a7d2ea826204e81fcec57d0aa57171a9Nick Chalko            SimpleCardView view = (SimpleCardView) viewHolder.itemView;
144d41f0075a7d2ea826204e81fcec57d0aa57171a9Nick Chalko            view.setText(R.string.channels_item_dvr);
145816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        } else {
146633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            viewHolder.itemView.setTag(getItemList().get(position).getChannel());
147816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            viewHolder.itemView.setOnClickListener(mChannelOnClickListener);
148816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        }
149633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        super.onBindViewHolder(viewHolder, position);
150816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    }
151816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
152816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    @Override
153816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    public void update() {
154633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (getItemCount() == 0) {
155633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            createItems();
156633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        } else {
157633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            updateItems();
158633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
159633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
160633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
161633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private void createItems() {
162633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        List<ChannelsRowItem> items = new ArrayList<>();
163633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        items.add(ChannelsRowItem.GUIDE_ITEM);
164633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (needToShowSetupItem()) {
165633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            items.add(ChannelsRowItem.SETUP_ITEM);
166633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
167633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (needToShowDvrItem()) {
168633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            items.add(ChannelsRowItem.DVR_ITEM);
169633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
170633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (needToShowAppLinkItem()) {
171633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            ChannelsRowItem.APP_LINK_ITEM.setChannel(
1720cc0713c1bf8027642987b750b80217569d2932aLive Channels Team                    new ChannelImpl.Builder(getMainActivity().getCurrentChannel()).build());
173633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            items.add(ChannelsRowItem.APP_LINK_ITEM);
174633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
175633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        for (Channel channel : getRecentChannels()) {
176633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            items.add(new ChannelsRowItem(channel, R.layout.menu_card_channel));
177633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
178633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        setItemList(items);
179633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
180633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
181633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private void updateItems() {
182633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        List<ChannelsRowItem> items = getItemList();
183633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        // The current index of the item list to iterate. It starts from 1 because the first item
184633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        // (GUIDE) is always visible and not updated.
185633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        int currentIndex = 1;
186633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (updateItem(needToShowSetupItem(), ChannelsRowItem.SETUP_ITEM, currentIndex)) {
187633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            ++currentIndex;
188633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
189633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (updateItem(needToShowDvrItem(), ChannelsRowItem.DVR_ITEM, currentIndex)) {
190633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            ++currentIndex;
191633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
192633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (updateItem(needToShowAppLinkItem(), ChannelsRowItem.APP_LINK_ITEM, currentIndex)) {
19395961816a768da387f0b5523cf4363ace2044089Nick Chalko            if (!getMainActivity()
19495961816a768da387f0b5523cf4363ace2044089Nick Chalko                    .getCurrentChannel()
195633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                    .hasSameReadOnlyInfo(ChannelsRowItem.APP_LINK_ITEM.getChannel())) {
196633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                ChannelsRowItem.APP_LINK_ITEM.setChannel(
1970cc0713c1bf8027642987b750b80217569d2932aLive Channels Team                        new ChannelImpl.Builder(getMainActivity().getCurrentChannel()).build());
198633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                notifyItemChanged(currentIndex);
199633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            }
200633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            ++currentIndex;
201633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
202633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        int numOldChannels = items.size() - currentIndex;
203633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (numOldChannels > 0) {
204633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            while (items.size() > currentIndex) {
205633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                items.remove(items.size() - 1);
206633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            }
207633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            notifyItemRangeRemoved(currentIndex, numOldChannels);
208633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
209633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        for (Channel channel : getRecentChannels()) {
210633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            items.add(new ChannelsRowItem(channel, R.layout.menu_card_channel));
211633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
212633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        int numNewChannels = items.size() - currentIndex;
213633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (numNewChannels > 0) {
214633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            notifyItemRangeInserted(currentIndex, numNewChannels);
215633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
216633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
217633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
21895961816a768da387f0b5523cf4363ace2044089Nick Chalko    /** Returns {@code true} if the item should be shown. */
219633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private boolean updateItem(boolean needToShow, ChannelsRowItem item, int index) {
220633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        List<ChannelsRowItem> items = getItemList();
221633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        boolean isItemInList = index < items.size() && item.equals(items.get(index));
222633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        if (needToShow && !isItemInList) {
223633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            items.add(index, item);
224633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            notifyItemInserted(index);
225633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        } else if (!needToShow && isItemInList) {
226633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            items.remove(index);
227633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            notifyItemRemoved(index);
228633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
229633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return needToShow;
230633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
231633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
232633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private boolean needToShowSetupItem() {
233944779887775bd950cf1abf348d2df461593f6abLive Channels Team        TvSingletons singletons = TvSingletons.getSingletons(mContext);
234944779887775bd950cf1abf348d2df461593f6abLive Channels Team        TvInputManagerHelper inputManager = singletons.getTvInputManagerHelper();
235944779887775bd950cf1abf348d2df461593f6abLive Channels Team        return singletons.getSetupUtils().hasNewInput(inputManager);
236633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
237633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
238633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private boolean needToShowDvrItem() {
239633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        TvInputManagerHelper inputManager =
240944779887775bd950cf1abf348d2df461593f6abLive Channels Team                TvSingletons.getSingletons(mContext).getTvInputManagerHelper();
241d41f0075a7d2ea826204e81fcec57d0aa57171a9Nick Chalko        if (mDvrDataManager != null) {
2422e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalko            for (TvInputInfo info : inputManager.getTvInputInfos(true, true)) {
2432e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalko                if (info.canRecord()) {
244633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                    return true;
2452e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalko                }
2462e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalko            }
2472e1279b8bbe0603fb4399b25b73121bed5953c46Nick Chalko        }
248633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return false;
249633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
250ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko
251633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private boolean needToShowAppLinkItem() {
252633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        TvInputManagerHelper inputManager =
253944779887775bd950cf1abf348d2df461593f6abLive Channels Team                TvSingletons.getSingletons(mContext).getTvInputManagerHelper();
254633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        Channel currentChannel = getMainActivity().getCurrentChannel();
255633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return currentChannel != null
256633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                && currentChannel.getAppLinkType(mContext) != Channel.APP_LINK_TYPE_NONE
257633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                // Sometimes applicationInfo can be null. b/28932537
258633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                && inputManager.getTvInputAppInfo(currentChannel.getInputId()) != null;
259816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    }
260816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko
261816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    private List<Channel> getRecentChannels() {
262816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        List<Channel> channelList = new ArrayList<>();
263633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        long currentChannelId = getMainActivity().getCurrentChannelId();
264633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        ArrayDeque<Long> recentChannels = getMainActivity().getRecentChannels();
265633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        // Add the last watched channel as the first one.
266633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        for (long channelId : recentChannels) {
267633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            if (addChannelToList(
268633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                    channelList, mRecommender.getChannel(channelId), currentChannelId)) {
269633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                break;
270633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            }
271633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
272633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        // Add the recommended channels.
273816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        for (Channel channel : mRecommender.recommendChannels(mMaxCount)) {
274633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            if (channelList.size() >= mMaxCount) {
275633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                break;
276816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            }
277633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            addChannelToList(channelList, channel, currentChannelId);
278816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        }
279816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        // If the number of recommended channels is not enough, add more from the recent channel
280816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        // list.
281633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        for (long channelId : recentChannels) {
282633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            if (channelList.size() >= mMinCount) {
283633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                break;
284816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko            }
285633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            addChannelToList(channelList, mRecommender.getChannel(channelId), currentChannelId);
286816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko        }
2877d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        return channelList;
288816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko    }
289633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
290633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private static boolean addChannelToList(
291633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            List<Channel> channelList, Channel channel, long currentChannelId) {
29295961816a768da387f0b5523cf4363ace2044089Nick Chalko        if (channel == null
29395961816a768da387f0b5523cf4363ace2044089Nick Chalko                || channel.getId() == currentChannelId
29495961816a768da387f0b5523cf4363ace2044089Nick Chalko                || channelList.contains(channel)
29595961816a768da387f0b5523cf4363ace2044089Nick Chalko                || !channel.isBrowsable()) {
296633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            return false;
297633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        }
298633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        channelList.add(channel);
299633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return true;
300633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
301816a4be1a0f34f6a48877c8afd3dbbca19eac435Nick Chalko}
302