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:
Graham Jones
2022-02-28 21:08:47 +00:00
parent 93f9d1ab11
commit 102375ac67

View File

@@ -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,12 +1552,58 @@ public class SdServer extends Service implements SdDataReceiver {
} }
} }
private void checkEvents() {
// Retrieve events from remote database
if (mLm.mWac.getEvents((JSONObject remoteEventsObj) -> {
Log.v(TAG, "CheckEvents.getEvents.Callback()");
long firstUnvalidatedEvent;
if (remoteEventsObj == null) {
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
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")) {
firstUnvalidatedEvent = id;
//Log.v(TAG,"CheckEventsTimer:setting firstUnvalidatedEvent to "+firstUnvalidatedEvent);
}
}
Log.v(TAG, "CheckEventsTimer.onFinish.callback - firstUnvalidatedEvent = " +
firstUnvalidatedEvent);
if (firstUnvalidatedEvent >= 0) {
showEventNotification(firstUnvalidatedEvent);
mNM.cancel(DATASHARE_NOTIFICATION_ID);
} else {
mNM.cancel(EVENT_NOTIFICATION_ID);
mNM.cancel(DATASHARE_NOTIFICATION_ID);
}
} catch (JSONException e) {
Log.e(TAG, "CheckEventsTimer.onFinish(): Error Parsing remoteEventsObj: " + e.getMessage());
//mUtil.showToast("Error Parsing remoteEventsObj - this should not happen!!!");
}
}
})) {
Log.v(TAG, "CheckEventsTimer() - requested events");
} else {
Log.v(TAG, "CheckEventsTimer() - Not Logged In");
mNM.cancel(EVENT_NOTIFICATION_ID);
showDatashareNotification();
}
}
/** /**
* Periodically check if we have unvalidated events in the remote database. * Periodically check if we have unvalidated events in the remote database.
* Show a notification if we do. * Show a notification if we do.
*/ */
private class CheckEventsTimer extends CountDownTimer { private class CheckEventsTimer extends CountDownTimer {
long mFirstUnvalidatedEvent;
public boolean mIsRunning = true; public boolean mIsRunning = true;
public CheckEventsTimer(long startTime, long interval) { public CheckEventsTimer(long startTime, long interval) {
@@ -1565,47 +1613,7 @@ public class SdServer extends Service implements SdDataReceiver {
@Override @Override
public void onFinish() { public void onFinish() {
Log.v(TAG, "CheckEventsTimer.onFinish()"); Log.v(TAG, "CheckEventsTimer.onFinish()");
// Retrieve events from remote database checkEvents();
if (mLm.mWac.getEvents((JSONObject remoteEventsObj) -> {
Log.v(TAG, "CheckEventsTimer.onFinish.getEvents.Callback()");
if (remoteEventsObj == null) {
Log.e(TAG, "CheckEventsTimer.onFinish() Callback: Error Retrieving events");
} else {
try {
JSONArray eventsArray = remoteEventsObj.getJSONArray("events");
// A bit of a hack to display in reverse chronological order
mFirstUnvalidatedEvent = -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);
}
}
Log.v(TAG, "CheckEventsTimer.onFinish.callback - mFirstUnvalidatedEvent = " +
mFirstUnvalidatedEvent);
if (mFirstUnvalidatedEvent >= 0) {
showEventNotification(mFirstUnvalidatedEvent);
mNM.cancel(DATASHARE_NOTIFICATION_ID);
} else {
mNM.cancel(EVENT_NOTIFICATION_ID);
mNM.cancel(DATASHARE_NOTIFICATION_ID);
}
} catch (JSONException e) {
Log.e(TAG, "CheckEventsTimer.onFinish(): Error Parsing remoteEventsObj: " + e.getMessage());
//mUtil.showToast("Error Parsing remoteEventsObj - this should not happen!!!");
}
}
})) {
Log.v(TAG, "CheckEventsTimer() - requested events");
} else {
Log.v(TAG, "CheckEventsTimer() - Not Logged In");
mNM.cancel(EVENT_NOTIFICATION_ID);
showDatashareNotification();
}
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,49 +1624,49 @@ 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.
*/ */
private void showEventNotification(long eventId) { private void showEventNotification(long eventId) {
Log.v(TAG, "showEventNotification()"); Log.v(TAG, "showEventNotification()");
int iconId; int iconId;
String titleStr; String titleStr;
Uri soundUri = null; Uri soundUri = null;
// 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
NotificationManager nM = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nM = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mEventNotChId); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mEventNotChId);
if (Build.VERSION.SDK_INT >= 26) { if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(mEventNotChId, NotificationChannel channel = new NotificationChannel(mEventNotChId,
mEventNotChName, mEventNotChName,
NotificationManager.IMPORTANCE_DEFAULT); NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(mEventNotChDesc); channel.setDescription(mEventNotChDesc);
nM.createNotificationChannel(channel); nM.createNotificationChannel(channel);
}
iconId = R.drawable.datasharing_query_24x24;
titleStr = getString(R.string.unvalidatedEventsTitle);
Intent i = new Intent(getApplicationContext(), LogManagerControlActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.setAction("None");
PendingIntent contentIntent =
PendingIntent.getActivity(getApplicationContext(),
0, i, PendingIntent.FLAG_UPDATE_CURRENT);
String contentStr = getString(R.string.please_confirm_seizure_events);
Notification notification = notificationBuilder.setContentIntent(contentIntent)
.setSmallIcon(iconId)
.setColor(0x00ffffff)
.setAutoCancel(false)
.setContentTitle(titleStr)
.setContentText(contentStr)
.setOnlyAlertOnce(true)
.build();
nM.notify(EVENT_NOTIFICATION_ID, notification);
} }
iconId = R.drawable.datasharing_query_24x24;
titleStr = getString(R.string.unvalidatedEventsTitle);
Intent i = new Intent(getApplicationContext(), LogManagerControlActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.setAction("None");
PendingIntent contentIntent =
PendingIntent.getActivity(getApplicationContext(),
0, i, PendingIntent.FLAG_UPDATE_CURRENT);
String contentStr = getString(R.string.please_confirm_seizure_events);
Notification notification = notificationBuilder.setContentIntent(contentIntent)
.setSmallIcon(iconId)
.setColor(0x00ffffff)
.setAutoCancel(false)
.setContentTitle(titleStr)
.setContentText(contentStr)
.setOnlyAlertOnce(true)
.build();
nM.notify(EVENT_NOTIFICATION_ID, notification);
} }