aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/ui/ChannelTileView.java
blob: 6e7550719949b86bea8c63fd64769ae98d4b7bab (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
/*
 * Copyright (C) 2014 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.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.internal.util.Preconditions;
import com.android.tv.R;
import com.android.tv.data.Channel;
import com.android.tv.data.Program;
import com.android.tv.util.BitmapUtils;
import com.android.tv.util.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * A view to render channel tile.
 */
public class ChannelTileView extends ShadowContainer
        implements ItemListView.TileView, Channel.LoadLogoCallback {
    private static final String TAG = "ChannelTileView";

    private float mRoundRadius;
    private float mPosterArtWidth;
    private float mPosterArtHeight;
    private LinearLayout mChannelInfosLayout;
    private ImageView mProgramPosterArtView;
    private ImageView mChannelLogoView;
    private TextView mChannelNameView;
    private TextView mChannelNumberView;
    private TextView mProgramNameView;
    private Channel mChannel;
    private Drawable mNormalBackgroud;
    private Drawable mBackgroundOnImage;

    public ChannelTileView(Context context) {
        super(context);
    }

    public ChannelTileView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChannelTileView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void loadViews() {
        mChannelInfosLayout = (LinearLayout) findViewById(R.id.channel_infos);
        mChannelLogoView = (ImageView) findViewById(R.id.channel_logo);
        mChannelNameView = (TextView) findViewById(R.id.channel_name);
        mChannelNumberView = (TextView) findViewById(R.id.channel_number);
        mProgramNameView = (TextView) findViewById(R.id.program_name);
        mProgramPosterArtView = (ImageView) findViewById(R.id.program_poster_art);
        mChannelNameView.setVisibility(INVISIBLE);

        mNormalBackgroud = getResources().getDrawable(R.drawable.channel_tile_top);
        mBackgroundOnImage = getResources().getDrawable(R.drawable.channel_tile_top_on_image);
        mRoundRadius = getResources().getDimension(R.dimen.channel_tile_round_radius);
        mPosterArtWidth = getResources().getDimension(R.dimen.channel_tile_poster_art_width);
        mPosterArtHeight = getResources().getDimension(R.dimen.channel_tile_poster_art_height);
    }

    @Override
    public void populateViews(View.OnClickListener listener, Object item) {
        Preconditions.checkNotNull(item);

        mChannel = (Channel) item;
        setOnClickListener(listener);
        setTag(MainMenuView.MenuTag.buildTag(mChannel));

        if (mChannel.getType() == R.integer.channel_type_guide) {
            mChannelInfosLayout.setBackgroundResource(R.drawable.channel_tile_guide_top);
            mChannelNumberView.setVisibility(INVISIBLE);
            mChannelNameView.setVisibility(INVISIBLE);
            mProgramPosterArtView.setVisibility(INVISIBLE);
            mChannelLogoView.setImageResource(R.drawable.ic_channel_guide);
            mChannelLogoView.setVisibility(VISIBLE);
            mProgramNameView.setText(R.string.menu_program_guide);
            mProgramNameView.setBackgroundResource(R.drawable.channel_tile_guide_bottom);
        } else {
            mChannelNumberView.setText(mChannel.getDisplayNumber());
            mChannelNumberView.setVisibility(VISIBLE);
            if (!mChannel.isLogoLoaded()) {
                showName();
            }
            mChannel.loadLogo(getContext(), this);
            updateProgramInformation();
        }
    }

    @Override
    public void onLoadLogoFinished(Channel channel, Bitmap logo) {
        if (channel.getId() != mChannel.getId()) {
            return;
        }
        if (logo == null) {
            showName();
        } else {
            showLogo(logo);
        }
    }

    private void showName() {
        mChannelNameView.setText(mChannel.getDisplayName());
        mChannelNameView.setVisibility(VISIBLE);
        mChannelLogoView.setVisibility(INVISIBLE);
    }

    private void showLogo(Bitmap logo) {
        mChannelLogoView.setImageBitmap(logo);
        mChannelLogoView.setVisibility(VISIBLE);
        mChannelNameView.setVisibility(INVISIBLE);
    }

    private void setProgramPosterArt(Bitmap bm) {
        mProgramPosterArtView.setImageBitmap(BitmapUtils.getRoundedCornerBitmap(bm,
                mRoundRadius, mPosterArtWidth, mPosterArtHeight));
        mProgramPosterArtView.setVisibility(VISIBLE);
        mChannelInfosLayout.setBackground(mBackgroundOnImage);
    }

    public void updateProgramInformation() {
        if (mProgramNameView == null || mProgramPosterArtView == null || mChannel == null
                || mChannel.getType() == R.integer.channel_type_guide) {
            return;
        }

        Program program = Utils.getCurrentProgram(getContext(),
                Utils.getChannelUri(mChannel.getId()));
        if (program == null || TextUtils.isEmpty(program.getTitle())) {
            mProgramNameView.setText(getContext().getText(R.string.no_program_information));
        } else {
            mProgramNameView.setText(program.getTitle());
        }

        String posterArtUri = program.getPosterArtUri();
        if (!TextUtils.isEmpty(posterArtUri)) {
            mProgramPosterArtView.setVisibility(INVISIBLE);
            mChannelInfosLayout.setBackground(mNormalBackgroud);
            new AsyncTask<String, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(String... params) {
                    return BitmapUtils.decodeSampledBitmapFromUriString(getContext(), params[0],
                            (int) mPosterArtWidth, (int) mPosterArtHeight);
                }

                @Override
                protected void onPostExecute(Bitmap bm) {
                    if (bm != null) {
                        setProgramPosterArt(bm);
                    }
                }
            }.execute(posterArtUri);
        } else {
            mProgramPosterArtView.setVisibility(INVISIBLE);
            mChannelInfosLayout.setBackground(mNormalBackgroud);
        }
    }
}