1/*
2 * Copyright (C) 2016 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
17package com.android.car.cluster.sample.cards;
18
19import android.animation.TimeInterpolator;
20import android.content.Context;
21import android.content.res.ColorStateList;
22import android.graphics.PorterDuff;
23import android.view.animation.AccelerateDecelerateInterpolator;
24import android.widget.LinearLayout;
25import android.widget.ProgressBar;
26import android.widget.TextView;
27
28import com.android.car.cluster.sample.DebugUtil;
29import com.android.car.cluster.sample.R;
30
31/**
32 * Card responsible to display media content.
33 */
34public class MediaCard extends CardView {
35
36    private LinearLayout mMediaPanel;
37    private ProgressBar mProgressBar;
38    private TextView mTitle;
39    private TextView mSubtitle;
40
41    public MediaCard(Context context, PriorityChangedListener listener) {
42        super(context, CardType.MEDIA, listener);
43    }
44
45    @Override
46    protected void init() {
47        inflate(R.layout.media_card);
48
49        mPriority = PRIORITY_MEDIA_NOTIFICATION;
50
51        mMediaPanel = viewById(R.id.message_panel);
52        mProgressBar = viewById(R.id.progress_bar);
53        mTitle = viewById(R.id.media_title);
54        mSubtitle = viewById(R.id.media_subtitle);
55
56        mDetailsPanel = mMediaPanel;
57        mLeftIconSwitcher.setVisibility(GONE);
58    }
59
60    @Override
61    public void onPlayRevealAnimation() {
62        super.onPlayRevealAnimation();
63
64        // Decrease priority once notification animation is complete.
65        runDelayed(3000, new Runnable() {
66            @Override
67            public void run() {
68                setPriority(PRIORITY_MEDIA_ACTIVE);
69            }
70        });
71
72        if (mBackgroundImage.getVisibility() != GONE) {
73            runDelayed(SHOW_ANIMATION_DURATION + 3000, new Runnable() {
74                @Override
75                public void run() {
76                    animateBackgroundFadeOut();
77                }
78            });
79        }
80    }
81
82    private void animateBackgroundFadeOut() {
83        TimeInterpolator interpolator =
84                new AccelerateDecelerateInterpolator(getContext(), null);
85
86        long duration = 500 * DebugUtil.ANIMATION_FACTOR;
87
88        mBackgroundImage.animate()
89                .alpha(0f)
90                .setDuration(duration);
91
92        mLeftIconSwitcher.setTranslationX(mLeftPadding);
93        mLeftIconSwitcher.setVisibility(VISIBLE);
94        mRightIconSwitcher.animate()
95                .translationX(mLeftPadding + mIconsOverlap)
96                .setInterpolator(interpolator)
97                .setDuration(duration);
98
99        mDetailsPanel.animate()
100                .translationX(
101                        mLeftPadding + mIconsOverlap + mIconSize + mLeftPadding)
102                .setInterpolator(interpolator)
103                .setDuration(duration);
104
105    }
106
107    public void setProgressColor(int color) {
108        mProgressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
109        mProgressBar.setProgressTintList(ColorStateList.valueOf(color));
110    }
111
112    public void setProgress(int progress) {
113        if (progress == -1) {
114            mProgressBar.setIndeterminate(true);
115            return;
116        } else {
117            mProgressBar.setIndeterminate(false);
118        }
119
120        if (progress > 100) {
121            progress = 100;
122        }
123        if (progress < 0) {
124            progress = 0;
125        }
126        mProgressBar.setProgress(progress);
127    }
128
129    public void setTitle(String album) {
130        mTitle.setText(album);
131    }
132
133    public void setSubtitle(String track) {
134        mSubtitle.setText(track);
135    }
136
137    public String getTitle() {
138        return mTitle.getText() == null ? null : String.valueOf(mTitle.getText());
139    }
140
141    public String getSubtitle() {
142        return mSubtitle.getText() == null ? null : String.valueOf(mSubtitle.getText());
143    }
144}
145