aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/ui/browse/RecordingCardView.java
blob: fe3c52d9ecbe36678cb2f2cd6cf064bbab8ec995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.dvr.ui.browse;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v17.leanback.widget.BaseCardView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.tv.R;
import com.android.tv.dvr.data.RecordedProgram;
import com.android.tv.ui.ViewUtils;
import com.android.tv.util.images.ImageLoader;

/**
 * A CardView for displaying info about a {@link com.android.tv.dvr.data.ScheduledRecording} or
 * {@link RecordedProgram} or {@link com.android.tv.dvr.data.SeriesRecording}.
 */
public class RecordingCardView extends BaseCardView {
    // This value should be the same with
    // android.support.v17.leanback.widget.FocusHighlightHelper.BrowseItemFocusHighlight.DURATION_MS
    private static final int ANIMATION_DURATION = 150;
    private final ImageView mImageView;
    private final int mImageWidth;
    private final int mImageHeight;
    private String mImageUri;
    private final TextView mMajorContentView;
    private final TextView mMinorContentView;
    private final ProgressBar mProgressBar;
    private final View mAffiliatedIconContainer;
    private final ImageView mAffiliatedIcon;
    private final Drawable mDefaultImage;
    private final FrameLayout mTitleArea;
    private final TextView mFoldedTitleView;
    private final TextView mExpandedTitleView;
    private final ValueAnimator mExpandTitleAnimator;
    private final int mFoldedTitleHeight;
    private final int mExpandedTitleHeight;
    private final boolean mExpandTitleWhenFocused;
    private boolean mExpanded;
    private String mDetailBackgroundImageUri;

    public RecordingCardView(Context context) {
        this(context, false);
    }

    public RecordingCardView(Context context, boolean expandTitleWhenFocused) {
        this(
                context,
                context.getResources()
                        .getDimensionPixelSize(R.dimen.dvr_library_card_image_layout_width),
                context.getResources()
                        .getDimensionPixelSize(R.dimen.dvr_library_card_image_layout_height),
                expandTitleWhenFocused);
    }

