diff --git a/app/src/main/java/com/nutomic/syncthingandroid/fragments/StatusFragment.java b/app/src/main/java/com/nutomic/syncthingandroid/fragments/StatusFragment.java index 6fa52383..3d20709f 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/fragments/StatusFragment.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/fragments/StatusFragment.java @@ -226,9 +226,6 @@ public class StatusFragment extends ListFragment implements SyncthingService.OnS * while the user is looking at the current tab. */ private void onTimerEvent() { - if (mServiceState != SyncthingService.State.ACTIVE) { - return; - } MainActivity mainActivity = (MainActivity) getActivity(); if (mainActivity == null) { return; @@ -236,6 +233,10 @@ public class StatusFragment extends ListFragment implements SyncthingService.OnS if (mainActivity.isFinishing()) { return; } + if (mServiceState != SyncthingService.State.ACTIVE) { + updateStatus(); + return; + } RestApi restApi = mainActivity.getApi(); if (restApi == null) { return; @@ -243,6 +244,7 @@ public class StatusFragment extends ListFragment implements SyncthingService.OnS Log.v(TAG, "Invoking REST status queries"); restApi.getSystemStatus(this::onReceiveSystemStatus); restApi.getConnections(this::onReceiveConnections); + // onReceiveSystemStatus, onReceiveConnections will call {@link #updateStatus}. } /** 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 21fd66ee..4309508d 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java @@ -227,30 +227,34 @@ public class RunConditionMonitor { // Run on wifi. if (prefRunOnWifi) { if (isWifiOrEthernetConnection()) { - mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_wifi); - if (prefRunOnMeteredWifi) { - mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_metered_nonmetered_wifi); - // We are on non-metered or metered wifi. Check if wifi whitelist run condition is met. - if (wifiWhitelistConditionMet(prefWifiWhitelistEnabled, whitelistedWifiSsids)) { - Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && prefRunOnMeteredWifi && wifiWhitelistConditionMet"); - mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_whitelisted_wifi); - return true; - } - mRunDecisionExplanation += "\n" + res.getString(R.string.reason_not_on_whitelisted_wifi); - } else { - // Check if we are on a non-metered wifi. - if (!isMeteredNetworkConnection()) { - mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_nonmetered_wifi); - // Check if wifi whitelist run condition is met. + try { + mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_wifi); + if (prefRunOnMeteredWifi) { + mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_metered_nonmetered_wifi); + // We are on non-metered or metered wifi. Check if wifi whitelist run condition is met. if (wifiWhitelistConditionMet(prefWifiWhitelistEnabled, whitelistedWifiSsids)) { - Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && !prefRunOnMeteredWifi && !isMeteredNetworkConnection && wifiWhitelistConditionMet"); + Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && prefRunOnMeteredWifi && wifiWhitelistConditionMet"); mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_whitelisted_wifi); return true; } mRunDecisionExplanation += "\n" + res.getString(R.string.reason_not_on_whitelisted_wifi); } else { - mRunDecisionExplanation += "\n" + res.getString(R.string.reason_not_nonmetered_wifi); + // Check if we are on a non-metered wifi. + if (!isMeteredNetworkConnection()) { + mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_nonmetered_wifi); + // Check if wifi whitelist run condition is met. + if (wifiWhitelistConditionMet(prefWifiWhitelistEnabled, whitelistedWifiSsids)) { + Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && !prefRunOnMeteredWifi && !isMeteredNetworkConnection && wifiWhitelistConditionMet"); + mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_whitelisted_wifi); + return true; + } + mRunDecisionExplanation += "\n" + res.getString(R.string.reason_not_on_whitelisted_wifi); + } else { + mRunDecisionExplanation += "\n" + res.getString(R.string.reason_not_nonmetered_wifi); + } } + } catch (LocationUnavailableException e) { + mRunDecisionExplanation += "\n" + res.getString(R.string.reason_location_unavailable); } } else { mRunDecisionExplanation += "\n" + res.getString(R.string.reason_not_on_wifi); @@ -283,8 +287,8 @@ public class RunConditionMonitor { * Return whether the wifi whitelist run condition is met. * Precondition: An active wifi connection has been detected. */ - private boolean wifiWhitelistConditionMet(boolean prefWifiWhitelistEnabled, - Set whitelistedWifiSsids) { + private boolean wifiWhitelistConditionMet (boolean prefWifiWhitelistEnabled, + Set whitelistedWifiSsids) throws LocationUnavailableException { if (!prefWifiWhitelistEnabled) { Log.v(TAG, "handleWifiWhitelist: !prefWifiWhitelistEnabled"); return true; @@ -403,7 +407,8 @@ public class RunConditionMonitor { } } - private boolean isWifiConnectionWhitelisted(Set whitelistedSsids) { + private boolean isWifiConnectionWhitelisted(Set whitelistedSsids) + throws LocationUnavailableException{ WifiManager wifiManager = (WifiManager) mContext.getApplicationContext() .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); @@ -413,11 +418,22 @@ public class RunConditionMonitor { return false; } String wifiSsid = wifiInfo.getSSID(); - if (wifiSsid == null) { - Log.w(TAG, "isWifiConnectionWhitelisted: Got null SSID. Try to enable android location service."); - return false; + if (wifiSsid == null || wifiSsid.equals("")) { + throw new LocationUnavailableException("isWifiConnectionWhitelisted: Got null SSID. Try to enable android location service."); } return whitelistedSsids.contains(wifiSsid); } + public class LocationUnavailableException extends Exception { + + public LocationUnavailableException(String message) { + super(message); + } + + public LocationUnavailableException(String message, Throwable throwable) { + super(message, throwable); + } + + } + } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ba9e2886..942e13f3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -634,6 +634,7 @@ Please report any problems you encounter via Github. Syncthing is not running as the current WiFi network\'s name is not whitelisted. Syncthing is allowed to run on non-metered WiFi connections. The active WiFi connection is non-metered. Syncthing is not running as you disallowed it to run on metered WiFi connections. + You enabled \'Run on selected WiFi networks\' in the settings. Android location services are currently turned off. According to Android restrictions, Syncthing cannot determine the current WiFi network name to decide if it should run. Solution: Turn location on and reconnect WiFi. Syncthing is running as you allowed it to run when flight mode is active.