29
29
30
30
import android .annotation .SuppressLint ;
31
31
import android .app .Activity ;
32
+ import android .content .Context ;
32
33
import android .content .pm .ActivityInfo ;
33
34
import android .content .res .Configuration ;
34
35
import android .os .Build ;
35
- import android .os .Handler ;
36
- import android .os .HandlerThread ;
37
- import android .os .Looper ;
38
36
import android .view .ViewTreeObserver ;
39
37
40
38
import androidx .annotation .NonNull ;
47
45
48
46
class ActivityLifecycleHandler implements OSSystemConditionController .OSSystemConditionHandler {
49
47
50
- public FocusHandlerThread getFocusHandlerThread () {
51
- return FocusHandlerThread .getFocusHandlerThread ();
52
- }
53
-
54
48
abstract static class ActivityAvailableListener {
55
49
void available (@ NonNull Activity activity ) {
56
50
}
@@ -68,6 +62,7 @@ void lostFocus() {
68
62
private static final Map <String , OSSystemConditionController .OSSystemConditionObserver > sSystemConditionObservers = new ConcurrentHashMap <>();
69
63
private static final Map <String , KeyboardListener > sKeyboardListeners = new ConcurrentHashMap <>();
70
64
65
+ private static AppFocusRunnable appFocusRunnable ;
71
66
@ SuppressLint ("StaticFieldLeak" )
72
67
private Activity curActivity = null ;
73
68
private boolean nextResumeIsFirstActivity = false ;
@@ -170,18 +165,24 @@ private void onOrientationChanged() {
170
165
}
171
166
172
167
private void handleLostFocus () {
173
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "Handling lost focus" );
174
- FocusHandlerThread .getFocusHandlerThread ().runRunnable (new AppFocusRunnable ());
168
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler Handling lost focus" );
169
+ if (appFocusRunnable != null && appFocusRunnable .backgrounded && !appFocusRunnable .completed )
170
+ return ;
171
+
172
+ OneSignal .getFocusTimeController ().appStopped ();
173
+ OSFocusDelaySync .getInstance ().scheduleSyncTask (OneSignal .appContext );
175
174
}
176
175
177
176
private void handleFocus () {
178
- if (FocusHandlerThread .getFocusHandlerThread ().hasBackgrounded () || nextResumeIsFirstActivity ) {
177
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler handleFocus, with runnable: " + appFocusRunnable + " nextResumeIsFirstActivity: " + nextResumeIsFirstActivity );
178
+ if (hasBackgrounded () || nextResumeIsFirstActivity ) {
179
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler reset background state, call app focus" );
179
180
nextResumeIsFirstActivity = false ;
180
- FocusHandlerThread . getFocusHandlerThread (). resetBackgroundState ();
181
+ resetBackgroundState ();
181
182
OneSignal .onAppFocus ();
182
183
} else {
183
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "Cancel focus lost worker " );
184
- FocusHandlerThread . getFocusHandlerThread ().stopScheduledRunnable ( );
184
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler cancel background lost focus sync task " );
185
+ OSFocusDelaySync . getInstance ().cancelBackgroundSyncTask ( OneSignal . appContext );
185
186
}
186
187
}
187
188
@@ -224,7 +225,6 @@ public Activity getCurActivity() {
224
225
}
225
226
226
227
public void setCurActivity (Activity activity ) {
227
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "setCurActivity activity: " + activity );
228
228
curActivity = activity ;
229
229
for (Map .Entry <String , ActivityAvailableListener > entry : sActivityAvailableListeners .entrySet ()) {
230
230
entry .getValue ().available (curActivity );
@@ -249,68 +249,47 @@ void setNextResumeIsFirstActivity(boolean nextResumeIsFirstActivity) {
249
249
this .nextResumeIsFirstActivity = nextResumeIsFirstActivity ;
250
250
}
251
251
252
- static class FocusHandlerThread extends HandlerThread {
253
- private static FocusHandlerThread timeoutHandler ;
254
- private Handler mHandler ;
255
- private AppFocusRunnable appFocusRunnable ;
256
-
257
- static FocusHandlerThread getFocusHandlerThread () {
258
- if (timeoutHandler == null ) {
259
- synchronized (SYNC_LOCK ) {
260
- if (timeoutHandler == null )
261
- timeoutHandler = new FocusHandlerThread ();
262
- }
263
- }
264
- return timeoutHandler ;
265
- }
266
-
267
- private FocusHandlerThread () {
268
- super ("FocusHandlerThread" );
269
- start ();
270
- mHandler = new Handler (getLooper ());
271
- }
272
-
273
- Looper getHandlerLooper () {
274
- return mHandler .getLooper ();
275
- }
276
-
277
- void resetBackgroundState () {
278
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "FocusHandlerThread resetBackgroundState" );
279
- if (appFocusRunnable != null )
280
- appFocusRunnable .backgrounded = false ;
281
- }
282
-
283
- void stopScheduledRunnable () {
284
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "FocusHandlerThread stopScheduledRunnable" );
285
- mHandler .removeCallbacksAndMessages (null );
286
- }
287
-
288
- void runRunnable (AppFocusRunnable runnable ) {
289
- if (appFocusRunnable != null && appFocusRunnable .backgrounded && !appFocusRunnable .completed )
290
- return ;
252
+ void resetBackgroundState () {
253
+ if (appFocusRunnable != null )
254
+ appFocusRunnable .backgrounded = false ;
255
+ }
291
256
292
- appFocusRunnable = runnable ;
293
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "Post delay on app lost focus method" );
294
- mHandler .removeCallbacksAndMessages (null );
295
- mHandler .postDelayed (runnable , 2000 );
296
- }
257
+ boolean hasBackgrounded () {
258
+ return appFocusRunnable != null && appFocusRunnable .backgrounded ;
259
+ }
297
260
298
- boolean hasBackgrounded () {
299
- return appFocusRunnable != null && appFocusRunnable .backgrounded ;
300
- }
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
+ );
301
271
}
302
272
303
- private static class AppFocusRunnable implements Runnable {
273
+ static class AppFocusRunnable implements Runnable {
304
274
private boolean backgrounded , completed ;
305
275
306
276
public void run () {
277
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "ActivityLifecycleHandler running AppFocusRunnable" );
307
278
backgrounded = true ;
308
279
for (Map .Entry <String , ActivityAvailableListener > entry : sActivityAvailableListeners .entrySet ()) {
309
280
entry .getValue ().lostFocus ();
310
281
}
311
282
OneSignal .onAppLostFocus ();
312
283
completed = true ;
313
284
}
285
+
286
+ @ Override
287
+ public String toString () {
288
+ return "AppFocusRunnable{" +
289
+ "backgrounded=" + backgrounded +
290
+ ", completed=" + completed +
291
+ '}' ;
292
+ }
314
293
}
315
294
316
295
static class KeyboardListener implements ViewTreeObserver .OnGlobalLayoutListener {
0 commit comments