Skip to content

Commit

Permalink
Restart now when importing config (fixes #386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zillode committed May 6, 2015
1 parent cde2768 commit 9ef1474
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
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;
Expand All @@ -16,6 +20,7 @@
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;
Expand Down Expand Up @@ -269,6 +274,16 @@ public boolean onPreferenceClick(Preference preference) {
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);

This comment has been minimized.

Copy link
@Nutomic

Nutomic May 17, 2015

Contributor

Just noticed this. Why are you not calling RestApi#requireRestart()?

This comment has been minimized.

Copy link
@Zillode

Zillode May 17, 2015

Author Contributor

If I remember well, it re-posts the old config

This comment has been minimized.

Copy link
@Nutomic

Nutomic May 17, 2015

Contributor

A that's right. I would just add a boolean param postConfig to avoid this,

} else {
Toast.makeText(getActivity(), getString(R.string.config_import_failed,
SyncthingService.EXPORT_PATH), Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ private JSONArray listToJson(String[] list) {
}
return json;
}

/**
* Sends the updated mConfig via Rest API to syncthing and displays a "restart"
* dialog or notification.
*/
@TargetApi(11)
public void requireRestart(Activity activity) {
new PostTask(mHttpsCertPath).execute(mUrl, PostTask.URI_CONFIG, mApiKey, mConfig.toString());
// TODO Should wait for completion...

if (mRestartPostponed)
return;
Expand Down

0 comments on commit 9ef1474

Please sign in to comment.