• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.development.BluetoothA2dpConfigStore;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Dialog preference controller to set the Bluetooth A2DP config of LDAC quality
35  */
36 public class BluetoothQualityDialogPreferenceController extends
37         AbstractBluetoothDialogPreferenceController {
38 
39     private static final String KEY = "bluetooth_a2dp_ldac_playback_quality";
40     private static final String TAG = "BtQualityCtr";
41 
BluetoothQualityDialogPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)42     public BluetoothQualityDialogPreferenceController(Context context, Lifecycle lifecycle,
43                                                       BluetoothA2dpConfigStore store) {
44         super(context, lifecycle, store);
45     }
46 
47     @Override
getPreferenceKey()48     public String getPreferenceKey() {
49         return KEY;
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         ((BaseBluetoothDialogPreference) mPreference).setCallback(this);
56     }
57 
58     @Override
writeConfigurationValues(final int index)59     protected void writeConfigurationValues(final int index) {
60         long codecSpecific1Value = 0; // default
61         switch (index) {
62             case 0:
63             case 1:
64             case 2:
65             case 3:
66                 codecSpecific1Value = 1000 + index;
67                 break;
68             default:
69                 break;
70         }
71         mBluetoothA2dpConfigStore.setCodecSpecific1Value(codecSpecific1Value);
72     }
73 
74     @Override
getCurrentIndexByConfig(BluetoothCodecConfig config)75     protected int getCurrentIndexByConfig(BluetoothCodecConfig config) {
76         if (config == null) {
77             Log.e(TAG, "Unable to get current config index. Config is null.");
78         }
79         return convertCfgToBtnIndex((int) config.getCodecSpecific1());
80     }
81 
82     @Override
getSelectableIndex()83     public List<Integer> getSelectableIndex() {
84         List<Integer> selectableIndex = new ArrayList<>();
85         // All four items of LDAC are available.
86         for (int i = 0; i < 4; i++) {
87             selectableIndex.add(i);
88         }
89         return selectableIndex;
90     }
91 
92     @Override
updateState(Preference preference)93     public void updateState(Preference preference) {
94         super.updateState(preference);
95         // Enable preference when current codec type is LDAC. For other cases, disable it.
96         final BluetoothCodecConfig currentConfig = getCurrentCodecConfig();
97         if (currentConfig != null
98                 && currentConfig.getCodecType() == BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC) {
99             preference.setEnabled(true);
100         } else {
101             preference.setEnabled(false);
102             preference.setSummary("");
103         }
104     }
105 
106     @Override
onHDAudioEnabled(boolean enabled)107     public void onHDAudioEnabled(boolean enabled) {
108         mPreference.setEnabled(false);
109     }
110 
111     @VisibleForTesting
convertCfgToBtnIndex(int config)112     int convertCfgToBtnIndex(int config) {
113         int index = config - 1000;
114         if (index < 0) {
115             index = getDefaultIndex();
116         }
117         return index;
118     }
119 }
120