@@ -28,6 +28,8 @@ class FocusTimeController {
28
28
// Only present if app is currently in focus.
29
29
private Long timeFocusedAtMs ;
30
30
31
+ private Object timeFocusedAtMsLock = new Object ();
32
+
31
33
private OSFocusTimeProcessorFactory processorFactory ;
32
34
private OSLogger logger ;
33
35
@@ -42,13 +44,17 @@ private enum FocusEventType {
42
44
}
43
45
44
46
void appForegrounded () {
45
- timeFocusedAtMs = OneSignal .getTime ().getElapsedRealtime ();
46
- logger .debug ("Application foregrounded focus time: " + timeFocusedAtMs );
47
+ synchronized (timeFocusedAtMsLock ) {
48
+ timeFocusedAtMs = OneSignal .getTime ().getElapsedRealtime ();
49
+ logger .debug ("Application foregrounded focus time: " + timeFocusedAtMs );
50
+ }
47
51
}
48
52
49
53
void appStopped () {
50
54
Long timeElapsed = getTimeFocusedElapsed ();
51
- logger .debug ("Application stopped focus time: " + timeFocusedAtMs + " timeElapsed: " + timeElapsed );
55
+ synchronized (timeFocusedAtMsLock ) {
56
+ logger .debug ("Application stopped focus time: " + timeFocusedAtMs + " timeElapsed: " + timeElapsed );
57
+ }
52
58
53
59
if (timeElapsed == null )
54
60
return ;
@@ -58,9 +64,11 @@ void appStopped() {
58
64
}
59
65
60
66
void appBackgrounded () {
61
- logger .debug ("Application backgrounded focus time: " + timeFocusedAtMs );
62
- processorFactory .getTimeProcessorSaved ().sendUnsentTimeNow ();
63
- timeFocusedAtMs = null ;
67
+ synchronized (timeFocusedAtMsLock ) {
68
+ logger .debug ("Application backgrounded focus time: " + timeFocusedAtMs );
69
+ processorFactory .getTimeProcessorSaved ().sendUnsentTimeNow ();
70
+ timeFocusedAtMs = null ;
71
+ }
64
72
}
65
73
66
74
void doBlockingBackgroundSyncOfUnsentTime () {
@@ -91,16 +99,19 @@ private boolean giveProcessorsValidFocusTime(@NonNull List<OSInfluence> influenc
91
99
// Get time past since app was put into focus.
92
100
// Will be null if time is invalid or 0
93
101
private @ Nullable Long getTimeFocusedElapsed () {
94
- // timeFocusedAtMs is cleared when the app goes into the background so we don't have a focus time
95
- if (timeFocusedAtMs == null )
96
- return null ;
102
+ synchronized (timeFocusedAtMsLock ) {
103
+ // timeFocusedAtMs is cleared when the app goes into the background so we don't have a focus time
104
+ if (timeFocusedAtMs == null )
105
+ return null ;
106
+
107
+ long timeElapsed = (long ) (((OneSignal .getTime ().getElapsedRealtime () - timeFocusedAtMs ) / 1_000d ) + 0.5d );
97
108
98
- long timeElapsed = (long )(((OneSignal .getTime ().getElapsedRealtime () - timeFocusedAtMs ) / 1_000d ) + 0.5d );
99
109
100
- // Time is invalid if below 1 or over a day
101
- if (timeElapsed < 1 || timeElapsed > 86_400 )
102
- return null ;
103
- return timeElapsed ;
110
+ // Time is invalid if below 1 or over a day
111
+ if (timeElapsed < 1 || timeElapsed > 86_400 )
112
+ return null ;
113
+ return timeElapsed ;
114
+ }
104
115
}
105
116
106
117
static class FocusTimeProcessorUnattributed extends FocusTimeProcessorBase {
0 commit comments