lmw
2023-06-21 bd6a8872438a35d47821c5d5da7fe149e5fed3ab
app/src/main/java/com/okgoincar/base/MyApplication.kt
@@ -3,7 +3,6 @@
import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
@@ -11,7 +10,9 @@
import androidx.multidex.MultiDex
import cn.sinata.rxnetty.NettyClient
import cn.sinata.xldutils.BaseApplication
import cn.sinata.xldutils.utils.TimeUtils
import cn.sinata.xldutils.utils.sysErr
import cn.sinata.xldutils.utils.toast
import com.amap.api.location.AMapLocation
import com.amap.api.track.AMapTrackClient
import com.amap.api.track.ErrorCode
@@ -26,9 +27,13 @@
import com.okgoincar.bean.OrderSimpleData
import com.okgoincar.bean.websocket.HeartBean
import com.okgoincar.netUtls.Api
import com.okgoincar.netUtls.callNet
import com.okgoincar.netUtls.getMapByAny
import com.okgoincar.netUtls.getUserId
import com.okgoincar.slab.MySlabBaseActivity
import com.okgoincar.utils.Cache.CacheKey
import com.okgoincar.utils.LogUtils
import com.tencent.bugly.crashreport.CrashReport
import org.jetbrains.anko.toast
import org.json.JSONObject
import java.security.SecureRandom
@@ -68,13 +73,16 @@
        registerActivityLifecycleCallbacks(this)
//        PlatformConfig.setWeixin("wx65d2c03f04352f90", "7a3a57f9ba78c1f400b75298eec4c7c0")
//        PlatformConfig.setQQZone("101877032", "eab94b262bf9789eec770cde42a6e71e") //qq互联
//        LogUtils.OpenLog(true)
        LogUtils.OpenLog(true)
        CrashReport.initCrashReport(applicationContext, "666d35fada", false)
        initUpPoint()
        initSocket()
    }
    private fun initSocket() {
        Handler(Looper.getMainLooper()).post {
//            toast("websocket初始化")
        }
        NettyClient.getInstance().init(this, Api.SOCKET_SERVER, Api.SOCKET_PORT, true)
        NettyClient.getInstance()
            .addOnMessageListener { message: String? ->
@@ -84,9 +92,12 @@
                    currentTime = System.currentTimeMillis()
                    val json = JSONObject(message)
                    val method = json.optString("method")
//                    Handler(Looper.getMainLooper()).post {
                    if (method!= "OK"&&method!= "PONG")
                        upLog("收到服务端推送,method:${method}")
                    Handler(Looper.getMainLooper()).post {
//                        toast("收到websocket=$method")
//                    }
                    }
                    val data = json.optString("data")
                    val code = json.optInt("code", -1)
                    if (code == 200) {
@@ -158,6 +169,7 @@
            .setOnConnectListener {
                sendHeart()
            }
        sendHeart()
    }
    override fun attachBaseContext(base: Context?) {
@@ -259,13 +271,13 @@
            NettyClient.getInstance()
                .sendMessage(Gson().toJson(bean))
            Handler(Looper.getMainLooper()).post {
                toast("发送location --> $dataBean")
//                toast("发送location --> $dataBean")
            }
            sysErr(dataBean.toString())
        }
    }
    private fun sendHeart() {
    open fun sendHeart() {
        if (getUserId() != -1) {
            Log.i(TAG, "sendHeart: userId === " + getUserId())
            val bean = HeartBean()
@@ -281,7 +293,13 @@
            if ((System.currentTimeMillis() - currentTime) / 1000 > 30) {
                sysErr("sendHeart_websocket 重新连接")
                currentTime = System.currentTimeMillis()
                NettyClient.getInstance().checkNettyState()
//                var s = CacheUtil.get().getAsString("reConnect")
//                s = "$s###初始化重连"
//                CacheUtil.get().put("reConnect", s)
//                NettyClient.getInstance().checkNettyState()
                NettyClient.getInstance().checkNettyStateNull() //reconnect
//                initSocket()
                heartHandler!!.postDelayed({ sendHeart() }, 5000)
            } else {
                sysErr("sendHeart_websocket 发送心跳")
@@ -321,8 +339,8 @@
                    return Gson().fromJson(locationStr, AMapLocation::class.java)
                } else {
                    val amap = AMapLocation("")
                    amap.latitude = -1.0
                    amap.longitude = -1.0
                    amap.latitude = 0.0
                    amap.longitude = 0.0
                    return amap
                }
            } else {
@@ -374,6 +392,16 @@
        fun removeOrderView(view: OrderStatueView?) {
            orderStatueViews.remove(view)
        }
        fun upLog(msg:String){
//            val map = getMapByAny()
//            map["content"] = "${TimeUtils.getCurrentTime()}:=====》$msg"
//            if (appContext!=null)
//                callNet(appContext!!,Api.saveVehicleTerminalLog,map,{}){
//                    toast("丢失一条日志")
//                }
        }
    }
    private val activities = ArrayList<Activity?>()
@@ -400,8 +428,7 @@
        showNum--
        Handler(Looper.getMainLooper()).postDelayed({
            if (showNum == 0) {
                setVideoText("OK出行司机已置于后台,可能会导致听单异常,里程计费差异,请勿切换。")
//                getTTsManager().setVideoText("OK出行司机已置于后台,可能会导致听单异常,里程计费差异,请勿切换。")
                getTTsManager().setVideoText("OK出行司机已置于后台,可能会导致听单异常,里程计费差异,请勿切换。")
            }
        }, 600)
@@ -411,11 +438,5 @@
        activities.add(activity)
    }
    fun setVideoText(s: String?) {
        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.action = "com.wisdom.gps.action.tts"
        intent.putExtra("msg", s)
        sendBroadcast(intent)
    }
}