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