Moved code to check for unvalidated events into separate function so we can call it from both OnStart and at end of EventsTimer so we do not get an error showing on the display while waiting for first check.
This commit is contained in:
@@ -279,6 +279,8 @@ public class SdServer extends Service implements SdDataReceiver {
|
||||
mUtil.writeToSysLogFile("SdServer.onStartCommand() - starting SdDataSource");
|
||||
mSdDataSource.start();
|
||||
|
||||
checkEvents();
|
||||
|
||||
// Initialise Notification channel for API level 26 and over
|
||||
// from https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26
|
||||
mNM = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
@@ -1550,45 +1552,33 @@ public class SdServer extends Service implements SdDataReceiver {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Periodically check if we have unvalidated events in the remote database.
|
||||
* Show a notification if we do.
|
||||
*/
|
||||
private class CheckEventsTimer extends CountDownTimer {
|
||||
long mFirstUnvalidatedEvent;
|
||||
public boolean mIsRunning = true;
|
||||
|
||||
public CheckEventsTimer(long startTime, long interval) {
|
||||
super(startTime, interval);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
Log.v(TAG, "CheckEventsTimer.onFinish()");
|
||||
private void checkEvents() {
|
||||
// Retrieve events from remote database
|
||||
if (mLm.mWac.getEvents((JSONObject remoteEventsObj) -> {
|
||||
Log.v(TAG, "CheckEventsTimer.onFinish.getEvents.Callback()");
|
||||
Log.v(TAG, "CheckEvents.getEvents.Callback()");
|
||||
long firstUnvalidatedEvent;
|
||||
if (remoteEventsObj == null) {
|
||||
Log.e(TAG, "CheckEventsTimer.onFinish() Callback: Error Retrieving events");
|
||||
Log.e(TAG, "CheckEvents.Callback: Error Retrieving events");
|
||||
} else {
|
||||
try {
|
||||
JSONArray eventsArray = remoteEventsObj.getJSONArray("events");
|
||||
// A bit of a hack to display in reverse chronological order
|
||||
mFirstUnvalidatedEvent = -1;
|
||||
firstUnvalidatedEvent = -1;
|
||||
for (int i = eventsArray.length() - 1; i >= 0; i--) {
|
||||
JSONObject eventObj = eventsArray.getJSONObject(i);
|
||||
Long id = eventObj.getLong("id");
|
||||
String typeStr = eventObj.getString("type");
|
||||
//Log.v(TAG,"CheckEventsTimer: id="+id+", typeStr="+typeStr);
|
||||
if (typeStr.equals("null")) {
|
||||
mFirstUnvalidatedEvent = id;
|
||||
//Log.v(TAG,"CheckEventsTimer:setting mFirstUnvalidatedEvent to "+mFirstUnvalidatedEvent);
|
||||
firstUnvalidatedEvent = id;
|
||||
//Log.v(TAG,"CheckEventsTimer:setting firstUnvalidatedEvent to "+firstUnvalidatedEvent);
|
||||
}
|
||||
}
|
||||
Log.v(TAG, "CheckEventsTimer.onFinish.callback - mFirstUnvalidatedEvent = " +
|
||||
mFirstUnvalidatedEvent);
|
||||
if (mFirstUnvalidatedEvent >= 0) {
|
||||
showEventNotification(mFirstUnvalidatedEvent);
|
||||
Log.v(TAG, "CheckEventsTimer.onFinish.callback - firstUnvalidatedEvent = " +
|
||||
firstUnvalidatedEvent);
|
||||
if (firstUnvalidatedEvent >= 0) {
|
||||
showEventNotification(firstUnvalidatedEvent);
|
||||
mNM.cancel(DATASHARE_NOTIFICATION_ID);
|
||||
} else {
|
||||
mNM.cancel(EVENT_NOTIFICATION_ID);
|
||||
@@ -1606,6 +1596,24 @@ public class SdServer extends Service implements SdDataReceiver {
|
||||
mNM.cancel(EVENT_NOTIFICATION_ID);
|
||||
showDatashareNotification();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Periodically check if we have unvalidated events in the remote database.
|
||||
* Show a notification if we do.
|
||||
*/
|
||||
private class CheckEventsTimer extends CountDownTimer {
|
||||
public boolean mIsRunning = true;
|
||||
|
||||
public CheckEventsTimer(long startTime, long interval) {
|
||||
super(startTime, interval);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
Log.v(TAG, "CheckEventsTimer.onFinish()");
|
||||
checkEvents();
|
||||
if (mIsRunning) {
|
||||
// Restart this timer.
|
||||
Log.v(TAG, "CheckEventsTimer.onFinish() - mIsRunning is true, so re-starting timer");
|
||||
@@ -1616,6 +1624,7 @@ public class SdServer extends Service implements SdDataReceiver {
|
||||
@Override
|
||||
public void onTick(long msRemaining) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a notification to tell the user that we have unvalidated events.
|
||||
@@ -1659,7 +1668,6 @@ public class SdServer extends Service implements SdDataReceiver {
|
||||
.build();
|
||||
nM.notify(EVENT_NOTIFICATION_ID, notification);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user