1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-22 20:31:16 +00:00

Simplify DeviceStateHolder code

This commit is contained in:
Felix Ableitner 2017-09-27 08:53:21 +09:00
parent 5c97251ffa
commit dca4c32723

View file

@ -2,7 +2,6 @@ package com.nutomic.syncthingandroid.service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
@ -70,11 +69,7 @@ public class DeviceStateHolder {
Log.i(TAG, "State updated, allowed network connection: " + mIsAllowedNetworkConnection +
", charging: " + mIsCharging);
if (mIsAllowedNetworkConnection) {
updateWifiSsid();
} else {
mWifiSsid = null;
}
updateWifiSsid();
}
private void updateWifiSsid() {
@ -88,10 +83,6 @@ public class DeviceStateHolder {
}
}
private String getWifiSsid() {
return mWifiSsid;
}
/**
* Determines if Syncthing should currently run.
*/
@ -108,14 +99,14 @@ public class DeviceStateHolder {
boolean prefStopNotCharging = mPreferences.getBoolean(SyncthingService.PREF_SYNC_ONLY_CHARGING, false);
return (isCharging() || !prefStopNotCharging) &&
(!prefStopMobileData || isAllowedWifiConnected());
(!prefStopMobileData || isWhitelistedNetworkConnection());
}
else {
return true;
}
}
private boolean isAllowedWifiConnected() {
private boolean isWhitelistedNetworkConnection() {
boolean wifiConnected = isAllowedNetworkConnection();
if (wifiConnected) {
Set<String> ssids = mPreferences.getStringSet(SyncthingService.PREF_SYNC_ONLY_WIFI_SSIDS, new HashSet<>());
@ -123,17 +114,11 @@ public class DeviceStateHolder {
Log.d(TAG, "All SSIDs allowed for syncing");
return true;
} else {
String ssid = getWifiSsid();
if (ssid != null) {
if (ssids.contains(ssid)) {
Log.d(TAG, "SSID [" + ssid + "] found in whitelist: " + ssids);
return true;
}
Log.i(TAG, "SSID [" + ssid + "] not whitelisted: " + ssids);
return false;
if (mWifiSsid != null && ssids.contains(mWifiSsid)) {
Log.d(TAG, "SSID [" + mWifiSsid + "] found in whitelist: " + ssids);
return true;
} else {
// Don't know the SSID (yet) (should not happen?!), so not allowing
Log.w(TAG, "SSID unknown (yet), cannot check SSID whitelist. Disallowing sync.");
Log.w(TAG, "SSID [" + mWifiSsid + "] unknown or not whitelisted, disallowing sync.");
return false;
}
}