99
1010import android .content .Context ;
1111import android .content .Intent ;
12+ import android .os .Handler ;
1213
1314public class SpaceState {
1415 private static final String POLL_URL = "http://techinc.nl/space/spacestate" ;
@@ -18,31 +19,45 @@ public class SpaceState {
1819 public static final String PARAM_ERROR = "error" ;
1920
2021 public static boolean updateState (Context context )
22+ {
23+ return updateState (context , 1000 );
24+ }
25+
26+ public static boolean updateState (final Context context , final int delayMillis )
2127 {
2228 NotifyApp application = (NotifyApp ) context .getApplicationContext ();
2329 boolean state = application .getSpaceState ();
2430 double curTime = System .currentTimeMillis () / 1000D ;
2531 if (curTime - application .getLastUpdated () > 60 )
2632 {
27- application .setLastUpdated (curTime );
2833 try {
2934 URLConnection connect = new URL (POLL_URL ).openConnection ();
3035 connect .connect ();
3136 BufferedReader in = new BufferedReader (new InputStreamReader (connect .getInputStream ()));
3237 String input = in .readLine ();
3338 in .close ();
39+ application .setLastUpdated (curTime );
3440 state = !(STATE_CLOSED .equalsIgnoreCase (input .trim ()));
3541 application .setSpaceState (state );
3642 broadcastState (context , state );
3743 } catch (MalformedURLException e ) {
3844 e .printStackTrace ();
3945 } catch (IOException e ) {
4046 e .printStackTrace ();
41- //TODO: Retry
42- Intent intent = new Intent ();
43- intent .setAction (ACTION_STATE );
44- intent .putExtra (PARAM_ERROR , true );
45- context .sendBroadcast (intent );
47+ if (delayMillis > 16000 )
48+ {
49+ Intent intent = new Intent ();
50+ intent .setAction (ACTION_STATE );
51+ intent .putExtra (PARAM_ERROR , true );
52+ context .sendBroadcast (intent );
53+ }
54+ Runnable runnable = new Runnable ()
55+ {
56+ public void run () {
57+ updateState (context , delayMillis *2 );
58+ }
59+ };
60+ new Handler ().postDelayed (runnable , delayMillis );
4661 }
4762 }
4863 else
0 commit comments