-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
LifecycleWatcher.java
166 lines (143 loc) · 4.76 KB
/
LifecycleWatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package io.sentry.android.core;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import io.sentry.Breadcrumb;
import io.sentry.IHub;
import io.sentry.SentryLevel;
import io.sentry.Session;
import io.sentry.android.core.internal.util.BreadcrumbFactory;
import io.sentry.transport.CurrentDateProvider;
import io.sentry.transport.ICurrentDateProvider;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicLong;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
final class LifecycleWatcher implements DefaultLifecycleObserver {
private final AtomicLong lastUpdatedSession = new AtomicLong(0L);
private final long sessionIntervalMillis;
private @Nullable TimerTask timerTask;
private final @Nullable Timer timer;
private final @NotNull Object timerLock = new Object();
private final @NotNull IHub hub;
private final boolean enableSessionTracking;
private final boolean enableAppLifecycleBreadcrumbs;
private final @NotNull ICurrentDateProvider currentDateProvider;
LifecycleWatcher(
final @NotNull IHub hub,
final long sessionIntervalMillis,
final boolean enableSessionTracking,
final boolean enableAppLifecycleBreadcrumbs) {
this(
hub,
sessionIntervalMillis,
enableSessionTracking,
enableAppLifecycleBreadcrumbs,
CurrentDateProvider.getInstance());
}
LifecycleWatcher(
final @NotNull IHub hub,
final long sessionIntervalMillis,
final boolean enableSessionTracking,
final boolean enableAppLifecycleBreadcrumbs,
final @NotNull ICurrentDateProvider currentDateProvider) {
this.sessionIntervalMillis = sessionIntervalMillis;
this.enableSessionTracking = enableSessionTracking;
this.enableAppLifecycleBreadcrumbs = enableAppLifecycleBreadcrumbs;
this.hub = hub;
this.currentDateProvider = currentDateProvider;
if (enableSessionTracking) {
timer = new Timer(true);
} else {
timer = null;
}
}
// App goes to foreground
@Override
public void onStart(final @NotNull LifecycleOwner owner) {
startSession();
addAppBreadcrumb("foreground");
}
private void startSession() {
if (enableSessionTracking) {
cancelTask();
final long currentTimeMillis = currentDateProvider.getCurrentTimeMillis();
hub.withScope(
scope -> {
long lastUpdatedSession = this.lastUpdatedSession.get();
if (lastUpdatedSession == 0L) {
@Nullable Session currentSession = scope.getSession();
if (currentSession != null && currentSession.getStarted() != null) {
lastUpdatedSession = currentSession.getStarted().getTime();
}
}
if (lastUpdatedSession == 0L
|| (lastUpdatedSession + sessionIntervalMillis) <= currentTimeMillis) {
addSessionBreadcrumb("start");
hub.startSession();
}
this.lastUpdatedSession.set(currentTimeMillis);
});
}
}
// App went to background and triggered this callback after 700ms
// as no new screen was shown
@Override
public void onStop(final @NotNull LifecycleOwner owner) {
if (enableSessionTracking) {
final long currentTimeMillis = currentDateProvider.getCurrentTimeMillis();
this.lastUpdatedSession.set(currentTimeMillis);
scheduleEndSession();
}
addAppBreadcrumb("background");
}
private void scheduleEndSession() {
synchronized (timerLock) {
cancelTask();
if (timer != null) {
timerTask =
new TimerTask() {
@Override
public void run() {
addSessionBreadcrumb("end");
hub.endSession();
}
};
timer.schedule(timerTask, sessionIntervalMillis);
}
}
}
private void cancelTask() {
synchronized (timerLock) {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
}
}
private void addAppBreadcrumb(final @NotNull String state) {
if (enableAppLifecycleBreadcrumbs) {
final Breadcrumb breadcrumb = new Breadcrumb();
breadcrumb.setType("navigation");
breadcrumb.setData("state", state);
breadcrumb.setCategory("app.lifecycle");
breadcrumb.setLevel(SentryLevel.INFO);
hub.addBreadcrumb(breadcrumb);
}
}
private void addSessionBreadcrumb(final @NotNull String state) {
final Breadcrumb breadcrumb = BreadcrumbFactory.forSession(state);
hub.addBreadcrumb(breadcrumb);
}
@TestOnly
@Nullable
TimerTask getTimerTask() {
return timerTask;
}
@TestOnly
@Nullable
Timer getTimer() {
return timer;
}
}