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