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; 1912d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbournimport android.support.v17.leanback.widget.ImageCardView; 2012d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbournimport android.support.v17.leanback.widget.Presenter; 21b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.support.v4.content.res.ResourcesCompat; 22f1f489269da4b349125df56d54d3259929d48a7dTim Kilbournimport android.text.TextUtils; 2312d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbournimport android.util.Log; 2487b904ffa106f12f2fecfe8b9a46084863678f29Dake Guimport android.view.ContextThemeWrapper; 257f3028385b0349cfc6c0d6784840be148943b296Dake Guimport android.view.View.MeasureSpec; 26b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Guimport android.view.ViewGroup; 277f3028385b0349cfc6c0d6784840be148943b296Dake Guimport android.view.ViewGroup.LayoutParams; 2812d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn 2908a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangliimport java.util.List; 30c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Guimport java.util.Random; 31c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu 3212d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbournpublic class CardPresenter extends Presenter { 3308a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli 3408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli // String constant 3512d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn private static final String TAG = "CardPresenter"; 3608a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli public static final String IMAGE = "ImageResourceId"; 3708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli public static final String TITLE = "Title"; 3808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli public static final String CONTENT = "Content"; 3912d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn 407f3028385b0349cfc6c0d6784840be148943b296Dake Gu private static final int IMAGE_HEIGHT_DP = 120; 417f3028385b0349cfc6c0d6784840be148943b296Dake Gu 42c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu private static Random sRand = new Random(); 4387b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu private int mRowHeight = 0; 4487b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu private int mExpandedRowHeight = 0; 457f3028385b0349cfc6c0d6784840be148943b296Dake Gu 4687b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu private int mCardThemeResId; 4787b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu private Context mContextThemeWrapper; 4887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu 4987b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu public CardPresenter(int cardThemeResId) { 5087b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu mCardThemeResId = cardThemeResId; 5187b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu } 5287b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu 5387b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu public CardPresenter() { 5487b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu mCardThemeResId = 0; 5587b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu } 5687b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu 5787b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu private void setupRowHeights(Context context) { 5887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu if (mRowHeight == 0) { 597f3028385b0349cfc6c0d6784840be148943b296Dake Gu float density = context.getResources().getDisplayMetrics().density; 607f3028385b0349cfc6c0d6784840be148943b296Dake Gu int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f); 617f3028385b0349cfc6c0d6784840be148943b296Dake Gu 627f3028385b0349cfc6c0d6784840be148943b296Dake Gu ImageCardView v = new ImageCardView(context); 637f3028385b0349cfc6c0d6784840be148943b296Dake Gu v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height); 647f3028385b0349cfc6c0d6784840be148943b296Dake Gu v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 6587b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu mRowHeight = v.getMeasuredHeight(); 667f3028385b0349cfc6c0d6784840be148943b296Dake Gu v.setActivated(true); 677f3028385b0349cfc6c0d6784840be148943b296Dake Gu v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 6887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu mExpandedRowHeight = v.getMeasuredHeight(); 697f3028385b0349cfc6c0d6784840be148943b296Dake Gu } 707f3028385b0349cfc6c0d6784840be148943b296Dake Gu } 717f3028385b0349cfc6c0d6784840be148943b296Dake Gu 7287b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu public int getRowHeight(Context context) { 737f3028385b0349cfc6c0d6784840be148943b296Dake Gu setupRowHeights(context); 7487b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu return mRowHeight; 757f3028385b0349cfc6c0d6784840be148943b296Dake Gu } 767f3028385b0349cfc6c0d6784840be148943b296Dake Gu 7787b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu public int getExpandedRowHeight(Context context) { 787f3028385b0349cfc6c0d6784840be148943b296Dake Gu setupRowHeights(context); 7987b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu return mExpandedRowHeight; 807f3028385b0349cfc6c0d6784840be148943b296Dake Gu } 817f3028385b0349cfc6c0d6784840be148943b296Dake Gu 827f3028385b0349cfc6c0d6784840be148943b296Dake Gu @Override 8312d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn public ViewHolder onCreateViewHolder(ViewGroup parent) { 8412d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn Log.d(TAG, "onCreateViewHolder"); 8587b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu Context context = parent.getContext(); 8687b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu if (mCardThemeResId != 0) { 8787b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu if (mContextThemeWrapper == null) { 8887b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu mContextThemeWrapper = new ContextThemeWrapper(context, mCardThemeResId); 8987b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu } 9087b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu context = mContextThemeWrapper; 9187b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu } 9287b904ffa106f12f2fecfe8b9a46084863678f29Dake Gu ImageCardView v = new ImageCardView(context); 9312d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn v.setFocusable(true); 9412d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn v.setFocusableInTouchMode(true); 95c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu // Randomly makes image view crop as a square or just stretch to original 96c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu // aspect ratio. 97c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu if (sRand.nextBoolean()) { 98c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu v.setMainImageAdjustViewBounds(false); 99c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu v.setMainImageDimensions(getRowHeight(parent.getContext()), 100c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu getRowHeight(parent.getContext())); 101c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu } else { 102c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu v.setMainImageAdjustViewBounds(true); 103c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, 104c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu getRowHeight(parent.getContext())); 105c84c57a1fdf1866ec0406e4723e49b0a1a32ab58Dake Gu } 10612d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn return new ViewHolder(v); 10712d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn } 10812d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn 1097f3028385b0349cfc6c0d6784840be148943b296Dake Gu @Override 11012d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn public void onBindViewHolder(ViewHolder viewHolder, Object item) { 11112d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn Log.d(TAG, "onBindViewHolder for " + item.toString()); 1127f3028385b0349cfc6c0d6784840be148943b296Dake Gu PhotoItem photoItem = (PhotoItem) item; 113b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu final Context context = viewHolder.view.getContext(); 11408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), 115b7087e036a48f5a3db28d02ff7f9b97fbbc46c4fDake Gu photoItem.getImageResourceId(), context.getTheme()); 1167f3028385b0349cfc6c0d6784840be148943b296Dake Gu ((ImageCardView) viewHolder.view).setMainImage(drawable); 1177f3028385b0349cfc6c0d6784840be148943b296Dake Gu ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle()); 118f1f489269da4b349125df56d54d3259929d48a7dTim Kilbourn if (!TextUtils.isEmpty(photoItem.getContent())) { 119f1f489269da4b349125df56d54d3259929d48a7dTim Kilbourn ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent()); 120f1f489269da4b349125df56d54d3259929d48a7dTim Kilbourn } 12112d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn } 12212d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn 1237f3028385b0349cfc6c0d6784840be148943b296Dake Gu @Override 12408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli public void onBindViewHolder(ViewHolder viewHolder, Object item, List<Object> payloads) { 12508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli if (payloads.isEmpty()) { 12608a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli super.onBindViewHolder(viewHolder, item, payloads); 12708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli } else { 12808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli PhotoItem photoItem = (PhotoItem) item; 12908a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli Bundle o = (Bundle) payloads.get(0); 13008a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli for (String key : o.keySet()) { 13108a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli if (key.equals(IMAGE)) { 13208a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli final Context context = viewHolder.view.getContext(); 13308a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), 13408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli photoItem.getImageResourceId(), context.getTheme()); 13508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli ((ImageCardView) viewHolder.view).setMainImage(drawable); 13608a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli } 13708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli if (key.equals(CONTENT)) { 13808a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent()); 13908a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli } 14008a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli if (key.equals(TITLE)) { 14108a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle()); 14208a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli } 14308a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli } 14408a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli } 14508a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli } 14608a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli 14708a38559a3751252fc8a1f36db0a431508a8f7d5jingjiangli @Override 14812d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn public void onUnbindViewHolder(ViewHolder viewHolder) { 14912d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn Log.d(TAG, "onUnbindViewHolder"); 15012d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn } 15112d4d450a8057b6d640fbe32fde63667ec2c8e83Tim Kilbourn} 152