1 /* 2 * Copyright (C) 2019 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.content.res.TypedArray; 21 import android.graphics.drawable.Drawable; 22 import android.util.AttributeSet; 23 import android.widget.SeekBar; 24 import android.widget.TextView; 25 26 import androidx.core.content.res.TypedArrayUtils; 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 31 /** A slider preference with left and right labels **/ 32 public class LabeledSeekBarPreference extends SeekBarPreference { 33 34 private final int mTextStartId; 35 private final int mTextEndId; 36 private final int mTickMarkId; 37 private OnPreferenceChangeListener mStopListener; 38 LabeledSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)39 public LabeledSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, 40 int defStyleRes) { 41 42 super(context, attrs, defStyleAttr, defStyleRes); 43 setLayoutResource(R.layout.preference_labeled_slider); 44 45 final TypedArray styledAttrs = context.obtainStyledAttributes(attrs, 46 R.styleable.LabeledSeekBarPreference); 47 mTextStartId = styledAttrs.getResourceId( 48 R.styleable.LabeledSeekBarPreference_textStart, 49 R.string.summary_placeholder); 50 mTextEndId = styledAttrs.getResourceId( 51 R.styleable.LabeledSeekBarPreference_textEnd, 52 R.string.summary_placeholder); 53 mTickMarkId = styledAttrs.getResourceId( 54 R.styleable.LabeledSeekBarPreference_tickMark, /* defValue= */ 0); 55 styledAttrs.recycle(); 56 } 57 LabeledSeekBarPreference(Context context, AttributeSet attrs)58 public LabeledSeekBarPreference(Context context, AttributeSet attrs) { 59 this(context, attrs, TypedArrayUtils.getAttr(context, 60 androidx.preference.R.attr.seekBarPreferenceStyle, 61 com.android.internal.R.attr.seekBarPreferenceStyle), 0); 62 } 63 64 @Override onBindViewHolder(PreferenceViewHolder holder)65 public void onBindViewHolder(PreferenceViewHolder holder) { 66 super.onBindViewHolder(holder); 67 68 final TextView startText = (TextView) holder.findViewById(android.R.id.text1); 69 final TextView endText = (TextView) holder.findViewById(android.R.id.text2); 70 startText.setText(mTextStartId); 71 endText.setText(mTextEndId); 72 73 if (mTickMarkId != 0) { 74 final Drawable tickMark = getContext().getDrawable(mTickMarkId); 75 final SeekBar seekBar = (SeekBar) holder.findViewById( 76 com.android.internal.R.id.seekbar); 77 seekBar.setTickMark(tickMark); 78 } 79 } 80 setOnPreferenceChangeStopListener(OnPreferenceChangeListener listener)81 public void setOnPreferenceChangeStopListener(OnPreferenceChangeListener listener) { 82 mStopListener = listener; 83 } 84 85 @Override onStopTrackingTouch(SeekBar seekBar)86 public void onStopTrackingTouch(SeekBar seekBar) { 87 super.onStopTrackingTouch(seekBar); 88 89 if (mStopListener != null) { 90 mStopListener.onPreferenceChange(this, seekBar.getProgress()); 91 } 92 } 93 } 94 95