From fa49c796ae5077e966cc0758645abe27e35d8d7e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 24 Oct 2017 00:01:12 +0900 Subject: [PATCH] Fixed crash when opening SettingsActivity --- .../activities/SettingsActivity.java | 13 ++++++++++--- .../nutomic/syncthingandroid/service/RestApi.java | 3 +++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 4d8055aa..95332f0c 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -98,7 +98,6 @@ public class SettingsActivity extends SyncthingActivity { super.onCreate(savedInstanceState); ((SyncthingApp) getActivity().getApplication()).component().inject(this); ((SyncthingActivity) getActivity()).registerOnServiceConnectedListener(this); - mPreferences.registerOnSharedPreferenceChangeListener(this); } /** @@ -297,7 +296,7 @@ public class SettingsActivity extends SyncthingActivity { } /** - * Sends the updated value to {@link }RestApi}, and sets it as the summary + * Sends the updated value to {@link RestApi}, and sets it as the summary * for EditTextPreference. */ @Override @@ -408,9 +407,17 @@ public class SettingsActivity extends SyncthingActivity { } } + /** + * Update notification after that preference changes. We can't use onPreferenceChange() as + * the preference value isn't persisted there, and the NotificationHandler accesses the + * preference directly. + * + * This function is called when the activity is opened, so we need to make sure the service + * is connected. + */ @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (key.equals(Constants.PREF_NOTIFICATION_TYPE)) { + if (key.equals(Constants.PREF_NOTIFICATION_TYPE) && mSyncthingService != null) { mNotificationHandler.updatePersistentNotification(mSyncthingService); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index 878b8aa5..c055b5c8 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -146,6 +146,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, }); new GetRequest(mContext, mUrl, GetRequest.URI_CONFIG, mApiKey, null, result -> { mConfig = new Gson().fromJson(result, Config.class); + if (mConfig == null) { + throw new RuntimeException("config is null: " + result); + } tryIsAvailable(); }); getSystemInfo(info -> {