Merge "Refactor methods to access/modify BluetoothDevice alias and name"

am: 67bef6ab92

Change-Id: Id0a50d482a7ff33df5fcd990a3ce5273956fcc5c
This commit is contained in:
Rahul Sabnis
2019-11-15 15:40:17 -08:00
committed by android-build-merger
10 changed files with 35 additions and 47 deletions

View File

@@ -8357,6 +8357,7 @@ package android.bluetooth {
method public int describeContents(); method public int describeContents();
method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public boolean fetchUuidsWithSdp(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public boolean fetchUuidsWithSdp();
method public String getAddress(); method public String getAddress();
method @Nullable @RequiresPermission(android.Manifest.permission.BLUETOOTH) public String getAlias();
method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public android.bluetooth.BluetoothClass getBluetoothClass(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public android.bluetooth.BluetoothClass getBluetoothClass();
method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public int getBondState(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public int getBondState();
method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public String getName(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public String getName();
@@ -8368,6 +8369,7 @@ package android.bluetooth {
field public static final String ACTION_ACL_CONNECTED = "android.bluetooth.device.action.ACL_CONNECTED"; field public static final String ACTION_ACL_CONNECTED = "android.bluetooth.device.action.ACL_CONNECTED";
field public static final String ACTION_ACL_DISCONNECTED = "android.bluetooth.device.action.ACL_DISCONNECTED"; field public static final String ACTION_ACL_DISCONNECTED = "android.bluetooth.device.action.ACL_DISCONNECTED";
field public static final String ACTION_ACL_DISCONNECT_REQUESTED = "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED"; field public static final String ACTION_ACL_DISCONNECT_REQUESTED = "android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED";
field public static final String ACTION_ALIAS_CHANGED = "android.bluetooth.action.ALIAS_CHANGED";
field public static final String ACTION_BOND_STATE_CHANGED = "android.bluetooth.device.action.BOND_STATE_CHANGED"; field public static final String ACTION_BOND_STATE_CHANGED = "android.bluetooth.device.action.BOND_STATE_CHANGED";
field public static final String ACTION_CLASS_CHANGED = "android.bluetooth.device.action.CLASS_CHANGED"; field public static final String ACTION_CLASS_CHANGED = "android.bluetooth.device.action.CLASS_CHANGED";
field public static final String ACTION_FOUND = "android.bluetooth.device.action.FOUND"; field public static final String ACTION_FOUND = "android.bluetooth.device.action.FOUND";

View File

@@ -1264,6 +1264,7 @@ package android.bluetooth {
method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public boolean isEncrypted(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public boolean isEncrypted();
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean isInSilenceMode(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean isInSilenceMode();
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) public boolean removeBond(); method @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) public boolean removeBond();
method @RequiresPermission(android.Manifest.permission.BLUETOOTH) public boolean setAlias(@NonNull String);
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setMetadata(int, @NonNull byte[]); method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setMetadata(int, @NonNull byte[]);
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setPhonebookAccessPermission(int); method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setPhonebookAccessPermission(int);
method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setSilenceMode(boolean); method @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setSilenceMode(boolean);

View File

@@ -173,13 +173,10 @@ public final class BluetoothDevice implements Parcelable {
* changed. * changed.
* <p>Always contains the extra field {@link #EXTRA_DEVICE}. * <p>Always contains the extra field {@link #EXTRA_DEVICE}.
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive. * <p>Requires {@link android.Manifest.permission#BLUETOOTH} to receive.
*
* @hide
*/ */
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
@UnsupportedAppUsage
public static final String ACTION_ALIAS_CHANGED = public static final String ACTION_ALIAS_CHANGED =
"android.bluetooth.device.action.ALIAS_CHANGED"; "android.bluetooth.action.ALIAS_CHANGED";
/** /**
* Broadcast Action: Indicates a change in the bond state of a remote * Broadcast Action: Indicates a change in the bond state of a remote
@@ -1048,10 +1045,11 @@ public final class BluetoothDevice implements Parcelable {
* Get the Bluetooth alias of the remote device. * Get the Bluetooth alias of the remote device.
* <p>Alias is the locally modified name of a remote device. * <p>Alias is the locally modified name of a remote device.
* *
* @return the Bluetooth alias, or null if no alias or there was a problem * @return the Bluetooth alias, the friendly device name if no alias, or
* @hide * null if there was a problem
*/ */
@UnsupportedAppUsage(publicAlternatives = "Use {@link #getName()} instead.") @Nullable
@RequiresPermission(Manifest.permission.BLUETOOTH)
public String getAlias() { public String getAlias() {
final IBluetooth service = sService; final IBluetooth service = sService;
if (service == null) { if (service == null) {
@@ -1059,7 +1057,11 @@ public final class BluetoothDevice implements Parcelable {
return null; return null;
} }
try { try {
return service.getRemoteAlias(this); String alias = service.getRemoteAlias(this);
if (alias == null) {
return getName();
}
return alias;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1076,8 +1078,9 @@ public final class BluetoothDevice implements Parcelable {
* @return true on success, false on error * @return true on success, false on error
* @hide * @hide
*/ */
@UnsupportedAppUsage @SystemApi
public boolean setAlias(String alias) { @RequiresPermission(Manifest.permission.BLUETOOTH)
public boolean setAlias(@NonNull String alias) {
final IBluetooth service = sService; final IBluetooth service = sService;
if (service == null) { if (service == null) {
Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); Log.e(TAG, "BT not enabled. Cannot set Remote Device name");
@@ -1091,24 +1094,6 @@ public final class BluetoothDevice implements Parcelable {
return false; return false;
} }
/**
* Get the Bluetooth alias of the remote device.
* If Alias is null, get the Bluetooth name instead.
*
* @return the Bluetooth alias, or null if no alias or there was a problem
* @hide
* @see #getAlias()
* @see #getName()
*/
@UnsupportedAppUsage(publicAlternatives = "Use {@link #getName()} instead.")
public String getAliasName() {
String name = getAlias();
if (name == null) {
name = getName();
}
return name;
}
/** /**
* Get the most recent identified battery level of this Bluetooth device * Get the most recent identified battery level of this Bluetooth device
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} * <p>Requires {@link android.Manifest.permission#BLUETOOTH}

View File

@@ -129,7 +129,7 @@ public class BluetoothDeviceFilterUtils {
@UnsupportedAppUsage @UnsupportedAppUsage
public static String getDeviceDisplayNameInternal(@NonNull BluetoothDevice device) { public static String getDeviceDisplayNameInternal(@NonNull BluetoothDevice device) {
return firstNotEmpty(device.getAliasName(), device.getAddress()); return firstNotEmpty(device.getAlias(), device.getAddress());
} }
@UnsupportedAppUsage @UnsupportedAppUsage

View File

@@ -142,7 +142,7 @@
<protected-broadcast android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" /> <protected-broadcast android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
<protected-broadcast android:name="android.bluetooth.device.action.UUID" /> <protected-broadcast android:name="android.bluetooth.device.action.UUID" />
<protected-broadcast android:name="android.bluetooth.device.action.MAS_INSTANCE" /> <protected-broadcast android:name="android.bluetooth.device.action.MAS_INSTANCE" />
<protected-broadcast android:name="android.bluetooth.device.action.ALIAS_CHANGED" /> <protected-broadcast android:name="android.bluetooth.action.ALIAS_CHANGED" />
<protected-broadcast android:name="android.bluetooth.device.action.FOUND" /> <protected-broadcast android:name="android.bluetooth.device.action.FOUND" />
<protected-broadcast android:name="android.bluetooth.device.action.CLASS_CHANGED" /> <protected-broadcast android:name="android.bluetooth.device.action.CLASS_CHANGED" />
<protected-broadcast android:name="android.bluetooth.device.action.ACL_CONNECTED" /> <protected-broadcast android:name="android.bluetooth.device.action.ACL_CONNECTED" />

View File

@@ -364,12 +364,12 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
/** /**
* Get name from remote device * Get name from remote device
* @return {@link BluetoothDevice#getAliasName()} if * @return {@link BluetoothDevice#getAlias()} if
* {@link BluetoothDevice#getAliasName()} is not null otherwise return * {@link BluetoothDevice#getAlias()} is not null otherwise return
* {@link BluetoothDevice#getAddress()} * {@link BluetoothDevice#getAddress()}
*/ */
public String getName() { public String getName() {
final String aliasName = mDevice.getAliasName(); final String aliasName = mDevice.getAlias();
return TextUtils.isEmpty(aliasName) ? getAddress() : aliasName; return TextUtils.isEmpty(aliasName) ? getAddress() : aliasName;
} }
@@ -427,7 +427,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
* @return true if device's alias name is not null nor empty, false otherwise * @return true if device's alias name is not null nor empty, false otherwise
*/ */
public boolean hasHumanReadableName() { public boolean hasHumanReadableName() {
return !TextUtils.isEmpty(mDevice.getAliasName()); return !TextUtils.isEmpty(mDevice.getAlias());
} }
/** /**
@@ -574,7 +574,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
} }
if (BluetoothUtils.D) { if (BluetoothUtils.D) {
Log.e(TAG, "updating profiles for " + mDevice.getAliasName() + ", " + mDevice); Log.e(TAG, "updating profiles for " + mDevice.getAlias() + ", " + mDevice);
BluetoothClass bluetoothClass = mDevice.getBluetoothClass(); BluetoothClass bluetoothClass = mDevice.getBluetoothClass();
if (bluetoothClass != null) Log.v(TAG, "Class: " + bluetoothClass.toString()); if (bluetoothClass != null) Log.v(TAG, "Class: " + bluetoothClass.toString());

View File

@@ -166,7 +166,7 @@ public class CachedBluetoothDeviceManager {
return cachedDevice.getName(); return cachedDevice.getName();
} }
String name = device.getAliasName(); String name = device.getAlias();
if (name != null) { if (name != null) {
return name; return name;
} }

View File

@@ -95,9 +95,9 @@ public class CachedBluetoothDeviceManagerTest {
when(mDevice1.getName()).thenReturn(DEVICE_NAME_1); when(mDevice1.getName()).thenReturn(DEVICE_NAME_1);
when(mDevice2.getName()).thenReturn(DEVICE_NAME_2); when(mDevice2.getName()).thenReturn(DEVICE_NAME_2);
when(mDevice3.getName()).thenReturn(DEVICE_NAME_3); when(mDevice3.getName()).thenReturn(DEVICE_NAME_3);
when(mDevice1.getAliasName()).thenReturn(DEVICE_ALIAS_1); when(mDevice1.getAlias()).thenReturn(DEVICE_ALIAS_1);
when(mDevice2.getAliasName()).thenReturn(DEVICE_ALIAS_2); when(mDevice2.getAlias()).thenReturn(DEVICE_ALIAS_2);
when(mDevice3.getAliasName()).thenReturn(DEVICE_ALIAS_3); when(mDevice3.getAlias()).thenReturn(DEVICE_ALIAS_3);
when(mDevice1.getBluetoothClass()).thenReturn(DEVICE_CLASS_1); when(mDevice1.getBluetoothClass()).thenReturn(DEVICE_CLASS_1);
when(mDevice2.getBluetoothClass()).thenReturn(DEVICE_CLASS_2); when(mDevice2.getBluetoothClass()).thenReturn(DEVICE_CLASS_2);
when(mDevice3.getBluetoothClass()).thenReturn(DEVICE_CLASS_2); when(mDevice3.getBluetoothClass()).thenReturn(DEVICE_CLASS_2);
@@ -224,7 +224,7 @@ public class CachedBluetoothDeviceManagerTest {
assertThat(cachedDevice1.getName()).isEqualTo(DEVICE_ALIAS_1); assertThat(cachedDevice1.getName()).isEqualTo(DEVICE_ALIAS_1);
final String newAliasName = "NewAliasName"; final String newAliasName = "NewAliasName";
when(mDevice1.getAliasName()).thenReturn(newAliasName); when(mDevice1.getAlias()).thenReturn(newAliasName);
mCachedDeviceManager.onDeviceNameUpdated(mDevice1); mCachedDeviceManager.onDeviceNameUpdated(mDevice1);
assertThat(cachedDevice1.getName()).isEqualTo(newAliasName); assertThat(cachedDevice1.getName()).isEqualTo(newAliasName);
} }

View File

@@ -699,7 +699,7 @@ public class CachedBluetoothDeviceTest {
@Test @Test
public void deviceName_testAliasNameAvailable() { public void deviceName_testAliasNameAvailable() {
when(mDevice.getAliasName()).thenReturn(DEVICE_ALIAS); when(mDevice.getAlias()).thenReturn(DEVICE_ALIAS);
when(mDevice.getName()).thenReturn(DEVICE_NAME); when(mDevice.getName()).thenReturn(DEVICE_NAME);
CachedBluetoothDevice cachedBluetoothDevice = CachedBluetoothDevice cachedBluetoothDevice =
new CachedBluetoothDevice(mContext, mProfileManager, mDevice); new CachedBluetoothDevice(mContext, mProfileManager, mDevice);
@@ -722,7 +722,7 @@ public class CachedBluetoothDeviceTest {
@Test @Test
public void deviceName_testRenameDevice() { public void deviceName_testRenameDevice() {
final String[] alias = {DEVICE_ALIAS}; final String[] alias = {DEVICE_ALIAS};
doAnswer(invocation -> alias[0]).when(mDevice).getAliasName(); doAnswer(invocation -> alias[0]).when(mDevice).getAlias();
doAnswer(invocation -> { doAnswer(invocation -> {
alias[0] = (String) invocation.getArguments()[0]; alias[0] = (String) invocation.getArguments()[0];
return true; return true;
@@ -839,14 +839,14 @@ public class CachedBluetoothDeviceTest {
@Test @Test
public void getName_aliasNameNotNull_returnAliasName() { public void getName_aliasNameNotNull_returnAliasName() {
when(mDevice.getAliasName()).thenReturn(DEVICE_NAME); when(mDevice.getAlias()).thenReturn(DEVICE_NAME);
assertThat(mCachedDevice.getName()).isEqualTo(DEVICE_NAME); assertThat(mCachedDevice.getName()).isEqualTo(DEVICE_NAME);
} }
@Test @Test
public void getName_aliasNameIsNull_returnAddress() { public void getName_aliasNameIsNull_returnAddress() {
when(mDevice.getAliasName()).thenReturn(null); when(mDevice.getAlias()).thenReturn(null);
assertThat(mCachedDevice.getName()).isEqualTo(DEVICE_ADDRESS); assertThat(mCachedDevice.getName()).isEqualTo(DEVICE_ADDRESS);
} }
@@ -854,7 +854,7 @@ public class CachedBluetoothDeviceTest {
@Test @Test
public void setName_setDeviceNameIsNotNull() { public void setName_setDeviceNameIsNotNull() {
final String name = "test name"; final String name = "test name";
when(mDevice.getAliasName()).thenReturn(DEVICE_NAME); when(mDevice.getAlias()).thenReturn(DEVICE_NAME);
mCachedDevice.setName(name); mCachedDevice.setName(name);

View File

@@ -75,8 +75,8 @@ public class HearingAidDeviceManagerTest {
when(mDevice2.getAddress()).thenReturn(DEVICE_ADDRESS_2); when(mDevice2.getAddress()).thenReturn(DEVICE_ADDRESS_2);
when(mDevice1.getName()).thenReturn(DEVICE_NAME_1); when(mDevice1.getName()).thenReturn(DEVICE_NAME_1);
when(mDevice2.getName()).thenReturn(DEVICE_NAME_2); when(mDevice2.getName()).thenReturn(DEVICE_NAME_2);
when(mDevice1.getAliasName()).thenReturn(DEVICE_ALIAS_1); when(mDevice1.getAlias()).thenReturn(DEVICE_ALIAS_1);
when(mDevice2.getAliasName()).thenReturn(DEVICE_ALIAS_2); when(mDevice2.getAlias()).thenReturn(DEVICE_ALIAS_2);
when(mDevice1.getBluetoothClass()).thenReturn(DEVICE_CLASS); when(mDevice1.getBluetoothClass()).thenReturn(DEVICE_CLASS);
when(mDevice2.getBluetoothClass()).thenReturn(DEVICE_CLASS); when(mDevice2.getBluetoothClass()).thenReturn(DEVICE_CLASS);
when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager); when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);