package com.xhly.manageapp.broadcastreceiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.xhly.corelib.Const
import com.xhly.corelib.eventbus.UIEvent
import com.xhly.manageapp.utils.InstallUtils

//(自定义广播类)  --自己命名的 XXXXReceiver
class AppInstallReceiver : BroadcastReceiver() {
    private val TAG = this.javaClass.simpleName
    override fun onReceive(context: Context, intent: Intent) {
        val pm = context.packageManager
        Log.i("执行监听", "--------应用监听" + intent.data!!.schemeSpecificPart)
        if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_ADDED)) {
            val packageName = intent.data!!.schemeSpecificPart
            Log.i(TAG, "--------安装成功$packageName")
            //Toast.makeText(context, "安装成功$packageName", Toast.LENGTH_LONG).show()
            InstallUtils.installSuccess(packageName)
            UIEvent(Const.APPINSTALL).setMessage(packageName).post()
        } else if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_REPLACED)) {
            val packageName = intent.data!!.schemeSpecificPart
            Log.i(TAG, "--------替换成功$packageName")
           // Toast.makeText(context, "替换成功$packageName", Toast.LENGTH_LONG).show()
        } else if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_REMOVED)) {
            val packageName = intent.data!!.schemeSpecificPart
            Log.i(TAG, "--------卸载成功$packageName")
          //  Toast.makeText(context, "卸载成功$packageName", Toast.LENGTH_LONG).show()
            UIEvent(Const.APPUNINSTALL).setMessage(packageName).post()
        }
    }
}
