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