From 50ea1e47de609de6f00c02ab1c2bdb2e379a3eb5 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Wed, 25 Jul 2018 16:33:53 +0200 Subject: [PATCH] Add run condition "Respect Android Auto-sync data quick toggle" (fixes #588) --- .../activities/SettingsActivity.java | 2 +- .../syncthingandroid/service/Constants.java | 6 ++++- .../service/RunConditionMonitor.java | 24 ++++++++++++++++++- app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/xml/app_settings.xml | 6 +++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 0512a309..80614397 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -104,8 +104,8 @@ public class SettingsActivity extends SyncthingActivity { private CheckBoxPreference mRunOnMobileData; private CheckBoxPreference mRunOnWifi; private CheckBoxPreference mRunOnMeteredWifi; - private CheckBoxPreference mRunInFlightMode; private WifiSsidPreference mWifiSsidWhitelist; + private CheckBoxPreference mRunInFlightMode; private Preference mCategorySyncthingOptions; private EditTextPreference mDeviceName; diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java b/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java index 76aeec15..32e53d18 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java @@ -10,14 +10,18 @@ public class Constants { public static final String FILENAME_SYNCTHING_BINARY = "libsyncthing.so"; + // Preferences - Run conditions public static final String PREF_ALWAYS_RUN_IN_BACKGROUND = "always_run_in_background"; public static final String PREF_RUN_ON_MOBILE_DATA = "run_on_mobile_data"; public static final String PREF_RUN_ON_WIFI = "run_on_wifi"; public static final String PREF_RUN_ON_METERED_WIFI = "run_on_metered_wifi"; public static final String PREF_WIFI_SSID_WHITELIST = "wifi_ssid_whitelist"; public static final String PREF_POWER_SOURCE = "power_source"; - public static final String PREF_RUN_IN_FLIGHT_MODE = "run_in_flight_mode"; public static final String PREF_RESPECT_BATTERY_SAVING = "respect_battery_saving"; + public static final String PREF_RESPECT_MASTER_SYNC = "respect_master_sync"; + public static final String PREF_RUN_IN_FLIGHT_MODE = "run_in_flight_mode"; + + // Preferences - Behaviour public static final String PREF_USE_ROOT = "use_root"; public static final String PREF_NOTIFICATION_TYPE = "notification_type"; public static final String PREF_ENVIRONMENT_VARIABLES = "environment_variables"; diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java index 4baef653..3a9f3b82 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java @@ -2,10 +2,12 @@ package com.nutomic.syncthingandroid.service; import android.annotation.TargetApi; import android.content.BroadcastReceiver; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.content.SyncStatusObserver; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; @@ -42,6 +44,13 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference private static final String POWER_SOURCE_AC = "ac_power"; private static final String POWER_SOURCE_BATTERY = "battery_power"; + private final SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() { + @Override + public void onStatusChanged(int i) { + updateShouldRunDecision(); + } + }; + public interface OnRunConditionChangedListener { void onRunConditionChanged(boolean shouldRun); } @@ -86,6 +95,10 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); } + // SyncStatusObserver to monitor android's "AutoSync" quick toggle. + ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, + mSyncStatusObserver); + // Initially determine if syncthing should run under current circumstances. updateShouldRunDecision(); } @@ -93,6 +106,7 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference public void shutdown() { Log.v(TAG, "Shutting down"); mPreferences.unregisterOnSharedPreferenceChangeListener(this); + ContentResolver.removeStatusChangeListener(mSyncStatusObserver); mReceiverManager.unregisterAllReceivers(mContext); } @@ -105,7 +119,8 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference Constants.PREF_WIFI_SSID_WHITELIST, Constants.PREF_POWER_SOURCE, Constants.PREF_RUN_IN_FLIGHT_MODE, - Constants.PREF_RESPECT_BATTERY_SAVING + Constants.PREF_RESPECT_BATTERY_SAVING, + Constants.PREF_RESPECT_MASTER_SYNC ); if (watched.contains(key)) { // Force a re-evaluation of which run conditions apply according to the changed prefs. @@ -166,6 +181,7 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference boolean prefRunInFlightMode = mPreferences.getBoolean(Constants.PREF_RUN_IN_FLIGHT_MODE, false); String prefPowerSource = mPreferences.getString(Constants.PREF_POWER_SOURCE, POWER_SOURCE_AC_BATTERY); boolean prefRespectPowerSaving = mPreferences.getBoolean(Constants.PREF_RESPECT_BATTERY_SAVING, true); + boolean prefRespectMasterSync = mPreferences.getBoolean(Constants.PREF_RESPECT_MASTER_SYNC, false); boolean prefAlwaysRunInBackground = mPreferences.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false); // PREF_POWER_SOURCE @@ -195,6 +211,12 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference } } + // Android global AutoSync setting. + if (prefRespectMasterSync && !ContentResolver.getMasterSyncAutomatically()) { + Log.v(TAG, "decideShouldRun: prefRespectMasterSync && !getMasterSyncAutomatically"); + return false; + } + // Run on mobile data. if (prefRunOnMobileData && isMobileDataConnection()) { Log.v(TAG, "decideShouldRun: prefRunOnMobileData && isMobileDataConnection"); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a9f88e37..bcdb5249 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -327,6 +327,9 @@ Please report any problems you encounter via Github. Respect Android battery saving setting Disable Syncthing if battery saving is active + Respect Android \'Auto-sync data\' setting + Disable Syncthing when the quick settings tile \'Auto-sync data\' is toggled off. + Run when device is in flight mode Enable if your phone has problems detecting manually connected wifi during flight mode. diff --git a/app/src/main/res/xml/app_settings.xml b/app/src/main/res/xml/app_settings.xml index 8f880860..ab4630b1 100644 --- a/app/src/main/res/xml/app_settings.xml +++ b/app/src/main/res/xml/app_settings.xml @@ -49,6 +49,12 @@ android:summary="@string/respect_battery_saving_summary" android:defaultValue="true" /> + +