1 /* 2 * Copyright (C) 2017 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; 18 19 import android.bluetooth.BluetoothCodecConfig; 20 import android.content.Context; 21 22 import com.android.settings.R; 23 import com.android.settingslib.core.lifecycle.Lifecycle; 24 25 public class BluetoothAudioCodecPreferenceController extends 26 AbstractBluetoothA2dpPreferenceController { 27 28 private static final int DEFAULT_INDEX = 0; 29 private static final String BLUETOOTH_SELECT_A2DP_CODEC_KEY = "bluetooth_select_a2dp_codec"; 30 BluetoothAudioCodecPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)31 public BluetoothAudioCodecPreferenceController(Context context, Lifecycle lifecycle, 32 BluetoothA2dpConfigStore store) { 33 super(context, lifecycle, store); 34 } 35 36 @Override getPreferenceKey()37 public String getPreferenceKey() { 38 return BLUETOOTH_SELECT_A2DP_CODEC_KEY; 39 } 40 41 @Override getListValues()42 protected String[] getListValues() { 43 return mContext.getResources().getStringArray( 44 R.array.bluetooth_a2dp_codec_values); 45 } 46 47 @Override getListSummaries()48 protected String[] getListSummaries() { 49 return mContext.getResources().getStringArray( 50 R.array.bluetooth_a2dp_codec_summaries); 51 } 52 53 @Override getDefaultIndex()54 protected int getDefaultIndex() { 55 return DEFAULT_INDEX; 56 } 57 58 @Override writeConfigurationValues(Object newValue)59 protected void writeConfigurationValues(Object newValue) { 60 final int index = mPreference.findIndexOfValue(newValue.toString()); 61 int codecTypeValue = BluetoothCodecConfig.SAMPLE_RATE_NONE; // default 62 int codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT; 63 switch (index) { 64 case 0: 65 // Reset the priority of the current codec to default 66 final String oldValue = mPreference.getValue(); 67 switch (mPreference.findIndexOfValue(oldValue)) { 68 case 0: 69 break; // No current codec 70 case 1: 71 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC; 72 break; 73 case 2: 74 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC; 75 break; 76 case 3: 77 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX; 78 break; 79 case 4: 80 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD; 81 break; 82 case 5: 83 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC; 84 break; 85 default: 86 break; 87 } 88 break; 89 case 1: 90 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC; 91 codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST; 92 break; 93 case 2: 94 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC; 95 codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST; 96 break; 97 case 3: 98 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX; 99 codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST; 100 break; 101 case 4: 102 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD; 103 codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST; 104 break; 105 case 5: 106 codecTypeValue = BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC; 107 codecPriorityValue = BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST; 108 break; 109 case 6: 110 synchronized (mBluetoothA2dpConfigStore) { 111 if (mBluetoothA2dp != null) { 112 mBluetoothA2dp.enableOptionalCodecs(null); // Use current active device 113 } 114 } 115 return; 116 case 7: 117 synchronized (mBluetoothA2dpConfigStore) { 118 if (mBluetoothA2dp != null) { 119 mBluetoothA2dp.disableOptionalCodecs(null); // Use current active device 120 } 121 } 122 return; 123 default: 124 break; 125 } 126 mBluetoothA2dpConfigStore.setCodecType(codecTypeValue); 127 mBluetoothA2dpConfigStore.setCodecPriority(codecPriorityValue); 128 } 129 130 @Override getCurrentA2dpSettingIndex(BluetoothCodecConfig config)131 protected int getCurrentA2dpSettingIndex(BluetoothCodecConfig config) { 132 final int codecType = config.getCodecType(); 133 int index = DEFAULT_INDEX; 134 switch (codecType) { 135 case BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC: 136 index = 1; 137 break; 138 case BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC: 139 index = 2; 140 break; 141 case BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX: 142 index = 3; 143 break; 144 case BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD: 145 index = 4; 146 break; 147 case BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC: 148 index = 5; 149 break; 150 case BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID: 151 default: 152 break; 153 } 154 return index; 155 } 156 } 157