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