1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.leanbackjank.app.presenter;
18 
19 import android.graphics.drawable.Drawable;
20 import android.net.Uri;
21 import androidx.leanback.widget.ImageCardView;
22 import androidx.leanback.widget.Presenter;
23 import android.view.ViewGroup;
24 
25 import com.bumptech.glide.Glide;
26 import com.bumptech.glide.load.resource.drawable.GlideDrawable;
27 import com.bumptech.glide.request.RequestListener;
28 import com.bumptech.glide.request.target.Target;
29 import android.leanbackjank.app.R;
30 import android.leanbackjank.app.model.Movie;
31 
32 /**
33  * A CardPresenter is used to generate Views and bind Objects to them on demand.
34  * It contains an Image CardView
35  */
36 public class CardPresenter extends Presenter {
37     private static int CARD_WIDTH = 560;
38     private static int CARD_HEIGHT = 320;
39     private static int sSelectedBackgroundColor;
40     private static int sDefaultBackgroundColor;
41     private Drawable mDefaultCardImage;
42 
43     @Override
onCreateViewHolder(ViewGroup parent)44     public ViewHolder onCreateViewHolder(ViewGroup parent) {
45         sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background, null);
46         sSelectedBackgroundColor =
47                 parent.getResources().getColor(R.color.selected_background, null);
48         mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie, null);
49 
50         ImageCardView cardView = new ImageCardView(parent.getContext()) {
51             @Override
52             public void setSelected(boolean selected) {
53                 findViewById(R.id.info_field).setBackgroundColor(
54                         selected ? sSelectedBackgroundColor : sDefaultBackgroundColor);
55                 super.setSelected(selected);
56             }
57         };
58 
59         cardView.setFocusable(true);
60         cardView.setFocusableInTouchMode(true);
61         return new ViewHolder(cardView);
62     }
63 
64     @Override
onBindViewHolder(Presenter.ViewHolder viewHolder, Object item)65     public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
66         Movie movie = (Movie) item;
67         ImageCardView cardView = (ImageCardView) viewHolder.view;
68 
69         cardView.setTitleText(movie.getTitle());
70         cardView.setContentText(movie.getStudio());
71         cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
72         cardView.setBackgroundColor(sDefaultBackgroundColor);
73 
74         Glide.with(viewHolder.view.getContext())
75                 .load(R.drawable.gradation)
76                 .centerCrop()
77                 .error(mDefaultCardImage)
78                 .listener(new RequestListener<Integer, GlideDrawable>() {
79                     @Override
80                     public boolean onException(Exception e, Integer i, Target<GlideDrawable> target,
81                             boolean b) {
82                         return false;
83                     }
84 
85                     @Override
86                     public boolean onResourceReady(GlideDrawable glideDrawable, Integer i,
87                             Target<GlideDrawable> target, boolean b, boolean b1) {
88                         // Remove the background color to reduce overdraw.
89                         cardView.setBackground(null);
90                         return false;
91                     }
92                 })
93                 .into(cardView.getMainImageView());
94     }
95 
96     @Override
onUnbindViewHolder(Presenter.ViewHolder viewHolder)97     public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
98         ImageCardView cardView = (ImageCardView) viewHolder.view;
99         // Remove references to images so that the garbage collector can free up memory
100         cardView.setBadgeImage(null);
101         cardView.setMainImage(null);
102     }
103 }
104