package com.xhly.manageapp.broadcastreceiver

import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.TelephonyManager
import com.xhly.corelib.Const
import com.xhly.corelib.eventbus.UIEvent
import com.xhly.corelib.utils.LogShow

class SimStateReceive : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        println("sim state changed")
        try {
            if (intent.action == ACTION_SIM_STATE_CHANGED) {
                val tm = context.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager
                val state = tm.simState
                when (state) {
                    TelephonyManager.SIM_STATE_READY -> {
                        LogShow("有SIM卡$SIM_VALID"+tm.simSerialNumber)
                        UIEvent(Const.SIMINSTALL).post()
                    }

                    TelephonyManager.SIM_STATE_UNKNOWN, TelephonyManager.SIM_STATE_ABSENT,
                    TelephonyManager.SIM_STATE_PIN_REQUIRED, TelephonyManager.SIM_STATE_PUK_REQUIRED,
                    TelephonyManager.SIM_STATE_NETWORK_LOCKED -> {
                        LogShow("没有SIM卡$SIM_INVALID")
                    }

                    else -> {
                        LogShow("没有SIM卡$SIM_INVALID")
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    companion object {
        private const val ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED"
        private const val SIM_VALID = 0
        private const val SIM_INVALID = 1
    }
}
