package com.gh.common.dialog import android.app.Dialog import android.content.Context import android.os.Bundle import android.view.KeyEvent import com.gh.common.util.MtaHelper import java.util.concurrent.atomic.AtomicBoolean open class TrackableDialog(context: Context, themeResId: Int, private var mEvent: String, private var mKey: String, private var mCancelValue: String? = null, private var mKeyBackValue: String? = null, private var mLogShowEvent: Boolean = true) : Dialog(context, themeResId) { // 区分此 dialog 是点击 dialog 外部取消的还是点击返回取消的 private val mIsCanceledByClickOutsideOfDialog = AtomicBoolean(true) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setOnCancelListener { if (mIsCanceledByClickOutsideOfDialog.get()) { MtaHelper.onEvent(mEvent, mKey, mCancelValue ?: "点击空白") } } setOnKeyListener { _, keyCode, event -> if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) { mIsCanceledByClickOutsideOfDialog.set(false) MtaHelper.onEvent(mEvent, mKey, mKeyBackValue ?: "点击返回") } false } } override fun show() { super.show() if (mLogShowEvent) { MtaHelper.onEvent(mEvent, mKey, "出现弹窗") } } }