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