Remove all associaitons when forget a device in Settings
The associations will be removed one forget the device. Test: manually test Bug: 365613753 Flag: com.android.settings.flags.enable_remove_association_bt_unpair Change-Id: Ic2224952b6f8e776ffcf07ce4fa6953a98475490
This commit is contained in:
@@ -16,31 +16,52 @@
|
||||
|
||||
package com.android.settings.bluetooth;
|
||||
|
||||
import static com.android.internal.util.CollectionUtils.filter;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.companion.AssociationInfo;
|
||||
import android.companion.CompanionDeviceManager;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.icu.text.ListFormatter;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||
import com.android.settings.flags.Flags;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/** Implements an AlertDialog for confirming that a user wishes to unpair or "forget" a paired
|
||||
* device*/
|
||||
public class ForgetDeviceDialogFragment extends InstrumentedDialogFragment {
|
||||
public static final String TAG = "ForgetBluetoothDevice";
|
||||
private static final String KEY_DEVICE_ADDRESS = "device_address";
|
||||
|
||||
private CachedBluetoothDevice mDevice;
|
||||
|
||||
@VisibleForTesting
|
||||
CachedBluetoothDevice mDevice;
|
||||
@VisibleForTesting
|
||||
CompanionDeviceManager mCompanionDeviceManager;
|
||||
@VisibleForTesting
|
||||
PackageManager mPackageManager;
|
||||
public static ForgetDeviceDialogFragment newInstance(String deviceAddress) {
|
||||
Bundle args = new Bundle(1);
|
||||
args.putString(KEY_DEVICE_ADDRESS, deviceAddress);
|
||||
@@ -63,29 +84,93 @@ public class ForgetDeviceDialogFragment extends InstrumentedDialogFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle inState) {
|
||||
Context context = getContext();
|
||||
public void onAttach(@NonNull Context context) {
|
||||
super.onAttach(context);
|
||||
mCompanionDeviceManager = context.getSystemService(CompanionDeviceManager.class);
|
||||
mPackageManager = context.getPackageManager();
|
||||
mDevice = getDevice(context);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(@Nullable Bundle inState) {
|
||||
if (mDevice == null) {
|
||||
Log.e(TAG, "onCreateDialog: Device is null.");
|
||||
return null;
|
||||
throw new IllegalStateException("Device must not be null when creating dialog.");
|
||||
}
|
||||
List<AssociationInfo> associationInfos = getAssociations(mDevice.getAddress());
|
||||
Set<String> packageNames = new HashSet<>();
|
||||
if (Flags.enableRemoveAssociationBtUnpair()) {
|
||||
for (AssociationInfo ai : associationInfos) {
|
||||
CharSequence appLabel = getAppLabel(ai.getPackageName());
|
||||
if (!TextUtils.isEmpty(appLabel)) {
|
||||
packageNames.add(appLabel.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DialogInterface.OnClickListener onConfirm = (dialog, which) -> {
|
||||
// 1. Unpair the device.
|
||||
mDevice.unpair();
|
||||
// 2. Remove the associations if any.
|
||||
if (Flags.enableRemoveAssociationBtUnpair()) {
|
||||
for (AssociationInfo ai : associationInfos) {
|
||||
mCompanionDeviceManager.disassociate(ai.getId());
|
||||
}
|
||||
}
|
||||
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
activity.finish();
|
||||
}
|
||||
};
|
||||
AlertDialog dialog = new AlertDialog.Builder(context)
|
||||
|
||||
AlertDialog dialog = new AlertDialog.Builder(getActivity())
|
||||
.setPositiveButton(R.string.bluetooth_unpair_dialog_forget_confirm_button,
|
||||
onConfirm)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.create();
|
||||
|
||||
dialog.setTitle(R.string.bluetooth_unpair_dialog_title);
|
||||
dialog.setMessage(context.getString(R.string.bluetooth_unpair_dialog_body,
|
||||
mDevice.getName()));
|
||||
String message = buildUnpairMessage(
|
||||
getActivity(), mDevice, associationInfos, packageNames.stream().toList());
|
||||
dialog.setMessage(message);
|
||||
|
||||
return dialog;
|
||||
}
|
||||
|
||||
private List<AssociationInfo> getAssociations(String address) {
|
||||
return filter(
|
||||
mCompanionDeviceManager.getAllAssociations(),
|
||||
a -> Objects.equal(address, a.getDeviceMacAddressAsString()));
|
||||
}
|
||||
|
||||
private String buildUnpairMessage(Context context, CachedBluetoothDevice device,
|
||||
List<AssociationInfo> associationInfos, List<String> packageNames) {
|
||||
if (Flags.enableRemoveAssociationBtUnpair() && !associationInfos.isEmpty()) {
|
||||
String appNamesString = getAppNamesString(packageNames.stream().toList());
|
||||
return context.getString(R.string.bluetooth_unpair_dialog_with_associations_body,
|
||||
device.getName(), appNamesString);
|
||||
} else {
|
||||
return context.getString(R.string.bluetooth_unpair_dialog_body, device.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private String getAppNamesString(List<String> appNames) {
|
||||
if (appNames == null || appNames.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
ListFormatter formatter = ListFormatter.getInstance(Locale.getDefault());
|
||||
return formatter.format(appNames);
|
||||
}
|
||||
|
||||
private CharSequence getAppLabel(String packageName) {
|
||||
try {
|
||||
return mPackageManager.getApplicationLabel(
|
||||
mPackageManager.getApplicationInfo(packageName, 0));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(TAG, "Package Not Found", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user