1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-29 15:51:17 +00:00

Tell user if location is disabled but "run on selected" WiFi is enabled (fixes #16) (#87)

* Tell user if location is disabled but "run on selected" WiFi is enabled.

* Update status tab while user looks at the UI and syncthing is disabled

* UI: Offer a solution
This commit is contained in:
Catfriend1 2018-10-10 02:16:38 +02:00 committed by GitHub
parent 12bc08c6dd
commit 28040869e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 26 deletions

View file

@ -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}.
}
/**

View file

@ -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<String> whitelistedWifiSsids) {
private boolean wifiWhitelistConditionMet (boolean prefWifiWhitelistEnabled,
Set<String> whitelistedWifiSsids) throws LocationUnavailableException {
if (!prefWifiWhitelistEnabled) {
Log.v(TAG, "handleWifiWhitelist: !prefWifiWhitelistEnabled");
return true;
@ -403,7 +407,8 @@ public class RunConditionMonitor {
}
}
private boolean isWifiConnectionWhitelisted(Set<String> whitelistedSsids) {
private boolean isWifiConnectionWhitelisted(Set<String> 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("<unknown ssid>")) {
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);
}
}
}

View file

@ -634,6 +634,7 @@ Please report any problems you encounter via Github.</string>
<string name="reason_not_on_whitelisted_wifi">Syncthing is not running as the current WiFi network\'s name is not whitelisted.</string>
<string name="reason_on_nonmetered_wifi">Syncthing is allowed to run on non-metered WiFi connections. The active WiFi connection is non-metered.</string>
<string name="reason_not_nonmetered_wifi">Syncthing is not running as you disallowed it to run on metered WiFi connections.</string>
<string name="reason_location_unavailable">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.</string>
<string name="reason_on_flight_mode">Syncthing is running as you allowed it to run when flight mode is active.</string>
<!-- SyncthingService -->