|
| 1 | +package tk.superl2.xwifi; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | +import android.util.Xml; |
| 5 | +import org.xmlpull.v1.XmlPullParser; |
| 6 | +import org.xmlpull.v1.XmlPullParserException; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.OutputStream; |
| 10 | +import java.util.ArrayList; |
| 11 | + |
| 12 | +public class WifiEntryLoaderJava { |
| 13 | + private static final String mOreoLocation = "/data/misc/wifi/WifiConfigStore.xml"; |
| 14 | + |
| 15 | + private static ArrayList<WifiEntry> readNetworkList(XmlPullParser parser) throws XmlPullParserException, IOException { |
| 16 | + ArrayList<WifiEntry> result = new ArrayList<>(); |
| 17 | + parser.require(XmlPullParser.START_TAG, null, "NetworkList"); |
| 18 | + boolean doLoop = true; |
| 19 | + while (doLoop) { |
| 20 | + try { |
| 21 | + parser.next(); |
| 22 | + String tagName = parser.getName(); |
| 23 | + if (tagName == null) { |
| 24 | + tagName = ""; |
| 25 | + } |
| 26 | + doLoop = (!tagName.equalsIgnoreCase("NetworkList")); |
| 27 | + if (parser.getEventType() != XmlPullParser.START_TAG) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + if (tagName.equals("Network")) { |
| 32 | + WifiEntry newWifi = readNetworkEntry(parser); |
| 33 | + if (newWifi.getTitle().length() != 0) { |
| 34 | + result.add(newWifi); |
| 35 | + } |
| 36 | + } else { |
| 37 | + skip(parser); |
| 38 | + } |
| 39 | + } catch (Exception e) { |
| 40 | + Log.e("LoadData.NetworkList", e.getMessage()); |
| 41 | + doLoop = false; |
| 42 | + } |
| 43 | + } |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + // Parses a "Network" entry |
| 48 | + private static WifiEntry readNetworkEntry(XmlPullParser parser) throws XmlPullParserException, IOException { |
| 49 | + parser.require(XmlPullParser.START_TAG, null, "Network"); |
| 50 | + WifiEntry result = new WifiEntry(); |
| 51 | + while (parser.next() != XmlPullParser.END_TAG) { |
| 52 | + if (parser.getEventType() != XmlPullParser.START_TAG) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + String tagName = parser.getName(); |
| 56 | + // Starts by looking for the entry tag |
| 57 | + if (tagName.equals("WifiConfiguration")) { |
| 58 | + result = readWiFiConfig(parser, result); |
| 59 | + } else if (tagName.equals("WifiEnterpriseConfiguration")) { |
| 60 | + result.type = WifiEntry.Type.ENTERPRISE; |
| 61 | + } else { |
| 62 | + skip(parser); |
| 63 | + } |
| 64 | + } |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + // Parses a "WifiConfiguration" entry |
| 69 | + private static WifiEntry readWiFiConfig(XmlPullParser parser, WifiEntry result) throws XmlPullParserException, IOException { |
| 70 | + try { |
| 71 | + while (parser.next() != XmlPullParser.END_TAG) { |
| 72 | + if (parser.getEventType() != XmlPullParser.START_TAG) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + String tagName = parser.getName(); |
| 76 | + String name = parser.getAttributeValue(null, "name"); |
| 77 | + if (name.equals("SSID") && !tagName.equalsIgnoreCase("null")) { |
| 78 | + result.setTitle(readTag(parser, tagName)); |
| 79 | + } else if (name.equals("PreSharedKey") && !tagName.equalsIgnoreCase("null")) { |
| 80 | + String newPwd = readTag(parser, tagName); |
| 81 | + if (newPwd.length() > 0) { |
| 82 | + result.setPassword(newPwd); |
| 83 | + result.type = WifiEntry.Type.WPA; |
| 84 | + } |
| 85 | + } else if (name.equals("WEPKeys") && !tagName.equalsIgnoreCase("null")) { |
| 86 | + result.type = WifiEntry.Type.WEP; |
| 87 | + if (tagName.equalsIgnoreCase("string-array")) { |
| 88 | + try { |
| 89 | + int numQty = Integer.parseInt(parser.getAttributeValue(null, "num")); |
| 90 | + int loopQty = 0; |
| 91 | + while ((parser.next() != XmlPullParser.END_DOCUMENT) && (loopQty < numQty)) { |
| 92 | + String innerTagName = parser.getName(); |
| 93 | + if ((innerTagName != null) && innerTagName.equalsIgnoreCase("item")) { |
| 94 | + loopQty ++; |
| 95 | + String newPwd = parser.getAttributeValue(null, "value"); |
| 96 | + if (newPwd.length() > 0) { |
| 97 | + result.setPassword(newPwd); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } catch (Exception error) { |
| 102 | + parser.getName(); |
| 103 | + } |
| 104 | + } else { |
| 105 | + String newPwd = readTag(parser, tagName); |
| 106 | + if (newPwd.length() > 0) { |
| 107 | + result.setPassword(readTag(parser, tagName)); |
| 108 | + } |
| 109 | + } |
| 110 | + } else { |
| 111 | + skip(parser); |
| 112 | + } |
| 113 | + } |
| 114 | + } catch (Exception error) { |
| 115 | + Log.e("LoadData.readWiFiConfig", error.getMessage() + "\n\nParser: " + parser.getText()); |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + // Return the text for a specified tag. |
| 121 | + private static String readTag(XmlPullParser parser, String tagName) throws IOException, XmlPullParserException { |
| 122 | + parser.require(XmlPullParser.START_TAG, null, tagName); |
| 123 | + String result = ""; |
| 124 | + if (parser.next() == XmlPullParser.TEXT) { |
| 125 | + result = parser.getText(); |
| 126 | + parser.nextTag(); |
| 127 | + } |
| 128 | + parser.require(XmlPullParser.END_TAG, null, tagName); |
| 129 | + if (tagName.equalsIgnoreCase("string") |
| 130 | + && Character.toString(result.charAt(0)).equals("\"")) { |
| 131 | + result = result.substring(1, result.length() - 1); |
| 132 | + } |
| 133 | + return result; |
| 134 | + } |
| 135 | + |
| 136 | + // Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e., |
| 137 | + // if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it |
| 138 | + // finds the matching END_TAG (as indicated by the value of "depth" being 0). |
| 139 | + private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException { |
| 140 | + if (parser.getEventType() != XmlPullParser.START_TAG) { |
| 141 | + throw new IllegalStateException(); |
| 142 | + } |
| 143 | + int depth = 1; |
| 144 | + while (depth != 0) { |
| 145 | + switch (parser.next()) { |
| 146 | + case XmlPullParser.END_TAG: |
| 147 | + depth--; |
| 148 | + break; |
| 149 | + case XmlPullParser.START_TAG: |
| 150 | + depth++; |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public static ArrayList<WifiEntry> readOreoFile() { |
| 157 | + ArrayList<WifiEntry> result = new ArrayList<>(); |
| 158 | + try { |
| 159 | + Process suOreoProcess = Runtime.getRuntime().exec("su"); |
| 160 | + OutputStream suOreoProcessOutputStream = suOreoProcess.getOutputStream(); |
| 161 | + suOreoProcessOutputStream.write(("/system/bin/cat "+ mOreoLocation +"\n").getBytes()); |
| 162 | + suOreoProcessOutputStream.write("exit\n".getBytes()); |
| 163 | + suOreoProcessOutputStream.flush(); |
| 164 | + suOreoProcessOutputStream.close(); |
| 165 | + try { |
| 166 | + suOreoProcess.waitFor(); |
| 167 | + } catch (InterruptedException e) { |
| 168 | + e.printStackTrace(); |
| 169 | + } |
| 170 | + XmlPullParser parser = Xml.newPullParser(); |
| 171 | + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); |
| 172 | + parser.setInput(suOreoProcess.getInputStream(), "UTF-8"); |
| 173 | + while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 174 | + if (parser.getEventType() != XmlPullParser.START_TAG) { |
| 175 | + continue; |
| 176 | + } |
| 177 | + if (parser.getName().equalsIgnoreCase("NetworkList")) { |
| 178 | + // Process the <Network> entries in the list |
| 179 | + result.addAll(readNetworkList(parser)); |
| 180 | + } |
| 181 | + } |
| 182 | + } catch (IOException | XmlPullParserException e) { |
| 183 | + e.printStackTrace(); |
| 184 | + } |
| 185 | + |
| 186 | + return result; |
| 187 | + } |
| 188 | +} |
| 189 | + |
0 commit comments