Register to device name and alias changes and update accordingly

This commit is contained in:
cpfeiffer 2016-07-11 00:28:15 +02:00
parent 80930ce42a
commit 76895aa2b1
2 changed files with 35 additions and 3 deletions

View File

@ -27,6 +27,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
public class DeviceManager { public class DeviceManager {
private static final Logger LOG = LoggerFactory.getLogger(DeviceManager.class); private static final Logger LOG = LoggerFactory.getLogger(DeviceManager.class);
public static final String BLUETOOTH_DEVICE_ACTION_ALIAS_CHANGED = "android.bluetooth.device.action.ALIAS_CHANGED";
/** /**
* Intent action to notify that the list of devices has changed. * Intent action to notify that the list of devices has changed.
*/ */
@ -50,10 +51,16 @@ public class DeviceManager {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
switch (action) { switch (action) {
case ACTION_REFRESH_DEVICELIST: case ACTION_REFRESH_DEVICELIST: // fall through
case BluetoothDevice.ACTION_BOND_STATE_CHANGED: case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
refreshPairedDevices(); refreshPairedDevices();
break; break;
case BluetoothDevice.ACTION_NAME_CHANGED:
case BLUETOOTH_DEVICE_ACTION_ALIAS_CHANGED:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String newName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
updateDeviceName(device, newName);
break;
case GBDevice.ACTION_DEVICE_CHANGED: case GBDevice.ACTION_DEVICE_CHANGED:
GBDevice dev = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); GBDevice dev = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
if (dev.getAddress() != null) { if (dev.getAddress() != null) {
@ -78,11 +85,28 @@ public class DeviceManager {
filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED); filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED);
filterLocal.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); filterLocal.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
LocalBroadcastManager.getInstance(context).registerReceiver(mReceiver, filterLocal); LocalBroadcastManager.getInstance(context).registerReceiver(mReceiver, filterLocal);
context.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
IntentFilter filterGlobal = new IntentFilter();
filterGlobal.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
filterGlobal.addAction(BLUETOOTH_DEVICE_ACTION_ALIAS_CHANGED);
filterGlobal.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
context.registerReceiver(mReceiver, filterGlobal);
refreshPairedDevices(); refreshPairedDevices();
} }
private void updateDeviceName(BluetoothDevice device, String newName) {
for (GBDevice dev : deviceList) {
if (device.getAddress().equals(dev.getAddress())) {
if (!dev.getName().equals(newName)) {
dev.setName(newName);
notifyDevicesChanged();
return;
}
}
}
}
private void updateSelectedDevice(GBDevice dev) { private void updateSelectedDevice(GBDevice dev) {
if (selectedDevice == null) { if (selectedDevice == null) {
selectedDevice = dev; selectedDevice = dev;

View File

@ -43,7 +43,7 @@ public class GBDevice implements Parcelable {
private static final String DEVINFO_HW_VER = "HW: "; private static final String DEVINFO_HW_VER = "HW: ";
private static final String DEVINFO_FW_VER = "FW: "; private static final String DEVINFO_FW_VER = "FW: ";
private static final String DEVINFO_ADDR = "ADDR: "; private static final String DEVINFO_ADDR = "ADDR: ";
private final String mName; private String mName;
private final String mAddress; private final String mAddress;
private final DeviceType mDeviceType; private final DeviceType mDeviceType;
private String mFirmwareVersion; private String mFirmwareVersion;
@ -109,6 +109,14 @@ public class GBDevice implements Parcelable {
return mName; return mName;
} }
public void setName(String name) {
if (name == null) {
LOG.warn("Ignoring setting of GBDevice name to null for " + this);
return;
}
mName = name;
}
public String getAddress() { public String getAddress() {
return mAddress; return mAddress;
} }