mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-27 13:21:30 +00:00
Merge pull request #694 from capi/foreground-service
Experimental "Run as foreground service" setting
This commit is contained in:
commit
d22fbc3f38
3 changed files with 31 additions and 2 deletions
|
@ -97,6 +97,7 @@ public class SyncthingService extends Service implements
|
|||
public static final String PREF_USE_ROOT = "use_root";
|
||||
private static final String PREF_NOTIFICATION_TYPE = "notification_type";
|
||||
public static final String PREF_USE_WAKE_LOCK = "wakelock_while_binary_running";
|
||||
public static final String PREF_FOREGROUND_SERVICE = "run_as_foreground_service";
|
||||
|
||||
private static final int NOTIFICATION_ACTIVE = 1;
|
||||
|
||||
|
@ -291,6 +292,13 @@ public class SyncthingService extends Service implements
|
|||
private void updateNotification() {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
String type = sp.getString(PREF_NOTIFICATION_TYPE, "low_priority");
|
||||
boolean foreground = sp.getBoolean(PREF_FOREGROUND_SERVICE, false);
|
||||
if ("none".equals(type) && foreground) {
|
||||
// foreground priority requires any notification
|
||||
// so this ensures that we either have a "default" or "low_priority" notification,
|
||||
// but not "none".
|
||||
type = "low_priority";
|
||||
}
|
||||
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
if ((mCurrentState == State.ACTIVE || mCurrentState == State.STARTING) &&
|
||||
!type.equals("none")) {
|
||||
|
@ -304,15 +312,23 @@ public class SyncthingService extends Service implements
|
|||
if (type.equals("low_priority"))
|
||||
builder.setPriority(NotificationCompat.PRIORITY_MIN);
|
||||
|
||||
nm.notify(NOTIFICATION_ACTIVE, builder.build());
|
||||
if (foreground) {
|
||||
builder.setContentText(getString(R.string.syncthing_active_foreground));
|
||||
startForeground(NOTIFICATION_ACTIVE, builder.build());
|
||||
} else {
|
||||
stopForeground(false); // ensure no longer running with foreground priority
|
||||
nm.notify(NOTIFICATION_ACTIVE, builder.build());
|
||||
}
|
||||
} else {
|
||||
// ensure no longer running with foreground priority
|
||||
stopForeground(false);
|
||||
nm.cancel(NOTIFICATION_ACTIVE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if (key.equals(PREF_NOTIFICATION_TYPE))
|
||||
if (key.equals(PREF_NOTIFICATION_TYPE) || key.equals(PREF_FOREGROUND_SERVICE))
|
||||
updateNotification();
|
||||
else if (key.equals(PREF_SYNC_ONLY_CHARGING) || key.equals(PREF_SYNC_ONLY_WIFI)
|
||||
|| key.equals(PREF_SYNC_ONLY_WIFI_SSIDS))
|
||||
|
@ -464,6 +480,7 @@ public class SyncthingService extends Service implements
|
|||
mApi.shutdown();
|
||||
|
||||
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
stopForeground(false);
|
||||
nm.cancel(NOTIFICATION_ACTIVE);
|
||||
|
||||
for (FolderObserver ro : mObservers) {
|
||||
|
|
|
@ -298,6 +298,10 @@ Please report any problems you encounter via Github.</string>
|
|||
|
||||
<string name="keep_wakelock_while_binary_running_summary">Use this setting if you experience unexpected disconnects while operating on battery. This will result in increased battery consumption.</string>
|
||||
|
||||
<string name="run_as_foreground_service">Run service with foreground priority</string>
|
||||
|
||||
<string name="run_as_foreground_service_summary">If enabled, Syncthing will run with foreground priority and is less likely to be stopped by Android. This might cause other services to be stopped if available memory is low.</string>
|
||||
|
||||
<!-- Toast shown after config was successfully exported -->
|
||||
<string name="config_export_successful">Config was exported to %1$s</string>
|
||||
|
||||
|
@ -417,6 +421,8 @@ Please report any problems you encounter via Github.</string>
|
|||
<!-- Title of the notification shown while syncthing is running and enabled -->
|
||||
<string name="syncthing_active">Syncthing is running</string>
|
||||
|
||||
<string name="syncthing_active_foreground">Service is running with foreground priority.</string>
|
||||
|
||||
<!-- Toast shown if folder observer fails to traverse a folder -->
|
||||
<string name="toast_folder_observer_stack_overflow">Directory tree too deep. Check for cyclic symlinks</string>
|
||||
|
||||
|
|
|
@ -162,6 +162,12 @@
|
|||
android:summary="@string/keep_wakelock_while_binary_running_summary"
|
||||
android:defaultValue="false" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="run_as_foreground_service"
|
||||
android:title="@string/run_as_foreground_service"
|
||||
android:summary="@string/run_as_foreground_service_summary"
|
||||
android:defaultValue="false" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
|
Loading…
Reference in a new issue