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

Commit 9d30bf15 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove the local cached name"

parents d821df2b cb6e197e
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -286,7 +286,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());
    }
}