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.development.bluetooth;
18 
19 import android.bluetooth.BluetoothCodecConfig;
20 import android.content.Context;
21 import android.util.Log;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.development.BluetoothA2dpConfigStore;
27 import com.android.settingslib.core.lifecycle.Lifecycle;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Dialog preference controller to set the Bluetooth A2DP config of sample rate
34  */
35 public class BluetoothSampleRateDialogPreferenceController extends
36         AbstractBluetoothDialogPreferenceController {
37 
38     private static final String KEY = "bluetooth_sample_rate_settings";
39     private static final String TAG = "BtSampleRateCtr";
40 
BluetoothSampleRateDialogPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)41     public BluetoothSampleRateDialogPreferenceController(Context context, Lifecycle lifecycle,
42                                                          BluetoothA2dpConfigStore store) {
43         super(context, lifecycle, store);
44     }
45 
46     @Override
getPreferenceKey()47     public String getPreferenceKey() {
48         return KEY;
49     }
50 
51     @Override
displayPreference(PreferenceScreen screen)52     public void displayPreference(PreferenceScreen screen) {
53         super.displayPreference(screen);
54         ((BaseBluetoothDialogPreference) mPreference).setCallback(this);
55     }
56 
57     @Override
writeConfigurationValues(final int index)58     protected void writeConfigurationValues(final int index) {
59         int sampleRateValue = BluetoothCodecConfig.SAMPLE_RATE_NONE; // default
60         switch (index) {
61             case 0:
62                 final BluetoothCodecConfig currentConfig = getCurrentCodecConfig();
63                 if (currentConfig != null) {
64                     sampleRateValue = getHighestSampleRate(getSelectableByCodecType(
65                             currentConfig.getCodecType()));
66                 }
67                 break;
68             case 1:
69                 sampleRateValue = BluetoothCodecConfig.SAMPLE_RATE_44100;
70                 break;
71             case 2:
72                 sampleRateValue = BluetoothCodecConfig.SAMPLE_RATE_48000;
73                 break;
74             case 3:
75                 sampleRateValue = BluetoothCodecConfig.SAMPLE_RATE_88200;
76                 break;
77             case 4:
78                 sampleRateValue = BluetoothCodecConfig.SAMPLE_RATE_96000;
79                 break;
80             default:
81                 break;
82         }
83         mBluetoothA2dpConfigStore.setSampleRate(sampleRateValue);
84     }
85 
86     @Override
getCurrentIndexByConfig(BluetoothCodecConfig config)87     protected int getCurrentIndexByConfig(BluetoothCodecConfig config) {
88         if (config == null) {
89             Log.e(TAG, "Unable to get current config index. Config is null.");
90         }
91         return convertCfgToBtnIndex(config.getSampleRate());
92     }
93 
94     @Override
getSelectableIndex()95     public List<Integer> getSelectableIndex() {
96         List<Integer> selectableIndex = new ArrayList<>();
97         selectableIndex.add(getDefaultIndex());
98         final BluetoothCodecConfig currentConfig = getCurrentCodecConfig();
99         if (currentConfig != null) {
100             final int configs =
101                     getSelectableByCodecType(currentConfig.getCodecType()).getSampleRate();
102             for (int sampleRate : SAMPLE_RATES) {
103                 if ((configs & sampleRate) != 0) {
104                     selectableIndex.add(convertCfgToBtnIndex(sampleRate));
105                 }
106             }
107         }
108         return selectableIndex;
109     }
110 
111     @VisibleForTesting
convertCfgToBtnIndex(int config)112     int convertCfgToBtnIndex(int config) {
113         int index = getDefaultIndex();
114         switch (config) {
115             case BluetoothCodecConfig.SAMPLE_RATE_44100:
116                 index = 1;
117                 break;
118             case BluetoothCodecConfig.SAMPLE_RATE_48000:
119                 index = 2;
120                 break;
121             case BluetoothCodecConfig.SAMPLE_RATE_88200:
122                 index = 3;
123                 break;
124             case BluetoothCodecConfig.SAMPLE_RATE_96000:
125                 index = 4;
126                 break;
127             default:
128                 Log.e(TAG, "Unsupported config:" + config);
129                 break;
130         }
131         return index;
132     }
133 }
134