Home | History | Annotate | Download | only in leanback
      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 com.example.android.leanback.R;
     19 import android.support.v17.leanback.widget.ImageCardView;
     20 import android.support.v17.leanback.widget.Presenter;
     21 import android.text.TextUtils;
     22 import android.util.Log;
     23 import android.view.ViewGroup;
     24 import android.view.ContextThemeWrapper;
     25 import android.view.View.MeasureSpec;
     26 import android.view.ViewGroup.LayoutParams;
     27 import android.widget.TextView;
     28 
     29 import java.util.Random;
     30 
     31 public class CardPresenter extends Presenter {
     32     private static final String TAG = "CardPresenter";
     33 
     34     private static final int IMAGE_HEIGHT_DP = 120;
     35 
     36     private static Random sRand = new Random();
     37     private int mRowHeight = 0;
     38     private int mExpandedRowHeight = 0;
     39 
     40     private int mCardThemeResId;
     41     private Context mContextThemeWrapper;
     42 
     43     public CardPresenter(int cardThemeResId) {
     44         mCardThemeResId = cardThemeResId;
     45     }
     46 
     47     public CardPresenter() {
     48         mCardThemeResId = 0;
     49     }
     50 
     51     private void setupRowHeights(Context context) {
     52         if (mRowHeight == 0) {
     53             float density = context.getResources().getDisplayMetrics().density;
     54             int height = (int) (IMAGE_HEIGHT_DP * density + 0.5f);
     55 
     56             ImageCardView v = new ImageCardView(context);
     57             v.setMainImageDimensions(LayoutParams.WRAP_CONTENT, height);
     58             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
     59             mRowHeight = v.getMeasuredHeight();
     60             v.setActivated(true);
     61             v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
     62             mExpandedRowHeight = v.getMeasuredHeight();
     63         }
     64     }
     65 
     66     public int getRowHeight(Context context) {
     67         setupRowHeights(context);
     68         return mRowHeight;
     69     }
     70 
     71     public int getExpandedRowHeight(Context context) {
     72         setupRowHeights(context);
     73         return mExpandedRowHeight;
     74     }
     75 
     76     @Override
     77     public ViewHolder onCreateViewHolder(ViewGroup parent) {
     78         Log.d(TAG, "onCreateViewHolder");
     79         Context context = parent.getContext();
     80         if (mCardThemeResId != 0) {
     81             if (mContextThemeWrapper == null) {
     82                 mContextThemeWrapper = new ContextThemeWrapper(context, mCardThemeResId);
     83             }
     84             context = mContextThemeWrapper;
     85         }
     86         ImageCardView v = new ImageCardView(context);
     87         v.setFocusable(true);
     88         v.setFocusableInTouchMode(true);
     89         // Randomly makes image view crop as a square or just stretch to original
     90         // aspect ratio.
     91         if (sRand.nextBoolean()) {
     92             v.setMainImageAdjustViewBounds(false);
     93             v.setMainImageDimensions(getRowHeight(parent.getContext()),
     94                     getRowHeight(parent.getContext()));
     95         } else {
     96             v.setMainImageAdjustViewBounds(true);
     97             v.setMainImageDimensions(LayoutParams.WRAP_CONTENT,
     98                     getRowHeight(parent.getContext()));
     99         }
    100         return new ViewHolder(v);
    101     }
    102 
    103     @Override
    104     public void onBindViewHolder(ViewHolder viewHolder, Object item) {
    105         Log.d(TAG, "onBindViewHolder for " + item.toString());
    106         PhotoItem photoItem = (PhotoItem) item;
    107         Drawable drawable =  viewHolder.view.getContext().getResources()
    108                 .getDrawable(photoItem.getImageResourceId());
    109         ((ImageCardView) viewHolder.view).setMainImage(drawable);
    110         ((ImageCardView) viewHolder.view).setTitleText(photoItem.getTitle());
    111         if (!TextUtils.isEmpty(photoItem.getContent())) {
    112             ((ImageCardView) viewHolder.view).setContentText(photoItem.getContent());
    113         }
    114     }
    115 
    116     @Override
    117     public void onUnbindViewHolder(ViewHolder viewHolder) {
    118         Log.d(TAG, "onUnbindViewHolder");
    119     }
    120 }
    121