Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cb6e197e authored by hughchen's avatar hughchen
Browse files

Remove the local cached name

1. Use BluetoothDevice.getAliasName() to get the cached name instead of
   use mName that is local cached.
2. Add test to verify following situations:
   1. Verify when alias name is not null return alias name otherwise return
   address.
   2. Verify the BluetoothDevice.setAlias() will be called when set
   name is not null.

Bug: 112517004
Test: make -j42 RunSettingsLibRoboTests ROBOTEST_FILTER=CachedBluetoothDeviceTest
Change-Id: Ib4f002da6ceac8e8e562bf17a167a0c9ae6a3953
parent 7dd9fce6
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -288,7 +288,6 @@ public class BluetoothEventManager {
            }
            cachedDevice.setRssi(rssi);
            cachedDevice.setBtClass(btClass);
            cachedDevice.setNewName(name);
            cachedDevice.setJustDiscovered(true);
        }
    }
+14 −33
Original line number Diff line number Diff line
@@ -51,8 +51,6 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
    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<CachedBluetoothDevice>
            }
            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<CachedBluetoothDevice>

    // 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<CachedBluetoothDevice>
        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<CachedBluetoothDevice>
     * @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<CachedBluetoothDevice>
    }

    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);
            }
            Log.d(TAG, "Device name: " + getName());
        }
        dispatchAttributesChanged();
    }

    /**
@@ -805,7 +786,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        if (comparison != 0) return comparison;

        // Fallback on name
        return mName.compareTo(another.mName);
        return getName().compareTo(another.getName());
    }

    public interface Callback {
+31 −0
Original line number Diff line number Diff line
@@ -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());
    }
}