    public RecordingCardView(
            Context context, int imageWidth, int imageHeight, boolean expandTitleWhenFocused) {
        super(context);
        // TODO(dvr): move these to the layout XML.
        setCardType(BaseCardView.CARD_TYPE_INFO_UNDER_WITH_EXTRA);
        setInfoVisibility(BaseCardView.CARD_REGION_VISIBLE_ALWAYS);
        setFocusable(true);
        setFocusableInTouchMode(true);
        mDefaultImage = getResources().getDrawable(R.drawable.dvr_default_poster, null);

        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.dvr_recording_card_view, this);
        mImageView = (ImageView) findViewById(R.id.image);
        mImageWidth = imageWidth;
        mImageHeight = imageHeight;
        mProgressBar = (ProgressBar) findViewById(R.id.recording_progress);
        mAffiliatedIconContainer = findViewById(R.id.affiliated_icon_container);
        mAffiliatedIcon = (ImageView) findViewById(R.id.affiliated_icon);
        mMajorContentView = (TextView) findViewById(R.id.content_major);
        mMinorContentView = (TextView) findViewById(R.id.content_minor);
        mTitleArea = (FrameLayout) findViewById(R.id.title_area);
        mFoldedTitleView = (TextView) findViewById(R.id.title_one_line);
        mExpandedTitleView = (TextView) findViewById(R.id.title_two_lines);
        mFoldedTitleHeight =
                getResources().getDimensionPixelSize(R.dimen.dvr_library_card_folded_title_height);
        mExpandedTitleHeight =
                getResources()
                        .getDimensionPixelSize(R.dimen.dvr_library_card_expanded_title_height);
        mExpandTitleAnimator = ValueAnimator.ofFloat(0.0f, 1.0f).setDuration(ANIMATION_DURATION);
        mExpandTitleAnimator.addUpdateListener(
                new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value = (Float) valueAnimator.getAnimatedValue();
                        mExpandedTitleView.setAlpha(value);
                        mFoldedTitleView.setAlpha(1.0f - value);
                        ViewUtils.setLayoutHeight(
                                mTitleArea,
                                (int)
                                        (mFoldedTitleHeight
                                                + (mExpandedTitleHeight - mFoldedTitleHeight)
                                                        * value));
                    }
                });
        mExpandTitleWhenFocused = expandTitleWhenFocused;
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        // Preload the background image going to be used in detail fragments here to prevent
        // loading and drawing background images during activity transitions.
        if (gainFocus) {
            if (!TextUtils.isEmpty(mDetailBackgroundImageUri)) {
                ImageLoader.loadBitmap(
                        getContext(),
                        mDetailBackgroundImageUri,
                        Integer.MAX_VALUE,
                        Integer.MAX_VALUE,
                        null);
            }
        }
        if (mExpandTitleWhenFocused) {
            if (gainFocus) {
                expandTitle(true, true);
            } else {
                expandTitle(false, true);
            }
        }
    }

    /**
     * Expands/folds the title area to show program title with two/one lines.
     *
     * @param expand {@code true} to expand the title area, or {@code false} to fold it.
     * @param withAnimation {@code true} to expand/fold with animation.
     */
    public void expandTitle(boolean expand, boolean withAnimation) {
        if (expand != mExpanded && mFoldedTitleView.getLayout().getEllipsisCount(0) > 0) {
            if (withAnimation) {
                if (expand) {
                    mExpandTitleAnimator.start();
                } else {
                    mExpandTitleAnimator.reverse();
                }
            } else {
                if (expand) {
                    mFoldedTitleView.setAlpha(0.0f);
                    mExpandedTitleView.setAlpha(1.0f);
                    ViewUtils.setLayoutHeight(mTitleArea, mExpandedTitleHeight);
                } else {
                    mFoldedTitleView.setAlpha(1.0f);
                    mExpandedTitleView.setAlpha(0.0f);
                    ViewUtils.setLayoutHeight(mTitleArea, mFoldedTitleHeight);
                }
            }
            mExpanded = expand;
        }
    }

    void setTitle(CharSequence title) {
        mFoldedTitleView.setText(title);
        mExpandedTitleView.setText(title);
    }

    void setContent(CharSequence majorContent, CharSequence minorContent) {
        if (!TextUtils.isEmpty(majorContent)) {
            mMajorContentView.setText(majorContent);
            mMajorContentView.setVisibility(View.VISIBLE);
        } else {
            mMajorContentView.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(minorContent)) {
            mMinorContentView.setText(minorContent);
            mMinorContentView.setVisibility(View.VISIBLE);
        } else {
            mMinorContentView.setVisibility(View.GONE);
        }
    }

    /** Sets progress bar. If progress is {@code null}, hides progress bar. */
    void setProgressBar(Integer progress) {
        if (progress == null) {
            mProgressBar.setVisibility(View.GONE);
        } else {
            mProgressBar.setProgress(progress);
            mProgressBar.setVisibility(View.VISIBLE);
        }
    }

    /** Sets the color of progress bar. */
    void setProgressBarColor(int color) {
        mProgressBar.getProgressDrawable().setTint(color);
    }

    /**
     * Sets the image URI of the poster should be shown on the card view.
     *
     * @param isChannelLogo {@code true} if the image is from channels' logo.
     */
    void setImageUri(String uri, boolean isChannelLogo) {
        if (isChannelLogo) {
            mImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        } else {
            mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
        mImageUri = uri;
        if (TextUtils.isEmpty(uri)) {
            mImageView.setImageDrawable(mDefaultImage);
        } else {
            ImageLoader.loadBitmap(
                    getContext(),
                    uri,
                    mImageWidth,
                    mImageHeight,
                    new RecordingCardImageLoaderCallback(this, uri));
        }
    }

    /** Sets the {@link Drawable} of the poster should be shown on the card view. */
    public void setImage(Drawable image) {
        if (image != null) {
            mImageView.setImageDrawable(image);
        }
    }

    /**
     * Sets the affiliated icon of the card view, which will be displayed at the lower-right corner
     * of the poster.
     */
    public void setAffiliatedIcon(int imageResId) {
        if (imageResId > 0) {
            mAffiliatedIconContainer.setVisibility(View.VISIBLE);
            mAffiliatedIcon.setImageResource(imageResId);
        } else {
            mAffiliatedIconContainer.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * Sets the background image URI of the card view, which will be displayed as background when
     * the view is clicked and shows its details fragment.
     */
    public void setDetailBackgroundImageUri(String uri) {
        mDetailBackgroundImageUri = uri;
    }

    /** Returns image view. */
    public ImageView getImageView() {
        return mImageView;
    }

    private static class RecordingCardImageLoaderCallback
            extends ImageLoader.ImageLoaderCallback<RecordingCardView> {
        private final String mUri;

        RecordingCardImageLoaderCallback(RecordingCardView referent, String uri) {
            super(referent);
            mUri = uri;
        }

        @Override
        public void onBitmapLoaded(RecordingCardView view, @Nullable Bitmap bitmap) {
            if (bitmap == null || !mUri.equals(view.mImageUri)) {
                view.mImageView.setImageDrawable(view.mDefaultImage);
            } else {
                view.mImageView.setImageDrawable(new BitmapDrawable(view.getResources(), bitmap));
            }
        }
    }

    public void reset() {
        mFoldedTitleView.setText(null);
        mExpandedTitleView.setText(null);
        setContent(null, null);
        mImageView.setImageDrawable(mDefaultImage);
    }
}