package  com.test.basd.smartjunkcleaner.display

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.test.basd.smartjunkcleaner.helps.EventHelper
import com.test.basd.smartjunkcleaner.helps.LogEx

class CloseNotificationReceiver : BroadcastReceiver() {
    private val TAG = "CloseNotificationReceiver"
    override fun onReceive(context: Context?, intent: Intent?) {
        LogEx.logDebug(TAG, "onReceive intent.action=${intent?.action}")
        //cancel事件,经过测试Intent.action这里无法传递参数
        if (intent?.action == null) {
            EventHelper.event("notification_cancel", "use cancel notification!")
        }
        intent?.let {
            EventHelper.event("notification_userCancel", "userCancel=true")
            val notificationId = it.getIntExtra("notificationId", -1)

            LogEx.logDebug(TAG, "notificationId=$notificationId action=$action")
            if (notificationId != -1 && it.action != "DELETE_NOTIFICATION") {
                val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
                notificationManager?.cancel(NotificationUtils.NOTIFICATION_TAG, notificationId)
            }
        }
    }

    companion object {
        const val action = "com.file.notification.CloseNotificationReceiver"
    }
}
