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