Skip to content

Properly handle in-app messages dismissed by back press #2328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.view.ViewGroup
import android.view.WindowManager
import android.view.animation.Animation
import android.webkit.WebView
import android.widget.PopupWindow
import android.widget.RelativeLayout
import androidx.cardview.widget.CardView
import androidx.core.widget.PopupWindowCompat
Expand Down Expand Up @@ -51,7 +50,7 @@ internal class InAppMessageView(
private val disableDragDismiss: Boolean,
private val hideGrayOverlay: Boolean,
) {
private var popupWindow: PopupWindow? = null
private var popupWindow: OSPopupWindow? = null

internal interface InAppMessageViewListener {
fun onMessageWasDisplayed()
Expand Down Expand Up @@ -86,6 +85,16 @@ internal class InAppMessageView(
private var isDismissTimerSet: Boolean = false
private var cancelDismissTimer: Boolean = false

private val popupWindowListener =
object : OSPopupWindow.PopupWindowListener {
override fun onDismiss(wasDismissedManually: Boolean?) {
if (wasDismissedManually != true) {
Logging.debug("PopupWindowListener.onDismiss called by the system.")
messageController?.onMessageWasDismissed()
}
}
}

init {
setMarginsFromContent(messageContent)
}
Expand Down Expand Up @@ -271,11 +280,12 @@ internal class InAppMessageView(
*/
private fun createPopupWindow(parentRelativeLayout: RelativeLayout) {
popupWindow =
PopupWindow(
OSPopupWindow(
parentRelativeLayout,
if (hasBackground) WindowManager.LayoutParams.MATCH_PARENT else pageWidth,
if (hasBackground) WindowManager.LayoutParams.MATCH_PARENT else WindowManager.LayoutParams.WRAP_CONTENT,
false,
popupWindowListener,
)
popupWindow?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
popupWindow?.isTouchable = true
Expand Down Expand Up @@ -486,17 +496,14 @@ internal class InAppMessageView(
*/
fun removeAllViews() {
Logging.debug("InAppMessageView.removeAllViews()")
popupWindow?.wasDismissedManually = true
if (isDismissTimerSet) {
// Dismissed before the dismiss delay
cancelDismissTimer = true
}
if (draggableRelativeLayout != null) {
draggableRelativeLayout!!.removeAllViews()
}

if (popupWindow != null) {
popupWindow!!.dismiss()
}
draggableRelativeLayout?.removeAllViews()
popupWindow?.dismiss()

dereferenceViews()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.onesignal.inAppMessages.internal.display.impl

import android.view.View
import android.widget.PopupWindow

/**
* Custom PopupWindow to listen to dismiss event
*/
internal class OSPopupWindow(
contentView: View?,
width: Int,
height: Int,
focusable: Boolean,
private val listener: PopupWindowListener,
) : PopupWindow(contentView, width, height, focusable) {
/**
* Used to differentiate when this popup window has been dismissed programmatically by OneSignal vs by the system.
* When the back button is pressed while an IAM is displaying, the system will dismiss the popup window
* without the SDK's awareness. We need to know of this event and run the post-dismissal flows.
* Using this flag will prevent duplicate flows from triggering.
*/
var wasDismissedManually: Boolean? = null

internal interface PopupWindowListener {
fun onDismiss(wasDismissedManually: Boolean?)
}

override fun dismiss() {
super.dismiss()
listener.onDismiss(wasDismissedManually)
}
}
Loading