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