1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-22 20:31:16 +00:00

Change restart handling to show a dialog first (ref #49).

The notification is only shown if the dialog is dismissed, in which
case the dialog won't show up as long as syncthing is not restarted.
This commit is contained in:
Felix Ableitner 2014-07-01 20:51:41 +02:00
parent 50c71c5b45
commit 61eba3da1d
5 changed files with 67 additions and 39 deletions

View file

@ -163,7 +163,7 @@ public class NodeSettingsActivity extends PreferenceActivity implements
Toast.makeText(this, R.string.node_name_required, Toast.LENGTH_LONG).show();
return true;
}
mSyncthingService.getApi().editNode(mNode, true);
mSyncthingService.getApi().editNode(mNode, true, this);
finish();
return true;
case R.id.share_node_id:
@ -216,7 +216,7 @@ public class NodeSettingsActivity extends PreferenceActivity implements
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mSyncthingService.getApi().deleteNode(mNode);
mSyncthingService.getApi().deleteNode(mNode, NodeSettingsActivity.this);
finish();
}
})
@ -246,7 +246,7 @@ public class NodeSettingsActivity extends PreferenceActivity implements
*/
private void nodeUpdated() {
if (getIntent().getAction().equals(ACTION_EDIT)) {
mSyncthingService.getApi().editNode(mNode, false);
mSyncthingService.getApi().editNode(mNode, false, this);
}
}

View file

@ -197,7 +197,7 @@ public class RepoSettingsActivity extends PreferenceActivity
Toast.makeText(this, R.string.repo_path_required, Toast.LENGTH_LONG).show();
return true;
}
mSyncthingService.getApi().editRepo(mRepo, true);
mSyncthingService.getApi().editRepo(mRepo, true, this);
finish();
return true;
case android.R.id.home:
@ -295,7 +295,7 @@ public class RepoSettingsActivity extends PreferenceActivity
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mSyncthingService.getApi().deleteRepo(mRepo);
mSyncthingService.getApi().deleteRepo(mRepo, RepoSettingsActivity.this);
finish();
}
})
@ -317,7 +317,7 @@ public class RepoSettingsActivity extends PreferenceActivity
private void repoUpdated() {
if (getIntent().getAction().equals(ACTION_EDIT)) {
mSyncthingService.getApi().editRepo(mRepo, false);
mSyncthingService.getApi().editRepo(mRepo, false, this);
}
}

View file

@ -161,11 +161,12 @@ public class SettingsActivity extends PreferenceActivity
if (mOptionsScreen.findPreference(preference.getKey()) != null) {
mSyncthingService.getApi().setValue(RestApi.TYPE_OPTIONS, preference.getKey(), o,
preference.getKey().equals("ListenAddress"));
preference.getKey().equals("ListenAddress"), this);
return true;
}
else if (mGuiScreen.findPreference(preference.getKey()) != null) {
mSyncthingService.getApi().setValue(RestApi.TYPE_GUI, preference.getKey(), o, false);
mSyncthingService.getApi().setValue(
RestApi.TYPE_GUI, preference.getKey(), o, false, this);
return true;
}
return false;

View file

