2222import be .shouldit .proxy .lib .APL ;
2323import be .shouldit .proxy .lib .APLNetworkId ;
2424import be .shouldit .proxy .lib .WiFiApConfig ;
25+ import be .shouldit .proxy .lib .enums .SecurityType ;
2526import be .shouldit .proxy .lib .reflection .android .ProxySetting ;
2627
2728
@@ -39,13 +40,16 @@ protected void onCreate(Bundle savedInstanceState) {
3940 if (intent .hasExtra (SSID )) {
4041 new ProxyChangeAsync ().execute (this , intent );
4142 } else {
42- Toast .makeText (getApplicationContext (),
43- "Error: No SSID given" ,
44- Toast .LENGTH_LONG ).show ();
43+ showPopup ("Error: No SSID given" );
4544 finish ();
4645 }
4746 }
4847
48+ public void showPopup (String msg ) {
49+ Toast .makeText (getBaseContext (), msg , Toast .LENGTH_SHORT ).show ();
50+ Log .e (TAG , msg );
51+ }
52+
4953 public class ProxyChangeAsync extends AsyncTask <Object , String , Void > {
5054
5155 private Activity activity ;
@@ -60,9 +64,16 @@ protected void onPreExecute() {
6064
6165 @ Override
6266 protected Void doInBackground (Object ... params ) {
63- Log .d (TAG , "Starting proxy change thread" );
64- Looper .prepare ();
6567 activity = (Activity ) params [0 ];
68+ onProgressUpdate ("Executing proxy change request..." );
69+
70+ // Looper is needed to handle broadcast messages
71+ try {
72+ Looper .prepare ();
73+ } catch (Exception e ){
74+ Log .e (TAG , "Error starting looper on thread" , e );
75+ }
76+
6677 executor .executeChange ((Intent ) params [1 ]);
6778 return null ;
6879 }
@@ -84,14 +95,9 @@ protected void onPostExecute(Void aVoid) {
8495 activity .finish ();
8596 }
8697
87- public void showPopup (String msg ) {
88- Toast .makeText (getBaseContext (), msg , Toast .LENGTH_SHORT ).show ();
89- Log .e (TAG , msg );
90- }
91-
9298 public class ProxyChangeExecutor extends BroadcastReceiver {
9399
94- private volatile boolean wifiConnected = false , waitingForConnection = false ;
100+ private volatile boolean wifiConnected = false ;
95101
96102 @ Override
97103 public void onReceive (Context context , Intent intent ) {
@@ -132,14 +138,13 @@ private void executeChange(Intent intent) {
132138
133139 APL .setup (getApplicationContext ());
134140
135- APLNetworkId networkId = findNetowrkId (ssid );
141+ APLNetworkId networkId = findNetowrkId (ssid , key != null );
136142 if (networkId == null && resetWifi ) {
137143 restartWifi (ssid , key );
138- networkId = findNetowrkId (ssid );
144+ networkId = findNetowrkId (ssid , key != null );
139145 }
140146
141147 if (networkId != null ) {
142- boolean proceed = true ;
143148 // get the remaining extras from the intent
144149 String host = intent .getStringExtra (HOST );
145150 String bypass = intent .getStringExtra (BYPASS );
@@ -152,6 +157,7 @@ private void executeChange(Intent intent) {
152157
153158 WiFiApConfig wiFiApConfig = APL .getWiFiApConfiguration (APL .getConfiguredNetwork (networkId ));
154159 if (wiFiApConfig != null ) {
160+ boolean proceed = true ;
155161 ProxySetting proxySetting = null ;
156162 int port = 8080 ;
157163 if (clearProxy ) {
@@ -220,8 +226,20 @@ private boolean setProxy(WiFiApConfig wiFiApConfig, APLNetworkId networkId,
220226 && newConfig .getProxyPort () == port
221227 && (newConfig .getProxyExclusionList ().isEmpty ()
222228 || newConfig .getProxyExclusionList ().equals (bypass ))) {
223- onProgressUpdate ("Proxy set to " + host + ":" + port
229+
230+ onProgressUpdate ("Proxy on " + newConfig .getSSID ()
231+ + " with security " + newConfig .getSecurityType ().name ()
232+ + " set to " + host + ":" + port
224233 + " bypass: " + bypass );
234+ try {
235+ onProgressUpdate ("Checking wifi connectivity..." );
236+ waitForWifiConnectivity ();
237+ onProgressUpdate ("Wifi connected and proxy set!" );
238+ } catch (Exception e ){
239+ onProgressUpdate ("Warning: Wifi is not connected. Check that the " +
240+ "correct SSID and key combination were given." );
241+ Log .e (TAG , "" , e );
242+ }
225243 return true ;
226244 } else {
227245 return false ;
@@ -272,10 +290,12 @@ private void restartWifi(String ssid, String key) {
272290 }
273291 }
274292
275- private APLNetworkId findNetowrkId (String _ssid ) {
293+ private APLNetworkId findNetowrkId (String ssid , boolean isSecured ) {
276294 Map <APLNetworkId , WifiConfiguration > networks = APL .getConfiguredNetworks ();
277295 for (APLNetworkId aplNetworkId : networks .keySet ()) {
278- if (aplNetworkId .SSID .equals (_ssid )) {
296+ if (aplNetworkId .SSID .equals (ssid )
297+ && ((isSecured && !aplNetworkId .Security .equals (SecurityType .SECURITY_NONE ))
298+ || (aplNetworkId .Security .equals (SecurityType .SECURITY_NONE ) && !isSecured ))) {
279299 return aplNetworkId ;
280300 }
281301 }
@@ -290,15 +310,15 @@ private void showGeneralError(int code) {
290310
291311 private void waitForWifiConnectivity () throws TimeoutException {
292312 long timeout = 10000 ;
293- long sleepTime = 100 ;
294- while ( timeout > 0 && ! wifiConnected ) {
313+ long sleepTime = 2000 ;
314+ do {
295315 try {
296316 Thread .sleep (sleepTime );
297317 } catch (Exception e ) {
298318 // no-op
299319 }
300320 timeout -= sleepTime ;
301- }
321+ } while ( timeout > 0 && ! wifiConnected );
302322 if (!wifiConnected ) {
303323 throw new TimeoutException ("Timeout while waiting for wifi to connect" );
304324 }
0 commit comments