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 writeConfigurationValues(index); 89 final BluetoothCodecConfig codecConfig = mBluetoothA2dpConfigStore.createCodecConfig(); 90 BluetoothDevice activeDevice = getA2dpActiveDevice(); 91 if (activeDevice != null) { 92 bluetoothA2dp.setCodecConfigPreference(activeDevice, codecConfig); 93 } 94 mPreference.setSummary(((BaseBluetoothDialogPreference) mPreference).generateSummary( 95 index)); 96 } 97 98 @Override getCurrentConfigIndex()99 public int getCurrentConfigIndex() { 100 final BluetoothCodecConfig codecConfig = getCurrentCodecConfig(); 101 if (codecConfig == null) { 102 Log.d(TAG, "Unable to get current config index. Current codec Config is null."); 103 return getDefaultIndex(); 104 } 105 return getCurrentIndexByConfig(codecConfig); 106 } 107 108 @Override onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp)109 public void onBluetoothServiceConnected(BluetoothA2dp bluetoothA2dp) { 110 super.onBluetoothServiceConnected(bluetoothA2dp); 111 initConfigStore(); 112 } 113 initConfigStore()114 private void initConfigStore() { 115 final BluetoothCodecConfig config = getCurrentCodecConfig(); 116 if (config == null) { 117 return; 118 } 119 mBluetoothA2dpConfigStore.setCodecType(config.getCodecType()); 120 mBluetoothA2dpConfigStore.setSampleRate(config.getSampleRate()); 121 mBluetoothA2dpConfigStore.setBitsPerSample(config.getBitsPerSample()); 122 mBluetoothA2dpConfigStore.setChannelMode(config.getChannelMode()); 123 mBluetoothA2dpConfigStore.setCodecPriority(CODEC_PRIORITY_HIGHEST); 124 mBluetoothA2dpConfigStore.setCodecSpecific1Value(config.getCodecSpecific1()); 125 } 126 127 /** 128 * Updates the new value to the {@link BluetoothA2dpConfigStore}. 129 * 130 * @param newValue the new setting value 131 */ writeConfigurationValues(int newValue)132 protected abstract void writeConfigurationValues(int newValue); 133 134 /** 135 * To get the current A2DP index value. 136 * 137 * @param config for the current {@link BluetoothCodecConfig}. 138 * @return the current index. 139 */ getCurrentIndexByConfig(BluetoothCodecConfig config)140 protected abstract int getCurrentIndexByConfig(BluetoothCodecConfig config); 141 142 /** 143 * @return the default index. 144 */ getDefaultIndex()145 protected int getDefaultIndex() { 146 return ((BaseBluetoothDialogPreference) mPreference).getDefaultIndex(); 147 } 148 149 /** 150 * To get the current A2DP codec config. 151 * 152 * @return {@link BluetoothCodecConfig}. 153 */ getCurrentCodecConfig()154 protected BluetoothCodecConfig getCurrentCodecConfig() { 155 final BluetoothA2dp bluetoothA2dp = mBluetoothA2dp; 156 if (bluetoothA2dp == null) { 157 return null; 158 } 159 BluetoothDevice activeDevice = getA2dpActiveDevice(); 160 if (activeDevice == null) { 161 Log.d(TAG, "Unable to get current codec config. No active device."); 162 return null; 163 } 164 final BluetoothCodecStatus codecStatus = 165 bluetoothA2dp.getCodecStatus(activeDevice); 166 if (codecStatus == null) { 167 Log.d(TAG, "Unable to get current codec config. Codec status is null"); 168 return null; 169 } 170 return codecStatus.getCodecConfig(); 171 } 172 173 /** 174 * To get the selectable A2DP configs. 175 * 176 * @return Array of {@link BluetoothCodecConfig}. 177 */ getSelectableConfigs(BluetoothDevice device)178 protected List<BluetoothCodecConfig> getSelectableConfigs(BluetoothDevice device) { 179 final BluetoothA2dp bluetoothA2dp = mBluetoothA2dp; 180 if (bluetoothA2dp == null) { 181 return null; 182 } 183 BluetoothDevice bluetoothDevice = 184 (device != null) ? device : getA2dpActiveDevice(); 185 if (bluetoothDevice == null) { 186 return null; 187 } 188 final BluetoothCodecStatus codecStatus = bluetoothA2dp.getCodecStatus(bluetoothDevice); 189 if (codecStatus != null) { 190 return codecStatus.getCodecsSelectableCapabilities(); 191 } 192 return null; 193 } 194 195 /** 196 * To get the selectable A2DP config by codec type. 197 * 198 * @return {@link BluetoothCodecConfig}. 199 */ getSelectableByCodecType(int codecTypeValue)200 protected BluetoothCodecConfig getSelectableByCodecType(int codecTypeValue) { 201 BluetoothDevice activeDevice = getA2dpActiveDevice(); 202 if (activeDevice == null) { 203 Log.d(TAG, "Unable to get selectable config. No active device."); 204 return null; 205 } 206 final List<BluetoothCodecConfig> configs = getSelectableConfigs(activeDevice); 207 for (BluetoothCodecConfig config : configs) { 208 if (config.getCodecType() == codecTypeValue) { 209 return config; 210 } 211 } 212 Log.d(TAG, "Unable to find matching codec config, type is " + codecTypeValue); 213 return null; 214 } 215 216 /** 217 * Method to notify controller when the HD audio(optional codec) state is changed. 218 * 219 * @param enabled Is {@code true} when the setting is enabled. 220 */ onHDAudioEnabled(boolean enabled)221 public void onHDAudioEnabled(boolean enabled) {} 222 getHighestCodec(BluetoothA2dp bluetoothA2dp, BluetoothDevice activeDevice, List<BluetoothCodecConfig> configs)223 static int getHighestCodec(BluetoothA2dp bluetoothA2dp, BluetoothDevice activeDevice, 224 List<BluetoothCodecConfig> configs) { 225 if (configs == null) { 226 Log.d(TAG, "Unable to get highest codec. Configs are empty"); 227 return BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID; 228 } 229 // If HD audio is not enabled, SBC is the only one available codec. 230 if (bluetoothA2dp.isOptionalCodecsEnabled(activeDevice) 231 != BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED) { 232 return BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC; 233 } 234 for (int i = 0; i < CODEC_TYPES.length; i++) { 235 for (BluetoothCodecConfig config : configs) { 236 if (config.getCodecType() == CODEC_TYPES[i]) { 237 return CODEC_TYPES[i]; 238 } 239 } 240 } 241 return BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID; 242 } 243 getHighestSampleRate(BluetoothCodecConfig config)244 static int getHighestSampleRate(BluetoothCodecConfig config) { 245 if (config == null) { 246 Log.d(TAG, "Unable to get highest sample rate. Config is empty"); 247 return BluetoothCodecConfig.SAMPLE_RATE_NONE; 248 } 249 final int capability = config.getSampleRate(); 250 for (int i = 0; i < SAMPLE_RATES.length; i++) { 251 if ((capability & SAMPLE_RATES[i]) != 0) { 252 return SAMPLE_RATES[i]; 253 } 254 } 255 return BluetoothCodecConfig.SAMPLE_RATE_NONE; 256 } 257 getHighestBitsPerSample(BluetoothCodecConfig config)258 static int getHighestBitsPerSample(BluetoothCodecConfig config) { 259 if (config == null) { 260 Log.d(TAG, "Unable to get highest bits per sample. Config is empty"); 261 return BluetoothCodecConfig.BITS_PER_SAMPLE_NONE; 262 } 263 final int capability = config.getBitsPerSample(); 264 for (int i = 0; i < BITS_PER_SAMPLES.length; i++) { 265 if ((capability & BITS_PER_SAMPLES[i]) != 0) { 266 return BITS_PER_SAMPLES[i]; 267 } 268 } 269 return BluetoothCodecConfig.BITS_PER_SAMPLE_NONE; 270 } 271 getHighestChannelMode(BluetoothCodecConfig config)272 static int getHighestChannelMode(BluetoothCodecConfig config) { 273 if (config == null) { 274 Log.d(TAG, "Unable to get highest channel mode. Config is empty"); 275 return BluetoothCodecConfig.CHANNEL_MODE_NONE; 276 } 277 final int capability = config.getChannelMode(); 278 for (int i = 0; i < CHANNEL_MODES.length; i++) { 279 if ((capability & CHANNEL_MODES[i]) != 0) { 280 return CHANNEL_MODES[i]; 281 } 282 } 283 return BluetoothCodecConfig.CHANNEL_MODE_NONE; 284 } 285 } 286