1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.example.android.leanback; 15 16 import android.content.Context; 17 import android.graphics.drawable.Drawable; 18 import android.os.Bundle; 19 import android.support.v17.leanback.widget.ImageCardView; 20 import android.support.v17.leanback.widget.Presenter; 21 import android.support.v4.content.res.ResourcesCompat; 22 import android.text.TextUtils; 23 import android.util.Log; 24 import android.view.ContextThemeWrapper; 25 import android.view.View.MeasureSpec; 26 import android.view.ViewGroup; 27 import android.view.ViewGroup.LayoutParams; 28 29 import java.util.List; 30 import java.util.Random; 31 32 public class CardPresenter extends Presenter { 33 34 // String constant 35 private static final String TAG = "CardPresenter"; 36 public static final String IMAGE = "ImageResourceId"; 37 public static final String TITLE = "Title"; 38 public static final String CONTENT = "Content"; 39 40 private static final int IMAGE_HEIGHT_DP = 120; 41 42 private static Random sRand = new Random(); 43 private int mRowHeight = 0; 44 private int mExpandedRowHeight = 0; 45 46 private int mCardThemeResId; 47 private Context mContextThemeWrapper; 48 49 public CardPresenter(int cardThemeResId) { 50 mCardThemeResId = cardThemeResId; 51 } 52 53 public CardPresenter() { 54 mCardThemeResId = 0; 55 } 56 57 private void setupRowHeights(Context context) { 58 if (mRowHeight == 0) { 59 float density = context.getResources().getDisplayMetrics().density; 60 int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f); 61 62 ImageCardView v = new ImageCardView(context); 63 v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height); 64 v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 65 mRowHeight = v.getMeasuredHeight(); 66 v.setActivated(true); 67 v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 68 mExpandedRowHeight = v.getMeasuredHeight(); 69 } 70 } 71 72 public int getRowHeight(Context context) { 73 setupRowHeights(context); 74 return mRowHeight; 75 } 76 77 public int getExpandedRowHeight(Context context) { 78 setupRowHeights(context); 79 return mExpandedRowHeight; 80 } 81 82 @Override 83 public ViewHolder onCreateViewHolder(ViewGroup parent) { 84 Log.d(TAG, "onCreateViewHolder"); 85 Context context = parent.getContext(); 86 if (mCardThemeResId != 0) { 87 if (mContextThemeWrapper == null) { 88 mContextThemeWrapper = new ContextThemeWrapper(context, mCardThemeResId); 89 } 90 context = mContextThemeWrapper; 91 } 92 ImageCardView v = new ImageCardView(context); 93 v.setFocusable(true); 94 v.setFocusableInTouchMode(true); 95 // Randomly makes image view crop as a square or just stretch to original 96 // aspect ratio. 97 if (sRand.nextBoolean()) { 98 v.setMainImageAdjustViewBounds(false); 99 v.setMainImageDimensions(getRowHeight(parent.getContext()), 100 getRowHeight(parent.getContext())); 101 } else { 102 v.setMainImageAdjustViewBounds(true); 103 v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, 104 getRowHeight(parent.getContext())); 105 } 106 return new ViewHolder(v); 107 } 108 109 @Override 110 public void onBindViewHolder(ViewHolder viewHolder, Object item) { 111 Log.d(TAG, "onBindViewHolder for " + item.toString()); 112 PhotoItem photoItem = (PhotoItem) item; 113 final Context context = viewHolder.view.getContext(); 114 Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), 115 photoItem.getImageResourceId(), context.getTheme()); 116 ((ImageCardView) viewHolder.view).setMainImage(drawable); 117 ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle()); 118 if (!TextUtils.isEmpty(photoItem.getContent())) { 119 ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent()); 120 } 121 } 122 123 @Override 124 public void onBindViewHolder(ViewHolder viewHolder, Object item, List<Object> payloads) { 125 if (payloads.isEmpty()) { 126 super.onBindViewHolder(viewHolder, item, payloads); 127 } else { 128 PhotoItem photoItem = (PhotoItem) item; 129 Bundle o = (Bundle) payloads.get(0); 130 for (String key : o.keySet()) { 131 if (key.equals(IMAGE)) { 132 final Context context = viewHolder.view.getContext(); 133 Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), 134 photoItem.getImageResourceId(), context.getTheme()); 135 ((ImageCardView) viewHolder.view).setMainImage(drawable); 136 } 137 if (key.equals(CONTENT)) { 138 ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent()); 139 } 140 if (key.equals(TITLE)) { 141 ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle()); 142 } 143 } 144 } 145 } 146 147 @Override 148 public void onUnbindViewHolder(ViewHolder viewHolder) { 149 Log.d(TAG, "onUnbindViewHolder"); 150 } 151 } 152