1 /* 2 * Copyright 2020 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.car.ui.preference; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 import android.widget.SeekBar; 23 import android.widget.TextView; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.preference.DialogPreference; 28 29 import com.android.car.ui.R; 30 import com.android.car.ui.utils.CarUiUtils; 31 32 /** A class implements some basic methods of a seekbar dialog preference. */ 33 public class CarUiSeekBarDialogPreference extends DialogPreference 34 implements DialogFragmentCallbacks { 35 36 private int mSeekBarProgress; 37 private SeekBar mSeekBar; 38 39 private int mSeekBarTopTextViewVisibility; 40 private TextView mSeekBarTopTextView; 41 private String mSeekBarTopText; 42 43 private int mSeekBarLeftTextViewVisibility; 44 private TextView mSeekBarLeftTextView; 45 private String mSeekBarLeftText; 46 47 private int mSeekBarRightTextViewVisibility; 48 private TextView mSeekBarRightTextView; 49 private String mSeekBarRightText; 50 51 private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener; 52 private int mMaxProgress = 100; 53 CarUiSeekBarDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54 public CarUiSeekBarDialogPreference(Context context, AttributeSet attrs, 55 int defStyleAttr, int defStyleRes) { 56 super(context, attrs, defStyleAttr, defStyleRes); 57 init(); 58 } 59 CarUiSeekBarDialogPreference(Context context, AttributeSet attrs, int defStyleAttr)60 public CarUiSeekBarDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { 61 super(context, attrs, defStyleAttr); 62 init(); 63 } 64 CarUiSeekBarDialogPreference(Context context, AttributeSet attrs)65 public CarUiSeekBarDialogPreference(Context context, AttributeSet attrs) { 66 super(context, attrs); 67 init(); 68 } 69 CarUiSeekBarDialogPreference(Context context)70 public CarUiSeekBarDialogPreference(Context context) { 71 super(context); 72 init(); 73 } 74 init()75 private void init() { 76 setDialogLayoutResource(R.layout.car_ui_seekbar_dialog); 77 setPositiveButtonText(R.string.car_ui_dialog_preference_positive); 78 setNegativeButtonText(R.string.car_ui_dialog_preference_negative); 79 } 80 81 @Override onAttached()82 public void onAttached() { 83 super.onAttached(); 84 mSeekBarProgress = getPersistedInt(0); 85 } 86 87 @Override onBindDialogView(@onNull View view)88 public void onBindDialogView(@NonNull View view) { 89 mSeekBar = CarUiUtils.findViewByRefId(view, R.id.seek_bar); 90 mSeekBarTopTextView = CarUiUtils.findViewByRefId(view, R.id.seek_bar_text_top); 91 mSeekBarLeftTextView = CarUiUtils.findViewByRefId(view, R.id.seek_bar_text_left); 92 mSeekBarRightTextView = CarUiUtils.findViewByRefId(view, R.id.seek_bar_text_right); 93 94 setProgress(mSeekBarProgress); 95 96 setSeekBarTopTextViewVisibility(mSeekBarTopTextViewVisibility); 97 setSeekBarTopTextViewText(mSeekBarTopText); 98 99 setSeekBarLeftTextViewVisibility(mSeekBarLeftTextViewVisibility); 100 setSeekBarLeftTextViewText(mSeekBarLeftText); 101 102 setSeekBarRightTextViewVisibility(mSeekBarRightTextViewVisibility); 103 setSeekBarRightTextViewText(mSeekBarRightText); 104 105 setMaxProgress(mMaxProgress); 106 setOnSeekBarChangeListener(mOnSeekBarChangeListener); 107 } 108 109 /** 110 * Get the progress bar's current level of progress. Return 0 when the progress bar is in 111 * indeterminate mode. 112 */ getProgress()113 public int getProgress() { 114 if (mSeekBar != null) { 115 return mSeekBar.getProgress(); 116 } 117 return mSeekBarProgress; 118 } 119 120 /** 121 * Sets the current progress to the specified value. 122 */ setProgress(int progress)123 public void setProgress(int progress) { 124 if (mSeekBar != null) { 125 mSeekBar.setProgress(progress); 126 } 127 mSeekBarProgress = progress; 128 } 129 130 @Override onDialogClosed(boolean positiveResult)131 public void onDialogClosed(boolean positiveResult) { 132 if (positiveResult) { 133 mSeekBarProgress = mSeekBar.getProgress(); 134 persistInt(mSeekBarProgress); 135 notifyChanged(); 136 } 137 138 mSeekBarTopTextView = null; 139 mSeekBarRightTextView = null; 140 mSeekBarLeftTextView = null; 141 mSeekBar = null; 142 } 143 144 /** 145 * Sets the text view visibility on top of the seekbar. 146 */ setSeekBarTopTextViewVisibility(int visibility)147 public void setSeekBarTopTextViewVisibility(int visibility) { 148 if (mSeekBarTopTextView != null) { 149 mSeekBarTopTextView.setVisibility(visibility); 150 } 151 mSeekBarTopTextViewVisibility = visibility; 152 } 153 154 /** 155 * Gets the text on top of the seekbar. 156 */ 157 @Nullable getSeekBarTopTextViewText()158 public String getSeekBarTopTextViewText() { 159 if (mSeekBarTopTextView != null) { 160 return mSeekBarTopTextView.getText().toString(); 161 } 162 return mSeekBarTopText; 163 } 164 165 /** 166 * Sets the text on top of the seekbar. 167 */ setSeekBarTopTextViewText(String text)168 public void setSeekBarTopTextViewText(String text) { 169 if (mSeekBarTopTextView != null) { 170 mSeekBarTopTextView.setText(text); 171 } 172 mSeekBarTopText = text; 173 } 174 175 /** 176 * Sets the text view visibility on left of the seekbar. 177 */ setSeekBarLeftTextViewVisibility(int visibility)178 public void setSeekBarLeftTextViewVisibility(int visibility) { 179 if (mSeekBarLeftTextView != null) { 180 mSeekBarLeftTextView.setVisibility(visibility); 181 } 182 mSeekBarLeftTextViewVisibility = visibility; 183 } 184 185 /** 186 * Gets the text on left of the seekbar. 187 */ 188 @Nullable getSeekBarLeftTextViewText()189 public String getSeekBarLeftTextViewText() { 190 if (mSeekBarLeftTextView != null) { 191 return mSeekBarLeftTextView.getText().toString(); 192 } 193 return mSeekBarLeftText; 194 } 195 196 /** 197 * Sets the text on Left of the seekbar. 198 */ setSeekBarLeftTextViewText(@ullable String text)199 public void setSeekBarLeftTextViewText(@Nullable String text) { 200 if (mSeekBarLeftTextView != null) { 201 mSeekBarLeftTextView.setText(text); 202 } 203 mSeekBarLeftText = text; 204 } 205 206 207 /** 208 * Sets the text view visibility on right of the seekbar. 209 */ setSeekBarRightTextViewVisibility(int visibility)210 public void setSeekBarRightTextViewVisibility(int visibility) { 211 if (mSeekBarRightTextView != null) { 212 mSeekBarRightTextView.setVisibility(visibility); 213 } 214 mSeekBarRightTextViewVisibility = visibility; 215 } 216 217 /** 218 * Gets the text on right of the seekbar. 219 */ 220 @Nullable getSeekBarRightTextViewText()221 public String getSeekBarRightTextViewText() { 222 if (mSeekBarRightTextView != null) { 223 return mSeekBarRightTextView.getText().toString(); 224 } 225 return mSeekBarRightText; 226 } 227 228 229 /** 230 * Sets the text on right of the seekbar. 231 */ setSeekBarRightTextViewText(@ullable String text)232 public void setSeekBarRightTextViewText(@Nullable String text) { 233 if (mSeekBarRightTextView != null) { 234 mSeekBarRightTextView.setText(text); 235 } 236 mSeekBarRightText = text; 237 } 238 239 /** 240 * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also 241 * provides notifications of when the user starts and stops a touch gesture within the SeekBar. 242 * 243 * @param listener The seek bar notification listener 244 * @see SeekBar.OnSeekBarChangeListener 245 */ setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener)246 public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener) { 247 if (mSeekBar != null) { 248 mSeekBar.setOnSeekBarChangeListener(listener); 249 } 250 mOnSeekBarChangeListener = listener; 251 } 252 253 /** Get the upper range of the progress bar */ getMaxProgress()254 public int getMaxProgress() { 255 if (mSeekBar != null) { 256 return mSeekBar.getMax(); 257 } 258 return mMaxProgress; 259 } 260 261 /** Set the upper range of the progress bar */ setMaxProgress(int maxProgress)262 public void setMaxProgress(int maxProgress) { 263 if (mSeekBar != null) { 264 mSeekBar.setMax(maxProgress); 265 } 266 mMaxProgress = maxProgress; 267 } 268 } 269