1 /*
2  * Copyright (C) 2017 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 com.android.settings.widget;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.drawable.Drawable;
22 import android.util.AttributeSet;
23 import android.widget.SeekBar;
24 
25 public class DefaultIndicatorSeekBar extends SeekBar {
26 
27     private int mDefaultProgress = -1;
28 
DefaultIndicatorSeekBar(Context context)29     public DefaultIndicatorSeekBar(Context context) {
30         super(context);
31     }
32 
DefaultIndicatorSeekBar(Context context, AttributeSet attrs)33     public DefaultIndicatorSeekBar(Context context, AttributeSet attrs) {
34         super(context, attrs);
35     }
36 
DefaultIndicatorSeekBar(Context context, AttributeSet attrs, int defStyleAttr)37     public DefaultIndicatorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
38         super(context, attrs, defStyleAttr);
39     }
40 
DefaultIndicatorSeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)41     public DefaultIndicatorSeekBar(Context context, AttributeSet attrs, int defStyleAttr,
42             int defStyleRes) {
43         super(context, attrs, defStyleAttr, defStyleRes);
44     }
45 
46     /**
47      * N.B. Only draws the default indicator tick mark, NOT equally spaced tick marks.
48      */
49     @Override
drawTickMarks(Canvas canvas)50     protected void drawTickMarks(Canvas canvas) {
51         if (isEnabled() && mDefaultProgress <= getMax() && mDefaultProgress >= getMin()) {
52             final Drawable defaultIndicator = getTickMark();
53 
54             // Adjust the drawable's bounds to center it at the point where it's drawn.
55             final int w = defaultIndicator.getIntrinsicWidth();
56             final int h = defaultIndicator.getIntrinsicHeight();
57             final int halfW = w >= 0 ? w / 2 : 1;
58             final int halfH = h >= 0 ? h / 2 : 1;
59             defaultIndicator.setBounds(-halfW, -halfH, halfW, halfH);
60 
61             // This mimics the computation of the thumb position, to get the true "default."
62             final int availableWidth = getWidth() - mPaddingLeft - mPaddingRight;
63             final int range = getMax() - getMin();
64             final float scale = range > 0f ? mDefaultProgress / (float) range : 0f;
65             final int offset = (int) ((scale * availableWidth) + 0.5f);
66             final int indicatorPosition = isLayoutRtl() && getMirrorForRtl()
67                     ? availableWidth - offset + mPaddingRight : offset + mPaddingLeft;
68 
69             final int saveCount = canvas.save();
70             canvas.translate(indicatorPosition, getHeight() / 2);
71             defaultIndicator.draw(canvas);
72             canvas.restoreToCount(saveCount);
73         }
74     }
75 
76     /**
77      * N.B. This sets the default *unadjusted* progress, i.e. in the SeekBar's [0 - max] terms.
78      */
setDefaultProgress(int defaultProgress)79     public void setDefaultProgress(int defaultProgress) {
80         if (mDefaultProgress != defaultProgress) {
81             mDefaultProgress = defaultProgress;
82             invalidate();
83         }
84     }
85 
getDefaultProgress()86     public int getDefaultProgress() {
87         return mDefaultProgress;
88     }
89 }
90