1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-23 12:51:16 +00:00

Close Add Folder/Device Activity after "Create" (fixes #417).

This commit is contained in:
Felix Ableitner 2015-07-07 02:10:12 +02:00
parent 7daba5789c
commit cb6a3fc884
6 changed files with 114 additions and 57 deletions

View file

@ -59,9 +59,6 @@
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.SettingsActivity" /> android:value=".activities.SettingsActivity" />
</activity> </activity>
<service android:name=".syncthing.SyncthingService" />
<activity <activity
android:name=".activities.FolderPickerActivity" android:name=".activities.FolderPickerActivity"
android:label="@string/folder_picker_title"> android:label="@string/folder_picker_title">
@ -69,6 +66,10 @@
android:name="android.support.UI_OPTIONS" android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" /> android:value="splitActionBarWhenNarrow" />
</activity> </activity>
<activity android:name=".activities.RestartActivity"
android:theme="@style/Translucent"/>
<service android:name=".syncthing.SyncthingService" />
<receiver android:name=".syncthing.NetworkReceiver"> <receiver android:name=".syncthing.NetworkReceiver">
<intent-filter> <intent-filter>

View file

@ -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 `<item name="windowActionBar">false</item>` 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();
}
}

View file

@ -202,6 +202,7 @@ public class DeviceSettingsFragment extends PreferenceFragment implements
return true; return true;
} }
mSyncthingService.getApi().editDevice(mDevice, getActivity(), this); mSyncthingService.getApi().editDevice(mDevice, getActivity(), this);
getActivity().finish();
return true; return true;
case R.id.share_device_id: case R.id.share_device_id:
RestApi.shareDeviceId(getActivity(), mDevice.deviceID); RestApi.shareDeviceId(getActivity(), mDevice.deviceID);

View file

@ -215,6 +215,7 @@ public class FolderSettingsFragment extends PreferenceFragment
return true; return true;
} }
mSyncthingService.getApi().editFolder(mFolder, true, getActivity()); mSyncthingService.getApi().editFolder(mFolder, true, getActivity());
getActivity().finish();
return true; return true;
case R.id.delete: case R.id.delete:
new AlertDialog.Builder(getActivity()) new AlertDialog.Builder(getActivity())

View file

@ -3,21 +3,18 @@ package com.nutomic.syncthingandroid.syncthing;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.BuildConfig;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.RestartActivity;
import com.nutomic.syncthingandroid.util.FolderObserver; import com.nutomic.syncthingandroid.util.FolderObserver;
import org.json.JSONArray; import org.json.JSONArray;
@ -139,8 +136,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
public String invalid; public String invalid;
} }
private static final int NOTIFICATION_RESTART = 2;
private final Context mContext; private final Context mContext;
private String mVersion; private String mVersion;
@ -276,7 +271,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
NotificationManager nm = (NotificationManager) NotificationManager nm = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE); mContext.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFICATION_RESTART); nm.cancel(RestartActivity.NOTIFICATION_RESTART);
mRestartPostponed = false; mRestartPostponed = false;
} }
@ -350,7 +345,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
* @param activity The calling activity. * @param activity The calling activity.
* @param updateConfig If true, {@link #mConfig} will be sent to `/rest/system/config`. * @param updateConfig If true, {@link #mConfig} will be sent to `/rest/system/config`.
*/ */
@TargetApi(11)
public void requireRestart(Activity activity, boolean updateConfig) { public void requireRestart(Activity activity, boolean updateConfig) {
if (updateConfig) { if (updateConfig) {
new PostTask(mHttpsCertPath) new PostTask(mHttpsCertPath)
@ -361,36 +355,14 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
if (mRestartPostponed) if (mRestartPostponed)
return; return;
final Intent intent = new Intent(mContext, SyncthingService.class) activity.startActivity(new Intent(mContext, RestartActivity.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();
} }
/** /**
* Reset Syncthing's indexes when confirmed by a dialog. * 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) { public void resetSyncthing(final Activity activity) {
final Intent intent = new Intent(mContext, SyncthingService.class) final Intent intent = new Intent(mContext, SyncthingService.class)
.setAction(SyncthingService.ACTION_RESET); .setAction(SyncthingService.ACTION_RESET);
@ -412,27 +384,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
.show(); .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. * Returns a list of all existing devices.
* *
@ -1077,4 +1028,11 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
}.execute(mUrl, GetTask.URI_REPORT, mApiKey); }.execute(mUrl, GetTask.URI_REPORT, mApiKey);
} }
/**
* Sets {@link #mRestartPostponed} to true.
*/
public void setRestartPostponed() {
mRestartPostponed = true;
}
} }

View file

@ -11,4 +11,12 @@
<style name="AppTheme" parent="BaseTheme"/> <style name="AppTheme" parent="BaseTheme"/>
<style name="Translucent" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
</resources> </resources>