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 android.support.v17.leanback.widget;
15 
16 import android.content.Context;
17 import android.content.res.TypedArray;
18 import android.graphics.drawable.Drawable;
19 import android.support.v17.leanback.R;
20 import android.text.TextUtils;
21 import android.util.AttributeSet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ImageView;
26 import android.widget.ImageView.ScaleType;
27 import android.widget.TextView;
28 
29 /**
30  * A card view with an {@link ImageView} as its main region.
31  */
32 public class ImageCardView extends BaseCardView {
33 
34     private ImageView mImageView;
35     private View mInfoArea;
36     private TextView mTitleView;
37     private TextView mContentView;
38     private ImageView mBadgeImage;
39     private ImageView mBadgeFadeMask;
40     private boolean mAttachedToWindow;
41 
ImageCardView(Context context)42     public ImageCardView(Context context) {
43         this(context, null);
44     }
45 
ImageCardView(Context context, AttributeSet attrs)46     public ImageCardView(Context context, AttributeSet attrs) {
47         this(context, attrs, R.attr.imageCardViewStyle);
48     }
49 
ImageCardView(Context context, AttributeSet attrs, int defStyle)50     public ImageCardView(Context context, AttributeSet attrs, int defStyle) {
51         super(context, attrs, defStyle);
52 
53         LayoutInflater inflater = LayoutInflater.from(context);
54         View v = inflater.inflate(R.layout.lb_image_card_view, this);
55 
56         mImageView = (ImageView) v.findViewById(R.id.main_image);
57         mImageView.setVisibility(View.INVISIBLE);
58         mInfoArea = v.findViewById(R.id.info_field);
59         mTitleView = (TextView) v.findViewById(R.id.title_text);
60         mContentView = (TextView) v.findViewById(R.id.content_text);
61         mBadgeImage = (ImageView) v.findViewById(R.id.extra_badge);
62         mBadgeFadeMask = (ImageView) v.findViewById(R.id.fade_mask);
63 
64         if (mInfoArea != null) {
65             TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbImageCardView,
66                     defStyle, 0);
67             try {
68                 setInfoAreaBackground(
69                         a.getDrawable(R.styleable.lbImageCardView_infoAreaBackground));
70             } finally {
71                 a.recycle();
72             }
73         }
74     }
75 
getMainImageView()76     public final ImageView getMainImageView() {
77         return mImageView;
78     }
79 
setMainImageAdjustViewBounds(boolean adjustViewBounds)80     public void setMainImageAdjustViewBounds(boolean adjustViewBounds) {
81         if (mImageView != null) {
82             mImageView.setAdjustViewBounds(adjustViewBounds);
83         }
84     }
85 
setMainImageScaleType(ScaleType scaleType)86     public void setMainImageScaleType(ScaleType scaleType) {
87         if (mImageView != null) {
88             mImageView.setScaleType(scaleType);
89         }
90     }
91 
92     /**
93      * Set drawable with fade-in animation.
94      */
setMainImage(Drawable drawable)95     public void setMainImage(Drawable drawable) {
96         setMainImage(drawable, true);
97     }
98 
99     /**
100      * Set drawable with optional fade-in animation.
101      */
setMainImage(Drawable drawable, boolean fade)102     public void setMainImage(Drawable drawable, boolean fade) {
103         if (mImageView == null) {
104             return;
105         }
106 
107         mImageView.setImageDrawable(drawable);
108         if (drawable == null) {
109             mImageView.animate().cancel();
110             mImageView.setAlpha(1f);
111             mImageView.setVisibility(View.INVISIBLE);
112         } else {
113             mImageView.setVisibility(View.VISIBLE);
114             if (fade) {
115                 fadeIn();
116             } else {
117                 mImageView.animate().cancel();
118                 mImageView.setAlpha(1f);
119             }
120         }
121     }
122 
setMainImageDimensions(int width, int height)123     public void setMainImageDimensions(int width, int height) {
124         ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
125         lp.width = width;
126         lp.height = height;
127         mImageView.setLayoutParams(lp);
128     }
129 
getMainImage()130     public Drawable getMainImage() {
131         if (mImageView == null) {
132             return null;
133         }
134 
135         return mImageView.getDrawable();
136     }
137 
getInfoAreaBackground()138     public Drawable getInfoAreaBackground() {
139         if (mInfoArea != null) {
140             return mInfoArea.getBackground();
141         }
142         return null;
143     }
144 
setInfoAreaBackground(Drawable drawable)145     public void setInfoAreaBackground(Drawable drawable) {
146         if (mInfoArea != null) {
147             mInfoArea.setBackground(drawable);
148             if (mBadgeImage != null) {
149                 mBadgeImage.setBackground(drawable);
150             }
151         }
152     }
153 
setInfoAreaBackgroundColor(int color)154     public void setInfoAreaBackgroundColor(int color) {
155         if (mInfoArea != null) {
156             mInfoArea.setBackgroundColor(color);
157             if (mBadgeImage != null) {
158                 mBadgeImage.setBackgroundColor(color);
159             }
160         }
161     }
162 
setTitleText(CharSequence text)163     public void setTitleText(CharSequence text) {
164         if (mTitleView == null) {
165             return;
166         }
167 
168         mTitleView.setText(text);
169         setTextMaxLines();
170     }
171 
getTitleText()172     public CharSequence getTitleText() {
173         if (mTitleView == null) {
174             return null;
175         }
176 
177         return mTitleView.getText();
178     }
179 
setContentText(CharSequence text)180     public void setContentText(CharSequence text) {
181         if (mContentView == null) {
182             return;
183         }
184 
185         mContentView.setText(text);
186         setTextMaxLines();
187     }
188 
getContentText()189     public CharSequence getContentText() {
190         if (mContentView == null) {
191             return null;
192         }
193 
194         return mContentView.getText();
195     }
196 
setBadgeImage(Drawable drawable)197     public void setBadgeImage(Drawable drawable) {
198         if (mBadgeImage == null) {
199             return;
200         }
201 
202         if (drawable != null) {
203             mBadgeImage.setImageDrawable(drawable);
204             mBadgeImage.setVisibility(View.VISIBLE);
205             mBadgeFadeMask.setVisibility(View.VISIBLE);
206         } else {
207             mBadgeImage.setVisibility(View.GONE);
208             mBadgeFadeMask.setVisibility(View.GONE);
209         }
210     }
211 
getBadgeImage()212     public Drawable getBadgeImage() {
213         if (mBadgeImage == null) {
214             return null;
215         }
216 
217         return mBadgeImage.getDrawable();
218     }
219 
fadeIn()220     private void fadeIn() {
221         mImageView.setAlpha(0f);
222         if (mAttachedToWindow) {
223             mImageView.animate().alpha(1f).setDuration(mImageView.getResources().getInteger(
224                     android.R.integer.config_shortAnimTime));
225         }
226     }
227 
228     @Override
hasOverlappingRendering()229     public boolean hasOverlappingRendering() {
230         return false;
231     }
232 
setTextMaxLines()233     private void setTextMaxLines() {
234         if (TextUtils.isEmpty(getTitleText())) {
235             mContentView.setMaxLines(2);
236         } else {
237             mContentView.setMaxLines(1);
238         }
239         if (TextUtils.isEmpty(getContentText())) {
240             mTitleView.setMaxLines(2);
241         } else {
242             mTitleView.setMaxLines(1);
243         }
244     }
245 
246     @Override
onAttachedToWindow()247     protected void onAttachedToWindow() {
248         super.onAttachedToWindow();
249         mAttachedToWindow = true;
250         if (mImageView.getAlpha() == 0) {
251             fadeIn();
252         }
253     }
254 
255     @Override
onDetachedFromWindow()256     protected void onDetachedFromWindow() {
257         mAttachedToWindow = false;
258         mImageView.animate().cancel();
259         mImageView.setAlpha(1f);
260         super.onDetachedFromWindow();
261     }
262 }
263