1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-11 04:25:53 +00:00

Add run condition "Respect Android Auto-sync data quick toggle" (fixes #588)

This commit is contained in:
Catfriend1 2018-07-25 16:33:53 +02:00
parent da77d3bb06
commit 50ea1e47de
5 changed files with 38 additions and 3 deletions

View file

@ -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;

View file

@ -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";

View file

@ -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");

View file

@ -327,6 +327,9 @@ Please report any problems you encounter via Github.</string>
<string name="respect_battery_saving_title">Respect Android battery saving setting</string>
<string name="respect_battery_saving_summary">Disable Syncthing if battery saving is active</string>
<string name="respect_master_sync_title">Respect Android \'Auto-sync data\' setting</string>
<string name="respect_master_sync_summary">Disable Syncthing when the quick settings tile \'Auto-sync data\' is toggled off.</string>
<string name="run_in_flight_mode_title">Run when device is in flight mode</string>
<string name="run_in_flight_mode_summary">Enable if your phone has problems detecting manually connected wifi during flight mode.</string>

View file

@ -49,6 +49,12 @@
android:summary="@string/respect_battery_saving_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="respect_master_sync"
android:title="@string/respect_master_sync_title"
android:summary="@string/respect_master_sync_summary"
android:defaultValue="false" />
<CheckBoxPreference
android:key="run_in_flight_mode"
android:title="@string/run_in_flight_mode_title"