/
SessionControllerImpl.java
216 lines (187 loc) · 6.66 KB
/
SessionControllerImpl.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.snowplowanalytics.snowplow.internal.session;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.util.Consumer;
import com.snowplowanalytics.snowplow.controller.SessionController;
import com.snowplowanalytics.snowplow.internal.Controller;
import com.snowplowanalytics.snowplow.internal.tracker.ServiceProviderInterface;
import com.snowplowanalytics.snowplow.internal.tracker.Tracker;
import com.snowplowanalytics.snowplow.internal.tracker.Logger;
import com.snowplowanalytics.snowplow.tracker.SessionState;
import com.snowplowanalytics.snowplow.util.TimeMeasure;
import java.util.concurrent.TimeUnit;
@RestrictTo(RestrictTo.Scope.LIBRARY)
public class SessionControllerImpl extends Controller implements SessionController {
private final String TAG = SessionControllerImpl.class.getName();
// Constructors
public SessionControllerImpl(@NonNull ServiceProviderInterface serviceProvider) {
super(serviceProvider);
}
// Control methods
@Override
public void pause() {
getDirtyConfig().isPaused = true;
getTracker().pauseSessionChecking();
}
@Override
public void resume() {
getDirtyConfig().isPaused = false;
getTracker().resumeSessionChecking();
}
@Override
public void startNewSession() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return;
}
session.startNewSession();
}
// Getters and Setters
@Override
public int getSessionIndex() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return -1;
}
return session.getSessionIndex();
}
@NonNull
@Override
public String getSessionId() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return "";
}
SessionState state = session.getState();
if (state == null) {
Logger.track(TAG, "Attempt to access session information before session was initialized, use the onSessionUpdate callback");
return "";
}
return state.getSessionId();
}
@NonNull
@Override
public String getUserId() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return "";
}
return session.getUserId();
}
@Override
public boolean isInBackground() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return false;
}
return session.isBackground();
}
@Override
public int getBackgroundIndex() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return -1;
}
return session.getBackgroundIndex();
}
@Override
public int getForegroundIndex() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return -1;
}
return session.getForegroundIndex();
}
@NonNull
@Override
public TimeMeasure getForegroundTimeout() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return new TimeMeasure(0, TimeUnit.SECONDS);
}
return new TimeMeasure(session.getForegroundTimeout(), TimeUnit.MILLISECONDS);
}
@Override
public void setForegroundTimeout(@NonNull TimeMeasure foregroundTimeout) {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return;
}
getDirtyConfig().foregroundTimeout = foregroundTimeout;
getDirtyConfig().foregroundTimeoutUpdated = true;
session.setForegroundTimeout(foregroundTimeout.convert(TimeUnit.MILLISECONDS));
}
@NonNull
@Override
public TimeMeasure getBackgroundTimeout() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return new TimeMeasure(0, TimeUnit.SECONDS);
}
return new TimeMeasure(session.getBackgroundTimeout(), TimeUnit.MILLISECONDS);
}
@Override
public void setBackgroundTimeout(@NonNull TimeMeasure backgroundTimeout) {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return;
}
getDirtyConfig().backgroundTimeout = backgroundTimeout;
getDirtyConfig().backgroundTimeoutUpdated = true;
session.setBackgroundTimeout(backgroundTimeout.convert(TimeUnit.MILLISECONDS));
}
/**
* The callback called everytime the session is updated.
*/
@Nullable
@Override
public Consumer<SessionState> getOnSessionUpdate() {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return null;
}
return session.onSessionUpdate;
}
/**
* The callback called everytime the session is updated.
*/
@Override
public void setOnSessionUpdate(@Nullable Consumer<SessionState> onSessionUpdate) {
Session session = getSession();
if (session == null) {
Logger.track(TAG, "Attempt to access SessionController fields when disabled");
return;
}
session.onSessionUpdate = onSessionUpdate;
}
// Service method
public boolean isEnabled() {
return getTracker().getSession() != null;
}
// Private methods
@NonNull
private Tracker getTracker() {
return serviceProvider.getOrMakeTracker();
}
@Nullable
private Session getSession() {
return serviceProvider.getOrMakeTracker().getSession();
}
@NonNull
private SessionConfigurationUpdate getDirtyConfig() {
return serviceProvider.getSessionConfigurationUpdate();
}
}