1/*
2 * Copyright 2018 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.media.common;
18
19import android.content.Context;
20import android.graphics.PorterDuff;
21import android.support.annotation.IntDef;
22import android.util.AttributeSet;
23import android.util.Log;
24import android.widget.ImageView;
25
26import com.android.car.apps.common.FabDrawable;
27
28import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30
31/**
32 * Custom {@link android.widget.ImageButton} that has four custom states:
33 * <ul>
34 * <li>state_playing</li>
35 * <li>state_paused</li>
36 * <li>state_stopped</li>
37 * <li>state_disabled</li>
38 * </ul>
39 */
40public class PlayPauseStopImageView extends ImageView {
41    private static final String TAG = "PlayPauseStopImageView";
42
43    private static final int[] STATE_PAUSE = {R.attr.state_pause};
44    private static final int[] STATE_STOP = {R.attr.state_stop};
45    private static final int[] STATE_PLAY = {R.attr.state_play};
46    private static final int[] STATE_DISABLED = {R.attr.state_disabled};
47
48    /**
49     * Possible states of this view
50     */
51    @IntDef({ACTION_PLAY, ACTION_STOP, ACTION_PAUSE, ACTION_DISABLED})
52    @Retention(RetentionPolicy.SOURCE)
53    public @interface Action {}
54
55    /** Used when no action can be executed at this time */
56    public static final int ACTION_DISABLED = 0;
57    /** Used when the media source is ready to start playing */
58    public static final int ACTION_PLAY = 1;
59    /** Used when the media source is playing and it only support stop action */
60    public static final int ACTION_STOP = 2;
61    /** Used when the media source is playing and it supports pause action */
62    public static final int ACTION_PAUSE = 3;
63
64    private int mAction = ACTION_DISABLED;
65
66    /**
67     * Constructs an instance of this view
68     */
69    public PlayPauseStopImageView(Context context, AttributeSet attrs) {
70        super(context, attrs);
71        setBackground(new FabDrawable(context));
72    }
73
74    /**
75     * Sets the action to display on this view
76     *
77     * @param action one of {@link Action}
78     */
79    public void setAction(@Action int action) {
80        mAction = action;
81        refreshDrawableState();
82    }
83
84    /**
85     * @return currently selected action
86     */
87    public int getAction() {
88        return mAction;
89    }
90
91    @Override
92    public int[] onCreateDrawableState(int extraSpace) {
93        // + 1 so we can potentially add our custom PlayState
94        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
95
96        switch(mAction) {
97            case ACTION_PLAY:
98                mergeDrawableStates(drawableState, STATE_PLAY);
99                break;
100            case ACTION_STOP:
101                mergeDrawableStates(drawableState, STATE_STOP);
102                break;
103            case ACTION_PAUSE:
104                mergeDrawableStates(drawableState, STATE_PAUSE);
105                break;
106            case ACTION_DISABLED:
107                mergeDrawableStates(drawableState, STATE_DISABLED);
108                break;
109            default:
110                Log.e(TAG, "Unknown action: " + mAction);
111        }
112        if (getBackground() != null) {
113            getBackground().setState(drawableState);
114        }
115        return drawableState;
116    }
117
118    /**
119     * Updates the primary color of this view.
120     * @param color fill or main color
121     * @param tintColor contrast color
122     */
123    public void setPrimaryActionColor(int color, int tintColor) {
124        ((FabDrawable) getBackground()).setFabAndStrokeColor(color);
125        if (getDrawable() != null) {
126            getDrawable().setColorFilter(tintColor, PorterDuff.Mode.SRC_IN);
127        }
128    }
129}
130