From d88828be822dd70da1a10c830f47efaa44976dd5 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 18 May 2015 00:10:25 +0200 Subject: [PATCH] Clean up config import code. --- .../fragments/SettingsFragment.java | 16 +--------------- .../syncthingandroid/syncthing/RestApi.java | 15 +++++++++++++-- .../syncthing/SyncthingService.java | 3 --- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java index c015a68d..35fa3850 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java @@ -1,9 +1,5 @@ package com.nutomic.syncthingandroid.fragments; -import android.app.AlarmManager; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Bundle; @@ -20,7 +16,6 @@ import android.view.MenuItem; import android.widget.Toast; import com.nutomic.syncthingandroid.R; -import com.nutomic.syncthingandroid.activities.MainActivity; import com.nutomic.syncthingandroid.activities.SyncthingActivity; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.SyncthingService; @@ -290,16 +285,7 @@ public class SettingsFragment extends PreferenceFragment if (mSyncthingService.importConfig()) { Toast.makeText(getActivity(), getString(R.string.config_imported_successful), Toast.LENGTH_SHORT).show(); - // Restart application because API key changed - // TODO Should be nicer - Intent mStartActivity = new Intent(this.getActivity(), MainActivity.class); - int mPendingIntentId = 838465; - PendingIntent mPendingIntent = PendingIntent.getActivity(this.getActivity(), - mPendingIntentId, - mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); - AlarmManager mgr = (AlarmManager)this.getActivity().getSystemService(Context.ALARM_SERVICE); - mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); - System.exit(0); + mSyncthingService.getApi().requireRestart(getActivity(), false); } else { Toast.makeText(getActivity(), getString(R.string.config_import_failed, SyncthingService.EXPORT_PATH), Toast.LENGTH_LONG).show(); diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index 0653c9c7..02c9e271 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -338,13 +338,24 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } return json; } + + public void requireRestart(Activity activity) { + requireRestart(activity, true); + } + /** * Sends the updated mConfig via Rest API to syncthing and displays a "restart" * dialog or notification. + * + * @param activity The calling activity. + * @param updateConfig If true, {@link #mConfig} will be sent to `/rest/system/config`. */ @TargetApi(11) - public void requireRestart(Activity activity) { - new PostTask(mHttpsCertPath).execute(mUrl, PostTask.URI_CONFIG, mApiKey, mConfig.toString()); + public void requireRestart(Activity activity, boolean updateConfig) { + if (updateConfig) { + new PostTask(mHttpsCertPath) + .execute(mUrl, PostTask.URI_CONFIG, mApiKey, mConfig.toString()); + } // TODO Should wait for completion... if (mRestartPostponed) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index f5a6ea2a..62206cce 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -513,9 +513,6 @@ public class SyncthingService extends Service { copyFile(config, new File(getFilesDir(), ConfigXml.CONFIG_FILE)); copyFile(privateKey, new File(getFilesDir(), PRIVATE_KEY_FILE)); copyFile(publicKey, new File(getFilesDir(), PUBLIC_KEY_FILE)); - - startService(new Intent(this, SyncthingService.class) - .setAction(SyncthingService.ACTION_RESTART)); return true; }