aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers/NetworkManager.java')
-rw-r--r--app/src/fil/libre/repwifiapp/helpers/NetworkManager.java170
1 files changed, 123 insertions, 47 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java b/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
index db27f7e..96805a6 100644
--- a/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
+++ b/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
@@ -20,13 +20,16 @@
package fil.libre.repwifiapp.helpers;
+import org.json.JSONArray;
+import org.json.JSONObject;
import java.io.File;
import java.util.ArrayList;
public class NetworkManager {
+ private static final String VERSION_NOTE = "RepWifiStorageVersion: 2.0\n";
private static final String F_SEP = "\t";
- private static final int NET_MAX_AGE = 365; // Expressed in days
+ private static final int NET_MAX_AGE = 1095; // Expressed in days
private String _knownNetworksFile = null;
@@ -34,7 +37,7 @@ public class NetworkManager {
this._knownNetworksFile = networksFilePath;
}
- private AccessPointInfo searchInFile(AccessPointInfo i) {
+ private AccessPointInfo getSavedInfo(AccessPointInfo i) {
if (i == null) {
return null;
@@ -63,12 +66,16 @@ public class NetworkManager {
// then return the best match (only ssid), if any
if (toTest.getSsid().equals(ssid)) {
+ i.setPassword(toTest.getPassword());
+ i.setDhcpConfiguration(toTest.getDhcpConfiguration());
+ i.setVpnProfileName(toTest.getVpnProfileName());
+
if (toTest.getBssid().equals(bssid)) {
- i.setPassword(toTest.getPassword());
+ // complete match, return.
return i;
} else {
- i.setPassword(toTest.getPassword());
+ // probable match
ret = i;
}
@@ -82,11 +89,6 @@ public class NetworkManager {
private boolean saveOrRemove(AccessPointInfo info, boolean save) {
- String iText = InfoToString(info);
- if (iText == null) {
- return false;
- }
-
AccessPointInfo[] existingNets = getKnownNetworks();
ArrayList<AccessPointInfo> newlist = new ArrayList<AccessPointInfo>();
@@ -151,7 +153,7 @@ public class NetworkManager {
}
- private AccessPointInfo getFromString(String savedString) {
+ private AccessPointInfo getFromStringOld(String savedString) {
if (savedString == null || savedString.trim().equals("")) {
return null;
@@ -168,11 +170,25 @@ public class NetworkManager {
String pass = fields[2];
String lastUsed = fields[3];
String auth = null;
+ String ipWmask = null;
+ String gw = null;
+ boolean useDhcp = true;
+ String vpnProfile = null;
if (fields.length > 4) {
auth = fields[4];
}
+ if (fields.length > 6) {
+ ipWmask = fields[5];
+ gw = fields[6];
+ useDhcp = false;
+ }
+
+ if (fields.length > 7) {
+ vpnProfile = fields[7];
+ }
+
long lastusedmillis = 0;
try {
lastusedmillis = Long.parseLong(lastUsed);
@@ -189,69 +205,129 @@ public class NetworkManager {
AccessPointInfo i = new AccessPointInfo(ssid, bssid, auth, null, null);
i.setPassword(pass);
i.setLastTimeUsed(lastusedmillis);
+ i.setVpnProfileName(vpnProfile);
+
+ if (!useDhcp) {
+ DhcpSettings s = DhcpSettings.parseSavedSettings(ipWmask, gw);
+ i.setDhcpConfiguration(s);
+ }
return i;
}
- private String InfoToString(AccessPointInfo info) {
+ private boolean saveList(AccessPointInfo[] list) {
- if (info == null) {
- return null;
- }
+ try {
- String bssid = info.getBssid();
- String ssid = info.getSsid();
- String pass = info.getPassword();
- String tsLastUsed = "" + info.getLastTimeUsed();
- String auth = info.getAuthType();
+ JSONArray jarr = new JSONArray();
+ for (AccessPointInfo i : list) {
- if (bssid == null || bssid.trim().equals("")) {
- return null;
- }
+ JSONObject jo = i.toJson();
+ if (jo == null) {
+ return false;
+ }
- if (ssid == null || ssid.trim().equals("")) {
- return null;
- }
+ jarr.put(jo);
- if (pass == null || pass.trim().equals("")) {
- return null;
- }
+ }
+
+ StringBuilder sb = new StringBuilder();
+ sb.append(VERSION_NOTE);
+ sb.append("\n");
+
+ sb.append(jarr.toString(2));
+
+ return Utils.writeFile(_knownNetworksFile, sb.toString(), true);
- if (auth == null) {
- auth = "";
+ } catch (Exception e) {
+ Utils.logError("Exception while saving AccessPointInfo array to JSON-formatted file.",
+ e);
+ return false;
}
- String iText = info.getBssid() + F_SEP + info.getSsid() + F_SEP + info.getPassword()
- + F_SEP + tsLastUsed + F_SEP + auth;
- return iText;
+ /*
+ * if (list == null) { return false; }
+ *
+ * String[] lines = new String[list.length];
+ *
+ * for (int i = 0; i < list.length; i++) {
+ *
+ * String storeString = InfoToString(list[i]); if (storeString == null)
+ * { return false; } lines[i] = storeString;
+ *
+ * }
+ *
+ * return Utils.writeFileLines(this._knownNetworksFile, lines, true);
+ */
}
- private boolean saveList(AccessPointInfo[] list) {
+ public boolean updateStorageVersion() {
- if (list == null) {
- return false;
+ String[] lines = Utils.readFileLines(_knownNetworksFile);
+ if (lines.length == 0) {
+ return true;
}
- String[] lines = new String[list.length];
+ if (lines[0].trim().equals(VERSION_NOTE)) {
+ return true;
- for (int i = 0; i < list.length; i++) {
+ } else {
- String storeString = InfoToString(list[i]);
- if (storeString == null) {
- return false;
+ AccessPointInfo[] infos = getKnownNetworksOld();
+ if (infos == null || infos.length == 0) {
+ return true;
}
- lines[i] = storeString;
+ return saveList(infos);
}
- return Utils.writeFileLines(this._knownNetworksFile, lines, true);
-
}
public AccessPointInfo[] getKnownNetworks() {
+ try {
+
+ String fconts = Utils.readFile(_knownNetworksFile);
+ if (fconts == null) {
+ return null;
+ }
+
+ if (!fconts.startsWith(VERSION_NOTE)) {
+ // wrong version, try to convert it
+ if (updateStorageVersion()) {
+ return getKnownNetworks();
+ } else {
+ return null;
+ }
+ }
+
+ JSONArray jarr = new JSONArray(fconts.replace(VERSION_NOTE, ""));
+ ArrayList<AccessPointInfo> list = new ArrayList<AccessPointInfo>();
+
+ int count = 0;
+ for (int i = 0; i < jarr.length(); i++) {
+ AccessPointInfo info = AccessPointInfo.fromJsonObject(jarr.getJSONObject(i));
+ if (info == null) {
+ continue;
+ }
+ count += 1;
+ list.add(info);
+ }
+
+ AccessPointInfo[] arr = new AccessPointInfo[count];
+ return list.toArray(arr);
+
+ } catch (Exception e) {
+ Utils.logError("Exception while parsing JSON content from storage file.", e);
+ return null;
+ }
+
+ }
+
+ public AccessPointInfo[] getKnownNetworksOld() {
+
ArrayList<AccessPointInfo> list = new ArrayList<AccessPointInfo>();
File f = new File(this._knownNetworksFile);
@@ -266,7 +342,7 @@ public class NetworkManager {
for (String l : lines) {
- AccessPointInfo info = getFromString(l);
+ AccessPointInfo info = getFromStringOld(l);
if (info != null) {
list.add(info);
}
@@ -282,7 +358,7 @@ public class NetworkManager {
public boolean isKnown(AccessPointInfo info) {
- AccessPointInfo i = searchInFile(info);
+ AccessPointInfo i = getSavedInfo(info);
if (i == null) {
return false;
} else {
@@ -300,7 +376,7 @@ public class NetworkManager {
}
public AccessPointInfo getSavedNetwork(AccessPointInfo i) {
- return searchInFile(i);
+ return getSavedInfo(i);
}
}