Merge "Bluetooth: Add flag for human readable name" into oc-mr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
34eac2c59b
@@ -50,8 +50,11 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
|
|||||||
private final LocalBluetoothAdapter mLocalAdapter;
|
private final LocalBluetoothAdapter mLocalAdapter;
|
||||||
private final LocalBluetoothProfileManager mProfileManager;
|
private final LocalBluetoothProfileManager mProfileManager;
|
||||||
private final BluetoothDevice mDevice;
|
private final BluetoothDevice mDevice;
|
||||||
|
//TODO: consider remove, BluetoothDevice.getName() is already cached
|
||||||
private String mName;
|
private String mName;
|
||||||
|
// Need this since there is no method for getting RSSI
|
||||||
private short mRssi;
|
private short mRssi;
|
||||||
|
//TODO: consider remove, BluetoothDevice.getBluetoothClass() is already cached
|
||||||
private BluetoothClass mBtClass;
|
private BluetoothClass mBtClass;
|
||||||
private HashMap<LocalBluetoothProfile, Integer> mProfileConnectionState;
|
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) {
|
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;
|
mName = name;
|
||||||
mDevice.setAlias(name);
|
mDevice.setAlias(name);
|
||||||
dispatchAttributesChanged();
|
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
|
* Get battery level from remote device
|
||||||
* @return battery level in percentage [0-100], or {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN}
|
* @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();
|
ParcelUuid[] localUuids = mLocalAdapter.getUuids();
|
||||||
if (localUuids == null) return false;
|
if (localUuids == null) return false;
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Now we know if the device supports PBAP, update permissions...
|
* Now we know if the device supports PBAP, update permissions...
|
||||||
*/
|
*/
|
||||||
processPhonebookAccess();
|
processPhonebookAccess();
|
||||||
|
|||||||
@@ -17,8 +17,12 @@ package com.android.settingslib.bluetooth;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
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.doAnswer;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
@@ -42,6 +46,10 @@ import org.robolectric.annotation.Config;
|
|||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, resourceDir =
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, resourceDir =
|
||||||
"../../res")
|
"../../res")
|
||||||
public class CachedBluetoothDeviceTest {
|
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
|
@Mock
|
||||||
private LocalBluetoothAdapter mAdapter;
|
private LocalBluetoothAdapter mAdapter;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -62,6 +70,7 @@ public class CachedBluetoothDeviceTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
|
when(mDevice.getAddress()).thenReturn(DEVICE_ADDRESS);
|
||||||
when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
||||||
when(mHfpProfile.isProfileReady()).thenReturn(true);
|
when(mHfpProfile.isProfileReady()).thenReturn(true);
|
||||||
when(mA2dpProfile.isProfileReady()).thenReturn(true);
|
when(mA2dpProfile.isProfileReady()).thenReturn(true);
|
||||||
@@ -152,4 +161,49 @@ public class CachedBluetoothDeviceTest {
|
|||||||
mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED);
|
mCachedDevice.onProfileStateChanged(mHidProfile, BluetoothProfile.STATE_DISCONNECTED);
|
||||||
assertThat(mCachedDevice.getConnectionSummary()).isNull();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user