diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java index 466d02bd3bcf0..f21a09b77d240 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java @@ -288,7 +288,6 @@ public class BluetoothEventManager { } cachedDevice.setRssi(rssi); cachedDevice.setBtClass(btClass); - cachedDevice.setNewName(name); cachedDevice.setJustDiscovered(true); } } diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java index 5ecbe80b96457..aa7d82ac1d6ce 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java @@ -51,8 +51,6 @@ public class CachedBluetoothDevice implements Comparable private final BluetoothAdapter mLocalAdapter; private final LocalBluetoothProfileManager mProfileManager; private final BluetoothDevice mDevice; - //TODO: consider remove, BluetoothDevice.getName() is already cached - private String mName; private long mHiSyncId; // Need this since there is no method for getting RSSI private short mRssi; @@ -299,7 +297,7 @@ public class CachedBluetoothDevice implements Comparable } return; } - Log.i(TAG, "Failed to connect " + profile.toString() + " to " + mName); + Log.i(TAG, "Failed to connect " + profile.toString() + " to " + getName()); } private boolean ensurePaired() { @@ -376,7 +374,6 @@ public class CachedBluetoothDevice implements Comparable // TODO: do any of these need to run async on a background thread? private void fillData() { - fetchName(); fetchBtClass(); updateProfiles(); fetchActiveDevices(); @@ -400,21 +397,15 @@ public class CachedBluetoothDevice implements Comparable return mDevice.getAddress(); } - public String getName() { - return mName; - } - /** - * Populate name from BluetoothDevice.ACTION_FOUND intent + * Get name from remote device + * @return {@link BluetoothDevice#getAliasName()} if + * {@link BluetoothDevice#getAliasName()} is not null otherwise return + * {@link BluetoothDevice#getAddress()} */ - void setNewName(String name) { - if (mName == null) { - mName = name; - if (mName == null || TextUtils.isEmpty(mName)) { - mName = mDevice.getAddress(); - } - dispatchAttributesChanged(); - } + public String getName() { + final String aliasName = mDevice.getAliasName(); + return TextUtils.isEmpty(aliasName) ? getAddress() : aliasName; } /** @@ -422,9 +413,8 @@ public class CachedBluetoothDevice implements Comparable * @param name new alias name to be set, should never be null */ public void setName(String name) { - // Prevent mName to be set to null if setName(null) is called - if (name != null && !TextUtils.equals(name, mName)) { - mName = name; + // Prevent getName() to be set to null if setName(null) is called + if (name != null && !TextUtils.equals(name, getName())) { mDevice.setAlias(name); dispatchAttributesChanged(); } @@ -461,19 +451,10 @@ public class CachedBluetoothDevice implements Comparable } void refreshName() { - fetchName(); - dispatchAttributesChanged(); - } - - private void fetchName() { - mName = mDevice.getAliasName(); - - if (TextUtils.isEmpty(mName)) { - mName = mDevice.getAddress(); - if (BluetoothUtils.D) { - Log.d(TAG, "Device has no name (yet), use address: " + mName); - } + if (BluetoothUtils.D) { + Log.d(TAG, "Device name: " + getName()); } + dispatchAttributesChanged(); } /** @@ -805,7 +786,7 @@ public class CachedBluetoothDevice implements Comparable if (comparison != 0) return comparison; // Fallback on name - return mName.compareTo(another.mName); + return getName().compareTo(another.getName()); } public interface Callback { diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java index 034574faf6db5..95ffd1d9983e3 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java @@ -606,4 +606,35 @@ public class CachedBluetoothDeviceTest { assertThat(mCachedDevice.isConnectedHearingAidDevice()).isFalse(); } + + @Test + public void getName_aliasNameNotNull_returnAliasName() { + when(mDevice.getAliasName()).thenReturn(DEVICE_NAME); + + assertThat(mCachedDevice.getName()).isEqualTo(DEVICE_NAME); + } + + @Test + public void getName_aliasNameIsNull_returnAddress() { + when(mDevice.getAliasName()).thenReturn(null); + + assertThat(mCachedDevice.getName()).isEqualTo(DEVICE_ADDRESS); + } + + @Test + public void setName_setDeviceNameIsNotNull() { + final String name = "test name"; + when(mDevice.getAliasName()).thenReturn(DEVICE_NAME); + + mCachedDevice.setName(name); + + verify(mDevice).setAlias(name); + } + + @Test + public void setName_setDeviceNameIsNull() { + mCachedDevice.setName(null); + + verify(mDevice, never()).setAlias(any()); + } }