29
29
30
30
import android .annotation .SuppressLint ;
31
31
import android .app .Activity ;
32
- import android .content .Context ;
33
32
import android .content .pm .ActivityInfo ;
34
33
import android .content .res .Configuration ;
35
34
import android .os .Build ;
@@ -56,17 +55,24 @@ void lostFocus() {
56
55
}
57
56
}
58
57
59
- private static final Object SYNC_LOCK = new Object ();
60
- private static final String FOCUS_LOST_WORKER_TAG = "FOCUS_LOST_WORKER_TAG" ;
61
58
private static final Map <String , ActivityAvailableListener > sActivityAvailableListeners = new ConcurrentHashMap <>();
62
59
private static final Map <String , OSSystemConditionController .OSSystemConditionObserver > sSystemConditionObservers = new ConcurrentHashMap <>();
63
60
private static final Map <String , KeyboardListener > sKeyboardListeners = new ConcurrentHashMap <>();
64
61
65
- private static AppFocusRunnable appFocusRunnable ;
62
+ private static final String FOCUS_LOST_WORKER_TAG = "FOCUS_LOST_WORKER_TAG" ;
63
+ // We want to perform a on_focus sync as soon as the session is done to report the time
64
+ private static final int SYNC_AFTER_BG_DELAY_MS = 2000 ;
65
+
66
+ private final OSFocusHandler focusHandler ;
67
+
66
68
@ SuppressLint ("StaticFieldLeak" )
67
69
private Activity curActivity = null ;
68
70
private boolean nextResumeIsFirstActivity = false ;
69
71
72
+ public ActivityLifecycleHandler (OSFocusHandler focusHandler ) {
73
+ this .focusHandler = focusHandler ;
74
+ }
75
+
70
76
void onConfigurationChanged (Configuration newConfig , Activity activity ) {
71
77
// If Activity contains the configChanges orientation flag, re-create the view this way
72
78
if (curActivity != null && OSUtils .hasConfigChangeFlag (curActivity , ActivityInfo .CONFIG_ORIENTATION )) {
@@ -166,23 +172,23 @@ private void onOrientationChanged(Activity activity) {
166
172
167
173
private void handleLostFocus () {
168
174
OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler Handling lost focus" );
169
- if (appFocusRunnable != null && appFocusRunnable . backgrounded && !appFocusRunnable . completed )
175
+ if (focusHandler == null || focusHandler . hasBackgrounded () && !focusHandler . hasCompleted () )
170
176
return ;
171
177
172
178
OneSignal .getFocusTimeController ().appStopped ();
173
- OSFocusDelaySync . getInstance (). scheduleSyncTask ( OneSignal .appContext );
179
+ focusHandler . startOnLostFocusWorker ( FOCUS_LOST_WORKER_TAG , SYNC_AFTER_BG_DELAY_MS , OneSignal .appContext );
174
180
}
175
181
176
182
private void handleFocus () {
177
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler handleFocus, with runnable: " + appFocusRunnable + " nextResumeIsFirstActivity: " + nextResumeIsFirstActivity );
178
- if (hasBackgrounded () || nextResumeIsFirstActivity ) {
183
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler handleFocus, nextResumeIsFirstActivity: " + nextResumeIsFirstActivity );
184
+ if (focusHandler . hasBackgrounded () || nextResumeIsFirstActivity ) {
179
185
OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler reset background state, call app focus" );
180
186
nextResumeIsFirstActivity = false ;
181
- resetBackgroundState ();
187
+ focusHandler . resetBackgroundState ();
182
188
OneSignal .onAppFocus ();
183
189
} else {
184
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler cancel background lost focus sync task " );
185
- OSFocusDelaySync . getInstance (). cancelBackgroundSyncTask ( OneSignal .appContext );
190
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler cancel background lost focus worker " );
191
+ focusHandler . cancelOnLostFocusWorker ( FOCUS_LOST_WORKER_TAG , OneSignal .appContext );
186
192
}
187
193
}
188
194
@@ -249,46 +255,6 @@ void setNextResumeIsFirstActivity(boolean nextResumeIsFirstActivity) {
249
255
this .nextResumeIsFirstActivity = nextResumeIsFirstActivity ;
250
256
}
251
257
252
- void resetBackgroundState () {
253
- if (appFocusRunnable != null )
254
- appFocusRunnable .backgrounded = false ;
255
- }
256
-
257
- boolean hasBackgrounded () {
258
- return appFocusRunnable != null && appFocusRunnable .backgrounded ;
259
- }
260
-
261
- static void runLostFocusLogic (Context context ) {
262
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler runLostFocusLogic" );
263
- ActivityLifecycleHandler activityLifecycleHandler = ActivityLifecycleListener .getActivityLifecycleHandler ();
264
- if (activityLifecycleHandler == null || activityLifecycleHandler .curActivity == null )
265
- OneSignal .setInForeground (false );
266
- appFocusRunnable = new AppFocusRunnable ();
267
- OSFocusDelaySync .getInstance ().doBackgroundSync (
268
- context ,
269
- appFocusRunnable
270
- );
271
- }
272
-
273
- static class AppFocusRunnable implements Runnable {
274
- private boolean backgrounded , completed ;
275
-
276
- public void run () {
277
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler running AppFocusRunnable" );
278
- backgrounded = true ;
279
- OneSignal .onAppLostFocus ();
280
- completed = true ;
281
- }
282
-
283
- @ Override
284
- public String toString () {
285
- return "AppFocusRunnable{" +
286
- "backgrounded=" + backgrounded +
287
- ", completed=" + completed +
288
- '}' ;
289
- }
290
- }
291
-
292
258
static class KeyboardListener implements ViewTreeObserver .OnGlobalLayoutListener {
293
259
294
260
private final OSSystemConditionController .OSSystemConditionObserver observer ;
0 commit comments