From 533557c13551a3aba2de903ce5b60f449b99d912 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 10 May 2018 19:13:49 +0200 Subject: [PATCH] Fix crash in settings onApiChange call (fixes #1084) --- .../service/SyncthingService.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) 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() {