[Audiosharing] Show/hide audio sharing settings based on BT state.

Flagged with enable_le_audio_sharing

Bug: 305620450
Test: atest
Change-Id: I1af8ae268194cfca8e03cc49151b68a25717e89a
This commit is contained in:
Yiyi Shen
2023-11-28 17:10:55 +08:00
parent ae728039ac
commit bdc870a4c1
8 changed files with 205 additions and 63 deletions

View File

@@ -31,6 +31,7 @@ import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
@@ -58,19 +59,20 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
private static final String PREF_KEY = "audio_sharing_main_switch";
interface OnSwitchBarChangedListener {
void onSwitchBarChanged(boolean newState);
void onSwitchBarChanged();
}
private final SettingsMainSwitchBar mSwitchBar;
private final BluetoothAdapter mBluetoothAdapter;
private final IntentFilter mIntentFilter;
private final LocalBluetoothManager mBtManager;
private final LocalBluetoothLeBroadcast mBroadcast;
private final LocalBluetoothLeBroadcastAssistant mAssistant;
private final Executor mExecutor;
private final OnSwitchBarChangedListener mListener;
private DashboardFragment mFragment;
@VisibleForTesting IntentFilter mIntentFilter;
@VisibleForTesting
BroadcastReceiver mReceiver =
new BroadcastReceiver() {
@Override
@@ -80,6 +82,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
mSwitchBar.setChecked(isBroadcasting());
mSwitchBar.setEnabled(adapterState == BluetoothAdapter.STATE_ON);
mListener.onSwitchBarChanged();
}
};
@@ -346,15 +349,19 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
}
private void updateSwitch() {
ThreadUtils.postOnMainThread(
() -> {
boolean isBroadcasting = isBroadcasting();
if (mSwitchBar.isChecked() != isBroadcasting) {
mSwitchBar.setChecked(isBroadcasting);
}
mSwitchBar.setEnabled(true);
mListener.onSwitchBarChanged(isBroadcasting);
});
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
boolean isBroadcasting = isBroadcasting();
ThreadUtils.postOnMainThread(
() -> {
if (mSwitchBar.isChecked() != isBroadcasting) {
mSwitchBar.setChecked(isBroadcasting);
}
mSwitchBar.setEnabled(true);
mListener.onSwitchBarChanged();
});
});
}
private boolean isBroadcasting() {
@@ -376,7 +383,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
TAG,
"Add broadcast with broadcastId: "
+ broadcastMetadata.getBroadcastId()
+ "to the device: "
+ " to the device: "
+ sink.getAnonymizedAddress());
mAssistant.addSource(sink, broadcastMetadata, /* isGroupOp= */ false);
}