aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/menu/AppLinkCardView.java
blob: 94ccd37f30ee121eaf583c709755a12335a45050 (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
/*
 * 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.menu;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.support.v7.graphics.Palette;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.tv.MainActivity;
import com.android.tv.R;
import com.android.tv.data.Channel;
import com.android.tv.util.BitmapUtils;
import com.android.tv.util.ImageLoader;
import com.android.tv.util.TvInputManagerHelper;

import java.util.Objects;

/**
 * A view to render an app link card.
 */
public class AppLinkCardView extends BaseCardView<ChannelsRowItem> {
    private static final String TAG = MenuView.TAG;
    private static final boolean DEBUG = MenuView.DEBUG;

    private final int mCardImageWidth;
    private final int mCardImageHeight;
    private final int mIconWidth;
    private final int mIconHeight;
    private final int mIconPadding;
    private final int mIconColorFilter;
    private final Drawable mDefaultDrawable;

    private ImageView mImageView;
    private TextView mAppInfoView;
    private View mMetaViewHolder;
    private Channel mChannel;
    private Intent mIntent;
    private final PackageManager mPackageManager;
    private final TvInputManagerHelper mTvInputManagerHelper;

    public AppLinkCardView(Context context) {
        this(context, null);
    }

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

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

        mCardImageWidth = getResources().getDimensionPixelSize(R.dimen.card_image_layout_width);
        mCardImageHeight = getResources().getDimensionPixelSize(R.dimen.card_image_layout_height);
        mIconWidth = getResources().getDimensionPixelSize(R.dimen.app_link_card_icon_width);
        mIconHeight = getResources().getDimensionPixelSize(R.dimen.app_link_card_icon_height);
        mIconPadding = getResources().getDimensionPixelOffset(R.dimen.app_link_card_icon_padding);
        mPackageManager = context.getPackageManager();
        mTvInputManagerHelper = ((MainActivity) context).getTvInputManagerHelper();
        mIconColorFilter = getResources().getColor(R.color.app_link_card_icon_color_filter, null);
        mDefaultDrawable = getResources().getDrawable(R.drawable.ic_recent_thumbnail_default, null);
    }

    /**
     * Returns the intent which will be started once this card is clicked.
     */
    public Intent getIntent() {
        return mIntent;
    }

    @Override
    public void onBind(ChannelsRowItem item, boolean selected) {
        Channel newChannel = item.getChannel();
        boolean channelChanged = !Objects.equals(mChannel, newChannel);
        String previousPosterArtUri = mChannel == null ? null : mChannel.getAppLinkPosterArtUri();
        boolean posterArtChanged = previousPosterArtUri == null
                || newChannel.getAppLinkPosterArtUri() == null
                || !TextUtils.equals(previousPosterArtUri, newChannel.getAppLinkPosterArtUri());
        mChannel = newChannel;
        if (DEBUG) {
            Log.d(TAG, "onBind(channelName=" + mChannel.getDisplayName() + ", selected=" + selected
                    + ")");
        }
        ApplicationInfo appInfo = mTvInputManagerHelper.getTvInputAppInfo(mChannel.getInputId());
        if (channelChanged) {
            int linkType = mChannel.getAppLinkType(getContext());
            mIntent = mChannel.getAppLinkIntent(getContext());

            CharSequence appLabel;
            switch (linkType) {
                case Channel.APP_LINK_TYPE_CHANNEL:
                    setText(mChannel.getAppLinkText());
                    mAppInfoView.setVisibility(VISIBLE);
                    mAppInfoView.setCompoundDrawablePadding(mIconPadding);
                    mAppInfoView.setCompoundDrawablesRelative(null, null, null, null);
                    appLabel = mTvInputManagerHelper
                            .getTvInputApplicationLabel(mChannel.getInputId());
                    if (appLabel != null) {
                        mAppInfoView.setText(appLabel);
                    } else {
                        new AsyncTask<Void, Void, CharSequence>() {
                            private final String mLoadTvInputId = mChannel.getInputId();

                            @Override
                            protected CharSequence doInBackground(Void... params) {
                                if (appInfo != null) {
                                    return mPackageManager.getApplicationLabel(appInfo);
                                }
                                return null;
                            }

                            @Override
                            protected void onPostExecute(CharSequence appLabel) {
                                mTvInputManagerHelper.setTvInputApplicationLabel(
                                        mLoadTvInputId, appLabel);
                                if (mLoadTvInputId != mChannel.getInputId()
                                        || !isAttachedToWindow()) {
                                    return;
                                }
                                mAppInfoView.setText(appLabel);
                            }
                        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    }
                    if (!TextUtils.isEmpty(mChannel.getAppLinkIconUri())) {
                        mChannel.loadBitmap(getContext(), Channel.LOAD_IMAGE_TYPE_APP_LINK_ICON,
                                mIconWidth, mIconHeight, createChannelLogoCallback(
                                        this, mChannel, Channel.LOAD_IMAGE_TYPE_APP_LINK_ICON));
                    } else if (appInfo.icon != 0) {
                        Drawable appIcon = mTvInputManagerHelper
                                .getTvInputApplicationIcon(mChannel.getInputId());
                        if (appIcon != null) {
                            BitmapUtils.setColorFilterToDrawable(mIconColorFilter, appIcon);
                            appIcon.setBounds(0, 0, mIconWidth, mIconHeight);
                            mAppInfoView.setCompoundDrawablesRelative(appIcon, null, null, null);
                        } else {
                            new AsyncTask<Void, Void, Drawable>() {
                                private final String mLoadTvInputId = mChannel.getInputId();

                                @Override
                                protected Drawable doInBackground(Void... params) {
                                    return mPackageManager.getApplicationIcon(appInfo);
                                }

                                @Override
                                protected void onPostExecute(Drawable appIcon) {
                                    mTvInputManagerHelper.setTvInputApplicationIcon(
                                            mLoadTvInputId, appIcon);
                                    if (!mLoadTvInputId.equals(mChannel.getInputId())
                                            || !isAttachedToWindow()) {
                                        return;
                                    }
                                    BitmapUtils.setColorFilterToDrawable(mIconColorFilter, appIcon);
                                    appIcon.setBounds(0, 0, mIconWidth, mIconHeight);
                                    mAppInfoView.setCompoundDrawablesRelative(
                                            appIcon, null, null, null);
                                }
                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    }
                    break;
                case Channel.APP_LINK_TYPE_APP:
                    appLabel = mTvInputManagerHelper
                        .getTvInputApplicationLabel(mChannel.getInputId());
                    if (appLabel != null) {
                        setText(getContext()
                            .getString(R.string.channels_item_app_link_app_launcher, appLabel));
                    } else {
                        new AsyncTask<Void, Void, CharSequence>() {
                            private final String mLoadTvInputId = mChannel.getInputId();

                            @Override
                            protected CharSequence doInBackground(Void... params) {
                                if (appInfo != null) {
                                    return mPackageManager.getApplicationLabel(appInfo);
                                }
                                return null;
                            }

                            @Override
                            protected void onPostExecute(CharSequence appLabel) {
                                mTvInputManagerHelper.setTvInputApplicationLabel(
                                    mLoadTvInputId, appLabel);
                                if (!mLoadTvInputId.equals(mChannel.getInputId())
                                    || !isAttachedToWindow()) {
                                    return;
                                }
                                setText(getContext()
                                    .getString(
                                        R.string.channels_item_app_link_app_launcher,
                                        appLabel));
                            }
                        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    }
                    mAppInfoView.setVisibility(GONE);
                    break;
                default:
                    mAppInfoView.setVisibility(GONE);
                    Log.d(TAG, "Should not be here.");
            }

            if (mChannel.getAppLinkColor() == 0) {
                mMetaViewHolder.setBackgroundResource(R.color.channel_card_meta_background);
            } else {
                mMetaViewHolder.setBackgroundColor(mChannel.getAppLinkColor());
            }
        }
        if (posterArtChanged) {
            mImageView.setImageDrawable(mDefaultDrawable);
            mImageView.setForeground(null);
            if (!TextUtils.isEmpty(mChannel.getAppLinkPosterArtUri())) {
                mChannel.loadBitmap(getContext(), Channel.LOAD_IMAGE_TYPE_APP_LINK_POSTER_ART,
                        mCardImageWidth, mCardImageHeight, createChannelLogoCallback(this, mChannel,
                                Channel.LOAD_IMAGE_TYPE_APP_LINK_POSTER_ART));
            } else {
                setCardImageWithBanner(appInfo);
            }
        }
        super.onBind(item, selected);
    }

    private static ImageLoader.ImageLoaderCallback<AppLinkCardView> createChannelLogoCallback(
            AppLinkCardView cardView, final Channel channel, final int type) {
        return new ImageLoader.ImageLoaderCallback<AppLinkCardView>(cardView) {
            @Override
            public void onBitmapLoaded(AppLinkCardView cardView, @Nullable Bitmap bitmap) {
                // mChannel can be changed before the image load finished.
                if (!cardView.mChannel.hasSameReadOnlyInfo(channel)) {
                    return;
                }
                cardView.updateChannelLogo(bitmap, type);
            }
        };
    }

    private void updateChannelLogo(@Nullable Bitmap bitmap, int type) {
        if (type == Channel.LOAD_IMAGE_TYPE_APP_LINK_ICON) {
            BitmapDrawable drawable = null;
            if (bitmap != null) {
                drawable = new BitmapDrawable(getResources(), bitmap);
                if (bitmap.getWidth() > bitmap.getHeight()) {
                    drawable.setBounds(0, 0, mIconWidth,
                            mIconWidth * bitmap.getHeight() / bitmap.getWidth());
                } else {
                    drawable.setBounds(0, 0,
                            mIconHeight * bitmap.getWidth() / bitmap.getHeight(),
                            mIconHeight);
                }
            }
            BitmapUtils.setColorFilterToDrawable(mIconColorFilter, drawable);
            mAppInfoView.setCompoundDrawablesRelative(drawable, null, null, null);
        } else if (type == Channel.LOAD_IMAGE_TYPE_APP_LINK_POSTER_ART) {
            if (bitmap == null) {
                setCardImageWithBanner(
                        mTvInputManagerHelper.getTvInputAppInfo(mChannel.getInputId()));
            } else {
                mImageView.setImageBitmap(bitmap);
                mImageView.setForeground(getContext().getDrawable(R.drawable.card_image_gradient));
                if (mChannel.getAppLinkColor() == 0) {
                    extractAndSetMetaViewBackgroundColor(bitmap);
                }
            }
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mImageView = (ImageView) findViewById(R.id.image);
        mAppInfoView = (TextView) findViewById(R.id.app_info);
        mMetaViewHolder = findViewById(R.id.app_link_text_holder);
    }

    // Try to set the card image with following order:
    // 1) Provided poster art image, 2) Activity banner, 3) Activity icon, 4) Application banner,
    // 5) Application icon, and 6) default image.
    private void setCardImageWithBanner(ApplicationInfo appInfo) {
        new AsyncTask<Void, Void, Drawable>() {
            private String mLoadTvInputId = mChannel.getInputId();
            @Override
            protected Drawable doInBackground(Void... params) {
                Drawable banner = null;
                if (mIntent != null) {
                    try {
                        banner = mPackageManager.getActivityBanner(mIntent);
                        if (banner == null) {
                            banner = mPackageManager.getActivityIcon(mIntent);
                        }
                    } catch (PackageManager.NameNotFoundException e) {
                        // do nothing.
                    }
                }
                return banner;
            }

            @Override
            protected void onPostExecute(Drawable banner) {
                if (mLoadTvInputId != mChannel.getInputId() || !isAttachedToWindow()) {
                    return;
                }
                if (banner != null) {
                    setCardImageWithBannerInternal(banner);
                } else {
                    setCardImageWithApplicationInfoBanner(appInfo);
                }
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    private void setCardImageWithApplicationInfoBanner(ApplicationInfo appInfo) {
        Drawable appBanner =
                mTvInputManagerHelper.getTvInputApplicationBanner(mChannel.getInputId());
        if (appBanner != null) {
            setCardImageWithBannerInternal(appBanner);
        } else {
            new AsyncTask<Void, Void, Drawable>() {
                private final String mLoadTvInputId = mChannel.getInputId();
                @Override
                protected Drawable doInBackground(Void... params) {
                    Drawable banner = null;
                    if (appInfo != null) {
                        if (appInfo.banner != 0) {
                            banner = mPackageManager.getApplicationBanner(appInfo);
                        }
                        if (banner == null && appInfo.icon != 0) {
                            banner = mPackageManager.getApplicationIcon(appInfo);
                        }
                    }
                    return banner;
                }

                @Override
                protected void onPostExecute(Drawable banner) {
                    mTvInputManagerHelper.setTvInputApplicationBanner(mLoadTvInputId, banner);
                    if (!TextUtils.equals(mLoadTvInputId, mChannel.getInputId())
                            || !isAttachedToWindow()) {
                        return;
                    }
                    setCardImageWithBannerInternal(banner);
                }
            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    }

    private void setCardImageWithBannerInternal(Drawable banner) {
        if (banner == null) {
            mImageView.setImageDrawable(mDefaultDrawable);
            mImageView.setBackgroundResource(R.color.channel_card);
        } else {
            Bitmap bitmap = Bitmap.createBitmap(
                    mCardImageWidth, mCardImageHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            banner.setBounds(0, 0, mCardImageWidth, mCardImageHeight);
            banner.draw(canvas);
            mImageView.setImageDrawable(banner);
            mImageView.setForeground(getContext().getDrawable(R.drawable.card_image_gradient));
            if (mChannel.getAppLinkColor() == 0) {
                extractAndSetMetaViewBackgroundColor(bitmap);
            }
        }
    }

    private void extractAndSetMetaViewBackgroundColor(Bitmap bitmap) {
        new Palette.Builder(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                mMetaViewHolder.setBackgroundColor(palette.getDarkVibrantColor(
                        getResources().getColor(R.color.channel_card_meta_background, null)));
            }
        });
    }
}