Fix race condition in alert service

This commit is contained in:
TebbeUbben 2019-01-19 05:36:02 +01:00
parent 437dee288e
commit f0a2307517

View file

@ -79,8 +79,10 @@ public class InsightAlertService extends Service implements InsightConnectionSer
} }
public void ignore(AlertType alertType) { public void ignore(AlertType alertType) {
ignoreTimestamp = System.currentTimeMillis(); synchronized ($alertLock) {
ignoreType = alertType; ignoreTimestamp = System.currentTimeMillis();
ignoreType = alertType;
}
} }
@Nullable @Nullable
@ -132,26 +134,26 @@ public class InsightAlertService extends Service implements InsightConnectionSer
if (alertActivity != null && alert != null) if (alertActivity != null && alert != null)
new Handler(Looper.getMainLooper()).post(() -> alertActivity.update(alert)); new Handler(Looper.getMainLooper()).post(() -> alertActivity.update(alert));
} }
} if (alert == null) {
if (alert == null) { stopAlerting();
stopAlerting(); if (connectionRequested) {
if (connectionRequested) { connectionService.withdrawConnectionRequest(this);
connectionService.withdrawConnectionRequest(this); connectionRequested = false;
connectionRequested = false; }
} if (alertActivity != null)
if (alertActivity != null) new Handler(Looper.getMainLooper()).post(() -> alertActivity.finish());
new Handler(Looper.getMainLooper()).post(() -> alertActivity.finish()); } else if (!(alert.getAlertType() == ignoreType && System.currentTimeMillis() - ignoreTimestamp < 10000)) {
} else if (!(alert.getAlertType() == ignoreType && System.currentTimeMillis() - ignoreTimestamp < 10000)) { if (alert.getAlertStatus() == AlertStatus.ACTIVE) alert();
if (alert.getAlertStatus() == AlertStatus.ACTIVE) alert(); else stopAlerting();
else stopAlerting(); if (!connectionRequested) {
if (!connectionRequested) { connectionService.requestConnection(this);
connectionService.requestConnection(this); connectionRequested = true;
connectionRequested = true; }
} if (alertActivity == null) {
if (alertActivity == null) { Intent intent = new Intent(InsightAlertService.this, InsightAlertActivity.class);
Intent intent = new Intent(InsightAlertService.this, InsightAlertActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); new Handler(Looper.getMainLooper()).post(() -> startActivity(intent));
new Handler(Looper.getMainLooper()).post(() -> startActivity(intent)); }
} }
} }
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {