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