package com.xhly.manageapp.broadcastreceiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.xhly.corelib.Const
import com.xhly.corelib.eventbus.UIEvent
import com.xhly.corelib.utils.LogShow
import com.xhly.corelib.utils.SystemUtil

class SDReceiver: BroadcastReceiver() {
    companion object{
        var installLastTime=0L
    }
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        when (action) {
            Intent.ACTION_MEDIA_CHECKING->
            {
                //SD卡正在检查
            }
             Intent.ACTION_MEDIA_MOUNTED->
             {
                 //SD卡挂载成功,可能会发送两次
                 if (installLastTime==0L||(System.currentTimeMillis()- installLastTime>1000)&&SystemUtil.isSDCardMounted(context)){
                     installLastTime=System.currentTimeMillis()
                     LogShow("sd卡已插入")
                     UIEvent(Const.SDINSTALL).post()
                 }
                 /*LogShow("sd卡已插入")
                 UIEvent(Const.SDINSTALL).post()*/
             }
            Intent.ACTION_MEDIA_EJECT->{
                //SD卡拔出
                LogShow("sd卡已拔出")
                UIEvent(Const.SDUNINSTALL).post()
            }
            Intent.ACTION_MEDIA_UNMOUNTED->{
                //SD卡卸载成功
            }
        }
    }
}