1 /* 2 * Copyright 2022 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; 18 19 import android.bluetooth.BluetoothAdapter; 20 import android.bluetooth.BluetoothManager; 21 import android.bluetooth.BluetoothStatusCodes; 22 import android.content.Context; 23 import android.os.SystemProperties; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.SwitchPreference; 28 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 31 32 /** 33 * Preference controller to control Bluetooth LE audio feature 34 */ 35 public class BluetoothLeAudioPreferenceController 36 extends DeveloperOptionsPreferenceController 37 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 38 39 private static final String PREFERENCE_KEY = "bluetooth_enable_leaudio"; 40 41 private static final String LE_AUDIO_DYNAMIC_SWITCH_PROPERTY = 42 "ro.bluetooth.leaudio_switcher.supported"; 43 @VisibleForTesting 44 static final String LE_AUDIO_DYNAMIC_ENABLED_PROPERTY = 45 "persist.bluetooth.leaudio_switcher.enabled"; 46 47 private final DevelopmentSettingsDashboardFragment mFragment; 48 49 @VisibleForTesting 50 BluetoothAdapter mBluetoothAdapter; 51 52 @VisibleForTesting 53 boolean mChanged = false; 54 BluetoothLeAudioPreferenceController(Context context, DevelopmentSettingsDashboardFragment fragment)55 public BluetoothLeAudioPreferenceController(Context context, 56 DevelopmentSettingsDashboardFragment fragment) { 57 super(context); 58 mFragment = fragment; 59 mBluetoothAdapter = context.getSystemService(BluetoothManager.class).getAdapter(); 60 } 61 62 @Override getPreferenceKey()63 public String getPreferenceKey() { 64 return PREFERENCE_KEY; 65 } 66 67 @Override onPreferenceChange(Preference preference, Object newValue)68 public boolean onPreferenceChange(Preference preference, Object newValue) { 69 BluetoothRebootDialog.show(mFragment); 70 mChanged = true; 71 return false; 72 } 73 74 @Override updateState(Preference preference)75 public void updateState(Preference preference) { 76 if (mBluetoothAdapter == null) { 77 return; 78 } 79 80 final boolean leAudioEnabled = 81 (mBluetoothAdapter.isLeAudioSupported() == BluetoothStatusCodes.FEATURE_SUPPORTED); 82 ((SwitchPreference) mPreference).setChecked(leAudioEnabled); 83 84 final boolean leAudioSwitchSupported = 85 SystemProperties.getBoolean(LE_AUDIO_DYNAMIC_SWITCH_PROPERTY, false); 86 if (!leAudioSwitchSupported) { 87 mPreference.setEnabled(false); 88 } else { 89 SystemProperties.set(LE_AUDIO_DYNAMIC_ENABLED_PROPERTY, 90 Boolean.toString(leAudioEnabled)); 91 } 92 } 93 94 /** 95 * Called when the RebootDialog confirm is clicked. 96 */ onRebootDialogConfirmed()97 public void onRebootDialogConfirmed() { 98 if (!mChanged || mBluetoothAdapter == null) { 99 return; 100 } 101 102 final boolean leAudioEnabled = 103 (mBluetoothAdapter.isLeAudioSupported() == BluetoothStatusCodes.FEATURE_SUPPORTED); 104 SystemProperties.set(LE_AUDIO_DYNAMIC_ENABLED_PROPERTY, 105 Boolean.toString(!leAudioEnabled)); 106 } 107 108 /** 109 * Called when the RebootDialog cancel is clicked. 110 */ onRebootDialogCanceled()111 public void onRebootDialogCanceled() { 112 mChanged = false; 113 } 114 } 115