package com.hongyegroup.zhurijob.app

import android.app.ActivityManager
import android.content.Context
import android.os.Build
import android.os.Process
import android.webkit.WebView
import com.guadou.cs_cptservices.YYConstants
import com.guadou.lib_baselib.base.BaseApplication
import com.guadou.lib_baselib.engine.SP
import com.guadou.lib_baselib.ext.checkEmpty
import com.guadou.lib_baselib.engine.getString
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MyApplication : BaseApplication() {

    override fun onCreate() {
        super.onCreate()

//        initBugly()

        //WebView的兼容
        setupWebView()

        //设置全局的一些常量与状态
        setupState()
    }

    private fun setupState() {

        val isLogin = !SP().getString(YYConstants.SP_KEY_TOKEN, "").checkEmpty()
        setUserIsLogin(isLogin)

    }

    //兼容Android-P
    private fun setupWebView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val processName = getProcessName(this)
            if ("com.monstarlab.yyjobs" != processName) {
                WebView.setDataDirectorySuffix(processName ?: "app")
            }
        }
    }

    //获取当前的进程名称
    private fun getProcessName(context: Context?): String? {
        if (context == null) return ""
        val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (processInfo in manager.runningAppProcesses) {
            if (processInfo.pid == Process.myPid()) {
                return processInfo.processName
            }
        }
        return ""
    }

    /**
     * 初始化Bugly，捕获到异常信息上传到Bugly平台
     */
//    private fun initBugly() {
//
//        CrashReport.initCrashReport(applicationContext, "afcd34c05f", true)
//
//    }

}