ChannelsRowAdapter.java revision 7d67089aa1e9aa2123c3cd2f386d7019a1544db1
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.tv.menu;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.Build;
22import android.view.View;
23
24import com.android.tv.MainActivity;
25import com.android.tv.R;
26import com.android.tv.TvApplication;
27import com.android.tv.analytics.Tracker;
28import com.android.tv.data.Channel;
29import com.android.tv.recommendation.Recommender;
30import com.android.tv.util.SetupUtils;
31
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * An adapter of the Channels row.
37 */
38public class ChannelsRowAdapter extends ItemListRowView.ItemListAdapter<Channel> {
39    private static final int POSITION_FIRST_CARD = 0;
40    private static final int POSITION_SECOND_CARD = 1;
41    private static final int POSITION_THIRD_CARD = 2;
42    private final Context mContext;
43    private final Tracker mTracker;
44    private final Recommender mRecommender;
45    private final int mMaxCount;
46    private final int mMinCount;
47    private boolean mShowSetupCard;
48    private boolean mShowAppLinkCard;
49
50    private final View.OnClickListener mGuideOnClickListener = new View.OnClickListener() {
51        @Override
52        public void onClick(View view) {
53            mTracker.sendMenuClicked(R.string.channels_item_program_guide);
54            getMainActivity().getOverlayManager().showProgramGuide();
55        }
56    };
57
58    private final View.OnClickListener mSetupOnClickListener = new View.OnClickListener() {
59        @Override
60        public void onClick(View view) {
61            mTracker.sendMenuClicked(R.string.channels_item_setup);
62            getMainActivity().getOverlayManager().showSetupDialog();
63        }
64    };
65
66    private final View.OnClickListener mAppLinkOnClickListener = new View.OnClickListener() {
67        @Override
68        public void onClick(View view) {
69            mTracker.sendMenuClicked(R.string.channels_item_app_link);
70            Intent intent = ((AppLinkCardView) view).getIntent();
71            if (intent != null) {
72                getMainActivity().startActivitySafe(intent);
73            }
74        }
75    };
76
77    private final View.OnClickListener mChannelOnClickListener = new View.OnClickListener() {
78        @Override
79        public void onClick(View view) {
80            // Always send the label "Channels" because the channel ID or name or number might be
81            // sensitive.
82            mTracker.sendMenuClicked(R.string.menu_title_channels);
83            getMainActivity().tuneToChannel((Channel) view.getTag());
84            getMainActivity().hideOverlaysForTune();
85        }
86    };
87
88    public ChannelsRowAdapter(Context context, Recommender recommender,
89            int minCount, int maxCount) {
90        super(context);
91        mTracker = ((TvApplication) context.getApplicationContext()).getTracker();
92        mContext = context;
93        mRecommender = recommender;
94        mMinCount = minCount;
95        mMaxCount = maxCount;
96    }
97
98    @Override
99    public int getItemViewType(int position) {
100        switch (position) {
101            case POSITION_FIRST_CARD:
102                return R.layout.menu_card_guide;
103            case POSITION_SECOND_CARD:
104                return mShowSetupCard
105                        ? R.layout.menu_card_setup
106                        : mShowAppLinkCard
107                                ? R.layout.menu_card_app_link
108                                : R.layout.menu_card_channel;
109            case POSITION_THIRD_CARD:
110                return (mShowSetupCard && mShowAppLinkCard)
111                        ? R.layout.menu_card_app_link
112                        : R.layout.menu_card_channel;
113            default:
114                return R.layout.menu_card_channel;
115        }
116    }
117
118    @Override
119    protected int getLayoutResId(int viewType) {
120        return viewType;
121    }
122
123    @Override
124    public void onBindViewHolder(MyViewHolder viewHolder, int position) {
125        super.onBindViewHolder(viewHolder, position);
126
127        int viewType = getItemViewType(position);
128        if (viewType == R.layout.menu_card_guide) {
129            viewHolder.itemView.setOnClickListener(mGuideOnClickListener);
130        } else if (viewType == R.layout.menu_card_setup) {
131            viewHolder.itemView.setOnClickListener(mSetupOnClickListener);
132        } else if (viewType == R.layout.menu_card_app_link) {
133            viewHolder.itemView.setOnClickListener(mAppLinkOnClickListener);
134        } else {
135            viewHolder.itemView.setTag(getItemList().get(position));
136            viewHolder.itemView.setOnClickListener(mChannelOnClickListener);
137        }
138    }
139
140    @Override
141    public void update() {
142        List<Channel> channelList = new ArrayList<>();
143        Channel dummyChannel = new Channel.Builder()
144                .build();
145        // For guide item
146        channelList.add(dummyChannel);
147        // For setup item
148        mShowSetupCard = SetupUtils.getInstance(mContext).hasNewInput(
149                ((MainActivity) mContext).getTvInputManagerHelper());
150        if (mShowSetupCard) {
151            channelList.add(dummyChannel);
152        }
153        if (Build.VERSION.SDK_INT >= 23) {
154            Channel currentChannel = ((MainActivity) mContext).getCurrentChannel();
155            mShowAppLinkCard = currentChannel != null
156                    && currentChannel.getAppLinkType(mContext) != Channel.APP_LINK_TYPE_NONE;
157            if (mShowAppLinkCard) {
158                channelList.add(currentChannel);
159            }
160        }
161
162        channelList.addAll(getRecentChannels());
163        setItemList(channelList);
164    }
165
166    private List<Channel> getRecentChannels() {
167        List<Channel> channelList = new ArrayList<>();
168        for (Channel channel : mRecommender.recommendChannels(mMaxCount)) {
169            if (channel.isBrowsable()) {
170                channelList.add(channel);
171            }
172        }
173        int count = channelList.size();
174        // If the number of recommended channels is not enough, add more from the recent channel
175        // list.
176        if (count < mMinCount && mContext instanceof MainActivity) {
177            for (long channelId : ((MainActivity) mContext).getRecentChannels()) {
178                Channel channel = mRecommender.getChannel(channelId);
179                if (channel == null || channelList.contains(channel)
180                        || !channel.isBrowsable()) {
181                   continue;
182                }
183                channelList.add(channel);
184                if (++count >= mMinCount) {
185                    break;
186                }
187            }
188        }
189        return channelList;
190    }
191}
192