Skip to content

Commit bee531b

Browse files
committed
fix(dex): [NetworkUtil] Restore networks across different APIs
Change-Id: I5d14e94b108e045c606957b842c79763f013794a
1 parent d3bd848 commit bee531b

4 files changed

Lines changed: 54 additions & 81 deletions

File tree

dex/app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dependencies {
4545
implementation(libs.refine.runtime)
4646
implementation(libs.httpclient5.fluent)
4747
implementation(libs.slf4j.simple)
48+
implementation(libs.gson)
4849

4950
compileOnly(project(":hiddenapi"))
5051
}

dex/app/src/main/java/com/xayah/dex/NetworkUtil.java

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import android.content.Context;
44
import android.net.wifi.WifiConfiguration;
5-
import android.net.wifi.WifiConfigurationHidden;
65
import android.net.wifi.WifiManagerHidden;
76

7+
import com.google.gson.ExclusionStrategy;
8+
import com.google.gson.FieldAttributes;
9+
import com.google.gson.Gson;
10+
import com.google.gson.GsonBuilder;
11+
812
import java.io.File;
913
import java.nio.file.Files;
14+
import java.util.Arrays;
1015
import java.util.HashSet;
1116
import java.util.List;
1217
import java.util.Set;
@@ -16,16 +21,20 @@
1621
public class NetworkUtil {
1722
private static final String NETWORK_PREFIX = "network";
1823
private static final String NETWORK_SPLIT_SYMBOL = "_";
24+
private static final String[] SKIP_FIELDS = {"mNetworkSeclectionDisableCounter"};
1925

2026
private static void onHelp() {
2127
System.out.println("NetworkUtil commands:");
2228
System.out.println(" help");
2329
System.out.println();
2430
System.out.println(" getNetworks");
31+
System.out.println(" Dump networks.");
2532
System.out.println();
26-
System.out.println(" saveNetworks PATH");
33+
System.out.println(" saveNetworks");
34+
System.out.println(" Print all networks as JSON to standard output.");
2735
System.out.println();
28-
System.out.println(" restoreNetworks PATH");
36+
System.out.println(" restoreNetworks FILE");
37+
System.out.println(" Restore all networks from a JSON file.");
2938
}
3039

3140
private static void onCommand(String cmd, String[] args) {
@@ -89,76 +98,65 @@ private static void getNetworks(String[] args) {
8998

9099
private static void saveNetworks(String[] args) {
91100
try {
92-
int status = 0;
93-
String savePath = args[1];
94101
Context ctx = HiddenApiHelper.getContext();
95102
WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE));
96103
List<WifiConfiguration> networks = wifiManager.getPrivilegedConfiguredNetworks();
97-
Set<Integer> networkIds = new HashSet<>();
98-
99-
File savePathDir = new File(savePath);
100-
if (!savePathDir.exists()) {
101-
savePathDir.mkdirs();
102-
}
103-
104-
for (int i = 0; i < networks.size(); i++) {
105-
WifiConfiguration network = networks.get(i);
106-
int networkId = network.networkId;
107-
String fileName;
108-
if (!networkIds.contains(networkId)) {
109-
fileName = NETWORK_PREFIX + NETWORK_SPLIT_SYMBOL + network.networkId + NETWORK_SPLIT_SYMBOL + "a";
110-
networkIds.add(networkId);
111-
System.out.println(network.SSID + " saved");
112-
} else {
113-
fileName = NETWORK_PREFIX + NETWORK_SPLIT_SYMBOL + network.networkId + NETWORK_SPLIT_SYMBOL + "b";
114-
}
115-
byte[] config = ParcelableHelper.marshall(network);
116-
File configFile = new File(savePath, fileName);
117-
configFile.delete();
118-
configFile.createNewFile();
119-
Files.write(configFile.toPath(), config);
120-
wifiManager.addNetwork(network);
121-
}
122-
System.exit(status);
104+
Gson gson = new Gson();
105+
System.out.println(gson.toJson(networks));
106+
System.exit(0);
123107
} catch (Exception e) {
124108
e.printStackTrace(System.out);
125109
System.exit(1);
126110
}
127111
}
128112

113+
public static class NetworkStrategy implements ExclusionStrategy {
114+
@Override
115+
public boolean shouldSkipField(FieldAttributes f) {
116+
return Arrays.asList(SKIP_FIELDS).contains(f.getName());
117+
}
118+
119+
@Override
120+
public boolean shouldSkipClass(Class<?> clazz) {
121+
return false;
122+
}
123+
}
124+
129125
private static void restoreNetworks(String[] args) {
130126
try {
131127
int status = 0;
132-
String savePath = args[1];
128+
String jsonPath = args[1];
133129
Context ctx = HiddenApiHelper.getContext();
134130
WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE));
135131
Set<Integer> networkIds = new HashSet<>();
136-
137-
File savePathDir = new File(savePath);
138-
if (!savePathDir.exists()) {
139-
System.out.println(savePath + " is not exists!");
132+
Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(new NetworkStrategy()).create();
133+
File jsonFile = new File(jsonPath);
134+
if (!jsonFile.exists()) {
135+
System.out.println(jsonPath + " not exists!");
140136
System.exit(1);
141137
}
142-
143-
File[] networkFiles = savePathDir.listFiles();
144-
if (networkFiles != null) {
145-
for (File networkFile : networkFiles) {
146-
String fileName = networkFile.getName();
147-
String[] fileNameArgs = fileName.split(NETWORK_SPLIT_SYMBOL);
148-
if (fileNameArgs.length == 3 && fileNameArgs[0].equals(NETWORK_PREFIX)) {
149-
int networkId = Integer.parseInt(fileNameArgs[1]);
150-
ParcelableHelper.unmarshall(Files.readAllBytes(networkFile.toPath()), parcel -> {
151-
WifiConfiguration network = WifiConfigurationHidden.CREATOR.createFromParcel(parcel);
152-
wifiManager.addNetwork(network);
153-
if (!networkIds.contains(networkId)) {
154-
networkIds.add(networkId);
155-
System.out.println(network.SSID + " restored");
156-
}
157-
});
138+
try {
139+
String json = new String(Files.readAllBytes(jsonFile.toPath()));
140+
WifiConfiguration[] networks = gson.fromJson(json, WifiConfiguration[].class);
141+
for (WifiConfiguration network : networks) {
142+
try {
143+
int networkId = network.networkId;
144+
network.networkId = -1;
145+
wifiManager.addNetwork(network);
146+
if (!networkIds.contains(networkId)) {
147+
networkIds.add(networkId);
148+
System.out.println(network.SSID + " restored");
149+
}
150+
} catch (Exception e) {
151+
e.printStackTrace(System.out);
152+
status = 1;
158153
}
154+
159155
}
156+
} catch (Exception e) {
157+
e.printStackTrace(System.out);
158+
status = 1;
160159
}
161-
162160
System.exit(status);
163161
} catch (Exception e) {
164162
e.printStackTrace(System.out);

dex/app/src/main/java/com/xayah/dex/ParcelableHelper.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

dex/gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[versions]
22
agp = "8.2.2"
3+
gson = "2.13.1"
34
httpclient5Fluent = "5.4.1"
45
junit = "4.13.2"
56
appcompat = "1.6.1"
67
refine = "4.4.0"
78
slf4jSimple = "1.7.36" # https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5-fluent/5.4.1
89

910
[libraries]
11+
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
1012
httpclient5-fluent = { module = "org.apache.httpcomponents.client5:httpclient5-fluent", version.ref = "httpclient5Fluent" }
1113
junit = { group = "junit", name = "junit", version.ref = "junit" }
1214
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }

0 commit comments

Comments
 (0)