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.support.mediarouter.app;
18
19import android.content.Context;
20import android.graphics.Color;
21import android.graphics.PorterDuff;
22import android.graphics.drawable.Drawable;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.widget.SeekBar;
26
27/**
28 * Volume slider with showing, hiding, and applying alpha supports to the thumb.
29 */
30public class MediaRouteVolumeSlider extends SeekBar {
31    private static final String TAG = "MediaRouteVolumeSlider";
32
33    private final float mDisabledAlpha;
34
35    private boolean mHideThumb;
36    private Drawable mThumb;
37    private int mColor;
38
39    public MediaRouteVolumeSlider(Context context) {
40        this(context, null);
41    }
42
43    public MediaRouteVolumeSlider(Context context, AttributeSet attrs) {
44        super(context, attrs);
45        mDisabledAlpha = MediaRouterThemeHelper.getDisabledAlpha(context);
46    }
47
48    public MediaRouteVolumeSlider(Context context, AttributeSet attrs, int defStyleAttr) {
49        super(context, attrs, defStyleAttr);
50        mDisabledAlpha = MediaRouterThemeHelper.getDisabledAlpha(context);
51    }
52
53    @Override
54    protected void drawableStateChanged() {
55        super.drawableStateChanged();
56        int alpha = isEnabled() ? 0xFF : (int) (0xFF * mDisabledAlpha);
57
58        // The thumb drawable is a collection of drawables and its current drawables are changed per
59        // state. Apply the color filter and alpha on every state change.
60        if (mThumb != null) {
61            mThumb.setColorFilter(mColor, PorterDuff.Mode.SRC_IN);
62            mThumb.setAlpha(alpha);
63        }
64
65        getProgressDrawable().setColorFilter(mColor, PorterDuff.Mode.SRC_IN);
66        getProgressDrawable().setAlpha(alpha);
67    }
68
69    @Override
70    public void setThumb(Drawable thumb) {
71        mThumb = thumb;
72        super.setThumb(mHideThumb ? null : mThumb);
73    }
74
75    /**
76     * Sets whether to show or hide thumb.
77     */
78    public void setHideThumb(boolean hideThumb) {
79        if (mHideThumb == hideThumb) {
80            return;
81        }
82        mHideThumb = hideThumb;
83        super.setThumb(mHideThumb ? null : mThumb);
84    }
85
86    /**
87     * Sets the volume slider color. The change takes effect next time drawable state is changed.
88     * <p>
89     * The color cannot be translucent, otherwise the underlying progress bar will be seen through
90     * the thumb.
91     * </p>
92     */
93    public void setColor(int color) {
94        if (mColor == color) {
95            return;
96        }
97        if (Color.alpha(color) != 0xFF) {
98            Log.e(TAG, "Volume slider color cannot be translucent: #" + Integer.toHexString(color));
99        }
100        mColor = color;
101    }
102}
103