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.mediarouter.R; 24import android.support.v7.widget.AppCompatSeekBar; 25import android.util.AttributeSet; 26import android.util.Log; 27 28/** 29 * Volume slider with showing, hiding, and applying alpha supports to the thumb. 30 */ 31class 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 40 public MediaRouteVolumeSlider(Context context) { 41 this(context, null); 42 } 43 44 public MediaRouteVolumeSlider(Context context, AttributeSet attrs) { 45 this(context, attrs, android.support.v7.appcompat.R.attr.seekBarStyle); 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 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 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 */ 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 */ 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