1 /*
2  * Copyright (C) 2020 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.text.TextUtils;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.ListPreference;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.PreferenceControllerMixin;
29 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
30 
31 /**
32  * Preference controller to control Bluetooth MAP version
33  */
34 public class BluetoothMapVersionPreferenceController extends DeveloperOptionsPreferenceController
35         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
36 
37     private static final String BLUETOOTH_SELECT_MAP_VERSION_KEY =
38             "bluetooth_select_map_version";
39 
40     @VisibleForTesting
41     static final String BLUETOOTH_MAP_VERSION_PROPERTY = "persist.bluetooth.mapversion";
42 
43     private final String[] mListValues;
44     private final String[] mListSummaries;
45 
BluetoothMapVersionPreferenceController(Context context)46     public BluetoothMapVersionPreferenceController(Context context) {
47         super(context);
48 
49         mListValues = context.getResources().getStringArray(R.array.bluetooth_map_version_values);
50         mListSummaries = context.getResources().getStringArray(R.array.bluetooth_map_versions);
51     }
52 
53     @Override
getPreferenceKey()54     public String getPreferenceKey() {
55         return BLUETOOTH_SELECT_MAP_VERSION_KEY;
56     }
57 
58     @Override
onPreferenceChange(Preference preference, Object newValue)59     public boolean onPreferenceChange(Preference preference, Object newValue) {
60         SystemProperties.set(BLUETOOTH_MAP_VERSION_PROPERTY, newValue.toString());
61         updateState(mPreference);
62         return true;
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         final ListPreference listPreference = (ListPreference) preference;
68         final String currentValue = SystemProperties.get(BLUETOOTH_MAP_VERSION_PROPERTY);
69         int index = 0; // Defaults to MAP 1.2
70         for (int i = 0; i < mListValues.length; i++) {
71             if (TextUtils.equals(currentValue, mListValues[i])) {
72                 index = i;
73                 break;
74             }
75         }
76         listPreference.setValue(mListValues[index]);
77         listPreference.setSummary(mListSummaries[index]);
78     }
79 }
80