forked from ndraiman/WifiPasswords
-
Notifications
You must be signed in to change notification settings - Fork 5
/
TaskLoadWifiEntries.java
415 lines (367 loc) · 15.9 KB
/
TaskLoadWifiEntries.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.pithsoftware.wifipasswords.task;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Xml;
import com.pithsoftware.wifipasswords.database.PasswordDB;
import com.pithsoftware.wifipasswords.dialogs.CustomAlertDialogListener;
import com.pithsoftware.wifipasswords.extras.MyApplication;
import com.pithsoftware.wifipasswords.extras.RootCheck;
import com.pithsoftware.wifipasswords.pojo.WifiEntry;
import com.pithsoftware.wifipasswords.recycler.WifiListLoadedListener;
import com.stericson.RootShell.RootShell;
import com.stericson.RootShell.execution.Command;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import static com.stericson.RootShell.RootShell.commandWait;
/***********************************************************************/
//Copy wpa_supplicant.conf from /data/misc/wifi to sdcard/WifiPasswords
/***********************************************************************/
public class TaskLoadWifiEntries extends AsyncTask<String, Void, ArrayList<WifiEntry>> {
WifiListLoadedListener mListListener;
boolean mRootAccess = false;
String mPath;
String mFileName;
CustomAlertDialogListener mDialogListener;
boolean mResetDB;
String[] mLocationList = {
"/data/misc/wifi/wpa_supplicant.conf",
"/data/wifi/bcm_supp.conf",
"/data/misc/wifi/wpa.conf"
};
String[] mOreoLocationList = {
"/data/misc/wifi/WifiConfigStore.xml"
};
boolean mManualLocation;
final String BSSID = "bssid";
final String SSID = "ssid";
final String WPA_PSK = "psk";
final String WEP_PSK = "wep_key0";
final String ENTRY_START = "network={";
final String ENTRY_END = "}";
//Constructor for Manual Path
public TaskLoadWifiEntries(String filePath, String fileName, boolean resetDB, WifiListLoadedListener listListener, CustomAlertDialogListener dialogListener) {
mListListener = listListener;
mPath = filePath;
mFileName = fileName;
mDialogListener = dialogListener;
mResetDB = resetDB;
mManualLocation = true;
mRootAccess = RootCheck.canRunRootCommands();
}
//Constructor for Known Paths
public TaskLoadWifiEntries(boolean resetDB, WifiListLoadedListener listListener, CustomAlertDialogListener dialogListener) {
mListListener = listListener;
mDialogListener = dialogListener;
mResetDB = resetDB;
mManualLocation = false;
mRootAccess = RootCheck.canRunRootCommands();
}
@Override
protected ArrayList<WifiEntry> doInBackground(String... params) {
ArrayList<WifiEntry> result;
mRootAccess = RootCheck.canRunRootCommands();
if (android.os.Build.VERSION.SDK_INT >= 26) { // Hard-CODED: Oreo
if (mManualLocation) {
if (mFileName.endsWith(".xml")) {
result = readOreoFile(mPath + mFileName);
if (result == null) {
cancel(true);
}
} else {
result = readFile();
}
} else if (!mRootAccess) {
cancel(true);
return null;
} else {
result = new ArrayList<>();
for (String oreoLocation : mOreoLocationList) {
ArrayList<WifiEntry> currentFile = readOreoFile(oreoLocation);
if ((currentFile != null) && (!currentFile.isEmpty())) {
result.addAll(currentFile);
}
}
}
} else {
result = readFile();
}
return result;
}
@Override
protected void onPostExecute(ArrayList<WifiEntry> wifiEntries) {
//Insert Wifi Entries to database
PasswordDB db = MyApplication.getWritableDatabase();
// if (mResetDB) {
// db.purgeDatabase();
// }
db.insertWifiEntries(wifiEntries, mResetDB, false); //keep Tags according to mResetDB
//Update RecyclerView
if (mListListener != null) {
wifiEntries = new ArrayList<>(db.getAllWifiEntries(false)); //re-read list from database as it removes duplicates
mListListener.onWifiListLoaded(wifiEntries, mResetDB ? wifiEntries.size() : PasswordDB.mNewEntriesOnLastInsert, mResetDB);
}
MyApplication.closeDatabase();
}
@Override
protected void onCancelled() {
super.onCancelled();
//Show "No Root Access" error
if (!mRootAccess) {
if (mDialogListener != null) {
mDialogListener.showRootErrorDialog();
}
}
}
/****************************************************/
/****************** Helper Methods ******************/
/****************************************************/
private static ArrayList<String> executeForResult(String cmd) {
final ArrayList<String> results = new ArrayList<>();
Command command = new Command(3, false, cmd) {
@Override
public void commandOutput(int id, String line) {
results.add(line);
super.commandOutput(id, line);
}
};
try {
RootShell.getShell(true).add(command);
commandWait(RootShell.getShell(true), command);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return results;
}
/****************** Oreo Helper Methods: BEGIN ******************/
private ArrayList<WifiEntry> readNetworkList(XmlPullParser parser) throws XmlPullParserException, IOException {
ArrayList<WifiEntry> result = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "NetworkList");
boolean doLoop = true;
while (doLoop) {
try {
parser.next();
String tagName = parser.getName();
if (tagName == null) {
tagName = "";
}
doLoop = (!tagName.equalsIgnoreCase("NetworkList"));
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
if (tagName.equals("Network")) {
WifiEntry newWifi = readNetworkEntry(parser);
if (newWifi.getTitle().length() != 0) {
result.add(newWifi);
}
} else {
skip(parser);
}
} catch (Exception e) {
Log.e("LoadData.NetworkList", e.getMessage());
doLoop = false;
}
}
return result;
}
// Parses a "Network" entry
private WifiEntry readNetworkEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, null, "Network");
WifiEntry result = new WifiEntry("", MyApplication.NO_PASSWORD_TEXT);
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tagName = parser.getName();
// Starts by looking for the entry tag
if (tagName.equals("WifiConfiguration")) {
result = readWiFiConfig(parser, result);
// } else if (tagName.equals("WifiEnterpriseConfiguration")) {
// result.setTyp(WifiObject.TYP_ENTERPRISE);
} else {
skip(parser);
}
}
return result;
}
// Parses a "WifiConfiguration" entry
private WifiEntry readWiFiConfig(XmlPullParser parser, WifiEntry result) throws XmlPullParserException, IOException {
try {
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String tagName = parser.getName();
String name = parser.getAttributeValue(null, "name");
if (name.equals("SSID") && !tagName.equalsIgnoreCase("null")) {
result.setTitle(readTag(parser, tagName));
} else if (name.equals("PreSharedKey") && !tagName.equalsIgnoreCase("null")) {
String newPwd = readTag(parser, tagName);
if (newPwd.length() > 0) {
result.setPassword(newPwd);
// result.setTyp(WifiObject.TYP_WPA);
}
} else if (name.equals("WEPKeys") && !tagName.equalsIgnoreCase("null")) {
// result.setTyp(WifiObject.TYP_WEP);
if (tagName.equalsIgnoreCase("string-array")) {
try {
int numQty = Integer.parseInt(parser.getAttributeValue(null, "num"));
int loopQty = 0;
while ((parser.next() != XmlPullParser.END_DOCUMENT) && (loopQty < numQty)) {
String innerTagName = parser.getName();
if ((innerTagName != null) && innerTagName.equalsIgnoreCase("item")) {
loopQty ++;
String newPwd = parser.getAttributeValue(null, "value");
if (newPwd.length() > 0) {
result.setPassword(newPwd);
}
}
}
} catch (Exception error) {
parser.getName();
}
} else {
String newPwd = readTag(parser, tagName);
if (newPwd.length() > 0) {
result.setPassword(readTag(parser, tagName));
}
}
} else {
skip(parser);
}
}
} catch (Exception error) {
Log.e("LoadData.readWiFiConfig", error.getMessage() + "\n\nParser: " + parser.getText());
}
return result;
}
// Return the text for a specified tag.
private String readTag(XmlPullParser parser, String tagName) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, null, tagName);
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
parser.require(XmlPullParser.END_TAG, null, tagName);
if (tagName.equalsIgnoreCase("string")
&& Character.toString(result.charAt(0)).equals("\"")) {
result = result.substring(1, result.length() - 1);
}
return result;
}
// Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
// if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
// finds the matching END_TAG (as indicated by the value of "depth" being 0).
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
private ArrayList<WifiEntry> readOreoFile(String configLocation) {
ArrayList<WifiEntry> result = new ArrayList<>();
try {
if (RootShell.exists(configLocation)) {
ArrayList<String> fileLines = executeForResult("su -c /system/bin/cat " + configLocation);
if (fileLines == null) {
return null;
}
StringBuilder buffer = new StringBuilder();
for (String thisLine : fileLines) {
buffer.append(thisLine).append("\n");
}
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(buffer.toString()));
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
if (parser.getName().equalsIgnoreCase("NetworkList")) {
// Process the <Network> entries in the list
result.addAll(readNetworkList(parser));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return result;
}
/****************** Oreo Helper Methods: END ******************/
private ArrayList<WifiEntry> readFile() {
ArrayList<WifiEntry> listWifi = new ArrayList<>();
try {
String configFileName = "";
if (mManualLocation) {
if (RootShell.exists(mPath + mFileName)) {
configFileName = mPath + mFileName;
}
} else {
for (String testFile : mLocationList) {
if (RootShell.exists(testFile)) {
configFileName = testFile;
break;
}
}
}
if (configFileName.length() > 0) {
String title = "";
String password = "";
boolean processingEntry = false;
ArrayList<String> fileLines = executeForResult("su -c /system/bin/cat " + configFileName);
for (String line : fileLines) {
if (processingEntry) {
if (line.contains(ENTRY_END)) {
// Finished with this entry now - clean up the data and add it to the list.
if (password.equals("")) {
password = MyApplication.NO_PASSWORD_TEXT;
}
WifiEntry current = new WifiEntry(title, password);
listWifi.add(current);
// clear the current entry variables.
title = "";
password = "";
processingEntry = false;
} else {
// still processing the current entry; check for valid data.
if (line.contains(SSID) && !line.contains(BSSID)) {
title = line.replace(SSID, "").replace("=", "").replace("\"", "").replace(" ", "");
}
if (password.length() == 0) {
if (line.contains(WPA_PSK)) {
password = line.replace(WPA_PSK, "").replace("=", "").replace("\"", "").replace(" ", "");
} else if (line.contains(WEP_PSK)) {
password = line.replace(WEP_PSK, "").replace("=", "").replace("\"", "").replace(" ", "");
}
}
}
} else {
processingEntry = line.contains(ENTRY_START);
}
}
} else if (mRootAccess) {
mDialogListener.showPathErrorDialog();
}
} catch (Exception e) {
e.printStackTrace();
}
return listWifi;
}
}