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