package com.simplecleaner.app.business.push.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.simplecleaner.app.bean.push.NotificationSendBean
import com.simplecleaner.app.bean.push.NotificationSendBean.Companion.POPUP_WHERE_PACKAGE
import com.simplecleaner.app.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))
    }
}