12
12
import org .apache .http .impl .client .DefaultHttpClient ;
13
13
import org .apache .http .message .BasicNameValuePair ;
14
14
15
+ import android .app .Service ;
15
16
import android .content .BroadcastReceiver ;
16
17
import android .content .Context ;
17
18
import android .content .Intent ;
18
19
import android .content .SharedPreferences ;
19
20
import android .net .NetworkInfo ;
20
21
import android .net .wifi .WifiInfo ;
21
22
import android .net .wifi .WifiManager ;
23
+ import android .os .IBinder ;
22
24
import android .util .Log ;
23
25
import android .widget .Toast ;
24
26
@@ -30,50 +32,64 @@ public void onReceive(Context context, Intent intent) {
30
32
NetworkInfo ni = (NetworkInfo ) intent .getParcelableExtra (WifiManager .EXTRA_NETWORK_INFO );
31
33
WifiInfo wi = ((WifiManager ) context .getSystemService (Context .WIFI_SERVICE )).getConnectionInfo ();
32
34
if (ni .isConnected () && wi != null && wi .getSSID ().equalsIgnoreCase (GTHive .SSID )) {
33
- SharedPreferences settings = context .getSharedPreferences (GTHive .PREFS_NAME , 0 );
34
- String user = settings .getString ("user" , null );
35
- String pass = settings .getString ("pass" , null );
36
- if (user != null && pass != null ) {
35
+ context .startService (new Intent (context , LoginService .class ));
36
+ }
37
+ }
38
+ }
39
+
40
+ public static class LoginService extends Service {
41
+
42
+ @ Override
43
+ public void onStart (Intent intent , int startId ) {
44
+ Context context = this ;
45
+
46
+ SharedPreferences settings = context .getSharedPreferences (GTHive .PREFS_NAME , 0 );
47
+ String user = settings .getString ("user" , null );
48
+ String pass = settings .getString ("pass" , null );
49
+ if (user != null && pass != null ) {
37
50
38
- HttpClient client = new DefaultHttpClient ();
39
- HttpPost post = new HttpPost ("https://auth.lawn.gatech.edu/index.php" );
40
- try {
41
- //set up post parameters
42
- List <NameValuePair > data = new ArrayList <NameValuePair >(3 );
43
- data .add (new BasicNameValuePair ("username" , user ));
44
- data .add (new BasicNameValuePair ("password" , pass ));
45
- data .add (new BasicNameValuePair ("output" , "text" ));
46
- //check preferences for ISS status
47
- if (Prefs .getISS (context )) {
48
- Log .d ("GTHive" , "ISS ENABLED" );
49
- data .add (new BasicNameValuePair ("iss" , "on" ));
50
- }
51
- post .setEntity (new UrlEncodedFormEntity (data ));
52
-
53
- ResponseHandler <String > rh = new BasicResponseHandler ();
54
- String result = client .execute (post ,rh );
51
+ HttpClient client = new DefaultHttpClient ();
52
+ HttpPost post = new HttpPost ("https://auth.lawn.gatech.edu/index.php" );
53
+ try {
54
+ //set up post parameters
55
+ List <NameValuePair > data = new ArrayList <NameValuePair >(3 );
56
+ data .add (new BasicNameValuePair ("username" , user ));
57
+ data .add (new BasicNameValuePair ("password" , pass ));
58
+ data .add (new BasicNameValuePair ("output" , "text" ));
59
+ //check preferences for ISS status
60
+ if (Prefs .getISS (context )) {
61
+ Log .d ("GTHive" , "ISS ENABLED" );
62
+ data .add (new BasicNameValuePair ("iss" , "on" ));
63
+ }
64
+ post .setEntity (new UrlEncodedFormEntity (data ));
55
65
56
- //check text response and alert user of success/failure
57
- if (result .equals ("Logging you into LAWN..." )) {
58
- result = "Successfully logged into LAWN" ;
59
- }
60
- else if (result .equals ("" )) {
61
- result = "Already logged into LAWN." ;
62
- }
63
- display (context , result );
64
- } catch (Exception e ) {
65
- display (context , "Error authenticating to LAWN. Try using GTHive." );
66
- Log .d ("GTHive_BROADCAST_RECEIVER" , e .toString ());
66
+ ResponseHandler <String > rh = new BasicResponseHandler ();
67
+ String result = client .execute (post ,rh );
68
+ //check text response and alert user of success/failure
69
+ if (result .equals ("Logging you into LAWN..." )) {
70
+ result = "Successfully logged into LAWN" ;
67
71
}
68
-
72
+ else if (result .equals ("" )) {
73
+ result = "Already logged into LAWN." ;
74
+ }
75
+ display (context , result );
76
+ } catch (Exception e ) {
77
+ display (context , "Error authenticating to LAWN. Try using GTHive." );
78
+ Log .d ("GTHive_BROADCAST_RECEIVER" , e .toString ());
69
79
}
70
80
}
71
81
}
72
- }
73
-
74
- private void display (Context c , String text ) {
75
- if (Prefs .showAutoSigninMessages (c )) {
76
- Toast .makeText (c , text , Toast .LENGTH_LONG ).show ();
82
+
83
+ private void display (Context c , String text ) {
84
+ if (Prefs .showAutoSigninMessages (c )) {
85
+ Toast .makeText (c , text , Toast .LENGTH_LONG ).show ();
86
+ }
87
+ }
88
+
89
+ @ Override
90
+ public IBinder onBind (Intent intent ) {
91
+ return null ;
77
92
}
93
+
78
94
}
79
95
}
0 commit comments