package com.dumpster.cleaner.business.push.receiver

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.dumpster.cleaner.bean.push.NotificationSendBean
import com.dumpster.cleaner.bean.push.NotificationSendBean.Companion.POPUP_WHERE_PACKAGE
import com.dumpster.cleaner.business.push.notification.MyNotificationManager

class PackageReceiver : BroadcastReceiver() {

    companion object {
        fun register(context: Context) {
            val intentFilter = IntentFilter().apply {
                addAction(Intent.ACTION_PACKAGE_REMOVED)
                addAction(Intent.ACTION_PACKAGE_ADDED)
                addDataScheme("package")
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                context.registerReceiver(PackageReceiver(), intentFilter, Context.RECEIVER_EXPORTED)
            } else {
                context.registerReceiver(PackageReceiver(), intentFilter)
            }
        }
    }

    override fun onReceive(context: Context, intent: Intent?) {
        MyNotificationManager.submitSendBean(NotificationSendBean(context, POPUP_WHERE_PACKAGE))
    }
}