Bluetooth: Only show devices when their names are resolved

* Add a developer menu option to allow name-less devices to be shown
  when a Bluetooth developer needs it, but hide it for non-developer
  users.
* Set BluetoothDevicePreference to invisible when CachedBluetoothDevice
  does not have a name besides MAC address and the above developer option
  is false.
* This affects BluetoothPairingDetail and DevicePickerFragment, but does
  not affect BluetoothSettings. BluetoothSettings will show all paired
  devices regardless whether an user friendly name exists.

Bug: 34685932
Test: pair Bluetooth device, send file over Bluetooth, unit tests
Change-Id: Idd7ad4b1671dfdcf3204efb50eddb6dae1065aa5
This commit is contained in:
Jack He
2017-05-31 18:37:28 -07:00
parent 77bd8c3a73
commit 19ba320263
6 changed files with 102 additions and 5 deletions

View File

@@ -54,16 +54,17 @@ public final class BluetoothDevicePreference extends GearPreference implements
private final UserManager mUserManager;
private AlertDialog mDisconnectDialog;
private String contentDescription = null;
private DeviceListPreferenceFragment mDeviceListPreferenceFragment;
/* Talk-back descriptions for various BT icons */
Resources mResources;
public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice) {
public BluetoothDevicePreference(Context context, CachedBluetoothDevice cachedDevice,
DeviceListPreferenceFragment deviceListPreferenceFragment) {
super(context, null);
mResources = getContext().getResources();
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mDeviceListPreferenceFragment = deviceListPreferenceFragment;
if (sDimAlpha == Integer.MIN_VALUE) {
TypedValue outValue = new TypedValue();
@@ -131,6 +132,11 @@ public final class BluetoothDevicePreference extends GearPreference implements
// Used to gray out the item
setEnabled(!mCachedDevice.isBusy());
// Device is only visible in the UI if it has a valid name besides MAC address or when user
// allows showing devices without user-friendly name in developer settings
setVisible(mDeviceListPreferenceFragment.shouldShowDevicesWithoutNames()
|| mCachedDevice.hasHumanReadableName());
// This could affect ordering, so notify that
notifyHierarchyChanged();
}

View File

@@ -140,6 +140,8 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
mBluetoothEnabler.resume(getActivity());
}
super.onStart();
// Always show paired devices regardless whether user-friendly name exists
mShowDevicesWithoutNames = true;
if (isUiRestricted()) {
getPreferenceScreen().removeAll();
if (!isUiRestrictedByOnlyAdmin()) {

View File

@@ -19,6 +19,7 @@ package com.android.settings.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.os.SystemProperties;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
@@ -52,6 +53,10 @@ public abstract class DeviceListPreferenceFragment extends
private static final String KEY_BT_SCAN = "bt_scan";
// Copied from DevelopmentSettings.java
private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY =
"persist.bluetooth.showdeviceswithoutnames";
private BluetoothDeviceFilter.Filter mFilter;
@VisibleForTesting
@@ -68,6 +73,8 @@ public abstract class DeviceListPreferenceFragment extends
final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();
boolean mShowDevicesWithoutNames;
DeviceListPreferenceFragment(String restrictedKey) {
super(restrictedKey);
mFilter = BluetoothDeviceFilter.ALL_FILTER;
@@ -103,6 +110,8 @@ public abstract class DeviceListPreferenceFragment extends
@Override
public void onStart() {
super.onStart();
mShowDevicesWithoutNames = SystemProperties.getBoolean(
BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, false);
if (mLocalManager == null || isUiRestricted()) return;
mLocalManager.setForegroundActivity(getActivity());
@@ -181,7 +190,7 @@ public abstract class DeviceListPreferenceFragment extends
BluetoothDevicePreference preference = (BluetoothDevicePreference) getCachedPreference(key);
if (preference == null) {
preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice);
preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice, this);
preference.setKey(key);
mDeviceListGroup.addPreference(preference);
} else {
@@ -271,4 +280,8 @@ public abstract class DeviceListPreferenceFragment extends
* Return the key of the {@link PreferenceGroup} that contains the bluetooth devices
*/
public abstract String getDeviceListKey();
public boolean shouldShowDevicesWithoutNames() {
return mShowDevicesWithoutNames;
}
}