Skip to content

Commit 1bffa3d

Browse files
committed
feat(dex): Implement NetworkUtil
Change-Id: Ief69731180e50aec777ef71dbb7412eb5bed384c
1 parent 0305f70 commit 1bffa3d

8 files changed

Lines changed: 279 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ private static void onCommand(String cmd, String[] args) {
1818
switch (cmd) {
1919
case "s2t":
2020
s2t(args);
21+
break;
2122
case "t2s":
2223
t2s(args);
24+
break;
2325
case "help":
2426
onHelp();
27+
break;
2528
default:
2629
System.out.println("Unknown command: " + cmd);
2730
System.exit(1);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,34 @@ private static void onCommand(String cmd, String[] args) {
6565
switch (cmd) {
6666
case "getPackageUid":
6767
getPackageUid(args);
68+
break;
6869
case "getPackageLabel":
6970
getPackageLabel(args);
71+
break;
7072
case "getPackageArchiveInfo":
7173
getPackageArchiveInfo(args);
74+
break;
7275
case "getInstalledPackagesAsUser":
7376
getInstalledPackagesAsUser(args);
77+
break;
7478
case "getRuntimePermissions":
7579
getRuntimePermissions(args);
80+
break;
7681
case "grantRuntimePermission":
7782
grantRuntimePermission(args);
83+
break;
7884
case "revokeRuntimePermission":
7985
revokeRuntimePermission(args);
86+
break;
8087
case "setOpsMode":
8188
setOpsMode(args);
89+
break;
8290
case "setDisplayPowerMode":
8391
setDisplayPowerMode(args);
92+
break;
8493
case "help":
8594
onHelp();
95+
break;
8696
default:
8797
System.out.println("Unknown command: " + cmd);
8898
System.exit(1);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ private static void onCommand(String cmd, String[] args) {
1616
switch (cmd) {
1717
case "get":
1818
get(args);
19+
break;
1920
case "help":
2021
onHelp();
22+
break;
2123
default:
2224
System.out.println("Unknown command: " + cmd);
2325
System.exit(1);
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.xayah.dex;
2+
3+
import android.content.Context;
4+
import android.net.wifi.WifiConfiguration;
5+
import android.net.wifi.WifiConfigurationHidden;
6+
import android.net.wifi.WifiManagerHidden;
7+
8+
import java.io.File;
9+
import java.nio.file.Files;
10+
import java.util.HashSet;
11+
import java.util.List;
12+
import java.util.Set;
13+
14+
import dev.rikka.tools.refine.Refine;
15+
16+
public class NetworkUtil {
17+
private static final String NETWORK_PREFIX = "network";
18+
private static final String NETWORK_SPLIT_SYMBOL = "_";
19+
20+
private static void onHelp() {
21+
System.out.println("NetworkUtil commands:");
22+
System.out.println(" help");
23+
System.out.println();
24+
System.out.println(" getNetworks");
25+
System.out.println();
26+
System.out.println(" saveNetworks PATH");
27+
System.out.println();
28+
System.out.println(" restoreNetworks PATH");
29+
}
30+
31+
private static void onCommand(String cmd, String[] args) {
32+
switch (cmd) {
33+
case "getNetworks":
34+
getNetworks(args);
35+
break;
36+
case "saveNetworks":
37+
saveNetworks(args);
38+
break;
39+
case "restoreNetworks":
40+
restoreNetworks(args);
41+
break;
42+
case "help":
43+
onHelp();
44+
break;
45+
default:
46+
System.out.println("Unknown command: " + cmd);
47+
System.exit(1);
48+
}
49+
}
50+
51+
public static void main(String[] args) {
52+
String cmd;
53+
if (args != null && args.length > 0) {
54+
cmd = args[0];
55+
onCommand(cmd, args);
56+
} else {
57+
onHelp();
58+
}
59+
System.exit(0);
60+
}
61+
62+
private static void getNetworks(String[] args) {
63+
try {
64+
Context ctx = HiddenApiHelper.getContext();
65+
WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE));
66+
List<WifiConfiguration> networks = wifiManager.getPrivilegedConfiguredNetworks();
67+
Set<Integer> networkIds = new HashSet<>();
68+
for (int i = 0; i < networks.size(); i++) {
69+
WifiConfiguration network = networks.get(i);
70+
int networkId = network.networkId;
71+
if (!networkIds.contains(networkId)) {
72+
String ssid = network.SSID;
73+
String preSharedKey = network.preSharedKey;
74+
StringBuilder out = new StringBuilder();
75+
out.append(networkId).append(" ").append(ssid);
76+
if (preSharedKey != null) {
77+
out.append(" ").append(preSharedKey);
78+
}
79+
System.out.println(out);
80+
networkIds.add(networkId);
81+
}
82+
}
83+
System.exit(0);
84+
} catch (Exception e) {
85+
e.printStackTrace(System.out);
86+
System.exit(1);
87+
}
88+
}
89+
90+
private static void saveNetworks(String[] args) {
91+
try {
92+
int status = 0;
93+
String savePath = args[1];
94+
Context ctx = HiddenApiHelper.getContext();
95+
WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE));
96+
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);
123+
} catch (Exception e) {
124+
e.printStackTrace(System.out);
125+
System.exit(1);
126+
}
127+
}
128+
129+
private static void restoreNetworks(String[] args) {
130+
try {
131+
int status = 0;
132+
String savePath = args[1];
133+
Context ctx = HiddenApiHelper.getContext();
134+
WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE));
135+
Set<Integer> networkIds = new HashSet<>();
136+
137+
File savePathDir = new File(savePath);
138+
if (!savePathDir.exists()) {
139+
System.out.println(savePath + " is not exists!");
140+
System.exit(1);
141+
}
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+
});
158+
}
159+
}
160+
}
161+
162+
System.exit(status);
163+
} catch (Exception e) {
164+
e.printStackTrace(System.out);
165+
System.exit(1);
166+
}
167+
}
168+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.xayah.dex;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
public class ParcelableHelper {
7+
8+
public interface ParcelBlock {
9+
void accept(Parcel parcel);
10+
}
11+
12+
public static void unmarshall(byte[] byteArray, ParcelBlock block) {
13+
Parcel parcel = Parcel.obtain();
14+
parcel.unmarshall(byteArray, 0, byteArray.length);
15+
parcel.setDataPosition(0);
16+
block.accept(parcel);
17+
parcel.recycle();
18+
}
19+
20+
public static byte[] marshall(Parcelable parcelable) {
21+
Parcel parcel = Parcel.obtain();
22+
parcel.setDataPosition(0);
23+
parcelable.writeToParcel(parcel, 0);
24+
byte[] bytes = parcel.marshall();
25+
parcel.recycle();
26+
return bytes;
27+
}
28+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@ private static void onCommand(String cmd, String[] args) {
121121
switch (cmd) {
122122
case "get":
123123
onGet(args);
124+
break;
124125
case "set":
125126
onSet(args);
127+
break;
126128
default:
127129
onHelp();
130+
break;
128131
}
129132
}
130133

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package android.net.wifi;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
import dev.rikka.tools.refine.RefineAs;
7+
8+
/**
9+
* A class representing a configured Wi-Fi network, including the
10+
* security configuration.
11+
*/
12+
@RefineAs(WifiConfiguration.class)
13+
public class WifiConfigurationHidden {
14+
public static final Parcelable.Creator<WifiConfiguration> CREATOR =
15+
new Parcelable.Creator<WifiConfiguration>() {
16+
public WifiConfiguration createFromParcel(Parcel in) {
17+
throw new RuntimeException("Stub!");
18+
}
19+
20+
public WifiConfiguration[] newArray(int size) {
21+
throw new RuntimeException("Stub!");
22+
}
23+
};
24+
}
25+
26+
// https://cs.android.com/android/platform/superproject/+/android-7.0.0_r36:frameworks/base/wifi/java/android/net/wifi/WifiConfiguration.java
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package android.net.wifi;
2+
3+
import java.util.List;
4+
5+
import dev.rikka.tools.refine.RefineAs;
6+
7+
/**
8+
* This class provides the primary API for managing all aspects of Wi-Fi
9+
* connectivity. Get an instance of this class by calling
10+
* {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.WIFI_SERVICE)}.
11+
12+
* It deals with several categories of items:
13+
* <ul>
14+
* <li>The list of configured networks. The list can be viewed and updated,
15+
* and attributes of individual entries can be modified.</li>
16+
* <li>The currently active Wi-Fi network, if any. Connectivity can be
17+
* established or torn down, and dynamic information about the state of
18+
* the network can be queried.</li>
19+
* <li>Results of access point scans, containing enough information to
20+
* make decisions about what access point to connect to.</li>
21+
* <li>It defines the names of various Intent actions that are broadcast
22+
* upon any sort of change in Wi-Fi state.
23+
* </ul>
24+
* This is the API to use when performing Wi-Fi specific operations. To
25+
* perform operations that pertain to network connectivity at an abstract
26+
* level, use {@link android.net.ConnectivityManager}.
27+
*/
28+
@RefineAs(WifiManager.class)
29+
public class WifiManagerHidden {
30+
public List<WifiConfiguration> getPrivilegedConfiguredNetworks() {
31+
throw new RuntimeException("Stub!");
32+
}
33+
34+
public int addNetwork(WifiConfiguration config) {
35+
throw new RuntimeException("Stub!");
36+
}
37+
}
38+
39+
// https://cs.android.com/android/platform/superproject/+/android-7.0.0_r36:frameworks/base/wifi/java/android/net/wifi/WifiManager.java

0 commit comments

Comments
 (0)