Skip to content

Commit 222c20b

Browse files
committed
Extend PopupWindow to know of dismissal
* By extending PopupWindow, we can mark it as dismissed by us, and we can add a custom listener to the dismiss event and receive this flag. * In InAppMessageView which owns the popup window, it will listen to the popup window's dismissal event and trigger the post-dismissal flow. It also sets the manual flag when `removeAllViews()` is invoked.
1 parent 8f470c0 commit 222c20b

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/display/impl/InAppMessageView.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import android.view.ViewGroup
1414
import android.view.WindowManager
1515
import android.view.animation.Animation
1616
import android.webkit.WebView
17-
import android.widget.PopupWindow
1817
import android.widget.RelativeLayout
1918
import androidx.cardview.widget.CardView
2019
import androidx.core.widget.PopupWindowCompat
@@ -51,7 +50,7 @@ internal class InAppMessageView(
5150
private val disableDragDismiss: Boolean,
5251
private val hideGrayOverlay: Boolean,
5352
) {
54-
private var popupWindow: PopupWindow? = null
53+
private var popupWindow: OSPopupWindow? = null
5554

5655
internal interface InAppMessageViewListener {
5756
fun onMessageWasDisplayed()
@@ -86,6 +85,16 @@ internal class InAppMessageView(
8685
private var isDismissTimerSet: Boolean = false
8786
private var cancelDismissTimer: Boolean = false
8887

88+
private val popupWindowListener =
89+
object : OSPopupWindow.PopupWindowListener {
90+
override fun onDismiss(wasDismissedManually: Boolean?) {
91+
if (wasDismissedManually != true) {
92+
Logging.debug("PopupWindowListener.onDismiss called by the system.")
93+
messageController?.onMessageWasDismissed()
94+
}
95+
}
96+
}
97+
8998
init {
9099
setMarginsFromContent(messageContent)
91100
}
@@ -271,11 +280,12 @@ internal class InAppMessageView(
271280
*/
272281
private fun createPopupWindow(parentRelativeLayout: RelativeLayout) {
273282
popupWindow =
274-
PopupWindow(
283+
OSPopupWindow(
275284
parentRelativeLayout,
276285
if (hasBackground) WindowManager.LayoutParams.MATCH_PARENT else pageWidth,
277286
if (hasBackground) WindowManager.LayoutParams.MATCH_PARENT else WindowManager.LayoutParams.WRAP_CONTENT,
278287
false,
288+
popupWindowListener
279289
)
280290
popupWindow?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
281291
popupWindow?.isTouchable = true
@@ -486,17 +496,14 @@ internal class InAppMessageView(
486496
*/
487497
fun removeAllViews() {
488498
Logging.debug("InAppMessageView.removeAllViews()")
499+
popupWindow?.wasDismissedManually = true
489500
if (isDismissTimerSet) {
490501
// Dismissed before the dismiss delay
491502
cancelDismissTimer = true
492503
}
493-
if (draggableRelativeLayout != null) {
494-
draggableRelativeLayout!!.removeAllViews()
495-
}
496504

497-
if (popupWindow != null) {
498-
popupWindow!!.dismiss()
499-
}
505+
draggableRelativeLayout?.removeAllViews()
506+
popupWindow?.dismiss()
500507

501508
dereferenceViews()
502509
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.onesignal.inAppMessages.internal.display.impl
2+
3+
import android.view.View
4+
import android.widget.PopupWindow
5+
6+
/**
7+
* Custom PopupWindow to listen to dismiss event
8+
*/
9+
internal class OSPopupWindow(
10+
contentView: View?,
11+
width: Int,
12+
height: Int,
13+
focusable: Boolean,
14+
private val listener: PopupWindowListener,
15+
) : PopupWindow(contentView, width, height, focusable) {
16+
/**
17+
* Used to differentiate when this popup window has been dismissed programmatically by OneSignal vs by the system.
18+
* When the back button is pressed while an IAM is displaying, the system will dismiss the popup window
19+
* without the SDK's awareness. We need to know of this event and run the post-dismissal flows.
20+
* Using this flag will prevent duplicate flows from triggering.
21+
*/
22+
var wasDismissedManually: Boolean? = null
23+
24+
internal interface PopupWindowListener {
25+
fun onDismiss(wasDismissedManually: Boolean?)
26+
}
27+
28+
override fun dismiss() {
29+
super.dismiss()
30+
listener.onDismiss(wasDismissedManually)
31+
}
32+
}

0 commit comments

Comments
 (0)