From f8a41ff4b706dabe9db8ee9ab8e75b844657e2c1 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 19 Oct 2017 12:11:00 +0900 Subject: [PATCH] Don't call startForegroundService if app is not running in background (fixes #972) --- .../syncthingandroid/fragments/DrawerFragment.java | 3 ++- .../syncthingandroid/receiver/AppConfigReceiver.java | 3 ++- .../syncthingandroid/receiver/BatteryReceiver.java | 2 +- .../syncthingandroid/receiver/BootReceiver.java | 8 +++++++- .../syncthingandroid/receiver/NetworkReceiver.java | 2 +- .../syncthingandroid/service/DeviceStateHolder.java | 11 ++++++++++- .../syncthingandroid/service/NotificationHandler.java | 4 ++-- .../syncthingandroid/service/SyncthingService.java | 10 ---------- 8 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java index 5c0b0d79..e2a7ae69 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java @@ -21,6 +21,7 @@ import com.nutomic.syncthingandroid.model.Connections; import com.nutomic.syncthingandroid.model.SystemInfo; import com.nutomic.syncthingandroid.model.SystemVersion; import com.nutomic.syncthingandroid.service.Constants; +import com.nutomic.syncthingandroid.service.DeviceStateHolder; import com.nutomic.syncthingandroid.service.RestApi; import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.util.Util; @@ -111,7 +112,7 @@ public class DrawerFragment extends Fragment implements View.OnClickListener { } private void updateExitButtonVisibility() { - boolean alwaysInBackground = SyncthingService.alwaysRunInBackground(getActivity()); + boolean alwaysInBackground = DeviceStateHolder.alwaysRunInBackground(getActivity()); mExitButton.setVisibility(alwaysInBackground ? View.GONE : View.VISIBLE); } diff --git a/src/main/java/com/nutomic/syncthingandroid/receiver/AppConfigReceiver.java b/src/main/java/com/nutomic/syncthingandroid/receiver/AppConfigReceiver.java index 01bf0169..2deb0e40 100644 --- a/src/main/java/com/nutomic/syncthingandroid/receiver/AppConfigReceiver.java +++ b/src/main/java/com/nutomic/syncthingandroid/receiver/AppConfigReceiver.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import com.nutomic.syncthingandroid.SyncthingApp; +import com.nutomic.syncthingandroid.service.DeviceStateHolder; import com.nutomic.syncthingandroid.service.NotificationHandler; import com.nutomic.syncthingandroid.service.SyncthingService; @@ -40,7 +41,7 @@ public class AppConfigReceiver extends BroadcastReceiver { break; case ACTION_STOP: - if (SyncthingService.alwaysRunInBackground(context)) { + if (DeviceStateHolder.alwaysRunInBackground(context)) { mNotificationHandler.showStopSyncthingWarningNotification(); } else { context.stopService(new Intent(context, SyncthingService.class)); diff --git a/src/main/java/com/nutomic/syncthingandroid/receiver/BatteryReceiver.java b/src/main/java/com/nutomic/syncthingandroid/receiver/BatteryReceiver.java index fde3976a..27536956 100644 --- a/src/main/java/com/nutomic/syncthingandroid/receiver/BatteryReceiver.java +++ b/src/main/java/com/nutomic/syncthingandroid/receiver/BatteryReceiver.java @@ -21,7 +21,7 @@ public class BatteryReceiver extends BroadcastReceiver { && !Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) return; - if (!SyncthingService.alwaysRunInBackground(context)) + if (!DeviceStateHolder.alwaysRunInBackground(context)) return; boolean isCharging = Intent.ACTION_POWER_CONNECTED.equals(intent.getAction()); diff --git a/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java b/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java index 9d1bde05..e97eddee 100644 --- a/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java +++ b/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.os.Build; +import com.nutomic.syncthingandroid.service.DeviceStateHolder; import com.nutomic.syncthingandroid.service.SyncthingService; public class BootReceiver extends BroadcastReceiver { @@ -15,7 +16,7 @@ public class BootReceiver extends BroadcastReceiver { !intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) return; - if (!SyncthingService.alwaysRunInBackground(context)) + if (!DeviceStateHolder.alwaysRunInBackground(context)) return; startServiceCompat(context); @@ -27,6 +28,11 @@ public class BootReceiver extends BroadcastReceiver { * https://stackoverflow.com/a/44505719/1837158 */ public static void startServiceCompat(Context context) { + // This method is called from {@link DeviceStateHolder#DeviceStateHolder()}, make sure it + // is only executed if run in background is enabled. + if (!DeviceStateHolder.alwaysRunInBackground(context)) + return; + Intent intent = new Intent(context, SyncthingService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); diff --git a/src/main/java/com/nutomic/syncthingandroid/receiver/NetworkReceiver.java b/src/main/java/com/nutomic/syncthingandroid/receiver/NetworkReceiver.java index f9e796bc..dd359eda 100644 --- a/src/main/java/com/nutomic/syncthingandroid/receiver/NetworkReceiver.java +++ b/src/main/java/com/nutomic/syncthingandroid/receiver/NetworkReceiver.java @@ -22,7 +22,7 @@ public class NetworkReceiver extends BroadcastReceiver { if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) return; - if (!SyncthingService.alwaysRunInBackground(context)) + if (!DeviceStateHolder.alwaysRunInBackground(context)) return; updateNetworkStatus(context); diff --git a/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java b/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java index 8f2b0ae9..d877ff79 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java @@ -7,6 +7,7 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; +import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; @@ -49,6 +50,14 @@ public class DeviceStateHolder { public static final String EXTRA_IS_POWER_SAVING = "com.nutomic.syncthingandroid.syncthing.DeviceStateHolder.IS_POWER_SAVING"; + /** + * Returns the value of "always_run_in_background" preference. + */ + public static boolean alwaysRunInBackground(Context context) { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + return sp.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false); + } + public interface OnDeviceStateChangedListener { void onDeviceStateChanged(); } @@ -114,7 +123,7 @@ public class DeviceStateHolder { if (prefRespectPowerSaving && mIsPowerSaving) return false; - if (SyncthingService.alwaysRunInBackground(mContext)) { + if (alwaysRunInBackground(mContext)) { boolean prefStopMobileData = mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_WIFI, false); boolean prefStopNotCharging = mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_CHARGING, false); diff --git a/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java b/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java index 1c9c7d58..9fc8c39c 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java @@ -46,7 +46,7 @@ public class NotificationHandler { // Android 8 does not allow starting service from background unless it's a foreground // service, so if "always run in background" is enabled, we have to use a foreground service. // https://stackoverflow.com/a/44505719/1837158 - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && SyncthingService.alwaysRunInBackground(mContext)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && DeviceStateHolder.alwaysRunInBackground(mContext)) { foreground = true; } @@ -87,7 +87,7 @@ public class NotificationHandler { } public void cancelPersistentNotification(SyncthingService service) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && SyncthingService.alwaysRunInBackground(mContext)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && DeviceStateHolder.alwaysRunInBackground(mContext)) return; service.stopForeground(false); diff --git a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 93156220..12d0ced0 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -2,7 +2,6 @@ package com.nutomic.syncthingandroid.service; import android.app.Service; import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; @@ -11,7 +10,6 @@ import android.os.AsyncTask; import android.os.Build; import android.os.IBinder; import android.os.PowerManager; -import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.util.Log; import android.widget.Toast; @@ -417,14 +415,6 @@ public class SyncthingService extends Service implements return mConfig.getWebGuiUrl(); } - /** - * Returns the value of "always_run_in_background" preference. - */ - public static boolean alwaysRunInBackground(Context context) { - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - return sp.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false); - } - public State getCurrentState() { return mCurrentState; }