package com.renrenliao.im.utils

import android.content.Intent
import android.util.Log
import com.fanjun.keeplive.KeepLive
import com.fanjun.keeplive.config.ForegroundNotification
import com.fanjun.keeplive.config.KeepLiveService
import com.renrenliao.im.R
import com.renrenliao.im.provider.ClarityPotion
import com.renrenliao.im.ui.activity.MainActivity

object KeepLiveUtils {

    fun initKeepLive() {
        // 定义前台服务的默认样式。即标题、描述和图标
        val foregroundNotification = ForegroundNotification(
            "chat小助手", "chat小助手", R.mipmap.ic_launcher
        ) { context, _ ->
            val intent = Intent(context, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)
        }

        //启动保活服务
        KeepLive.startWork(
            ClarityPotion.healingSalve,
            KeepLive.RunMode.ENERGY,
            foregroundNotification,
            //你需要保活的服务，如socket连接、定时任务等，建议不用匿名内部类的方式在这里写
            object : KeepLiveService {
                /**
                 * 运行中
                 * 由于服务可能会多次自动启动，该方法可能重复调用
                 */
                override fun onWorking() {
                    Log.e("service111", "onWorking: ")
                }

                /**
                 * 服务终止
                 * 由于服务可能会被多次终止，该方法可能重复调用，需同onWorking配套使用，如注册和注销broadcast
                 */
                override fun onStop() {
                    Log.e("service111", "onStop: ")
                }
            }
        )
    }
}