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.text.TextUtils;
20 import android.util.Log;
21 import android.view.ContextThemeWrapper;
22 import android.view.View.MeasureSpec;
23 import android.view.ViewGroup;
24 import android.view.ViewGroup.LayoutParams;
25 
26 import androidx.core.content.res.ResourcesCompat;
27 import androidx.leanback.widget.ImageCardView;
28 import androidx.leanback.widget.Presenter;
29 
30 import org.jspecify.annotations.NonNull;
31 import org.jspecify.annotations.Nullable;
32 
33 import java.util.List;
34 import java.util.Random;
35 
36 public class CardPresenter extends Presenter {
37 
38     // String constant
39     private static final String TAG = "CardPresenter";
40     public static final String IMAGE = "ImageResourceId";
41     public static final String TITLE = "Title";
42     public static final String CONTENT = "Content";
43 
44     private static final int IMAGE_HEIGHT_DP = 120;
45 
46     private static Random sRand = new Random();
47     private int mRowHeight = 0;
48     private int mExpandedRowHeight = 0;
49 
50     private int mCardThemeResId;
51     private Context mContextThemeWrapper;
52 
CardPresenter(int cardThemeResId)53     public CardPresenter(int cardThemeResId) {
54         mCardThemeResId = cardThemeResId;
55     }
56 
CardPresenter()57     public CardPresenter() {
58         mCardThemeResId = 0;
59     }
60 
setupRowHeights(Context context)61     private void setupRowHeights(Context context) {
62         if (mRowHeight == 0) {
63             float density = context.getResources().getDisplayMetrics().density;
64             int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f);
65 
66             ImageCardView v = new ImageCardView(context);
67             v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height);
68             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
69             mRowHeight = v.getMeasuredHeight();
70             v.setActivated(true);
71             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
72             mExpandedRowHeight = v.getMeasuredHeight();
73         }
74     }
75 
getRowHeight(Context context)76     public int getRowHeight(Context context) {
77         setupRowHeights(context);
78         return mRowHeight;
79     }
80 
getExpandedRowHeight(Context context)81     public int getExpandedRowHeight(Context context) {
82         setupRowHeights(context);
83         return mExpandedRowHeight;
84     }
85 
86     @Override
onCreateViewHolder(ViewGroup parent)87     public ViewHolder onCreateViewHolder(ViewGroup parent) {
88         Log.d(TAG, "onCreateViewHolder");
89         Context context = parent.getContext();
90         if (mCardThemeResId != 0) {
91             if (mContextThemeWrapper == null) {
92                 mContextThemeWrapper = new ContextThemeWrapper(context, mCardThemeResId);
93             }
94             context = mContextThemeWrapper;
95         }
96         ImageCardView v = new ImageCardView(context);
97         v.setFocusable(true);
98         v.setFocusableInTouchMode(true);
99         // Randomly makes image view crop as a square or just stretch to original
100         // aspect ratio.
101         if (sRand.nextBoolean()) {
102             v.setMainImageAdjustViewBounds(false);
103             v.setMainImageDimensions(getRowHeight(parent.getContext()),
104                     getRowHeight(parent.getContext()));
105         } else {
106             v.setMainImageAdjustViewBounds(true);
107             v.setMainImageDimensions(LayoutParams.WRAP_CONTENT,
108                     getRowHeight(parent.getContext()));
109         }
110         return new ViewHolder(v);
111     }
112 
113     @Override
onBindViewHolder(@onNull ViewHolder viewHolder, @Nullable Object item)114     public void onBindViewHolder(@NonNull ViewHolder viewHolder, @Nullable Object item) {
115         Log.d(TAG, "onBindViewHolder for " + item);
116         PhotoItem photoItem = (PhotoItem) item;
117         final Context context = viewHolder.view.getContext();
118         Drawable drawable = photoItem == null ? null :
119                 ResourcesCompat.getDrawable(context.getResources(),
120                         photoItem.getImageResourceId(), context.getTheme());
121         ((ImageCardView) viewHolder.view).setMainImage(drawable);
122         String title = photoItem == null ? null : photoItem.getTitle();
123         ((ImageCardView) viewHolder.view).setTitleText(title);
124         String content = photoItem == null ? null : photoItem.getContent();
125         if (!TextUtils.isEmpty(content)) {
126             ((ImageCardView) viewHolder.view).setContentText(content);
127         }
128     }
129 
130     @Override
onBindViewHolder(ViewHolder viewHolder, Object item, List<Object> payloads)131     public void onBindViewHolder(ViewHolder viewHolder, Object item, List<Object> payloads) {
132         if (payloads.isEmpty()) {
133             super.onBindViewHolder(viewHolder, item, payloads);
134         } else {
135             PhotoItem photoItem = (PhotoItem) item;
136             Bundle o = (Bundle) payloads.get(0);
137             for (String key : o.keySet()) {
138                 if (key.equals(IMAGE)) {
139                     final Context context = viewHolder.view.getContext();
140                     Drawable drawable = ResourcesCompat.getDrawable(context.getResources(),
141                             photoItem.getImageResourceId(), context.getTheme());
142                     ((ImageCardView) viewHolder.view).setMainImage(drawable);
143                 }
144                 if (key.equals(CONTENT)) {
145                     ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent());
146                 }
147                 if (key.equals(TITLE)) {
148                     ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle());
149                 }
150             }
151         }
152     }
153 
154     @Override
onUnbindViewHolder(ViewHolder viewHolder)155     public void onUnbindViewHolder(ViewHolder viewHolder) {
156         Log.d(TAG, "onUnbindViewHolder");
157     }
158 }
159