7
7
import android .content .Context ;
8
8
import android .graphics .Color ;
9
9
import android .graphics .drawable .ColorDrawable ;
10
- import android .os .Build ;
11
10
import android .os .Handler ;
12
11
import android .support .annotation .NonNull ;
13
12
import android .support .annotation .Nullable ;
16
15
import android .view .Gravity ;
17
16
import android .view .View ;
18
17
import android .view .ViewGroup ;
19
- import android .view .Window ;
20
18
import android .view .WindowManager ;
21
19
import android .view .animation .Animation ;
22
20
import android .webkit .WebView ;
23
21
import android .widget .LinearLayout ;
24
22
import android .widget .PopupWindow ;
25
23
import android .widget .RelativeLayout ;
26
24
27
- import java .lang .ref .WeakReference ;
28
-
29
25
import static com .onesignal .OSViewUtils .dpToPx ;
30
26
31
27
/**
@@ -76,14 +72,14 @@ interface InAppMessageViewListener {
76
72
private RelativeLayout parentRelativeLayout ;
77
73
private DraggableRelativeLayout draggableRelativeLayout ;
78
74
private InAppMessageViewListener messageController ;
79
- private Runnable dismissSchedule ;
75
+ private Runnable scheduleDismissRunnable ;
80
76
81
77
InAppMessageView (@ NonNull WebView webView , @ NonNull WebViewManager .Position displayLocation , int pageHeight , double dismissDuration ) {
82
78
this .webView = webView ;
83
79
this .displayLocation = displayLocation ;
84
80
this .pageHeight = pageHeight ;
85
81
this .pageWidth = ViewGroup .LayoutParams .MATCH_PARENT ;
86
- this .dismissDuration = dismissDuration ;
82
+ this .dismissDuration = Double . isNaN ( dismissDuration ) ? 0 : dismissDuration ;
87
83
this .hasBackground = !displayLocation .isBanner ();
88
84
}
89
85
@@ -99,20 +95,6 @@ void setMessageController(InAppMessageViewListener messageController) {
99
95
return displayLocation ;
100
96
}
101
97
102
- void destroyView (WeakReference <Activity > weakReference ) {
103
- // WeakReference is the Activity when onStop is called
104
- if (weakReference .get () != null ) {
105
- if (draggableRelativeLayout != null ) {
106
- draggableRelativeLayout .removeAllViews ();
107
- }
108
- if (parentRelativeLayout != null ) {
109
- removeParentLinearLayout (weakReference .get ());
110
- parentRelativeLayout .removeAllViews ();
111
- }
112
- }
113
- markAsDismissed ();
114
- }
115
-
116
98
void showView (Activity activity ) {
117
99
delayShowUntilAvailable (activity );
118
100
}
@@ -252,7 +234,7 @@ public void run() {
252
234
messageController .onMessageWasShown ();
253
235
}
254
236
255
- initDismissIfNeeded ();
237
+ startDismissTimerIfNeeded ();
256
238
}
257
239
});
258
240
}
@@ -298,7 +280,7 @@ private void createPopupWindow(@NonNull RelativeLayout parentRelativeLayout) {
298
280
);
299
281
}
300
282
301
- private void setUpParentLinearLayout (Context context ) {
283
+ private void setUpParentLinearLayout (Context context ) {
302
284
parentRelativeLayout = new RelativeLayout (context );
303
285
parentRelativeLayout .setBackgroundDrawable (new ColorDrawable (Color .TRANSPARENT ));
304
286
parentRelativeLayout .setClipChildren (false );
@@ -321,7 +303,7 @@ void onDismiss() {
321
303
});
322
304
323
305
if (webView .getParent () != null )
324
- ((ViewGroup ) webView .getParent ()).removeAllViews ();
306
+ ((ViewGroup ) webView .getParent ()).removeAllViews ();
325
307
326
308
CardView cardView = createCardView (context );
327
309
cardView .addView (webView );
@@ -359,24 +341,27 @@ private CardView createCardView(Context context) {
359
341
}
360
342
361
343
/**
362
- * Schedule dismiss behavior
344
+ * Schedule dismiss behavior, if IAM has a dismiss after X number of seconds timer.
363
345
*/
364
- private void initDismissIfNeeded () {
365
- if (dismissDuration > 0 && dismissSchedule == null ) {
366
- dismissSchedule = new Runnable () {
367
- public void run () {
368
- if (currentActivity != null ) {
369
- dismissAndAwaitNextMessage (null );
370
- dismissSchedule = null ;
371
- } else {
372
- //for cases when the app is on background and the dismiss is triggered
373
- shouldDismissWhenActive = true ;
374
- }
375
- }
376
- };
346
+ private void startDismissTimerIfNeeded () {
347
+ if (dismissDuration <= 0 )
348
+ return ;
377
349
378
- handler .postDelayed (dismissSchedule , (long ) dismissDuration * 1_000 );
379
- }
350
+ if (scheduleDismissRunnable != null )
351
+ return ;
352
+
353
+ scheduleDismissRunnable = new Runnable () {
354
+ public void run () {
355
+ if (currentActivity != null ) {
356
+ dismissAndAwaitNextMessage (null );
357
+ scheduleDismissRunnable = null ;
358
+ } else {
359
+ // For cases when the app is on background and the dismiss is triggered
360
+ shouldDismissWhenActive = true ;
361
+ }
362
+ }
363
+ };
364
+ handler .postDelayed (scheduleDismissRunnable , (long ) dismissDuration * 1_000 );
380
365
}
381
366
382
367
// Do not add view until activity is ready
@@ -398,8 +383,8 @@ public void run() {
398
383
*/
399
384
void dismissAndAwaitNextMessage (@ Nullable WebViewManager .OneSignalGenericCallback callback ) {
400
385
if (draggableRelativeLayout == null ) {
401
- OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "No host presenter to trigger dismiss animation, counting as dismissed already" );
402
- markAsDismissed ();
386
+ OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "No host presenter to trigger dismiss animation, counting as dismissed already" , new Throwable () );
387
+ dereferenceViews ();
403
388
if (callback != null )
404
389
callback .onComplete ();
405
390
return ;
@@ -417,48 +402,47 @@ private void finishAfterDelay(final WebViewManager.OneSignalGenericCallback call
417
402
OSUtils .runOnMainThreadDelayed (new Runnable () {
418
403
@ Override
419
404
public void run () {
420
- if (hasBackground && parentRelativeLayout != null ) {
405
+ if (hasBackground && parentRelativeLayout != null )
421
406
animateAndDismissLayout (parentRelativeLayout , callback );
422
- } else {
423
- removeViews (callback );
407
+ else {
408
+ cleanupViewsAfterDismiss ();
409
+ if (callback != null )
410
+ callback .onComplete ();
424
411
}
425
412
}
426
413
}, ACTIVITY_FINISH_AFTER_DISMISS_DELAY_MS );
427
414
}
428
415
429
416
/**
430
- * Remove references from the views
417
+ * IAM has been fully dismissed, remove all views and call the onMessageWasDismissed callback
431
418
*/
432
- private void removeViews (WebViewManager .OneSignalGenericCallback callback ) {
433
- if (dismissSchedule != null ) {
434
- //dismissed before the dismiss delay
435
- handler .removeCallbacks (dismissSchedule );
436
- dismissSchedule = null ;
437
- }
438
- if (draggableRelativeLayout != null ) {
439
- draggableRelativeLayout .removeAllViews ();
440
- }
441
-
442
- removeParentLinearLayout (currentActivity );
443
-
444
- if (messageController != null ) {
419
+ private void cleanupViewsAfterDismiss () {
420
+ removeAllViews ();
421
+ if (messageController != null )
445
422
messageController .onMessageWasDismissed ();
446
- }
447
- markAsDismissed ();
448
-
449
- if (callback != null )
450
- callback .onComplete ();
451
423
}
452
424
453
- private void removeParentLinearLayout (Activity currentActivity ) {
454
- if (popupWindow != null )
455
- popupWindow .dismiss ();
425
+ /**
426
+ * Remove all views and dismiss PopupWindow
427
+ */
428
+ void removeAllViews () {
429
+ if (scheduleDismissRunnable != null ) {
430
+ // Dismissed before the dismiss delay
431
+ handler .removeCallbacks (scheduleDismissRunnable );
432
+ scheduleDismissRunnable = null ;
433
+ }
434
+ if (draggableRelativeLayout != null )
435
+ draggableRelativeLayout .removeAllViews ();
436
+
437
+ if (popupWindow != null )
438
+ popupWindow .dismiss ();
439
+ dereferenceViews ();
456
440
}
457
441
458
442
/**
459
443
* Cleans all layout references so this can be cleaned up in the next GC
460
444
*/
461
- private void markAsDismissed () {
445
+ private void dereferenceViews () {
462
446
// Dereference so this can be cleaned up in the next GC
463
447
parentRelativeLayout = null ;
464
448
draggableRelativeLayout = null ;
@@ -531,7 +515,9 @@ private void animateAndDismissLayout(View backgroundView, final WebViewManager.O
531
515
Animator .AnimatorListener animCallback = new AnimatorListenerAdapter () {
532
516
@ Override
533
517
public void onAnimationEnd (Animator animation ) {
534
- removeViews (callback );
518
+ cleanupViewsAfterDismiss ();
519
+ if (callback != null )
520
+ callback .onComplete ();
535
521
}
536
522
};
537
523
0 commit comments