diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 77122a39..f22e8e5a 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -93,6 +93,7 @@ public class SyncthingService extends Service { private EventProcessor mEventProcessor; private DeviceStateHolder mDeviceStateHolder; private SyncthingRunnable mSyncthingRunnable; + private Handler mHandler; private final HashSet mOnApiChangeListeners = new HashSet<>(); private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this); @@ -180,7 +181,8 @@ public class SyncthingService extends Service { super.onCreate(); PRNGFixes.apply(); ((SyncthingApp) getApplication()).component().inject(this); - + mHandler = new Handler(); + mDeviceStateHolder = new DeviceStateHolder(SyncthingService.this, this::updateState); updateState(); mNotificationHandler.updatePersistentNotification(this); @@ -354,21 +356,21 @@ public class SyncthingService extends Service { /** * Called to notifiy listeners of an API change. - * - * Must only be called from SyncthingService or {@link RestApi} on the main thread. */ private void onApiChange(State newState) { - mCurrentState = newState; - mNotificationHandler.updatePersistentNotification(this); - for (Iterator i = mOnApiChangeListeners.iterator(); - i.hasNext(); ) { - OnApiChangeListener listener = i.next(); - if (listener != null) { - listener.onApiChange(mCurrentState); - } else { - i.remove(); + mHandler.post(() -> { + mCurrentState = newState; + mNotificationHandler.updatePersistentNotification(this); + for (Iterator i = mOnApiChangeListeners.iterator(); + i.hasNext(); ) { + OnApiChangeListener listener = i.next(); + if (listener != null) { + listener.onApiChange(mCurrentState); + } else { + i.remove(); + } } - } + }); } public URL getWebGuiUrl() {