|
2 | 2 |
|
3 | 3 | import android.content.Context; |
4 | 4 | import android.net.wifi.WifiConfiguration; |
5 | | -import android.net.wifi.WifiConfigurationHidden; |
6 | 5 | import android.net.wifi.WifiManagerHidden; |
7 | 6 |
|
| 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 | + |
8 | 12 | import java.io.File; |
9 | 13 | import java.nio.file.Files; |
| 14 | +import java.util.Arrays; |
10 | 15 | import java.util.HashSet; |
11 | 16 | import java.util.List; |
12 | 17 | import java.util.Set; |
|
16 | 21 | public class NetworkUtil { |
17 | 22 | private static final String NETWORK_PREFIX = "network"; |
18 | 23 | private static final String NETWORK_SPLIT_SYMBOL = "_"; |
| 24 | + private static final String[] SKIP_FIELDS = {"mNetworkSeclectionDisableCounter"}; |
19 | 25 |
|
20 | 26 | private static void onHelp() { |
21 | 27 | System.out.println("NetworkUtil commands:"); |
22 | 28 | System.out.println(" help"); |
23 | 29 | System.out.println(); |
24 | 30 | System.out.println(" getNetworks"); |
| 31 | + System.out.println(" Dump networks."); |
25 | 32 | 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."); |
27 | 35 | 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."); |
29 | 38 | } |
30 | 39 |
|
31 | 40 | private static void onCommand(String cmd, String[] args) { |
@@ -89,76 +98,65 @@ private static void getNetworks(String[] args) { |
89 | 98 |
|
90 | 99 | private static void saveNetworks(String[] args) { |
91 | 100 | try { |
92 | | - int status = 0; |
93 | | - String savePath = args[1]; |
94 | 101 | Context ctx = HiddenApiHelper.getContext(); |
95 | 102 | WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE)); |
96 | 103 | 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); |
123 | 107 | } catch (Exception e) { |
124 | 108 | e.printStackTrace(System.out); |
125 | 109 | System.exit(1); |
126 | 110 | } |
127 | 111 | } |
128 | 112 |
|
| 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 | + |
129 | 125 | private static void restoreNetworks(String[] args) { |
130 | 126 | try { |
131 | 127 | int status = 0; |
132 | | - String savePath = args[1]; |
| 128 | + String jsonPath = args[1]; |
133 | 129 | Context ctx = HiddenApiHelper.getContext(); |
134 | 130 | WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE)); |
135 | 131 | 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!"); |
140 | 136 | System.exit(1); |
141 | 137 | } |
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; |
158 | 153 | } |
| 154 | + |
159 | 155 | } |
| 156 | + } catch (Exception e) { |
| 157 | + e.printStackTrace(System.out); |
| 158 | + status = 1; |
160 | 159 | } |
161 | | - |
162 | 160 | System.exit(status); |
163 | 161 | } catch (Exception e) { |
164 | 162 | e.printStackTrace(System.out); |
|
0 commit comments