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");
|
mUtil.writeToSysLogFile("SdServer.onStartCommand() - starting SdDataSource");
|
||||||
mSdDataSource.start();
|
mSdDataSource.start();
|
||||||
|
|
||||||
|
checkEvents();
|
||||||
|
|
||||||
// Initialise Notification channel for API level 26 and over
|
// Initialise Notification channel for API level 26 and over
|
||||||
// from https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26
|
// from https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26
|
||||||
mNM = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
|
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) {
|
private void checkEvents() {
|
||||||
super(startTime, interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFinish() {
|
|
||||||
Log.v(TAG, "CheckEventsTimer.onFinish()");
|
|
||||||
// Retrieve events from remote database
|
// Retrieve events from remote database
|
||||||
if (mLm.mWac.getEvents((JSONObject remoteEventsObj) -> {
|
if (mLm.mWac.getEvents((JSONObject remoteEventsObj) -> {
|
||||||
Log.v(TAG, "CheckEventsTimer.onFinish.getEvents.Callback()");
|
Log.v(TAG, "CheckEvents.getEvents.Callback()");
|
||||||
|
long firstUnvalidatedEvent;
|
||||||
if (remoteEventsObj == null) {
|
if (remoteEventsObj == null) {
|
||||||
Log.e(TAG, "CheckEventsTimer.onFinish() Callback: Error Retrieving events");
|
Log.e(TAG, "CheckEvents.Callback: Error Retrieving events");
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
JSONArray eventsArray = remoteEventsObj.getJSONArray("events");
|
JSONArray eventsArray = remoteEventsObj.getJSONArray("events");
|
||||||
// A bit of a hack to display in reverse chronological order
|
// A bit of a hack to display in reverse chronological order
|
||||||
mFirstUnvalidatedEvent = -1;
|
firstUnvalidatedEvent = -1;
|
||||||
for (int i = eventsArray.length() - 1; i >= 0; i--) {
|
for (int i = eventsArray.length() - 1; i >= 0; i--) {
|
||||||
JSONObject eventObj = eventsArray.getJSONObject(i);
|
JSONObject eventObj = eventsArray.getJSONObject(i);
|
||||||
Long id = eventObj.getLong("id");
|
Long id = eventObj.getLong("id");
|
||||||
String typeStr = eventObj.getString("type");
|
String typeStr = eventObj.getString("type");
|
||||||
//Log.v(TAG,"CheckEventsTimer: id="+id+", typeStr="+typeStr);
|
//Log.v(TAG,"CheckEventsTimer: id="+id+", typeStr="+typeStr);
|
||||||
if (typeStr.equals("null")) {
|
if (typeStr.equals("null")) {
|
||||||
mFirstUnvalidatedEvent = id;
|
firstUnvalidatedEvent = id;
|
||||||
//Log.v(TAG,"CheckEventsTimer:setting mFirstUnvalidatedEvent to "+mFirstUnvalidatedEvent);
|
//Log.v(TAG,"CheckEventsTimer:setting firstUnvalidatedEvent to "+firstUnvalidatedEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.v(TAG, "CheckEventsTimer.onFinish.callback - mFirstUnvalidatedEvent = " +
|
Log.v(TAG, "CheckEventsTimer.onFinish.callback - firstUnvalidatedEvent = " +
|
||||||
mFirstUnvalidatedEvent);
|
firstUnvalidatedEvent);
|
||||||
if (mFirstUnvalidatedEvent >= 0) {
|
if (firstUnvalidatedEvent >= 0) {
|
||||||
showEventNotification(mFirstUnvalidatedEvent);
|
showEventNotification(firstUnvalidatedEvent);
|
||||||
mNM.cancel(DATASHARE_NOTIFICATION_ID);
|
mNM.cancel(DATASHARE_NOTIFICATION_ID);
|
||||||
} else {
|
} else {
|
||||||
mNM.cancel(EVENT_NOTIFICATION_ID);
|
mNM.cancel(EVENT_NOTIFICATION_ID);
|
||||||
@@ -1606,6 +1596,24 @@ public class SdServer extends Service implements SdDataReceiver {
|
|||||||
mNM.cancel(EVENT_NOTIFICATION_ID);
|
mNM.cancel(EVENT_NOTIFICATION_ID);
|
||||||
showDatashareNotification();
|
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) {
|
if (mIsRunning) {
|
||||||
// Restart this timer.
|
// Restart this timer.
|
||||||
Log.v(TAG, "CheckEventsTimer.onFinish() - mIsRunning is true, so re-starting 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
|
@Override
|
||||||
public void onTick(long msRemaining) {
|
public void onTick(long msRemaining) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show a notification to tell the user that we have unvalidated events.
|
* Show a notification to tell the user that we have unvalidated events.
|
||||||
@@ -1659,7 +1668,6 @@ public class SdServer extends Service implements SdDataReceiver {
|
|||||||
.build();
|
.build();
|
||||||
nM.notify(EVENT_NOTIFICATION_ID, notification);
|
nM.notify(EVENT_NOTIFICATION_ID, notification);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user