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 audio channel mode
34  */
35 public class BluetoothChannelModeDialogPreferenceController extends
36         AbstractBluetoothDialogPreferenceController {
37 
38     private static final String KEY = "bluetooth_channel_mode_settings";
39     private static final String TAG = "BtChannelModeCtr";
40 
BluetoothChannelModeDialogPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store)41     public BluetoothChannelModeDialogPreferenceController(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 channelModeValue = BluetoothCodecConfig.CHANNEL_MODE_NONE; // default
60         switch (index) {
61             case 0:
62                 final BluetoothCodecConfig currentConfig = getCurrentCodecConfig();
63                 if (currentConfig != null) {
64                     channelModeValue = getHighestChannelMode(getSelectableByCodecType(
65                             currentConfig.getCodecType()));
66                 }
67                 break;
68             case 1:
69                 channelModeValue = BluetoothCodecConfig.CHANNEL_MODE_MONO;
70                 break;
71             case 2:
72                 channelModeValue = BluetoothCodecConfig.CHANNEL_MODE_STEREO;
73                 break;
74             default:
75                 break;
76         }
77         mBluetoothA2dpConfigStore.setChannelMode(channelModeValue);
78     }
79 
80     @Override
getCurrentIndexByConfig(BluetoothCodecConfig config)81     protected int getCurrentIndexByConfig(BluetoothCodecConfig config) {
82         if (config == null) {
83             Log.e(TAG, "Unable to get current config index. Config is null.");
84         }
85         return convertCfgToBtnIndex(config.getChannelMode());
86     }
87 
88     @Override
getSelectableIndex()89     public List<Integer> getSelectableIndex() {
90         List<Integer> selectableIndex = new ArrayList<>();
91         selectableIndex.add(getDefaultIndex());
92         final BluetoothCodecConfig currentConfig = getCurrentCodecConfig();
93         if (currentConfig != null) {
94             final int configs =
95                     getSelectableByCodecType(currentConfig.getCodecType()).getChannelMode();
96             for (int i = 0; i < CHANNEL_MODES.length; i++) {
97                 if ((configs & CHANNEL_MODES[i]) != 0) {
98                     selectableIndex.add(convertCfgToBtnIndex(CHANNEL_MODES[i]));
99                 }
100             }
101         }
102         return selectableIndex;
103     }
104 
105     @VisibleForTesting
convertCfgToBtnIndex(int config)106     int convertCfgToBtnIndex(int config) {
107         int index = getDefaultIndex();
108         switch (config) {
109             case BluetoothCodecConfig.CHANNEL_MODE_MONO:
110                 index = 1;
111                 break;
112             case BluetoothCodecConfig.CHANNEL_MODE_STEREO:
113                 index = 2;
114                 break;
115             default:
116                 Log.e(TAG, "Unsupported config:" + config);
117                 break;
118         }
119         return index;
120     }
121 }
122