diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers/AccessPointInfo.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/helpers/AccessPointInfo.java | 173 |
1 files changed, 159 insertions, 14 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/AccessPointInfo.java b/app/src/fil/libre/repwifiapp/helpers/AccessPointInfo.java index d6e2eb3..eee569d 100644 --- a/app/src/fil/libre/repwifiapp/helpers/AccessPointInfo.java +++ b/app/src/fil/libre/repwifiapp/helpers/AccessPointInfo.java @@ -20,25 +20,38 @@ package fil.libre.repwifiapp.helpers; -import java.io.File; +import org.json.JSONException; +import org.json.JSONObject; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; -import fil.libre.repwifiapp.Commons; public class AccessPointInfo implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 2L; private static final int MAX_SSID_LENGTH = 32; - + protected static final String SCAN_FILE_HDR = "bssid / frequency / signal level / flags / ssid"; + + private static final String JSONKEY_BSSID = "BSSID"; + private static final String JSONKEY_SSID = "SSID"; + private static final String JSONKEY_LASTUSED = "LastUsed"; + private static final String JSONKEY_AUTH = "Auth"; + private static final String JSONKEY_PSK = "PSK"; + private static final String JSONKEY_VPN_PROFILE = "VpnProfile"; + private static final String JSONKEY_DHCPSETS = "DhcpSettings"; + private String _ssid; private String _bssid; private String _auth; private String _level; private String _freq; private String _password; + private String _vpnProfileName = null; private boolean _isHidden = false; private long _lastTimeUsed; + private DhcpSettings _dhcpsets; public AccessPointInfo(String ssid, String bssid, String authType, String level, String freq) { @@ -50,6 +63,23 @@ public class AccessPointInfo implements Serializable { } + private AccessPointInfo(){ + // for inner use; + } + + public void setDhcpConfiguration(DhcpSettings sets) { + this._dhcpsets = sets; + } + + public DhcpSettings getDhcpConfiguration() { + + if (this._dhcpsets == null) { + return DhcpSettings.getDefault(); + } else { + return this._dhcpsets; + } + } + public String getSsid() { return this._ssid; } @@ -78,12 +108,42 @@ public class AccessPointInfo implements Serializable { this._bssid = bssid; } + public String getVpnProfileName(){ + if (_vpnProfileName == null){ + return ""; + } else { + return _vpnProfileName; + } + } + + public void setVpnProfileName(String profileName){ + _vpnProfileName = profileName; + } + public String getAuthType() { + if (_auth == null){ + return ""; + } return this._auth; } - public String getSignlalStrength() { - return this._level; + public int getSignlalStrength() { + // return this._level; + + if (this._level == null || this._level.isEmpty()) { + return 0; + } + + int retval = 0; + + try { + retval = Integer.parseInt(this._level); + } catch (NumberFormatException e) { + retval = 0; + } + + return retval; + } public String getFrequency() { @@ -116,6 +176,9 @@ public class AccessPointInfo implements Serializable { } public String getPassword() { + if (_password == null){ + return ""; + } return this._password; } @@ -170,19 +233,18 @@ public class AccessPointInfo implements Serializable { } - public static AccessPointInfo[] parseScanResult(String scanResultFile) { + public static AccessPointInfo[] parseScanResult(String scanResultContent) { try { - Utils.logDebug("AccesPointInfo trying to parse file: " + scanResultFile); - - File f = new File(scanResultFile); - if (!f.exists()) { - Utils.logError("AccessPointInfo.parseScanResult(): The provided scan result file doesn't exist"); + if (scanResultContent == null) { return null; } - String[] lines = Utils.readFileLines(scanResultFile); + Utils.logDebug("AccesPointInfo trying to parse file scan content:\n" + + scanResultContent); + + String[] lines = scanResultContent.split("\n"); List<AccessPointInfo> nets = new ArrayList<AccessPointInfo>(); if (lines == null) { @@ -190,7 +252,7 @@ public class AccessPointInfo implements Serializable { } for (String l : lines) { - if (l.startsWith(Commons.SCAN_FILE_HDR)) { + if (l.startsWith(SCAN_FILE_HDR)) { // strip off the header continue; } @@ -211,6 +273,8 @@ public class AccessPointInfo implements Serializable { } + sortInfosBySignalStrength(nets); + AccessPointInfo[] a = new AccessPointInfo[nets.size()]; a = nets.toArray(a); return a; @@ -222,4 +286,85 @@ public class AccessPointInfo implements Serializable { } + public JSONObject toJson(){ + + try { + + JSONObject j = new JSONObject(); + + j.put(JSONKEY_BSSID, getBssid()); + j.put(JSONKEY_SSID, getSsid()); + j.put(JSONKEY_PSK, getPassword()); + j.put(JSONKEY_AUTH, getAuthType()); + j.put(JSONKEY_LASTUSED, getLastTimeUsed()); + j.put(JSONKEY_VPN_PROFILE, getVpnProfileName()); + + DhcpSettings sets = getDhcpConfiguration(); + if (sets != null){ + JSONObject dhcpj = sets.toJson(); + if (dhcpj != null){ + j.put(JSONKEY_DHCPSETS, dhcpj); + } + + } + + return j; + + } catch (JSONException e) { + Utils.logError("Exception while converting AccessPointInfo to JSON.", e); + return null; + } + + } + + public static AccessPointInfo fromJsonObject(JSONObject json){ + + if (json == null || json.isNull(JSONKEY_BSSID) || json.isNull(JSONKEY_SSID)){ + return null; + } + + AccessPointInfo info = new AccessPointInfo(); + + try { + info._bssid = json.getString(JSONKEY_BSSID); + info._ssid = json.getString(JSONKEY_SSID); + info._auth = json.getString(JSONKEY_AUTH); + info._lastTimeUsed = json.getLong(JSONKEY_LASTUSED); + + if (json.has(JSONKEY_PSK) && ! json.isNull(JSONKEY_PSK)){ + info._password = json.getString(JSONKEY_PSK); + } + + if ( json.has(JSONKEY_VPN_PROFILE) && ! json.isNull(JSONKEY_VPN_PROFILE)){ + info._vpnProfileName = json.getString(JSONKEY_VPN_PROFILE); + } + + if (json.has(JSONKEY_DHCPSETS) && ! json.isNull(JSONKEY_DHCPSETS)){ + info._dhcpsets = DhcpSettings.fromJsonObject(json.getJSONObject(JSONKEY_DHCPSETS)); + } + + return info; + + } catch (JSONException e) { + Utils.logError("Exception while parsing json object to AccessPointInfo", e); + return null; + } + + + + + } + + private static void sortInfosBySignalStrength(List<AccessPointInfo> toSort) { + + Collections.sort(toSort, new Comparator<AccessPointInfo>() { + public int compare(AccessPointInfo o1, AccessPointInfo o2) { + if (o1.getSignlalStrength() == o2.getSignlalStrength()) + return 0; + return o1.getSignlalStrength() < o2.getSignlalStrength() ? -1 : 1; + } + }); + + } + } |