Skip to content

Commit 22839b7

Browse files
committed
Get state on boot
Started work on reinserting polling Exponential back-off on poll
1 parent e6eba91 commit 22839b7

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

Diff for: bin/Techinc-Notify.apk

217 Bytes
Binary file not shown.

Diff for: bin/classes.dex

436 Bytes
Binary file not shown.

Diff for: bin/classes/nl/techinc/notify/BootClass.class

193 Bytes
Binary file not shown.

Diff for: bin/classes/nl/techinc/notify/SpaceState$1.class

697 Bytes
Binary file not shown.

Diff for: bin/classes/nl/techinc/notify/SpaceState.class

415 Bytes
Binary file not shown.

Diff for: src/nl/techinc/notify/BootClass.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@ public class BootClass extends BroadcastReceiver {
1616
@Override
1717
public void onReceive(Context context, Intent intent) {
1818
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
19-
if(!sharedPreferences.getBoolean("boot", true))
20-
return;
21-
if(sharedPreferences.getBoolean("gcm_enabled", false))
19+
if(intent.getAction() == Intent.ACTION_BOOT_COMPLETED)
2220
{
23-
final String regId = GCMRegistrar.getRegistrationId(context);
24-
if (regId.equals("")) {
25-
GCMRegistrar.register(context, SENDER_ID);
26-
} else {
27-
//Log.v("GCM", "Already registered");
21+
if(!sharedPreferences.getBoolean("boot", true))
22+
return;
23+
if(sharedPreferences.getBoolean("gcm_enabled", false))
24+
{
25+
final String regId = GCMRegistrar.getRegistrationId(context);
26+
if (regId.equals("")) {
27+
GCMRegistrar.register(context, SENDER_ID);
28+
} else {
29+
//Log.v("GCM", "Already registered");
30+
}
31+
}
32+
else
33+
{
34+
if(GCMRegistrar.isRegistered(context))
35+
GCMRegistrar.unregister(context);
2836
}
29-
return;
3037
}
31-
if(GCMRegistrar.isRegistered(context))
32-
GCMRegistrar.unregister(context);
38+
SpaceState.updateState(context);
3339
}
3440

3541
}

Diff for: src/nl/techinc/notify/SpaceState.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import android.content.Context;
1111
import android.content.Intent;
12+
import android.os.Handler;
1213

1314
public 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

Comments
 (0)