5
5
import android .support .annotation .Nullable ;
6
6
import android .support .annotation .WorkerThread ;
7
7
8
+ import com .onesignal .influence .model .OSInfluence ;
9
+
8
10
import org .json .JSONException ;
9
11
import org .json .JSONObject ;
10
12
13
+ import java .util .ArrayList ;
11
14
import java .util .Arrays ;
15
+ import java .util .HashSet ;
12
16
import java .util .List ;
17
+ import java .util .Set ;
13
18
import java .util .concurrent .atomic .AtomicBoolean ;
14
19
15
20
/**
18
23
* 2. App is foregrounded (appForegrounded) - Set start focused time
19
24
* 3. App is backgrounded (appBackgrounded) - Kick off job to sync when session ends
20
25
*/
26
+
21
27
class FocusTimeController {
22
28
// Only present if app is currently in focus.
23
29
@ Nullable private Long timeFocusedAtMs ;
@@ -44,13 +50,13 @@ void appForegrounded() {
44
50
}
45
51
46
52
void appBackgrounded () {
47
- giveProcessorsValidFocusTime (OneSignal .getSessionManager ().getSessionResult (), FocusEventType .BACKGROUND );
53
+ giveProcessorsValidFocusTime (OneSignal .getSessionManager ().getInfluences (), FocusEventType .BACKGROUND );
48
54
timeFocusedAtMs = null ;
49
55
}
50
56
51
- void onSessionEnded (@ NonNull OSSessionManager . SessionResult lastSessionResult ) {
57
+ void onSessionEnded (@ NonNull List < OSInfluence > lastInfluences ) {
52
58
final FocusEventType focusEventType = FocusEventType .END_SESSION ;
53
- boolean hadValidTime = giveProcessorsValidFocusTime (lastSessionResult , focusEventType );
59
+ boolean hadValidTime = giveProcessorsValidFocusTime (lastInfluences , focusEventType );
54
60
55
61
// If there is no in focus time to be added we just need to send the time from the last session that just ended.
56
62
if (!hadValidTime ) {
@@ -67,13 +73,13 @@ void doBlockingBackgroundSyncOfUnsentTime() {
67
73
focusTimeProcessor .syncUnsentTimeFromSyncJob ();
68
74
}
69
75
70
- private boolean giveProcessorsValidFocusTime (@ NonNull OSSessionManager . SessionResult lastSessionResult , @ NonNull FocusEventType focusType ) {
76
+ private boolean giveProcessorsValidFocusTime (@ NonNull List < OSInfluence > influences , @ NonNull FocusEventType focusType ) {
71
77
Long timeElapsed = getTimeFocusedElapsed ();
72
78
if (timeElapsed == null )
73
79
return false ;
74
80
75
81
for (FocusTimeProcessorBase focusTimeProcessor : focusTimeProcessors )
76
- focusTimeProcessor .addTime (timeElapsed , lastSessionResult . session , focusType );
82
+ focusTimeProcessor .addTime (timeElapsed , influences , focusType );
77
83
return true ;
78
84
}
79
85
@@ -98,8 +104,14 @@ private static class FocusTimeProcessorUnattributed extends FocusTimeProcessorBa
98
104
PREF_KEY_FOR_UNSENT_TIME = OneSignalPrefs .PREFS_GT_UNSENT_ACTIVE_TIME ;
99
105
}
100
106
101
- protected boolean timeTypeApplies (@ NonNull OSSessionManager .Session session ) {
102
- return session .isUnattributed () || session .isDisabled ();
107
+ protected boolean timeTypeApplies (@ NonNull List <OSInfluence > influences ) {
108
+ for (OSInfluence influence : influences ) {
109
+ // If at least one channel attributed the session then it is an attributed session.
110
+ if (influence .getInfluenceType ().isAttributed ())
111
+ return false ;
112
+ }
113
+ OneSignal .Log (OneSignal .LOG_LEVEL .DEBUG , this .getClass ().getSimpleName () + ":timeTypeApplies for influences: " + influences .toString () + " true" );
114
+ return true ;
103
115
}
104
116
105
117
protected void sendTime (@ NonNull FocusEventType focusType ) {
@@ -109,6 +121,11 @@ protected void sendTime(@NonNull FocusEventType focusType) {
109
121
110
122
syncUnsentTimeOnBackgroundEvent ();
111
123
}
124
+
125
+ @ Override
126
+ protected void saveInfluences (List <OSInfluence > influences ) {
127
+ // We don't save influences for unattributed
128
+ }
112
129
}
113
130
114
131
private static class FocusTimeProcessorAttributed extends FocusTimeProcessorBase {
@@ -117,12 +134,53 @@ private static class FocusTimeProcessorAttributed extends FocusTimeProcessorBase
117
134
PREF_KEY_FOR_UNSENT_TIME = OneSignalPrefs .PREFS_OS_UNSENT_ATTRIBUTED_ACTIVE_TIME ;
118
135
}
119
136
120
- protected boolean timeTypeApplies (@ NonNull OSSessionManager .Session session ) {
121
- return session .isAttributed ();
137
+ private List <OSInfluence > getInfluences () {
138
+ List <OSInfluence > influences = new ArrayList <>();
139
+ Set <String > influenceJSONs = OneSignalPrefs .getStringSet (
140
+ OneSignalPrefs .PREFS_ONESIGNAL ,
141
+ OneSignalPrefs .PREFS_OS_ATTRIBUTED_INFLUENCES ,
142
+ new HashSet <String >()
143
+ );
144
+
145
+ for (String influenceJSON : influenceJSONs ) {
146
+ try {
147
+ influences .add (new OSInfluence (influenceJSON ));
148
+ } catch (JSONException exception ) {
149
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , this .getClass ().getSimpleName () + ": error generation OSInfluence from json object: " + exception );
150
+ }
151
+ }
152
+ return influences ;
153
+ }
154
+
155
+ @ Override
156
+ protected void saveInfluences (List <OSInfluence > influences ) {
157
+ Set <String > setInfluences = new HashSet <>();
158
+ for (OSInfluence influence : influences ) {
159
+ try {
160
+ setInfluences .add (influence .toJSONString ());
161
+ } catch (JSONException exception ) {
162
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , this .getClass ().getSimpleName () + ": error generation json object OSInfluence: " + exception );
163
+ }
164
+ }
165
+
166
+ OneSignalPrefs .saveStringSet (
167
+ OneSignalPrefs .PREFS_ONESIGNAL ,
168
+ OneSignalPrefs .PREFS_OS_ATTRIBUTED_INFLUENCES ,
169
+ setInfluences
170
+ );
171
+ }
172
+
173
+ protected boolean timeTypeApplies (@ NonNull List <OSInfluence > influences ) {
174
+ for (OSInfluence influence : influences ) {
175
+ // Is true is at least one channel attributed the session
176
+ if (influence .getInfluenceType ().isAttributed ())
177
+ return true ;
178
+ }
179
+ return false ;
122
180
}
123
181
124
182
protected void additionalFieldsToAddToOnFocusPayload (@ NonNull JSONObject jsonBody ) {
125
- OneSignal .getSessionManager ().addSessionNotificationsIds (jsonBody );
183
+ OneSignal .getSessionManager ().addSessionIds (jsonBody , getInfluences () );
126
184
}
127
185
128
186
protected void sendTime (@ NonNull FocusEventType focusType ) {
@@ -134,12 +192,14 @@ protected void sendTime(@NonNull FocusEventType focusType) {
134
192
}
135
193
136
194
private static abstract class FocusTimeProcessorBase {
195
+
137
196
// These values are set by child classes that inherit this base class
138
197
protected long MIN_ON_FOCUS_TIME_SEC ;
139
198
protected @ NonNull String PREF_KEY_FOR_UNSENT_TIME ;
140
199
141
- protected abstract boolean timeTypeApplies (@ NonNull OSSessionManager . Session session );
200
+ protected abstract boolean timeTypeApplies (@ NonNull List < OSInfluence > influences );
142
201
protected abstract void sendTime (@ NonNull FocusEventType focusType );
202
+ protected abstract void saveInfluences (List <OSInfluence > influences );
143
203
144
204
@ Nullable private Long unsentActiveTime = null ;
145
205
@@ -165,10 +225,12 @@ private void saveUnsentActiveTime(long time) {
165
225
);
166
226
}
167
227
168
- private void addTime (long time , @ NonNull OSSessionManager . Session session , @ NonNull FocusEventType focusType ) {
169
- if (!timeTypeApplies (session ))
228
+ private void addTime (long time , @ NonNull List < OSInfluence > influences , @ NonNull FocusEventType focusType ) {
229
+ if (!timeTypeApplies (influences ))
170
230
return ;
171
231
232
+ saveInfluences (influences );
233
+ OneSignal .Log (OneSignal .LOG_LEVEL .DEBUG , this .getClass ().getSimpleName () + ":addTime with lastFocusTimeInfluences: " + influences .toString ());
172
234
long totalTime = getUnsentActiveTime () + time ;
173
235
saveUnsentActiveTime (totalTime );
174
236
sendUnsentTimeNow (focusType );
@@ -247,6 +309,7 @@ protected void additionalFieldsToAddToOnFocusPayload(@NonNull JSONObject jsonBod
247
309
248
310
private void sendOnFocus (long totalTimeActive ) {
249
311
try {
312
+ OneSignal .Log (OneSignal .LOG_LEVEL .DEBUG , this .getClass ().getSimpleName () + ":sendOnFocus with totalTimeActive: " + totalTimeActive );
250
313
JSONObject jsonBody = generateOnFocusPayload (totalTimeActive );
251
314
additionalFieldsToAddToOnFocusPayload (jsonBody );
252
315
sendOnFocusToPlayer (OneSignal .getUserId (), jsonBody );
0 commit comments