Bluetooth: Add flag for human readable name
* Add hasHumanReadableName() method to CachedBluetoothDevice that returns True when BluetoothDevice.getAliasName() is not null nor empty Bug: 34685932 Test: pair Bluetooth device, send file over Bluetooth, unit tests Change-Id: I197f3f7f3bcb7d785729473b25638006449ae862
This commit is contained in:
parent
cb065bc33e
commit
c219bc9b88
@ -50,8 +50,11 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
||||
private final LocalBluetoothAdapter mLocalAdapter;
|
||||
private final LocalBluetoothProfileManager mProfileManager;
|
||||
private final BluetoothDevice mDevice;
|
||||
//TODO: consider remove, BluetoothDevice.getName() is already cached
|
||||
private String mName;
|
||||
// Need this since there is no method for getting RSSI
|
||||
private short mRssi;
|
||||
//TODO: consider remove, BluetoothDevice.getBluetoothClass() is already cached
|
||||
private BluetoothClass mBtClass;
|
||||
private HashMap<LocalBluetoothProfile, Integer> mProfileConnectionState;
|
||||
|
||||
@ -394,10 +397,12 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
||||
}
|
||||
|
||||
/**
|
||||
* user changes the device name
|
||||
* User changes the device name
|
||||
* @param name new alias name to be set, should never be null
|
||||
*/
|
||||
public void setName(String name) {
|
||||
if (!mName.equals(name)) {
|
||||
// Prevent mName to be set to null if setName(null) is called
|
||||
if (name != null && !TextUtils.equals(name, mName)) {
|
||||
mName = name;
|
||||
mDevice.setAlias(name);
|
||||
dispatchAttributesChanged();
|
||||
@ -418,6 +423,14 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if device has a human readable name besides MAC address
|
||||
* @return true if device's alias name is not null nor empty, false otherwise
|
||||
*/
|
||||
public boolean hasHumanReadableName() {
|
||||
return !TextUtils.isEmpty(mDevice.getAliasName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get battery level from remote device
|
||||
* @return battery level in percentage [0-100], or {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN}
|
||||
@ -495,7 +508,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
||||
ParcelUuid[] localUuids = mLocalAdapter.getUuids();
|
||||
if (localUuids == null) return false;
|
||||
|
||||
/**
|
||||
/*
|
||||
* Now we know if the device supports PBAP, update permissions...
|
||||
*/
|
||||
processPhonebookAccess();
|
||||
|
@ -17,8 +17,12 @@ package com.android.settingslib.bluetooth;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
@ -42,6 +46,10 @@ import org.robolectric.annotation.Config;
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, resourceDir =
|
||||
"../../res")
|
||||
public class CachedBluetoothDeviceTest {
|
||||
private final static String DEVICE_NAME = "TestName";
|
||||
private final static String DEVICE_ALIAS = "TestAlias";
|
||||
private final static String DEVICE_ADDRESS = "AA:BB:CC:DD:EE:FF";
|
||||
private final static String DEVICE_ALIAS_NEW = "TestAliasNew";
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mAdapter;
|
||||
@Mock
|
||||
@ -62,6 +70,7 @@ public class CachedBluetoothDeviceTest {
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
when(mDevice.getAddress()).thenReturn(DEVICE_ADDRESS);
|
||||
when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
||||
when(mHfpProfile.isProfileReady()).thenReturn(true);
|
||||
when(mA2dpProfile.isProfileReady()).thenReturn(true);
|
||||
@ -152,4 +161,49 @@ public class CachedBluetoothDeviceTest {
|
||||
mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED);
|
||||
assertThat(mCachedDevice.getConnectionSummary()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeviceName_testAliasNameAvailable() {
|
||||
when(mDevice.getAliasName()).thenReturn(DEVICE_ALIAS);
|
||||
when(mDevice.getName()).thenReturn(DEVICE_NAME);
|
||||
CachedBluetoothDevice cachedBluetoothDevice =
|
||||
new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice);
|
||||
// Verify alias is returned on getName
|
||||
assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS);
|
||||
// Verify device is visible
|
||||
assertThat(cachedBluetoothDevice.hasHumanReadableName()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeviceName_testNameNotAvailable() {
|
||||
CachedBluetoothDevice cachedBluetoothDevice =
|
||||
new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice);
|
||||
// Verify device address is returned on getName
|
||||
assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ADDRESS);
|
||||
// Verify device is not visible
|
||||
assertThat(cachedBluetoothDevice.hasHumanReadableName()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeviceName_testRenameDevice() {
|
||||
final String[] alias = {DEVICE_ALIAS};
|
||||
doAnswer(invocation -> alias[0]).when(mDevice).getAliasName();
|
||||
doAnswer(invocation -> {
|
||||
alias[0] = (String) invocation.getArguments()[0];
|
||||
return true;
|
||||
}).when(mDevice).setAlias(anyString());
|
||||
when(mDevice.getName()).thenReturn(DEVICE_NAME);
|
||||
CachedBluetoothDevice cachedBluetoothDevice =
|
||||
new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice);
|
||||
// Verify alias is returned on getName
|
||||
assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS);
|
||||
// Verify null name does not get set
|
||||
cachedBluetoothDevice.setName(null);
|
||||
verify(mDevice, never()).setAlias(any());
|
||||
// Verify new name is set properly
|
||||
cachedBluetoothDevice.setName(DEVICE_ALIAS_NEW);
|
||||
verify(mDevice).setAlias(DEVICE_ALIAS_NEW);
|
||||
// Verify new alias is returned on getName
|
||||
assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS_NEW);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user