1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 19:31:30 +00:00

Don't call startForegroundService if app is not running in background (fixes #972)

This commit is contained in:
Felix Ableitner 2017-10-19 12:11:00 +09:00
parent 18c419b2ed
commit f8a41ff4b7
8 changed files with 25 additions and 18 deletions

View file

@ -21,6 +21,7 @@ import com.nutomic.syncthingandroid.model.Connections;
import com.nutomic.syncthingandroid.model.SystemInfo; import com.nutomic.syncthingandroid.model.SystemInfo;
import com.nutomic.syncthingandroid.model.SystemVersion; import com.nutomic.syncthingandroid.model.SystemVersion;
import com.nutomic.syncthingandroid.service.Constants; import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.service.RestApi; import com.nutomic.syncthingandroid.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Util; import com.nutomic.syncthingandroid.util.Util;
@ -111,7 +112,7 @@ public class DrawerFragment extends Fragment implements View.OnClickListener {
} }
private void updateExitButtonVisibility() { private void updateExitButtonVisibility() {
boolean alwaysInBackground = SyncthingService.alwaysRunInBackground(getActivity()); boolean alwaysInBackground = DeviceStateHolder.alwaysRunInBackground(getActivity());
mExitButton.setVisibility(alwaysInBackground ? View.GONE : View.VISIBLE); mExitButton.setVisibility(alwaysInBackground ? View.GONE : View.VISIBLE);
} }

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import com.nutomic.syncthingandroid.SyncthingApp; import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.service.NotificationHandler; import com.nutomic.syncthingandroid.service.NotificationHandler;
import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingService;
@ -40,7 +41,7 @@ public class AppConfigReceiver extends BroadcastReceiver {
break; break;
case ACTION_STOP: case ACTION_STOP:
if (SyncthingService.alwaysRunInBackground(context)) { if (DeviceStateHolder.alwaysRunInBackground(context)) {
mNotificationHandler.showStopSyncthingWarningNotification(); mNotificationHandler.showStopSyncthingWarningNotification();
} else { } else {
context.stopService(new Intent(context, SyncthingService.class)); context.stopService(new Intent(context, SyncthingService.class));

View file

@ -21,7 +21,7 @@ public class BatteryReceiver extends BroadcastReceiver {
&& !Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) && !Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction()))
return; return;
if (!SyncthingService.alwaysRunInBackground(context)) if (!DeviceStateHolder.alwaysRunInBackground(context))
return; return;
boolean isCharging = Intent.ACTION_POWER_CONNECTED.equals(intent.getAction()); boolean isCharging = Intent.ACTION_POWER_CONNECTED.equals(intent.getAction());

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import com.nutomic.syncthingandroid.service.DeviceStateHolder;
import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingService;
public class BootReceiver extends BroadcastReceiver { public class BootReceiver extends BroadcastReceiver {
@ -15,7 +16,7 @@ public class BootReceiver extends BroadcastReceiver {
!intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) !intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED))
return; return;
if (!SyncthingService.alwaysRunInBackground(context)) if (!DeviceStateHolder.alwaysRunInBackground(context))
return; return;
startServiceCompat(context); startServiceCompat(context);
@ -27,6 +28,11 @@ public class BootReceiver extends BroadcastReceiver {
* https://stackoverflow.com/a/44505719/1837158 * https://stackoverflow.com/a/44505719/1837158
*/ */
public static void startServiceCompat(Context context) { 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); Intent intent = new Intent(context, SyncthingService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent); context.startForegroundService(intent);

View file

@ -22,7 +22,7 @@ public class NetworkReceiver extends BroadcastReceiver {
if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) if (!ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
return; return;
if (!SyncthingService.alwaysRunInBackground(context)) if (!DeviceStateHolder.alwaysRunInBackground(context))
return; return;
updateNetworkStatus(context); updateNetworkStatus(context);

View file

@ -7,6 +7,7 @@ import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
@ -49,6 +50,14 @@ public class DeviceStateHolder {
public static final String EXTRA_IS_POWER_SAVING = public static final String EXTRA_IS_POWER_SAVING =
"com.nutomic.syncthingandroid.syncthing.DeviceStateHolder.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 { public interface OnDeviceStateChangedListener {
void onDeviceStateChanged(); void onDeviceStateChanged();
} }
@ -114,7 +123,7 @@ public class DeviceStateHolder {
if (prefRespectPowerSaving && mIsPowerSaving) if (prefRespectPowerSaving && mIsPowerSaving)
return false; return false;
if (SyncthingService.alwaysRunInBackground(mContext)) { if (alwaysRunInBackground(mContext)) {
boolean prefStopMobileData = mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_WIFI, false); boolean prefStopMobileData = mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_WIFI, false);
boolean prefStopNotCharging = mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_CHARGING, false); boolean prefStopNotCharging = mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_CHARGING, false);

View file

@ -46,7 +46,7 @@ public class NotificationHandler {
// Android 8 does not allow starting service from background unless it's a foreground // 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. // service, so if "always run in background" is enabled, we have to use a foreground service.
// https://stackoverflow.com/a/44505719/1837158 // 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; foreground = true;
} }
@ -87,7 +87,7 @@ public class NotificationHandler {
} }
public void cancelPersistentNotification(SyncthingService service) { 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; return;
service.stopForeground(false); service.stopForeground(false);

View file

@ -2,7 +2,6 @@ package com.nutomic.syncthingandroid.service;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -11,7 +10,6 @@ import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@ -417,14 +415,6 @@ public class SyncthingService extends Service implements
return mConfig.getWebGuiUrl(); 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() { public State getCurrentState() {
return mCurrentState; return mCurrentState;
} }