112d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn/*
212d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * Copyright (C) 2014 The Android Open Source Project
312d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn *
412d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
512d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * in compliance with the License. You may obtain a copy of the License at
612d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn *
712d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * http://www.apache.org/licenses/LICENSE-2.0
812d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn *
912d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * Unless required by applicable law or agreed to in writing, software distributed under the License
1012d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1112d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * or implied. See the License for the specific language governing permissions and limitations under
1212d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn * the License.
1312d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn */
1412d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbournpackage com.example.android.leanback;
1512d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn
167f3028385b0349cfc6c0d6784840be148943b296Dake Guimport android.content.Context;
177f3028385b0349cfc6c0d6784840be148943b296Dake Guimport android.graphics.drawable.Drawable;
1808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangliimport android.os.Bundle;
19f1f489269da4b349125df56d54d3259929d48a7dTim Kilbournimport android.text.TextUtils;
2012d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbournimport android.util.Log;
2187b904ffa106f12f2fecfe8b9a46084863678f29Dake Guimport android.view.ContextThemeWrapper;
227f3028385b0349cfc6c0d6784840be148943b296Dake Guimport android.view.View.MeasureSpec;
23b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.view.ViewGroup;
247f3028385b0349cfc6c0d6784840be148943b296Dake Guimport android.view.ViewGroup.LayoutParams;
2512d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn
26def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.core.content.res.ResourcesCompat;
27def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.leanback.widget.ImageCardView;
28def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.leanback.widget.Presenter;
29def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas
3008a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangliimport java.util.List;
31c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Guimport java.util.Random;
32c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu
3312d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbournpublic class CardPresenter extends Presenter {
3408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli
3508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli    // String constant
3612d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn    private static final String TAG = "CardPresenter";
3708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli    public static final String IMAGE = "ImageResourceId";
3808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli    public static final String TITLE = "Title";
3908a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli    public static final String CONTENT = "Content";
4012d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn
417f3028385b0349cfc6c0d6784840be148943b296Dake Gu    private static final int IMAGE_HEIGHT_DP = 120;
427f3028385b0349cfc6c0d6784840be148943b296Dake Gu
43c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu    private static Random sRand = new Random();
4487b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    private int mRowHeight = 0;
4587b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    private int mExpandedRowHeight = 0;
467f3028385b0349cfc6c0d6784840be148943b296Dake Gu
4787b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    private int mCardThemeResId;
4887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    private Context mContextThemeWrapper;
4987b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu
5087b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    public CardPresenter(int cardThemeResId) {
5187b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        mCardThemeResId = cardThemeResId;
5287b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    }
5387b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu
5487b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    public CardPresenter() {
5587b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        mCardThemeResId = 0;
5687b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    }
5787b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu
5887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    private void setupRowHeights(Context context) {
5987b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        if (mRowHeight == 0) {
607f3028385b0349cfc6c0d6784840be148943b296Dake Gu            float density = context.getResources().getDisplayMetrics().density;
617f3028385b0349cfc6c0d6784840be148943b296Dake Gu            int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f);
627f3028385b0349cfc6c0d6784840be148943b296Dake Gu
637f3028385b0349cfc6c0d6784840be148943b296Dake Gu            ImageCardView v = new ImageCardView(context);
647f3028385b0349cfc6c0d6784840be148943b296Dake Gu            v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height);
657f3028385b0349cfc6c0d6784840be148943b296Dake Gu            v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
6687b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu            mRowHeight = v.getMeasuredHeight();
677f3028385b0349cfc6c0d6784840be148943b296Dake Gu            v.setActivated(true);
687f3028385b0349cfc6c0d6784840be148943b296Dake Gu            v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
6987b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu            mExpandedRowHeight = v.getMeasuredHeight();
707f3028385b0349cfc6c0d6784840be148943b296Dake Gu        }
717f3028385b0349cfc6c0d6784840be148943b296Dake Gu    }
727f3028385b0349cfc6c0d6784840be148943b296Dake Gu
7387b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    public int getRowHeight(Context context) {
747f3028385b0349cfc6c0d6784840be148943b296Dake Gu        setupRowHeights(context);
7587b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        return mRowHeight;
767f3028385b0349cfc6c0d6784840be148943b296Dake Gu    }
777f3028385b0349cfc6c0d6784840be148943b296Dake Gu
7887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu    public int getExpandedRowHeight(Context context) {
797f3028385b0349cfc6c0d6784840be148943b296Dake Gu        setupRowHeights(context);
8087b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        return mExpandedRowHeight;
817f3028385b0349cfc6c0d6784840be148943b296Dake Gu    }
827f3028385b0349cfc6c0d6784840be148943b296Dake Gu
837f3028385b0349cfc6c0d6784840be148943b296Dake Gu    @Override
8412d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn    public ViewHolder onCreateViewHolder(ViewGroup parent) {
8512d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn        Log.d(TAG, "onCreateViewHolder");
8687b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        Context context = parent.getContext();
8787b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        if (mCardThemeResId != 0) {
8887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu            if (mContextThemeWrapper == null) {
8987b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu                mContextThemeWrapper = new ContextThemeWrapper(context, mCardThemeResId);
9087b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu            }
9187b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu            context = mContextThemeWrapper;
9287b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        }
9387b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu        ImageCardView v = new ImageCardView(context);
9412d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn        v.setFocusable(true);
9512d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn        v.setFocusableInTouchMode(true);
96c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu        // Randomly makes image view crop as a square or just stretch to original
97c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu        // aspect ratio.
98c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu        if (sRand.nextBoolean()) {
99c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu            v.setMainImageAdjustViewBounds(false);
100c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu            v.setMainImageDimensions(getRowHeight(parent.getContext()),
101c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu                    getRowHeight(parent.getContext()));
102c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu        } else {
103c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu            v.setMainImageAdjustViewBounds(true);
104c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu            v.setMainImageDimensions(LayoutParams.WRAP_CONTENT,
105c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu                    getRowHeight(parent.getContext()));
106c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu        }
10712d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn        return new ViewHolder(v);
10812d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn    }
10912d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn
1107f3028385b0349cfc6c0d6784840be148943b296Dake Gu    @Override
11112d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
11212d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn        Log.d(TAG, "onBindViewHolder for " + item.toString());
1137f3028385b0349cfc6c0d6784840be148943b296Dake Gu        PhotoItem photoItem = (PhotoItem) item;
114b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu        final Context context = viewHolder.view.getContext();
11508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli        Drawable drawable = ResourcesCompat.getDrawable(context.getResources(),
116b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu                photoItem.getImageResourceId(), context.getTheme());
1177f3028385b0349cfc6c0d6784840be148943b296Dake Gu        ((ImageCardView) viewHolder.view).setMainImage(drawable);
1187f3028385b0349cfc6c0d6784840be148943b296Dake Gu        ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle());
119f1f489269da4b349125df56d54d3259929d48a7dTim Kilbourn        if (!TextUtils.isEmpty(photoItem.getContent())) {
120f1f489269da4b349125df56d54d3259929d48a7dTim Kilbourn            ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent());
121f1f489269da4b349125df56d54d3259929d48a7dTim Kilbourn        }
12212d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn    }
12312d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn
1247f3028385b0349cfc6c0d6784840be148943b296Dake Gu    @Override
12508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli    public void onBindViewHolder(ViewHolder viewHolder, Object item, List<Object> payloads) {
12608a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli        if (payloads.isEmpty()) {
12708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli            super.onBindViewHolder(viewHolder, item, payloads);
12808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli        } else {
12908a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli            PhotoItem photoItem = (PhotoItem) item;
13008a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli            Bundle o = (Bundle) payloads.get(0);
13108a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli            for (String key : o.keySet()) {
13208a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                if (key.equals(IMAGE)) {
13308a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                    final Context context = viewHolder.view.getContext();
13408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                    Drawable drawable = ResourcesCompat.getDrawable(context.getResources(),
13508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                            photoItem.getImageResourceId(), context.getTheme());
13608a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                    ((ImageCardView) viewHolder.view).setMainImage(drawable);
13708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                }
13808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                if (key.equals(CONTENT)) {
13908a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                    ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent());
14008a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                }
14108a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                if (key.equals(TITLE)) {
14208a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                    ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle());
14308a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli                }
14408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli            }
14508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli        }
14608a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli    }
14708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli
14808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli    @Override
14912d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn    public void onUnbindViewHolder(ViewHolder viewHolder) {
15012d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn        Log.d(TAG, "onUnbindViewHolder");
15112d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn    }
15212d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn}
153