Merge "[Audiosharing] Block pairing during audio sharing" into main

This commit is contained in:
Yiyi Shen
2025-02-10 21:10:43 -08:00
committed by Android (Google) Code Review
4 changed files with 126 additions and 6 deletions

View File

@@ -48,6 +48,7 @@ import com.android.settings.widget.GearPreference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.flags.Flags;
import com.android.settingslib.utils.ThreadUtils;
import java.lang.annotation.Retention;
@@ -93,6 +94,7 @@ public final class BluetoothDevicePreference extends GearPreference {
private final int mType;
private AlertDialog mDisconnectDialog;
@Nullable private AlertDialog mBlockPairingDialog;
private String contentDescription = null;
private boolean mHideSecondTarget = false;
private boolean mIsCallbackRemoved = true;
@@ -409,13 +411,24 @@ public final class BluetoothDevicePreference extends GearPreference {
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_CONNECT);
mCachedDevice.connect();
} else if (bondState == BluetoothDevice.BOND_NONE) {
metricsFeatureProvider.action(context,
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR);
if (!mCachedDevice.hasHumanReadableName()) {
var unused = ThreadUtils.postOnBackgroundThread(() -> {
if (Flags.enableTemporaryBondDevicesUi() && Utils.shouldBlockPairingInAudioSharing(
mLocalBtManager)) {
// TODO: collect metric
context.getMainExecutor().execute(() ->
mBlockPairingDialog =
Utils.showBlockPairingDialog(context, mBlockPairingDialog,
mLocalBtManager));
return;
}
metricsFeatureProvider.action(context,
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
}
pair();
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR);
if (!mCachedDevice.hasHumanReadableName()) {
metricsFeatureProvider.action(context,
SettingsEnums.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
}
context.getMainExecutor().execute(() -> pair());
});
}
}

View File

@@ -34,6 +34,7 @@ import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
@@ -44,6 +45,7 @@ import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
@@ -324,4 +326,33 @@ public final class Utils {
.map(d -> BluetoothUtils.getGroupId(deviceManager.findDevice(d))).collect(
Collectors.toSet()).size() >= 2);
}
/**
* Show block pairing dialog during audio sharing
* @param context The dialog context
* @param dialog The dialog if already exists
* @param localBtManager {@link LocalBluetoothManager}
* @return The block pairing dialog
*/
@Nullable
static AlertDialog showBlockPairingDialog(@NonNull Context context,
@Nullable AlertDialog dialog, @Nullable LocalBluetoothManager localBtManager) {
if (!com.android.settingslib.flags.Flags.enableTemporaryBondDevicesUi()) return null;
if (dialog != null && dialog.isShowing()) return dialog;
if (dialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setNegativeButton(android.R.string.cancel, null)
.setTitle(R.string.audio_sharing_block_pairing_dialog_title)
.setMessage(R.string.audio_sharing_block_pairing_dialog_content);
LocalBluetoothLeBroadcast broadcast = localBtManager == null ? null :
localBtManager.getProfileManager().getLeAudioBroadcastProfile();
if (broadcast != null) {
builder.setPositiveButton(R.string.audio_sharing_turn_off_button_label,
(dlg, which) -> broadcast.stopLatestBroadcast());
}
dialog = builder.create();
}
dialog.show();
return dialog;
}
}