From cb6a3fc884e1822a29bd4e39757c5621c1078ffd Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 7 Jul 2015 02:10:12 +0200 Subject: [PATCH] Close Add Folder/Device Activity after "Create" (fixes #417). --- src/main/AndroidManifest.xml | 7 +- .../activities/RestartActivity.java | 88 +++++++++++++++++++ .../fragments/DeviceSettingsFragment.java | 1 + .../fragments/FolderSettingsFragment.java | 1 + .../syncthingandroid/syncthing/RestApi.java | 66 +++----------- src/main/res/values/styles.xml | 8 ++ 6 files changed, 114 insertions(+), 57 deletions(-) create mode 100644 src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 96ec99b1..54bfe2d5 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -59,9 +59,6 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".activities.SettingsActivity" /> - - - @@ -69,6 +66,10 @@ android:name="android.support.UI_OPTIONS" android:value="splitActionBarWhenNarrow" /> + + + diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java new file mode 100644 index 00000000..9843f4e7 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/activities/RestartActivity.java @@ -0,0 +1,88 @@ +package com.nutomic.syncthingandroid.activities; + +import android.annotation.TargetApi; +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.os.Build; +import android.os.Bundle; +import android.support.v4.app.NotificationCompat; +import android.support.v7.app.AppCompatActivity; + +import com.nutomic.syncthingandroid.BuildConfig; +import com.nutomic.syncthingandroid.R; +import com.nutomic.syncthingandroid.syncthing.SyncthingService; + +/** + * Shows restart dialog. + * + * The user can choose to restart Syncthing immediately. Otherwise, a restart notification is + * displayed. + */ +public class RestartActivity extends SyncthingActivity { + + public static final int NOTIFICATION_RESTART = 2; + + @Override + @TargetApi(11) + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // Using `false` in style causes crash. + getSupportActionBar().hide(); + + 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); + finish(); + } + }) + .setNegativeButton(R.string.restart_later, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + createRestartNotification(); + finish(); + } + }) + .setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + createRestartNotification(); + finish(); + } + }) + .show(); + } + + /** + * Creates a notification prompting the user to restart the app. + */ + private void createRestartNotification() { + Intent intent = new Intent(this, SyncthingService.class) + .setAction(SyncthingService.ACTION_RESTART); + PendingIntent pi = PendingIntent.getService(this, 0, intent, 0); + + Notification n = new NotificationCompat.Builder(this) + .setContentTitle(getString(R.string.restart_title)) + .setContentText(getString(R.string.restart_notification_text)) + .setSmallIcon(R.drawable.ic_stat_notify) + .setContentIntent(pi) + .build(); + n.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL; + NotificationManager nm = (NotificationManager) + getSystemService(Context.NOTIFICATION_SERVICE); + nm.notify(NOTIFICATION_RESTART, n); + getApi().setRestartPostponed(); + } + +} diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceSettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceSettingsFragment.java index 2f7150f7..274d70e3 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceSettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceSettingsFragment.java @@ -202,6 +202,7 @@ public class DeviceSettingsFragment extends PreferenceFragment implements return true; } mSyncthingService.getApi().editDevice(mDevice, getActivity(), this); + getActivity().finish(); return true; case R.id.share_device_id: RestApi.shareDeviceId(getActivity(), mDevice.deviceID); diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java index d8fd0ef7..226fc30d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderSettingsFragment.java @@ -215,6 +215,7 @@ public class FolderSettingsFragment extends PreferenceFragment return true; } mSyncthingService.getApi().editFolder(mFolder, true, getActivity()); + getActivity().finish(); return true; case R.id.delete: new AlertDialog.Builder(getActivity()) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index e7c38a46..11b0a99f 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -3,21 +3,18 @@ package com.nutomic.syncthingandroid.syncthing; import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; -import android.app.Notification; import android.app.NotificationManager; -import android.app.PendingIntent; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.os.Build; -import android.support.v4.app.NotificationCompat; import android.util.Log; import android.widget.Toast; import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.R; +import com.nutomic.syncthingandroid.activities.RestartActivity; import com.nutomic.syncthingandroid.util.FolderObserver; import org.json.JSONArray; @@ -139,8 +136,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, public String invalid; } - private static final int NOTIFICATION_RESTART = 2; - private final Context mContext; private String mVersion; @@ -276,7 +271,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - nm.cancel(NOTIFICATION_RESTART); + nm.cancel(RestartActivity.NOTIFICATION_RESTART); mRestartPostponed = false; } @@ -350,7 +345,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, * @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, boolean updateConfig) { if (updateConfig) { new PostTask(mHttpsCertPath) @@ -361,36 +355,14 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, if (mRestartPostponed) return; - final Intent intent = new Intent(mContext, SyncthingService.class) - .setAction(SyncthingService.ACTION_RESTART); - - new AlertDialog.Builder(activity) - .setMessage(R.string.restart_title) - .setPositiveButton(R.string.restart_now, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - mContext.startService(intent); - } - }) - .setNegativeButton(R.string.restart_later, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - createRestartNotification(); - } - }) - .setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - createRestartNotification(); - } - }) - .show(); + activity.startActivity(new Intent(mContext, RestartActivity.class)); } /** * Reset Syncthing's indexes when confirmed by a dialog. + * + * TODO: why is this here and not in fragment? */ - @TargetApi(11) public void resetSyncthing(final Activity activity) { final Intent intent = new Intent(mContext, SyncthingService.class) .setAction(SyncthingService.ACTION_RESET); @@ -412,27 +384,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, .show(); } - /** - * Creates a notification prompting the user to restart the app. - */ - private void createRestartNotification() { - Intent intent = new Intent(mContext, SyncthingService.class) - .setAction(SyncthingService.ACTION_RESTART); - PendingIntent pi = PendingIntent.getService(mContext, 0, intent, 0); - - Notification n = new NotificationCompat.Builder(mContext) - .setContentTitle(mContext.getString(R.string.restart_title)) - .setContentText(mContext.getString(R.string.restart_notification_text)) - .setSmallIcon(R.drawable.ic_stat_notify) - .setContentIntent(pi) - .build(); - n.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL; - NotificationManager nm = (NotificationManager) - mContext.getSystemService(Context.NOTIFICATION_SERVICE); - nm.notify(NOTIFICATION_RESTART, n); - mRestartPostponed = true; - } - /** * Returns a list of all existing devices. * @@ -1077,4 +1028,11 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, }.execute(mUrl, GetTask.URI_REPORT, mApiKey); } + /** + * Sets {@link #mRestartPostponed} to true. + */ + public void setRestartPostponed() { + mRestartPostponed = true; + } + } diff --git a/src/main/res/values/styles.xml b/src/main/res/values/styles.xml index f96197a7..098fbf8c 100644 --- a/src/main/res/values/styles.xml +++ b/src/main/res/values/styles.xml @@ -11,4 +11,12 @@ +