@ -1,10 +1,12 @@
package com.nutomic.syncthingandroid.syncthing;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
@ -131,6 +133,8 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
private final NotificationManager mNotificationManager;
private boolean mRestartPostponed = false;
/**
* Stores the result of the last successful request to {@link GetTask#URI_CONNECTIONS},
* or an empty HashMap.
@ -267,12 +271,12 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
* @param value The new value to set, either String, Boolean or Integer.
* @param isArray True iff value is a space seperated String that should be converted to array.
*/
public <T> void setValue(String name, String key, T value, boolean isArray) {
public <T> void setValue(String name, String key, T value, boolean isArray, Activity activity) {
try {
mConfig.getJSONObject(name).put(key, (isArray)
? listToJson(((String) value).split(" "))
: value);
configUpdated();
configUpdated(activity);
}
catch (JSONException e) {
Log.w(TAG, "Failed to set value for " + key, e);
@ -294,21 +298,41 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/**
* Sends the updated mConfig via Rest API to syncthing and displays a "restart" notification.
*/
private void configUpdated() {
private void configUpdated(Activity activity) {
new PostTask().execute(mUrl, PostTask.URI_CONFIG, mApiKey, mConfig.toString());
Intent i = new Intent(mSyncthingService, SyncthingService.class)
.setAction(SyncthingService.ACTION_RESTART);
PendingIntent pi = PendingIntent.getService(mSyncthingService, 0, i, 0);
if (mRestartPostponed) {
return;
}
Notification n = new NotificationCompat.Builder(mSyncthingService)
.setContentTitle(mSyncthingService.getString(R.string.restart_notif_title))
.setContentText(mSyncthingService.getString(R.string.restart_notif_text))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_RESTART, n);
final Intent intent = new Intent(mSyncthingService, SyncthingService.class)
.setAction(SyncthingService.ACTION_RESTART);
new AlertDialog.Builder(activity)
.setMessage(R.string.restart_title)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mSyncthingService.startService(intent);
}
})
.setNegativeButton(R.string.restart_later, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
PendingIntent pi = PendingIntent.getService(mSyncthingService, 0, intent, 0);
Notification n = new NotificationCompat.Builder(mSyncthingService)
.setContentTitle(mSyncthingService.getString(R.string.restart_title))
.setContentText(mSyncthingService.getString(R.string.restart_notification_text))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_RESTART, n);
mRestartPostponed = true;
}
})
.show();
}
/**
@ -557,7 +581,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/**
* Updates or creates the given node.
*/
public void editNode(Node node, boolean create) {
public void editNode(Node node, boolean create, Activity activity) {
try {
JSONArray nodes = mConfig.getJSONArray("Nodes");
JSONObject n = null;
@ -577,7 +601,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
n.put("NodeID", node.NodeID);
n.put("Name", node.Name);
n.put("Addresses", listToJson(node.Addresses.split(" ")));
configUpdated();
configUpdated(activity);
}
catch (JSONException e) {
Log.w(TAG, "Failed to read nodes", e);
@ -587,7 +611,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/**
* Deletes the given node from syncthing.
*/
public void deleteNode(Node node) {
public void deleteNode(Node node, Activity activity) {
try {
JSONArray nodes = mConfig.getJSONArray("Nodes");
@ -599,7 +623,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
break;
}
}
configUpdated();
configUpdated(activity);
}
catch (JSONException e) {
Log.w(TAG, "Failed to edit repo", e);
@ -609,7 +633,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/**
* Updates or creates the given node.
*/
public void editRepo(Repo repo, boolean create) {
public void editRepo(Repo repo, boolean create, Activity activity) {
try {
JSONArray repos = mConfig.getJSONArray("Repositories");
JSONObject r = null;
@ -647,7 +671,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
params.put(key, repo.Versioning.getParams().get(key));
}
r.put("Versioning", versioning);
configUpdated();
configUpdated(activity);
}
catch (JSONException e) {
Log.w(TAG, "Failed to edit repo " + repo.ID + " at " + repo.Directory, e);
@ -657,7 +681,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/**
* Deletes the given repository from syncthing.
*/
public void deleteRepo(Repo repo) {
public void deleteRepo(Repo repo, Activity activity) {
try {
JSONArray repos = mConfig.getJSONArray("Repositories");
@ -669,7 +693,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
break;
}
}
configUpdated();
configUpdated(activity);
}
catch (JSONException e) {
Log.w(TAG, "Failed to edit repo", e);

View file

@ -3,15 +3,6 @@
<string name="app_name">Syncthing</string>
<!-- Title of the notification shown when a restart is needed -->
<string name="restart_notif_title">Restart Needed</string>
<!-- Text of the notification shown when a restart is needed -->
<string name="restart_notif_text">Click here to restart syncthing now</string>
<!-- ActionBar title shown when the drawer is open -->
<string name="system_info">System Info</string>
<!-- MainActivity -->
@ -68,6 +59,9 @@
<!-- LocalNodeInfoFragment -->
<!-- ActionBar title shown when the drawer is open -->
<string name="system_info">System Info</string>
<!-- Same as download_title with a colon and space appended -->
<string name="download_title_colon">Download:\u0020</string>
@ -220,6 +214,15 @@ Please report any problems you encounter.</string>
<!-- RestApi -->
<!-- Title of the notification shown when a restart is needed -->
<string name="restart_title">Restart Needed</string>
<!-- Text of the notification shown when a restart is needed -->
<string name="restart_notification_text">Click here to restart syncthing now</string>
<!-- Text for the dismiss button of the restart Activity -->
<string name="restart_later">Restart Later</string>
<!-- Strings representing units for file sizes, from smallest to largest -->
<string-array name="file_size_units">
<item>B</item>