From ae728039ac88929c6ff2140b2d276747f2a681dd Mon Sep 17 00:00:00 2001 From: Yiyi Shen Date: Fri, 24 Nov 2023 15:41:43 +0800 Subject: [PATCH] [Audiosharing] Enable/disable the main switch base on BT state. Flagged with enable_le_audio_sharing Bug: 305620450 Test: Manual Change-Id: I48bf0c6fb40ff629610e6e1a48e209fea44ebdde --- .../AudioSharingSwitchBarController.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java index be02519c47c..96a55790e6e 100644 --- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java +++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java @@ -16,12 +16,16 @@ package com.android.settings.connecteddevice.audiosharing; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothLeBroadcast; import android.bluetooth.BluetoothLeBroadcastAssistant; import android.bluetooth.BluetoothLeBroadcastMetadata; import android.bluetooth.BluetoothLeBroadcastReceiveState; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.util.Log; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; @@ -58,6 +62,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController } private final SettingsMainSwitchBar mSwitchBar; + private final BluetoothAdapter mBluetoothAdapter; + private final IntentFilter mIntentFilter; private final LocalBluetoothManager mBtManager; private final LocalBluetoothLeBroadcast mBroadcast; private final LocalBluetoothLeBroadcastAssistant mAssistant; @@ -65,6 +71,18 @@ public class AudioSharingSwitchBarController extends BasePreferenceController private final OnSwitchBarChangedListener mListener; private DashboardFragment mFragment; + BroadcastReceiver mReceiver = + new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) return; + int adapterState = + intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR); + mSwitchBar.setChecked(isBroadcasting()); + mSwitchBar.setEnabled(adapterState == BluetoothAdapter.STATE_ON); + } + }; + private final BluetoothLeBroadcast.Callback mBroadcastCallback = new BluetoothLeBroadcast.Callback() { @Override @@ -245,27 +263,34 @@ public class AudioSharingSwitchBarController extends BasePreferenceController super(context, PREF_KEY); mSwitchBar = switchBar; mListener = listener; + mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); mBtManager = Utils.getLocalBtManager(context); mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile(); mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile(); mExecutor = Executors.newSingleThreadExecutor(); - mSwitchBar.setChecked(isBroadcasting()); } @Override public void onStart(@NonNull LifecycleOwner owner) { mSwitchBar.addOnSwitchChangeListener(this); + mContext.registerReceiver(mReceiver, mIntentFilter, Context.RECEIVER_EXPORTED_UNAUDITED); if (mBroadcast != null) { mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback); } if (mAssistant != null) { mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback); } + if (isAvailable()) { + mSwitchBar.setChecked(isBroadcasting()); + mSwitchBar.setEnabled(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()); + } } @Override public void onStop(@NonNull LifecycleOwner owner) { mSwitchBar.removeOnSwitchChangeListener(this); + mContext.unregisterReceiver(mReceiver); if (mBroadcast != null) { mBroadcast.unregisterServiceCallBack(mBroadcastCallback); }