Skip to content

Commit

Permalink
Fix crash in settings onApiChange call (fixes #1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic authored and AudriusButkevicius committed May 10, 2018
1 parent 49853d7 commit 533557c
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public enum State {
private EventProcessor mEventProcessor;
private DeviceStateHolder mDeviceStateHolder;
private SyncthingRunnable mSyncthingRunnable;
private Handler mHandler;

private final HashSet<OnApiChangeListener> mOnApiChangeListeners = new HashSet<>();
private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this);
Expand Down Expand Up @@ -180,7 +181,8 @@ public void onCreate() {
super.onCreate();
PRNGFixes.apply();
((SyncthingApp) getApplication()).component().inject(this);

mHandler = new Handler();

mDeviceStateHolder = new DeviceStateHolder(SyncthingService.this, this::updateState);
updateState();
mNotificationHandler.updatePersistentNotification(this);
Expand Down Expand Up @@ -354,21 +356,21 @@ private void pollWebGui() {

/**
* 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<OnApiChangeListener> 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<OnApiChangeListener> i = mOnApiChangeListeners.iterator();
i.hasNext(); ) {
OnApiChangeListener listener = i.next();
if (listener != null) {
listener.onApiChange(mCurrentState);
} else {
i.remove();
}
}
}
});
}

public URL getWebGuiUrl() {
Expand Down

0 comments on commit 533557c

Please sign in to comment.