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 android.bluetooth.BluetoothCodecConfig; 20 import android.content.Context; 21 import android.util.Log; 22 23 import androidx.annotation.VisibleForTesting; 24 import androidx.preference.PreferenceScreen; 25 26 import com.android.settings.development.BluetoothA2dpConfigStore; 27 import com.android.settingslib.core.lifecycle.Lifecycle; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * Dialog preference controller to set the Bluetooth A2DP config of bit per sample 34 */ 35 public class BluetoothBitPerSampleDialogPreferenceController extends 36 AbstractBluetoothDialogPreferenceController { 37 38 private static final String KEY = "bluetooth_bit_per_sample_settings"; 39 private static final String TAG = "BtBitPerSampleCtr"; 40 BluetoothBitPerSampleDialogPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)41 public BluetoothBitPerSampleDialogPreferenceController(Context context, Lifecycle lifecycle, 42 BluetoothA2dpConfigStore store) { 43 super(context, lifecycle, store); 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return KEY; 49 } 50 51 @Override displayPreference(PreferenceScreen screen)52 public void displayPreference(PreferenceScreen screen) { 53 super.displayPreference(screen); 54 ((BaseBluetoothDialogPreference) mPreference).setCallback(this); 55 } 56 57 @Override writeConfigurationValues(final int index)58 protected void writeConfigurationValues(final int index) { 59 int bitsPerSampleValue = BluetoothCodecConfig.BITS_PER_SAMPLE_NONE; 60 switch (index) { 61 case 0: 62 final BluetoothCodecConfig currentConfig = getCurrentCodecConfig(); 63 if (currentConfig != null) { 64 bitsPerSampleValue = getHighestBitsPerSample(getSelectableByCodecType( 65 currentConfig.getCodecType())); 66 } 67 break; 68 case 1: 69 bitsPerSampleValue = BluetoothCodecConfig.BITS_PER_SAMPLE_16; 70 break; 71 case 2: 72 bitsPerSampleValue = BluetoothCodecConfig.BITS_PER_SAMPLE_24; 73 break; 74 case 3: 75 bitsPerSampleValue = BluetoothCodecConfig.BITS_PER_SAMPLE_32; 76 break; 77 default: 78 break; 79 } 80 mBluetoothA2dpConfigStore.setBitsPerSample(bitsPerSampleValue); 81 } 82 83 @Override getCurrentIndexByConfig(BluetoothCodecConfig config)84 protected int getCurrentIndexByConfig(BluetoothCodecConfig config) { 85 if (config == null) { 86 Log.e(TAG, "Unable to get current config index. Config is null."); 87 } 88 return convertCfgToBtnIndex(config.getBitsPerSample()); 89 } 90 91 @Override getSelectableIndex()92 public List<Integer> getSelectableIndex() { 93 List<Integer> selectableIndex = new ArrayList<>(); 94 selectableIndex.add(getDefaultIndex()); 95 final BluetoothCodecConfig currentConfig = getCurrentCodecConfig(); 96 if (currentConfig != null) { 97 final int configs = 98 getSelectableByCodecType(currentConfig.getCodecType()).getBitsPerSample(); 99 for (int i = 0; i < BITS_PER_SAMPLES.length; i++) { 100 if ((configs & BITS_PER_SAMPLES[i]) != 0) { 101 selectableIndex.add(convertCfgToBtnIndex(BITS_PER_SAMPLES[i])); 102 } 103 } 104 } 105 return selectableIndex; 106 } 107 108 @VisibleForTesting convertCfgToBtnIndex(int config)109 int convertCfgToBtnIndex(int config) { 110 int index = getDefaultIndex(); 111 switch (config) { 112 case BluetoothCodecConfig.BITS_PER_SAMPLE_16: 113 index = 1; 114 break; 115 case BluetoothCodecConfig.BITS_PER_SAMPLE_24: 116 index = 2; 117 break; 118 case BluetoothCodecConfig.BITS_PER_SAMPLE_32: 119 index = 3; 120 break; 121 default: 122 Log.e(TAG, "Unsupported config:" + config); 123 break; 124 } 125 return index; 126 } 127 } 128