From af5ba5c0d944db72214a17217e1a1ca41955ee9d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 9 Feb 2016 22:06:02 +0100 Subject: [PATCH] Don't restart before config has been updated (fixes #398). --- .../activities/RestartActivity.java | 4 +-- .../fragments/SettingsFragment.java | 2 +- .../syncthingandroid/syncthing/RestApi.java | 34 +++++++++++-------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java index bf8c7578..7cb1bf21 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java @@ -27,15 +27,13 @@ public class RestartActivity extends SyncthingActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - final Intent intent = new Intent(this, SyncthingService.class) - .setAction(SyncthingService.ACTION_RESTART); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(R.string.restart_title) .setPositiveButton(R.string.restart_now, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { - startService(intent); + getService().getApi().updateConfig(); finish(); } }) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java index 64a0dcd1..1cfd7c33 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java @@ -385,7 +385,7 @@ public class SettingsFragment extends PreferenceFragment Toast.makeText(getActivity(), getString(R.string.config_imported_successful), Toast.LENGTH_SHORT).show(); - mSyncthingService.getApi().requireRestart(getActivity(), false); + mSyncthingService.getApi().requireRestart(getActivity()); } else { Toast.makeText(getActivity(), getString(R.string.config_import_failed, diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index ffefd437..c0dbbaf6 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -341,28 +341,32 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, return json; } + /** + * Either shows a restart dialog, or only updates the config, depending on + * {@link #mRestartPostponed}. + */ public void requireRestart(Activity activity) { - requireRestart(activity, true); + if (mRestartPostponed) { + new PostConfigTask(mHttpsCertPath).execute(mUrl, mApiKey, mConfig.toString()); + } else { + activity.startActivity(new Intent(mContext, RestartActivity.class)); + } } /** - * Sends the updated mConfig via Rest API to syncthing and displays a "restart" - * dialog or notification. + * Sends the current config to Syncthing and restarts it. * - * @param activity The calling activity. - * @param updateConfig If true, {@link #mConfig} will be sent to `/rest/system/config`. + * This executes a restart immediately, and does not show a dialog. */ - public void requireRestart(Activity activity, boolean updateConfig) { - if (updateConfig) { - new PostConfigTask(mHttpsCertPath) - .execute(mUrl, mApiKey, mConfig.toString()); - } - // TODO Should wait until PostConfigTask is completed, see #398 + public void updateConfig() { + new PostConfigTask(mHttpsCertPath) { + @Override + protected void onPostExecute(Boolean aBoolean) { + mContext.startService(new Intent(mContext, SyncthingService.class) + .setAction(SyncthingService.ACTION_RESTART)); + } + }.execute(mUrl, mApiKey, mConfig.toString()); - if (mRestartPostponed) - return; - - activity.startActivity(new Intent(mContext, RestartActivity.class)); } /**