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