1 /* 2 * Copyright (C) 2018 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.development; 18 19 import android.content.Context; 20 import android.os.SystemProperties; 21 import android.support.annotation.VisibleForTesting; 22 import android.support.v7.preference.ListPreference; 23 import android.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceScreen; 25 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 28 29 public class BluetoothMaxConnectedAudioDevicesPreferenceController extends 30 DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, 31 PreferenceControllerMixin { 32 33 private static final String MAX_CONNECTED_AUDIO_DEVICES_PREFERENCE_KEY = 34 "bluetooth_max_connected_audio_devices"; 35 36 @VisibleForTesting 37 static final String MAX_CONNECTED_AUDIO_DEVICES_PROPERTY = 38 "persist.bluetooth.maxconnectedaudiodevices"; 39 40 private final int mDefaultMaxConnectedAudioDevices; 41 BluetoothMaxConnectedAudioDevicesPreferenceController(Context context)42 public BluetoothMaxConnectedAudioDevicesPreferenceController(Context context) { 43 super(context); 44 mDefaultMaxConnectedAudioDevices = mContext.getResources().getInteger( 45 com.android.internal.R.integer.config_bluetooth_max_connected_audio_devices); 46 } 47 48 @Override getPreferenceKey()49 public String getPreferenceKey() { 50 return MAX_CONNECTED_AUDIO_DEVICES_PREFERENCE_KEY; 51 } 52 53 @Override displayPreference(PreferenceScreen screen)54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 final ListPreference listPreference = (ListPreference) mPreference; 57 final CharSequence[] entries = listPreference.getEntries(); 58 entries[0] = String.format(entries[0].toString(), mDefaultMaxConnectedAudioDevices); 59 listPreference.setEntries(entries); 60 } 61 62 @Override onPreferenceChange(Preference preference, Object newValue)63 public boolean onPreferenceChange(Preference preference, Object newValue) { 64 String newValueString = newValue.toString(); 65 final ListPreference listPreference = (ListPreference) preference; 66 if (listPreference.findIndexOfValue(newValueString) <= 0) { 67 // Reset property value when default is chosen or when value is illegal 68 newValueString = ""; 69 } 70 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, newValueString); 71 updateState(preference); 72 return true; 73 } 74 75 @Override updateState(Preference preference)76 public void updateState(Preference preference) { 77 final ListPreference listPreference = (ListPreference) preference; 78 final CharSequence[] entries = listPreference.getEntries(); 79 final String currentValue = SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY); 80 int index = 0; 81 if (!currentValue.isEmpty()) { 82 index = listPreference.findIndexOfValue(currentValue); 83 if (index < 0) { 84 // Reset property value when value is illegal 85 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, ""); 86 index = 0; 87 } 88 } 89 listPreference.setValueIndex(index); 90 listPreference.setSummary(entries[index]); 91 } 92 93 @Override onDeveloperOptionsSwitchEnabled()94 protected void onDeveloperOptionsSwitchEnabled() { 95 super.onDeveloperOptionsSwitchEnabled(); 96 updateState(mPreference); 97 } 98 99 @Override onDeveloperOptionsSwitchDisabled()100 protected void onDeveloperOptionsSwitchDisabled() { 101 super.onDeveloperOptionsSwitchDisabled(); 102 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, ""); 103 updateState(mPreference); 104 } 105 } 106 107