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.accessibility; 18 19 import android.content.Context; 20 import android.os.UserHandle; 21 import android.provider.Settings; 22 import android.util.AttributeSet; 23 import android.widget.ImageView; 24 25 import androidx.core.content.res.TypedArrayUtils; 26 import androidx.preference.PreferenceViewHolder; 27 28 import com.android.settings.R; 29 import com.android.settings.widget.SeekBarPreference; 30 31 /** A slider preference that directly controls audio balance **/ 32 public class BalanceSeekBarPreference extends SeekBarPreference { 33 private static final int BALANCE_CENTER_VALUE = 100; 34 private static final int BALANCE_MAX_VALUE = 200; 35 36 private final Context mContext; 37 private BalanceSeekBar mSeekBar; 38 private ImageView mIconView; 39 BalanceSeekBarPreference(Context context, AttributeSet attrs)40 public BalanceSeekBarPreference(Context context, AttributeSet attrs) { 41 super(context, attrs, TypedArrayUtils.getAttr(context, 42 com.android.settingslib.R.attr.preferenceStyle, 43 android.R.attr.preferenceStyle)); 44 mContext = context; 45 setLayoutResource(R.layout.preference_balance_slider); 46 } 47 48 @Override onBindViewHolder(PreferenceViewHolder view)49 public void onBindViewHolder(PreferenceViewHolder view) { 50 super.onBindViewHolder(view); 51 mSeekBar = (BalanceSeekBar) view.findViewById(com.android.internal.R.id.seekbar); 52 mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon); 53 init(); 54 } 55 init()56 private void init() { 57 if (mSeekBar == null) { 58 return; 59 } 60 final float balance = Settings.System.getFloatForUser( 61 mContext.getContentResolver(), Settings.System.MASTER_BALANCE, 62 0.f /* default */, UserHandle.USER_CURRENT); 63 // Rescale balance to range 0-BALANCE_MAX_VALUE centered at BALANCE_MAX_VALUE / 2. 64 mSeekBar.setMax(BALANCE_MAX_VALUE); 65 mSeekBar.setProgress((int) (balance * 100.f) + BALANCE_CENTER_VALUE); 66 mSeekBar.setEnabled(isEnabled()); 67 } 68 } 69