Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class FocusTimeController {
// Only present if app is currently in focus.
private Long timeFocusedAtMs;

private Object timeFocusedAtMsLock = new Object();

private OSFocusTimeProcessorFactory processorFactory;
private OSLogger logger;

Expand All @@ -42,13 +44,17 @@ private enum FocusEventType {
}

void appForegrounded() {
timeFocusedAtMs = OneSignal.getTime().getElapsedRealtime();
logger.debug("Application foregrounded focus time: " + timeFocusedAtMs);
synchronized (timeFocusedAtMsLock) {
timeFocusedAtMs = OneSignal.getTime().getElapsedRealtime();
logger.debug("Application foregrounded focus time: " + timeFocusedAtMs);
}
}

void appStopped() {
Long timeElapsed = getTimeFocusedElapsed();
logger.debug("Application stopped focus time: " + timeFocusedAtMs + " timeElapsed: " + timeElapsed);
synchronized (timeFocusedAtMsLock) {
logger.debug("Application stopped focus time: " + timeFocusedAtMs + " timeElapsed: " + timeElapsed);
}

if (timeElapsed == null)
return;
Expand All @@ -58,9 +64,11 @@ void appStopped() {
}

void appBackgrounded() {
logger.debug("Application backgrounded focus time: " + timeFocusedAtMs);
processorFactory.getTimeProcessorSaved().sendUnsentTimeNow();
timeFocusedAtMs = null;
synchronized (timeFocusedAtMsLock) {
logger.debug("Application backgrounded focus time: " + timeFocusedAtMs);
processorFactory.getTimeProcessorSaved().sendUnsentTimeNow();
timeFocusedAtMs = null;
}
}

void doBlockingBackgroundSyncOfUnsentTime() {
Expand Down Expand Up @@ -91,16 +99,19 @@ private boolean giveProcessorsValidFocusTime(@NonNull List<OSInfluence> influenc
// Get time past since app was put into focus.
// Will be null if time is invalid or 0
private @Nullable Long getTimeFocusedElapsed() {
// timeFocusedAtMs is cleared when the app goes into the background so we don't have a focus time
if (timeFocusedAtMs == null)
return null;
synchronized (timeFocusedAtMsLock) {
// timeFocusedAtMs is cleared when the app goes into the background so we don't have a focus time
if (timeFocusedAtMs == null)
return null;

long timeElapsed = (long) (((OneSignal.getTime().getElapsedRealtime() - timeFocusedAtMs) / 1_000d) + 0.5d);

long timeElapsed = (long)(((OneSignal.getTime().getElapsedRealtime() - timeFocusedAtMs) / 1_000d) + 0.5d);

// Time is invalid if below 1 or over a day
if (timeElapsed < 1 || timeElapsed > 86_400)
return null;
return timeElapsed;
// Time is invalid if below 1 or over a day
if (timeElapsed < 1 || timeElapsed > 86_400)
return null;
return timeElapsed;
}
}

static class FocusTimeProcessorUnattributed extends FocusTimeProcessorBase {
Expand Down