[Audiosharing] Enable/disable the main switch base on BT state.
Flagged with enable_le_audio_sharing Bug: 305620450 Test: Manual Change-Id: I48bf0c6fb40ff629610e6e1a48e209fea44ebdde
This commit is contained in:
@@ -16,12 +16,16 @@
|
|||||||
|
|
||||||
package com.android.settings.connecteddevice.audiosharing;
|
package com.android.settings.connecteddevice.audiosharing;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothLeBroadcast;
|
import android.bluetooth.BluetoothLeBroadcast;
|
||||||
import android.bluetooth.BluetoothLeBroadcastAssistant;
|
import android.bluetooth.BluetoothLeBroadcastAssistant;
|
||||||
import android.bluetooth.BluetoothLeBroadcastMetadata;
|
import android.bluetooth.BluetoothLeBroadcastMetadata;
|
||||||
import android.bluetooth.BluetoothLeBroadcastReceiveState;
|
import android.bluetooth.BluetoothLeBroadcastReceiveState;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||||
@@ -58,6 +62,8 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final SettingsMainSwitchBar mSwitchBar;
|
private final SettingsMainSwitchBar mSwitchBar;
|
||||||
|
private final BluetoothAdapter mBluetoothAdapter;
|
||||||
|
private final IntentFilter mIntentFilter;
|
||||||
private final LocalBluetoothManager mBtManager;
|
private final LocalBluetoothManager mBtManager;
|
||||||
private final LocalBluetoothLeBroadcast mBroadcast;
|
private final LocalBluetoothLeBroadcast mBroadcast;
|
||||||
private final LocalBluetoothLeBroadcastAssistant mAssistant;
|
private final LocalBluetoothLeBroadcastAssistant mAssistant;
|
||||||
@@ -65,6 +71,18 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
private final OnSwitchBarChangedListener mListener;
|
private final OnSwitchBarChangedListener mListener;
|
||||||
private DashboardFragment mFragment;
|
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 =
|
private final BluetoothLeBroadcast.Callback mBroadcastCallback =
|
||||||
new BluetoothLeBroadcast.Callback() {
|
new BluetoothLeBroadcast.Callback() {
|
||||||
@Override
|
@Override
|
||||||
@@ -245,27 +263,34 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
|
|||||||
super(context, PREF_KEY);
|
super(context, PREF_KEY);
|
||||||
mSwitchBar = switchBar;
|
mSwitchBar = switchBar;
|
||||||
mListener = listener;
|
mListener = listener;
|
||||||
|
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
|
||||||
mBtManager = Utils.getLocalBtManager(context);
|
mBtManager = Utils.getLocalBtManager(context);
|
||||||
mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
|
mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
|
||||||
mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
|
mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
|
||||||
mExecutor = Executors.newSingleThreadExecutor();
|
mExecutor = Executors.newSingleThreadExecutor();
|
||||||
mSwitchBar.setChecked(isBroadcasting());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart(@NonNull LifecycleOwner owner) {
|
public void onStart(@NonNull LifecycleOwner owner) {
|
||||||
mSwitchBar.addOnSwitchChangeListener(this);
|
mSwitchBar.addOnSwitchChangeListener(this);
|
||||||
|
mContext.registerReceiver(mReceiver, mIntentFilter, Context.RECEIVER_EXPORTED_UNAUDITED);
|
||||||
if (mBroadcast != null) {
|
if (mBroadcast != null) {
|
||||||
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
|
mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback);
|
||||||
}
|
}
|
||||||
if (mAssistant != null) {
|
if (mAssistant != null) {
|
||||||
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
|
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
|
||||||
}
|
}
|
||||||
|
if (isAvailable()) {
|
||||||
|
mSwitchBar.setChecked(isBroadcasting());
|
||||||
|
mSwitchBar.setEnabled(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop(@NonNull LifecycleOwner owner) {
|
public void onStop(@NonNull LifecycleOwner owner) {
|
||||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||||
|
mContext.unregisterReceiver(mReceiver);
|
||||||
if (mBroadcast != null) {
|
if (mBroadcast != null) {
|
||||||
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
|
mBroadcast.unregisterServiceCallBack(mBroadcastCallback);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user