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 15 package com.example.android.leanback; 16 17 import android.content.Context; 18 import android.graphics.Bitmap; 19 import android.graphics.drawable.BitmapDrawable; 20 import android.graphics.drawable.Drawable; 21 import android.support.v17.leanback.widget.ImageCardView; 22 import android.support.v17.leanback.widget.Presenter; 23 import android.util.Log; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 import com.squareup.picasso.Picasso; 28 import com.squareup.picasso.Target; 29 30 import java.net.URI; 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 final String TAG = "CardPresenter"; 38 39 private static Context mContext; 40 private static int CARD_WIDTH = 313; 41 private static int CARD_HEIGHT = 176; 42 43 static class ViewHolder extends Presenter.ViewHolder { 44 private Movie mMovie; 45 private ImageCardView mCardView; 46 private Drawable mDefaultCardImage; 47 private PicassoImageCardViewTarget mImageCardViewTarget; 48 49 public ViewHolder(View view) { 50 super(view); 51 mCardView = (ImageCardView) view; 52 mImageCardViewTarget = new PicassoImageCardViewTarget(mCardView); 53 mDefaultCardImage = mContext.getResources().getDrawable(R.drawable.movie); 54 } 55 56 public void setMovie(Movie m) { 57 mMovie = m; 58 } 59 60 public Movie getMovie() { 61 return mMovie; 62 } 63 64 public ImageCardView getCardView() { 65 return mCardView; 66 } 67 68 protected void updateCardViewImage(URI uri) { 69 Picasso.with(mContext) 70 .load(uri.toString()) 71 .resize(Utils.dpToPx(CARD_WIDTH, mContext), Utils.dpToPx(CARD_HEIGHT, mContext)) 72 .error(mDefaultCardImage) 73 .into(mImageCardViewTarget); 74 } 75 } 76 77 @Override 78 public ViewHolder onCreateViewHolder(ViewGroup parent) { 79 Log.d(TAG, "onCreateViewHolder"); 80 mContext = parent.getContext(); 81 82 ImageCardView cardView = new ImageCardView(mContext); 83 cardView.setFocusable(true); 84 cardView.setFocusableInTouchMode(true); 85 cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background)); 86 return new ViewHolder(cardView); 87 } 88 89 @Override 90 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 91 Movie movie = (Movie) item; 92 ((ViewHolder) viewHolder).setMovie(movie); 93 94 Log.d(TAG, "onBindViewHolder"); 95 if (movie.getCardImageUrl() != null) { 96 ((ViewHolder) viewHolder).mCardView.setTitleText(movie.getTitle()); 97 ((ViewHolder) viewHolder).mCardView.setContentText(movie.getStudio()); 98 ((ViewHolder) viewHolder).mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT); 99 ((ViewHolder) viewHolder).updateCardViewImage(movie.getCardImageURI()); 100 } 101 } 102 103 @Override 104 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 105 Log.d(TAG, "onUnbindViewHolder"); 106 } 107 108 @Override 109 public void onViewAttachedToWindow(Presenter.ViewHolder viewHolder) { 110 // TO DO 111 } 112 113 public static class PicassoImageCardViewTarget implements Target { 114 private ImageCardView mImageCardView; 115 116 public PicassoImageCardViewTarget(ImageCardView imageCardView) { 117 mImageCardView = imageCardView; 118 } 119 120 @Override 121 public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) { 122 Drawable bitmapDrawable = new BitmapDrawable(mContext.getResources(), bitmap); 123 mImageCardView.setMainImage(bitmapDrawable); 124 } 125 126 @Override 127 public void onBitmapFailed(Drawable drawable) { 128 mImageCardView.setMainImage(drawable); 129 } 130 131 @Override 132 public void onPrepareLoad(Drawable drawable) { 133 // Do nothing, default_background manager has its own transitions 134 } 135 } 136 } 137