|
27 | 27 |
|
28 | 28 | public class MainActivity extends Activity { |
29 | 29 |
|
30 | | - private static final String HOST = "host", PORT = "port", SSID = "ssid", TAG = "ProxySetterApp", |
31 | | - CLEAR = "clear", BYPASS = "bypass", RESET_WIFI = "reset-wifi", KEY = "key"; |
| 30 | + private static final String TAG = "ProxySetterApp"; |
| 31 | + private static Activity thisActivity; |
| 32 | + |
| 33 | + public MainActivity(){ |
| 34 | + thisActivity = this; |
| 35 | + } |
32 | 36 |
|
33 | 37 | @Override |
34 | 38 | protected void onCreate(Bundle savedInstanceState) { |
35 | 39 | super.onCreate(savedInstanceState); |
36 | 40 | Intent intent = getIntent(); |
37 | | - if (!intent.hasExtra(HOST) && !intent.hasExtra(CLEAR)) { |
38 | | - showPopup("Error: No HOST given, stopping"); |
| 41 | + if(!validateIntent(intent)) { |
39 | 42 | finish(); |
| 43 | + } else { |
| 44 | + new ProxyChangeAsync(this).execute(intent); |
40 | 45 | } |
41 | | - if (!intent.hasExtra(SSID)) { |
42 | | - showPopup("Error: No SSID given, setting on the fist one"); |
| 46 | + } |
| 47 | + |
| 48 | + private boolean validateIntent(Intent intent) { |
| 49 | + if (!intent.hasExtra(ProxyChangeParams.HOST) && !intent.hasExtra(ProxyChangeParams.CLEAR)) { |
| 50 | + showPopup("Error: No HOST given or not clearing proxy"); |
| 51 | + return false; |
43 | 52 | } |
44 | | - new ProxyChangeAsync().execute(this, intent); |
| 53 | + if (!intent.hasExtra(ProxyChangeParams.SSID)) { |
| 54 | + showPopup("Warning: No SSID given, setting on the fist one available"); |
| 55 | + } |
| 56 | + return true; |
45 | 57 | } |
46 | 58 |
|
47 | 59 | /** |
48 | 60 | * Shows a toast and logs to logcat |
49 | 61 | * |
50 | 62 | * @param msg Message to show/log |
51 | 63 | */ |
52 | | - public void showPopup(String msg) { |
53 | | - Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show(); |
54 | | - Log.e(TAG, msg); |
55 | | - } |
56 | | - |
57 | | - /** |
58 | | - * Async task that handles executing the proxy change request |
59 | | - */ |
60 | | - public class ProxyChangeAsync extends AsyncTask<Object, String, Void> { |
61 | | - |
62 | | - private Activity activity; |
63 | | - private ProxyChangeExecutor executor; |
64 | | - |
65 | | - @Override |
66 | | - protected void onPreExecute() { |
67 | | - super.onPreExecute(); |
68 | | - // init executor and register it to receive wifi state change broadcasts |
69 | | - executor = new ProxyChangeExecutor(); |
70 | | - getApplicationContext().registerReceiver(executor, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); |
71 | | - } |
72 | | - |
73 | | - @Override |
74 | | - protected Void doInBackground(Object... params) { |
75 | | - activity = (Activity) params[0]; |
76 | | - onProgressUpdate("Executing proxy change request..."); |
77 | | - |
78 | | - // Looper is needed to handle broadcast messages |
79 | | - try { |
80 | | - Looper.prepare(); |
81 | | - } catch (Exception e) { |
82 | | - Log.e(TAG, "Error starting looper on thread", e); |
83 | | - } |
84 | | - |
85 | | - executor.executeChange((Intent) params[1]); |
86 | | - return null; |
87 | | - } |
88 | | - |
89 | | - @Override |
90 | | - protected void onProgressUpdate(String... values) { |
91 | | - super.onProgressUpdate(values); |
92 | | - final String msg = values[0]; |
93 | | - activity.runOnUiThread(new Runnable() { |
94 | | - public void run() { |
95 | | - showPopup(msg); |
96 | | - } |
97 | | - }); |
98 | | - |
99 | | - } |
100 | | - |
101 | | - @Override |
102 | | - protected void onPostExecute(Void aVoid) { |
103 | | - activity.finish(); |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * Class that executes the proxy change and listens to wifi state changes |
108 | | - */ |
109 | | - public class ProxyChangeExecutor extends BroadcastReceiver { |
110 | | - |
111 | | - private volatile boolean wifiConnected = false; |
112 | | - |
| 64 | + public static void showPopup(final String msg) { |
| 65 | + thisActivity.runOnUiThread(new Runnable() { |
113 | 66 | @Override |
114 | | - public void onReceive(Context context, Intent intent) { |
115 | | - final ConnectivityManager connMgr = (ConnectivityManager) context |
116 | | - .getSystemService(Context.CONNECTIVITY_SERVICE); |
117 | | - final NetworkInfo wifi = connMgr |
118 | | - .getNetworkInfo(ConnectivityManager.TYPE_WIFI); |
119 | | - wifiConnected = wifi.isAvailable() && wifi.isConnected(); |
120 | | - Log.d(TAG, "Received broadcast about wifi. Connected = " + wifiConnected); |
121 | | - debugIntent(intent, TAG); |
122 | | - } |
| 67 | + public void run() { |
| 68 | + Toast.makeText(thisActivity.getBaseContext(), msg, Toast.LENGTH_SHORT).show(); |
| 69 | + Log.d(TAG, msg); |
123 | 70 |
|
124 | | - private void debugIntent(Intent intent, String tag) { |
125 | | - Log.v(tag, "action: " + intent.getAction()); |
126 | | - Log.v(tag, "component: " + intent.getComponent()); |
127 | | - Bundle extras = intent.getExtras(); |
128 | | - if (extras != null) { |
129 | | - for (String key : extras.keySet()) { |
130 | | - Log.v(tag, "key [" + key + "]: " + |
131 | | - extras.get(key)); |
132 | | - } |
133 | | - } else { |
134 | | - Log.v(tag, "no extras"); |
135 | | - } |
136 | 71 | } |
137 | | - |
138 | | - private void executeChange(Intent intent) { |
139 | | - debugIntent(intent, TAG); |
140 | | - String ssid = intent.getStringExtra(SSID); |
141 | | - String key = intent.getStringExtra(KEY); |
142 | | - // Resetting wifi will fix problems with a new wifi network being created by Genymotion |
143 | | - // that has an identical SSID or a null SSID |
144 | | - boolean resetWifi; |
145 | | - try { |
146 | | - resetWifi = Boolean.parseBoolean(intent.getStringExtra(RESET_WIFI)); |
147 | | - } catch (Exception e) { |
148 | | - resetWifi = false; |
149 | | - } |
150 | | - |
151 | | - APL.setup(getApplicationContext()); |
152 | | - |
153 | | - // find the network id to make proxy change on |
154 | | - APLNetworkId networkId = findNetworkId(ssid, key != null); |
155 | | - if (networkId == null && resetWifi && ssid != null) { |
156 | | - restartWifi(ssid, key); |
157 | | - networkId = findNetworkId(ssid, key != null); |
158 | | - } else if (ssid != null) { |
159 | | - try { |
160 | | - connectToWifiNetwork(ssid, key); |
161 | | - networkId = findNetworkId(ssid, key != null); |
162 | | - } catch (Exception e) { |
163 | | - onProgressUpdate("Unable to connect to ssid: " + ssid); |
164 | | - Log.e(TAG, "Error connecting to ssid " + ssid, e); |
165 | | - } |
166 | | - } |
167 | | - if (networkId != null) { |
168 | | - ssid = networkId.SSID; |
169 | | - // get the remaining extras from the intent |
170 | | - String host = intent.getStringExtra(HOST); |
171 | | - String bypass = intent.getStringExtra(BYPASS); |
172 | | - boolean clearProxy; |
173 | | - try { |
174 | | - clearProxy = Boolean.parseBoolean(intent.getStringExtra(CLEAR)); |
175 | | - } catch (Exception e) { |
176 | | - clearProxy = false; |
177 | | - } |
178 | | - |
179 | | - WiFiApConfig wiFiApConfig = APL.getWiFiApConfiguration(APL.getConfiguredNetwork(networkId)); |
180 | | - if (wiFiApConfig != null) { |
181 | | - boolean proceed = true; |
182 | | - ProxySetting proxySetting = null; |
183 | | - int port = 8080; |
184 | | - if (clearProxy) { |
185 | | - proxySetting = ProxySetting.NONE; |
186 | | - } else if (host != null) { |
187 | | - try { |
188 | | - port = Integer.parseInt(intent.getStringExtra(PORT)); |
189 | | - } catch (Exception e) { |
190 | | - onProgressUpdate("Invalid port or none given, defaulting to 8080."); |
191 | | - } |
192 | | - proxySetting = ProxySetting.STATIC; |
193 | | - wiFiApConfig.setProxyHost(host); |
194 | | - wiFiApConfig.setProxyPort(port); |
195 | | - wiFiApConfig.setProxyExclusionString(bypass); |
196 | | - } else { |
197 | | - onProgressUpdate("Error: proxy not set. No host given or clear flag not set."); |
198 | | - proceed = false; |
199 | | - } |
200 | | - |
201 | | - if (proceed) { |
202 | | - wiFiApConfig.setProxySetting(proxySetting); |
203 | | - if (!setProxy(wiFiApConfig, networkId, proxySetting, host, port, bypass, clearProxy)) { |
204 | | - if (resetWifi) { |
205 | | - onProgressUpdate("Error: proxy not set. Trying to reset wifi and set again."); |
206 | | - restartWifi(ssid, key); |
207 | | - if (!setProxy(wiFiApConfig, networkId, proxySetting, host, port, bypass, clearProxy)) { |
208 | | - showGeneralError(1); |
209 | | - } |
210 | | - } else { |
211 | | - showGeneralError(2); |
212 | | - } |
213 | | - } |
214 | | - } |
215 | | - } else { |
216 | | - showGeneralError(3); |
217 | | - } |
218 | | - } else { |
219 | | - showGeneralError(4); |
220 | | - } |
221 | | - |
222 | | - } |
223 | | - |
224 | | - private boolean setProxy(WiFiApConfig wiFiApConfig, APLNetworkId networkId, |
225 | | - ProxySetting proxySetting, String host, int port, String bypass, |
226 | | - boolean clearProxy) { |
227 | | - |
228 | | - if (wiFiApConfig != null) { |
229 | | - try { |
230 | | - APL.writeWifiAPConfig(wiFiApConfig); |
231 | | - } catch (Exception e) { |
232 | | - if (!clearProxy) { |
233 | | - onProgressUpdate("APL Error: proxy not set"); |
234 | | - Log.e(TAG, "APL Error", e); |
235 | | - return false; |
236 | | - } |
237 | | - } |
238 | | - } |
239 | | - |
240 | | - // Get the current config settings to see if proxy was changed |
241 | | - WiFiApConfig newConfig = APL.getWiFiApConfiguration(APL.getConfiguredNetwork(networkId)); |
242 | | - if (newConfig != null && newConfig.getProxySetting().equals(proxySetting)) { |
243 | | - if (proxySetting.equals(ProxySetting.NONE)) { |
244 | | - onProgressUpdate("Proxy cleared"); |
245 | | - return true; |
246 | | - } else if (newConfig.getProxyHost().equals(host) |
247 | | - && newConfig.getProxyPort() == port |
248 | | - && (newConfig.getProxyExclusionList().isEmpty() |
249 | | - || newConfig.getProxyExclusionList().equals(bypass))) { |
250 | | - |
251 | | - onProgressUpdate("Proxy on " + newConfig.getSSID() |
252 | | - + " with security " + newConfig.getSecurityType().name() |
253 | | - + " set to " + host + ":" + port |
254 | | - + " bypass: " + bypass); |
255 | | - try { |
256 | | - onProgressUpdate("Checking wifi connectivity..."); |
257 | | - waitForWifiConnectivity(); |
258 | | - onProgressUpdate("Wifi connected and proxy set!"); |
259 | | - } catch (Exception e) { |
260 | | - onProgressUpdate("Warning: Wifi is not connected. Check that the " + |
261 | | - "correct SSID and key combination were given."); |
262 | | - Log.e(TAG, "", e); |
263 | | - } |
264 | | - return true; |
265 | | - } else { |
266 | | - return false; |
267 | | - } |
268 | | - } else { |
269 | | - return false; |
270 | | - } |
271 | | - |
272 | | - } |
273 | | - |
274 | | - private void restartWifi(String ssid, String key) { |
275 | | - try { |
276 | | - WifiManager wifiManager = APL.getWifiManager(); |
277 | | - try { |
278 | | - // remove the existing configurations to ensure that the newly inserted one is the only one |
279 | | - for (WifiConfiguration wifiConfiguration : wifiManager.getConfiguredNetworks()) { |
280 | | - wifiManager.removeNetwork(wifiConfiguration.networkId); |
281 | | - } |
282 | | - } catch (Exception e) { |
283 | | - Log.e(TAG, "Error clearing wifi configs", e); |
284 | | - } |
285 | | - APL.enableWifi(); |
286 | | - wifiManager.saveConfiguration(); |
287 | | - |
288 | | - connectToWifiNetwork(ssid, key); |
289 | | - |
290 | | - } catch (Exception e) { |
291 | | - Log.e(TAG, "Error resetting wifi", e); |
292 | | - } |
293 | | - } |
294 | | - |
295 | | - private void connectToWifiNetwork(String ssid, String key) throws Exception { |
296 | | - WifiManager wifiManager = APL.getWifiManager(); |
297 | | - // create new config with given ssid and key and connect to it |
298 | | - WifiConfiguration wifiConfiguration = new WifiConfiguration(); |
299 | | - wifiConfiguration.SSID = "\"" + ssid + "\""; |
300 | | - if (key != null && key.length() >= 8) { |
301 | | - wifiConfiguration.preSharedKey = "\"" + key + "\""; |
302 | | - } else { |
303 | | - BitSet bitSet = new BitSet(); |
304 | | - bitSet.set(WifiConfiguration.KeyMgmt.NONE); |
305 | | - wifiConfiguration.allowedKeyManagement = bitSet; |
306 | | - } |
307 | | - int netId = wifiManager.addNetwork(wifiConfiguration); |
308 | | - if (netId < 0) { |
309 | | - netId = wifiManager.updateNetwork(wifiConfiguration); |
310 | | - if (netId < 0) { |
311 | | - onProgressUpdate("Having trouble resetting wifi, hard resetting..."); |
312 | | - APL.disableWifi(); |
313 | | - APL.enableWifi(); |
314 | | - try { |
315 | | - waitForWifiConnectivity(); |
316 | | - } catch (Exception e) { |
317 | | - Log.e(TAG, "Timeout when trying to hard reset wifi", e); |
318 | | - } |
319 | | - netId = wifiManager.addNetwork(wifiConfiguration); |
320 | | - if (netId < 0) { |
321 | | - throw new RuntimeException("Unable to add or update network configuration for " + ssid); |
322 | | - } |
323 | | - } |
324 | | - } |
325 | | - |
326 | | - wifiManager.saveConfiguration(); |
327 | | - wifiManager.disconnect(); |
328 | | - wifiManager.enableNetwork(netId, true); |
329 | | - wifiManager.reconnect(); |
330 | | - waitForWifiConnectivity(); |
331 | | - } |
332 | | - |
333 | | - private APLNetworkId findNetworkId(String ssid, boolean isSecured) { |
334 | | - Map<APLNetworkId, WifiConfiguration> networks = APL.getConfiguredNetworks(); |
335 | | - Log.d(TAG, networks.toString()); |
336 | | - for (APLNetworkId aplNetworkId : networks.keySet()) { |
337 | | - if ((aplNetworkId.SSID.equals(ssid) || ssid == null) |
338 | | - && ((isSecured && !aplNetworkId.Security.equals(SecurityType.SECURITY_NONE)) |
339 | | - || (aplNetworkId.Security.equals(SecurityType.SECURITY_NONE) && !isSecured))) { |
340 | | - return aplNetworkId; |
341 | | - } else { |
342 | | - Log.d(TAG, String.format("NetowrkID %s with security %s does not match requirements ssid = %s and isSecured = %b", |
343 | | - aplNetworkId.SSID, aplNetworkId.Security.toString(), ssid, isSecured)); |
344 | | - } |
345 | | - } |
346 | | - return null; |
347 | | - } |
348 | | - |
349 | | - private void showGeneralError(int code) { |
350 | | - String errorMessage = "Error: proxy not set. Try clearing the proxy setting manually first. Error Code: " + code; |
351 | | - onProgressUpdate(errorMessage); |
352 | | - |
353 | | - } |
354 | | - |
355 | | - private void waitForWifiConnectivity() throws TimeoutException { |
356 | | - long timeout = 10000; |
357 | | - long sleepTime = 2000; |
358 | | - do { |
359 | | - try { |
360 | | - Thread.sleep(sleepTime); |
361 | | - } catch (Exception e) { |
362 | | - // no-op |
363 | | - } |
364 | | - timeout -= sleepTime; |
365 | | - } while (timeout > 0 && !wifiConnected); |
366 | | - if (!wifiConnected) { |
367 | | - throw new TimeoutException("Timeout while waiting for wifi to connect"); |
368 | | - } |
369 | | - } |
370 | | - |
371 | | - } |
| 72 | + }); |
372 | 73 | } |
373 | 74 | } |
0 commit comments