• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static android.bluetooth.BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST;
20 
21 import android.bluetooth.BluetoothA2dp;
22 import android.bluetooth.BluetoothCodecConfig;
23 import android.bluetooth.BluetoothCodecStatus;
24 import android.bluetooth.BluetoothDevice;
25 import android.content.Context;
26 import android.util.Log;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.settings.development.BluetoothA2dpConfigStore;
31 import com.android.settingslib.core.lifecycle.Lifecycle;
32 
33 import java.util.List;
34 
35 /**
36  * Abstract class for Bluetooth A2DP config dialog controller in developer option.
37  */
38 public abstract class AbstractBluetoothDialogPreferenceController extends
39         AbstractBluetoothPreferenceController implements BaseBluetoothDialogPreference.Callback {
40 
41     private static final String TAG = "AbstractBtDlgCtr";
42 
43     protected static final int[] CODEC_TYPES = {
44             BluetoothCodecConfig.SOURCE_CODEC_TYPE_OPUS,
45             BluetoothCodecConfig.SOURCE_CODEC_TYPE_LC3,
46             BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC,
47             BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD,
48             BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX,
49             BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
50             BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC};
51     protected static final int[] SAMPLE_RATES = {BluetoothCodecConfig.SAMPLE_RATE_192000,
52             BluetoothCodecConfig.SAMPLE_RATE_176400,
53             BluetoothCodecConfig.SAMPLE_RATE_96000,
54             BluetoothCodecConfig.SAMPLE_RATE_88200,
55             BluetoothCodecConfig.SAMPLE_RATE_48000,
56             BluetoothCodecConfig.SAMPLE_RATE_44100};
57     protected static final int[] BITS_PER_SAMPLES = {BluetoothCodecConfig.BITS_PER_SAMPLE_32,
58             BluetoothCodecConfig.BITS_PER_SAMPLE_24,
59             BluetoothCodecConfig.BITS_PER_SAMPLE_16};
60     protected static final int[] CHANNEL_MODES = {BluetoothCodecConfig.CHANNEL_MODE_STEREO,
61             BluetoothCodecConfig.CHANNEL_MODE_MONO};
62 
63     protected final BluetoothA2dpConfigStore mBluetoothA2dpConfigStore;
64 
AbstractBluetoothDialogPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)65     public AbstractBluetoothDialogPreferenceController(Context context, Lifecycle lifecycle,
66                                                        BluetoothA2dpConfigStore store) {
67         super(context, lifecycle, store);
68         mBluetoothA2dpConfigStore = store;
69     }
70 
71     @Override
updateState(Preference preference)72     public void updateState(Preference preference) {
73         super.updateState(preference);
74     }
75 
76     @Override
getSummary()77     public CharSequence getSummary() {
78         return ((BaseBluetoothDialogPreference) mPreference).generateSummary(
79                 getCurrentConfigIndex());
80     }
81 
82     @Override
onIndexUpdated(int index)83     public void onIndexUpdated(int index) {
84         final BluetoothA2dp bluetoothA2dp = mBluetoothA2dp;
85         if (bluetoothA2dp == null) {
86             return;
87         }
88         // update the cache of a2dp config(mBluetoothA2dpConfigStore) before writing config.
89         initConfigStore();
90         writeConfigurationValues(index);
91         final BluetoothCodecConfig codecConfig = mBluetoothA2dpConfigStore.createCodecConfig();
92         BluetoothDevice activeDevice = getA2dpActiveDevice();
93         if (activeDevice != null) {
94             bluetoothA2dp.setCodecConfigPreference(activeDevice, codecConfig);
95         }
96         mPreference.setSummary(((BaseBluetoothDialogPreference) mPreference).generateSummary(
97                 index));
98     }
99 
100     @Override
getCurrentConfigIndex()101     public int getCurrentConfigIndex() {
102         final BluetoothCodecConfig codecConfig = getCurrentCodecConfig();
103         if (codecConfig == null) {
104             Log.d(TAG, "Unable to get current config index. Current codec Config is null.");
105             return getDefaultIndex();
106         }
107         return getCurrentIndexByConfig(codecConfig);
108     }
109 
110     @Override
onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp)111     public void onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp) {
112         super.onBluetoothServiceConnected(bluetoothA2dp);
113         initConfigStore();
114     }
115 
initConfigStore()116     private void initConfigStore() {
117         final BluetoothCodecConfig config = getCurrentCodecConfig();
118         if (config == null) {
119             return;
120         }
121         mBluetoothA2dpConfigStore.setCodecType(config.getExtendedCodecType());
122         mBluetoothA2dpConfigStore.setSampleRate(config.getSampleRate());
123         mBluetoothA2dpConfigStore.setBitsPerSample(config.getBitsPerSample());
124         mBluetoothA2dpConfigStore.setChannelMode(config.getChannelMode());
125         mBluetoothA2dpConfigStore.setCodecPriority(CODEC_PRIORITY_HIGHEST);
126         mBluetoothA2dpConfigStore.setCodecSpecific1Value(config.getCodecSpecific1());
127     }
128 
129     /**
130      * Updates the new value to the {@link BluetoothA2dpConfigStore}.
131      *
132      * @param newValue the new setting value
133      */
writeConfigurationValues(int newValue)134     protected abstract void writeConfigurationValues(int newValue);
135 
136     /**
137      * To get the current A2DP index value.
138      *
139      * @param config for the current {@link BluetoothCodecConfig}.
140      * @return the current index.
141      */
getCurrentIndexByConfig(BluetoothCodecConfig config)142     protected abstract int getCurrentIndexByConfig(BluetoothCodecConfig config);
143 
144     /**
145      * @return the default index.
146      */
getDefaultIndex()147     protected int getDefaultIndex() {
148         return ((BaseBluetoothDialogPreference) mPreference).getDefaultIndex();
149     }
150 
151     /**
152      * To get the current A2DP codec config.
153      *
154      * @return {@link BluetoothCodecConfig}.
155      */
getCurrentCodecConfig()156     protected BluetoothCodecConfig getCurrentCodecConfig() {
157         final BluetoothA2dp bluetoothA2dp = mBluetoothA2dp;
158         if (bluetoothA2dp == null) {
159             return null;
160         }
161         BluetoothDevice activeDevice = getA2dpActiveDevice();
162         if (activeDevice == null) {
163             Log.d(TAG, "Unable to get current codec config. No active device.");
164             return null;
165         }
166         final BluetoothCodecStatus codecStatus =
167                 bluetoothA2dp.getCodecStatus(activeDevice);
168         if (codecStatus == null) {
169             Log.d(TAG, "Unable to get current codec config. Codec status is null");
170             return null;
171         }
172         return codecStatus.getCodecConfig();
173     }
174 
175     /**
176      * To get the selectable A2DP configs.
177      *
178      * @return Array of {@link BluetoothCodecConfig}.
179      */
getSelectableConfigs(BluetoothDevice device)180     protected List<BluetoothCodecConfig> getSelectableConfigs(BluetoothDevice device) {
181         final BluetoothA2dp bluetoothA2dp = mBluetoothA2dp;
182         if (bluetoothA2dp == null) {
183             return null;
184         }
185         BluetoothDevice bluetoothDevice =
186                 (device != null) ? device : getA2dpActiveDevice();
187         if (bluetoothDevice == null) {
188             return null;
189         }
190         final BluetoothCodecStatus codecStatus = bluetoothA2dp.getCodecStatus(bluetoothDevice);
191         if (codecStatus != null) {
192             return codecStatus.getCodecsSelectableCapabilities();
193         }
194         return null;
195     }
196 
197     /**
198      * To get the selectable A2DP config by codec type.
199      *
200      * @return {@link BluetoothCodecConfig}.
201      */
getSelectableByCodecType(int codecTypeValue)202     protected BluetoothCodecConfig getSelectableByCodecType(int codecTypeValue) {
203         BluetoothDevice activeDevice = getA2dpActiveDevice();
204         if (activeDevice == null) {
205             Log.d(TAG, "Unable to get selectable config. No active device.");
206             return null;
207         }
208         final List<BluetoothCodecConfig> configs = getSelectableConfigs(activeDevice);
209         for (BluetoothCodecConfig config : configs) {
210             if (config.getCodecType() == codecTypeValue) {
211                 return config;
212             }
213         }
214         Log.d(TAG, "Unable to find matching codec config, type is " + codecTypeValue);
215         return null;
216     }
217 
218     /**
219      * Method to notify controller when the HD audio(optional codec) state is changed.
220      *
221      * @param enabled Is {@code true} when the setting is enabled.
222      */
onHDAudioEnabled(boolean enabled)223     public void onHDAudioEnabled(boolean enabled) {}
224 
getHighestCodec(BluetoothA2dp bluetoothA2dp, BluetoothDevice activeDevice, List<BluetoothCodecConfig> configs)225     static int getHighestCodec(BluetoothA2dp bluetoothA2dp, BluetoothDevice activeDevice,
226             List<BluetoothCodecConfig> configs) {
227         if (configs == null) {
228             Log.d(TAG, "Unable to get highest codec. Configs are empty");
229             return BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID;
230         }
231         // If HD audio is not enabled, SBC is the only one available codec.
232         if (bluetoothA2dp.isOptionalCodecsEnabled(activeDevice)
233                 != BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED) {
234             return BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC;
235         }
236         for (int i = 0; i < CODEC_TYPES.length; i++) {
237             for (BluetoothCodecConfig config : configs) {
238                 if (config.getCodecType() == CODEC_TYPES[i]) {
239                     return CODEC_TYPES[i];
240                 }
241             }
242         }
243         return BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID;
244     }
245 
getHighestSampleRate(BluetoothCodecConfig config)246     static int getHighestSampleRate(BluetoothCodecConfig config) {
247         if (config == null) {
248             Log.d(TAG, "Unable to get highest sample rate. Config is empty");
249             return BluetoothCodecConfig.SAMPLE_RATE_NONE;
250         }
251         final int capability = config.getSampleRate();
252         for (int i = 0; i < SAMPLE_RATES.length; i++) {
253             if ((capability & SAMPLE_RATES[i]) != 0) {
254                 return SAMPLE_RATES[i];
255             }
256         }
257         return BluetoothCodecConfig.SAMPLE_RATE_NONE;
258     }
259 
getHighestBitsPerSample(BluetoothCodecConfig config)260     static int getHighestBitsPerSample(BluetoothCodecConfig config) {
261         if (config == null) {
262             Log.d(TAG, "Unable to get highest bits per sample. Config is empty");
263             return BluetoothCodecConfig.BITS_PER_SAMPLE_NONE;
264         }
265         final int capability = config.getBitsPerSample();
266         for (int i = 0; i < BITS_PER_SAMPLES.length; i++) {
267             if ((capability & BITS_PER_SAMPLES[i]) != 0) {
268                 return BITS_PER_SAMPLES[i];
269             }
270         }
271         return BluetoothCodecConfig.BITS_PER_SAMPLE_NONE;
272     }
273 
getHighestChannelMode(BluetoothCodecConfig config)274     static int getHighestChannelMode(BluetoothCodecConfig config) {
275         if (config == null) {
276             Log.d(TAG, "Unable to get highest channel mode. Config is empty");
277             return BluetoothCodecConfig.CHANNEL_MODE_NONE;
278         }
279         final int capability = config.getChannelMode();
280         for (int i = 0; i < CHANNEL_MODES.length; i++) {
281             if ((capability & CHANNEL_MODES[i]) != 0) {
282                 return CHANNEL_MODES[i];
283             }
284         }
285         return BluetoothCodecConfig.CHANNEL_MODE_NONE;
286     }
287 }
288