package com.guadou.cs_cptservices.base

import android.app.ActivityManager
import android.content.Context
import android.os.Build
import android.os.Process
import android.webkit.WebView
import com.guadou.lib_baselib.base.BaseApplication

/**
 * 业务逻辑层的Application
 */
open class YYApplication : BaseApplication() {

    companion object {

        var mCurAppVersion = "" //当前App的版本号 用于网络请求全局添加请求头

    }

    override fun onCreate() {
        super.onCreate()

        //赋值App全局版本号
        mCurAppVersion = packageManager.getPackageInfo(packageName, 0).versionName

//        initBugly()

        //WebView的兼容
        setupWebView()
    }

    //兼容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)
            }
        }
    }

    //获取当前的进程名称
    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)
//        val debug: Boolean = BuildConfig.DEBUG
//        //能捕获自己的异常，上报给bugly
//        Cockroach.install(this, object : ExceptionHandler() {
//            override fun onUncaughtExceptionHappened(thread: Thread, throwable: Throwable) {
//                YYLogUtils.e("--->onUncaughtExceptionHappened:" + thread + "<--->" + throwable.message)
//                Handler(Looper.getMainLooper()).post {
//                    if (debug) {
//                        //如果是测试环境那么吐司错误信息
//                        toast(throwable.toString())
//                    }
//                    //上报错误信息
//                    CrashReport.postCatchedException(throwable)
//                }
//            }
//
//            override fun onBandageExceptionHappened(throwable: Throwable) {
//                throwable.printStackTrace() //打印警告级别log，该throwable可能是最开始的bug导致的，无需关心
//                YYLogUtils.e("Cockroach Worked:" + throwable.message)
//                if (debug) {
//                    //如果是测试环境那么吐司错误信息
//                    toast(throwable.toString())
//                }
//                //上报错误信息
//                CrashReport.postCatchedException(throwable)
//            }
//
//            override fun onEnterSafeMode() {
//                YYLogUtils.e("Cockroach onEnterSafeMode")
//            }
//
//            override fun onMayBeBlackScreen(e: Throwable) {
//                val thread = Looper.getMainLooper().thread
//                Log.e("AndroidRuntime", "--->onUncaughtExceptionHappened:$thread<---", e)
//                //黑屏时建议直接杀死app
//                ActivityManage.finishAllActivity()
//                exitProcess(0)
//            }
//        })
//    }

}