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.BluetoothA2dp;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.Context;
22 import android.util.Log;
23 
24 import androidx.annotation.Nullable;
25 import androidx.preference.Preference;
26 import androidx.preference.TwoStatePreference;
27 
28 import com.android.settings.development.BluetoothA2dpConfigStore;
29 import com.android.settingslib.core.lifecycle.Lifecycle;
30 
31 /**
32  * Switch preference controller for HD audio(optional codec)
33  */
34 public class BluetoothHDAudioPreferenceController extends AbstractBluetoothPreferenceController
35         implements Preference.OnPreferenceChangeListener {
36 
37     private static final String KEY = "bluetooth_hd_audio_settings";
38     private static final String TAG = "BtHDAudioCtr";
39 
40     @Nullable private final Callback mCallback;
41 
BluetoothHDAudioPreferenceController(Context context, Lifecycle lifecycle, BluetoothA2dpConfigStore store, @Nullable Callback callback)42     public BluetoothHDAudioPreferenceController(Context context, Lifecycle lifecycle,
43                                                 BluetoothA2dpConfigStore store,
44                                                 @Nullable Callback callback) {
45         super(context, lifecycle, store);
46         mCallback = callback;
47     }
48 
49     @Override
updateState(Preference preference)50     public void updateState(Preference preference) {
51         final BluetoothA2dp bluetoothA2dp = mBluetoothA2dp;
52         if (bluetoothA2dp == null) {
53             mPreference.setEnabled(false);
54             return;
55         }
56         final BluetoothDevice activeDevice = getA2dpActiveDevice();
57         if (activeDevice == null) {
58             Log.e(TAG, "Active device is null. To disable HD audio button");
59             mPreference.setEnabled(false);
60             return;
61         }
62         final boolean supported = (bluetoothA2dp.isOptionalCodecsSupported(activeDevice)
63                 == BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
64         mPreference.setEnabled(supported);
65         if (supported) {
66             final boolean isEnabled = bluetoothA2dp.isOptionalCodecsEnabled(activeDevice)
67                     == BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED;
68             ((TwoStatePreference) mPreference).setChecked(isEnabled);
69         }
70     }
71 
72     @Override
getPreferenceKey()73     public String getPreferenceKey() {
74         return KEY;
75     }
76 
77     @Override
onPreferenceChange(Preference preference, Object newValue)78     public boolean onPreferenceChange(Preference preference, Object newValue) {
79         final BluetoothA2dp bluetoothA2dp = mBluetoothA2dp;
80         if (bluetoothA2dp == null) {
81             mPreference.setEnabled(false);
82             return true;
83         }
84         final boolean enabled = (Boolean) newValue;
85         final int prefValue = enabled
86                 ? BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED
87                 : BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED;
88         BluetoothDevice activeDevice = getA2dpActiveDevice();
89         if (activeDevice == null) {
90             mPreference.setEnabled(false);
91             return true;
92         }
93         bluetoothA2dp.setOptionalCodecsEnabled(activeDevice, prefValue);
94         if (enabled) {
95             bluetoothA2dp.enableOptionalCodecs(activeDevice);
96         } else {
97             bluetoothA2dp.disableOptionalCodecs(activeDevice);
98         }
99         mCallback.onBluetoothHDAudioEnabled(enabled);
100         return true;
101     }
102 }
103