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