Ensure that a newly discovered and initialized device will be displayed

Fixed a race condition between ACTION_DEVICE_CHANGED event handlers:
- DeviceCommunicationService added the device to the database
- DeviceManager notified the views to be refreshed

When the latter happened before the former, thew new device would not
be displayed.

=> Let DeviceManager do both.
master
cpfeiffer 2017-04-07 21:17:37 +02:00
parent e1797fc9f7
commit 202ae53d71
2 changed files with 9 additions and 11 deletions

View File

@ -34,6 +34,9 @@ import java.util.Comparator;
import java.util.List;
import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
@ -88,6 +91,12 @@ public class DeviceManager {
} else {
deviceList.add(dev);
}
if (dev.isInitialized()) {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
DBHelper.getDevice(dev, dbHandler.getDaoSession()); // implicitly creates the device in database if not present, and updates device attributes
} catch (Exception ignore) {
}
}
}
updateSelectedDevice(dev);
refreshPairedDevices();

View File

@ -41,9 +41,6 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmClockReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver;
@ -207,14 +204,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized());
setReceiversEnableState(enableReceivers);
GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), mGBDevice.isInitialized(), context);
if (device.isInitialized()) {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
DaoSession session = dbHandler.getDaoSession();
DBHelper.getDevice(device, session); // implicitly creates the device in database if not present, and updates device attributes
} catch (Exception ignore) {
}
}
} else {
LOG.error("Got ACTION_DEVICE_CHANGED from unexpected device: " + mGBDevice);
}