| | |
| | | package com.driver.igotrip.base |
| | | |
| | | import android.R.id.message |
| | | import android.annotation.SuppressLint |
| | | import android.app.Activity |
| | | import android.app.Application |
| | |
| | | import android.content.Intent |
| | | import android.os.* |
| | | import android.util.Log |
| | | import androidx.annotation.RequiresApi |
| | | import androidx.fragment.app.Fragment |
| | | import androidx.fragment.app.FragmentManager |
| | | import cn.jpush.android.api.JPushInterface |
| | | import cn.jpush.android.api.TagAliasCallback |
| | | import cn.sinata.rxnetty.LogHelper.initLogs |
| | | import cn.sinata.rxnetty.LogHelper.logRecord |
| | | import cn.sinata.rxnetty.NettyClient |
| | | import cn.sinata.xldutils.BaseApplication |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import cn.sinata.xldutils.utils.sysErr |
| | | import com.driver.igotrip.BuildConfig |
| | | import com.driver.igotrip.R |
| | | import com.driver.igotrip.base.BaseEvent.REFREASH_ORDERLIST |
| | | import com.driver.igotrip.bean.OrderBean |
| | | import com.driver.igotrip.bean.OrderResData |
| | | import com.driver.igotrip.bean.OrderSimpleData |
| | | import com.driver.igotrip.bean.websocket.HeartBean |
| | | import com.driver.igotrip.netUtls.* |
| | | import com.driver.igotrip.netUtls.Api.getDriverOrderMessage |
| | | import com.driver.igotrip.ui.main.TripActivity |
| | | import com.driver.igotrip.ui.main.small_cargo.SmallCargoActivity |
| | | import com.driver.igotrip.ui.pub.LoginActivity |
| | | import com.driver.igotrip.utils.AudioRecoderUtils |
| | | import com.driver.igotrip.utils.Cache.CacheKey |
| | | import com.driver.igotrip.utils.Cache.CacheUtil |
| | | import com.driver.igotrip.utils.DES |
| | | import com.driver.igotrip.utils.LanguageManager |
| | | import com.driver.igotrip.utils.LanguageUtil |
| | | import com.driver.igotrip.utils.LogUtils |
| | | import com.google.android.gms.maps.model.LatLng |
| | | import com.google.cloud.texttospeech.v1.* |
| | |
| | | import com.tencent.bugly.crashreport.CrashReport |
| | | import com.umeng.commonsdk.UMConfigure |
| | | import com.umeng.socialize.PlatformConfig |
| | | import com.umeng.socialize.utils.DeviceConfigInternal.context |
| | | import com.xuexiang.xui.XUI |
| | | import kotlinx.coroutines.MainScope |
| | | import kotlinx.coroutines.delay |
| | | import kotlinx.coroutines.launch |
| | | import org.greenrobot.eventbus.EventBus |
| | | import org.jetbrains.anko.startActivity |
| | | import org.jetbrains.anko.toast |
| | |
| | | return "MyApplication" |
| | | } |
| | | |
| | | @RequiresApi(api = Build.VERSION_CODES.DONUT) |
| | | override fun onCreate() { |
| | | super.onCreate() |
| | | appContext = this |
| | |
| | | PlatformConfig.setQQZone("1111276329", "JuAotVjQcUiesxgp") //qq互联 |
| | | CrashReport.initCrashReport(this, "163c95c374", true); |
| | | LogUtils.OpenLog(true) |
| | | initLogs() |
| | | initSocket() |
| | | JPushInterface.setDebugMode(true) |
| | | JPushInterface.init(this) |
| | |
| | | |
| | | override fun attachBaseContext(base: Context?) { |
| | | var language = SPUtils.getString("language") |
| | | // super.attachBaseContext(LanguageUtil.changeAppLanguage(base, language, "")) |
| | | // super.attachBaseContext(LanguageUtil.changeAppLanguage(base, language, "")) |
| | | |
| | | super.attachBaseContext(LanguageManager.wrapContext(base)) |
| | | } |
| | |
| | | } |
| | | |
| | | private fun initSocket() { |
| | | Log.e("sendLocation: ","开始链接" ) |
| | | Log.e("sendLocation: ", "开始链接") |
| | | NettyClient.getInstance().init(this, Api.SOCKET_SERVER, Api.SOCKET_PORT, true) |
| | | NettyClient.getInstance() |
| | | .addOnMessageListener { message: String -> |
| | | initnetdata(message) |
| | | } |
| | | NettyClient.getInstance() |
| | | .setOnConnectListener { |
| | | sendHeart() |
| | | } |
| | | if(frist){ |
| | | /* MainScope().launch { |
| | | while (true){ |
| | | delay(5000) |
| | | var map = getMapByAny() |
| | | if (getDriverOrderMessage.startsWith("api")) { |
| | | map["appid"] = getAppId() |
| | | var sign = DES.getSign(map) |
| | | map["sign"] = sign |
| | | } |
| | | HttpManager.getInstance().post(getDriverOrderMessage, map, object : MyNoMsgObserver(this@MyApplication) { |
| | | override fun success(responseString: String?) { |
| | | Log.e("长连接接口消息: ",responseString.toString() ) |
| | | initnetdata(responseString.toString()) |
| | | NettyClient.getInstance().addOnMessageListener { message: String -> |
| | | initnetdata(message) |
| | | } |
| | | NettyClient.getInstance().setOnConnectListener { |
| | | sendHeart() |
| | | } |
| | | if (frist) { |
| | | /* MainScope().launch { |
| | | while (true){ |
| | | delay(5000) |
| | | var map = getMapByAny() |
| | | if (getDriverOrderMessage.startsWith("api")) { |
| | | map["appid"] = getAppId() |
| | | var sign = DES.getSign(map) |
| | | map["sign"] = sign |
| | | } |
| | | }) |
| | | } |
| | | }*/ |
| | | frist=false |
| | | } |
| | | HttpManager.getInstance().post(getDriverOrderMessage, map, object : MyNoMsgObserver(this@MyApplication) { |
| | | override fun success(responseString: String?) { |
| | | Log.e("长连接接口消息: ",responseString.toString() ) |
| | | initnetdata(responseString.toString()) |
| | | } |
| | | }) |
| | | } |
| | | }*/ |
| | | frist = false |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | fun initnetdata(message:String,isnet: Boolean?=false){ |
| | | fun initnetdata(message: String, isnet: Boolean? = false) { |
| | | try { |
| | | Log.e("socket_message_by", message) |
| | | Log.d("zmc-stock", message) |
| | | // sysErr("收到websocket消息") |
| | | if (!isLogin&&isnet==false) { |
| | | if (!isLogin && isnet == false) { |
| | | return |
| | | } |
| | | currentTime = System.currentTimeMillis() |
| | | val json = JSONObject(message) |
| | | val method = json.optString("method") |
| | | Handler(Looper.getMainLooper()).post { |
| | | // ToastUtils.showShortToast(this, "收到websocket=$method") |
| | | if (method != "OK") { |
| | | Log.d("zmc-socket", message) |
| | | } |
| | | val data = json.optString("data") |
| | | val code = json.optInt("code", -1) |
| | |
| | | CacheKey.putKeyStr("isAgreen", "1") |
| | | NettyClient.getInstance().stopService() |
| | | isLogin = false |
| | | var intent = Intent() |
| | | val intent = Intent() |
| | | intent.setClass(this, LoginActivity::class.java) |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| | | startActivity(intent) |
| | |
| | | ) |
| | | EventBus.getDefault().post(BaseEvent(BaseEvent.FORCED_OFFLINE)) |
| | | val act = activities[activities.size - 1] |
| | | (act as PublicServiceActivity).forceOffline(simpleData.audioUrl, simpleData.msg) |
| | | (act as PublicServiceActivity).forceOffline( |
| | | simpleData.audioUrl, |
| | | simpleData.msg |
| | | ) |
| | | if (act is PublicServiceDialogActivity) act.finish() |
| | | } |
| | | |
| | |
| | | ) |
| | | if (act is PublicServiceDialogActivity) act.finish() |
| | | } |
| | | 4,5->{ |
| | | act?.startActivity<SmallCargoActivity>("orderId" to orderSimpleData.orderId.toString(), "orderType" to orderSimpleData.orderType.toString()) |
| | | |
| | | 4, 5 -> { |
| | | act?.startActivity<SmallCargoActivity>( |
| | | "orderId" to orderSimpleData.orderId.toString(), |
| | | "orderType" to orderSimpleData.orderType.toString() |
| | | ) |
| | | } |
| | | |
| | | 3 -> { |
| | | |
| | | } |
| | |
| | | } |
| | | |
| | | Const.SOCKET_METHOD.REASSIGN -> { |
| | | Log.e( |
| | | "zmc-socket", |
| | | "EventBus.getDefault().post(BaseEvent(BaseEvent.REFREASH_MAIN))" |
| | | ) |
| | | EventBus.getDefault().post(BaseEvent(BaseEvent.REFREASH_MAIN)) |
| | | |
| | | val orderSimpleData = |
| | | Gson().fromJson( |
| | | data, |
| | | OrderResData::class.java |
| | | ) |
| | | val act = activities[activities.size - 1] |
| | | when (orderSimpleData.orderType) { |
| | | // 1, 2, 4 -> { |
| | | // (act as PublicServiceActivity).showOrder( |
| | | // orderSimpleData.audioUrl, |
| | | // orderSimpleData.orderId.toString(), |
| | | // orderSimpleData.orderType.toString(), |
| | | // 30, |
| | | // 2 |
| | | // ) |
| | | // if (act is PublicServiceDialogActivity) act.finish() |
| | | // } |
| | | 4,5->{ |
| | | act?.startActivity<SmallCargoActivity>("orderId" to orderSimpleData.orderId.toString(), "orderType" to orderSimpleData.orderType.toString()) |
| | | } |
| | | else->{ |
| | | act?.startActivity<TripActivity>("orderId" to orderSimpleData.orderId.toString(), "orderType" to orderSimpleData.orderType.toString()) |
| | | } |
| | | } |
| | | // val orderSimpleData = |
| | | // Gson().fromJson( |
| | | // data, |
| | | // OrderResData::class.java |
| | | // ) |
| | | // val act = activities[activities.size - 1] |
| | | // when (orderSimpleData.orderType) { |
| | | //// 1, 2, 4 -> { |
| | | //// (act as PublicServiceActivity).showOrder( |
| | | //// orderSimpleData.audioUrl, |
| | | //// orderSimpleData.orderId.toString(), |
| | | //// orderSimpleData.orderType.toString(), |
| | | //// 30, |
| | | //// 2 |
| | | //// ) |
| | | //// if (act is PublicServiceDialogActivity) act.finish() |
| | | //// } |
| | | // 4, 5 -> { |
| | | // act?.startActivity<SmallCargoActivity>( |
| | | // "orderId" to orderSimpleData.orderId.toString(), |
| | | // "orderType" to orderSimpleData.orderType.toString() |
| | | // ) |
| | | // } |
| | | // |
| | | // else -> { |
| | | // act?.startActivity<TripActivity>( |
| | | // "orderId" to orderSimpleData.orderId.toString(), |
| | | // "orderType" to orderSimpleData.orderType.toString() |
| | | // ) |
| | | // } |
| | | // } |
| | | |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private fun getOrderWhenNoSocket(){ |
| | | Log.e("sendLocation: ", "定时获取司机订单数据" + (getUserId() != -1).toString()) |
| | | if (getUserId() != -1) { |
| | | val map = getMapByAny() |
| | | callNet1(this, Api.getDriverOrderMessage, map, clickAction = { |
| | | logRecord(TAG, "getOrderWhenNoSocket 获取订单成功 $it") |
| | | initnetdata(it ?: "") |
| | | }, { |
| | | logRecord(TAG, "getOrderWhenNoSocket 获取订单失败") |
| | | }, false) |
| | | } |
| | | } |
| | | |
| | | private fun sendLocation() { |
| | | Log.e("sendLocation: ","发送定位"+(getUserId() != -1).toString() ) |
| | | Log.e("sendLocation: ","发送定位"+(aMapLocation != null).toString() ) |
| | | Log.e("sendLocation: ", "发送定位 userId = ${getUserId()}, aMapLocation = ${Gson().toJson(aMapLocation)}") |
| | | if (getUserId() != -1 && aMapLocation != null) { |
| | | // val bean = CarLocationBean() |
| | | // val dataBean = CarLocationBean.DataBean( |
| | | // getUserId(), |
| | | // currentOrderId, |
| | | // currentOrderType, |
| | | // aMapLocation!!.longitude, |
| | | // aMapLocation!!.latitude, |
| | | // 0.0, |
| | | // 0.0 |
| | | // ) |
| | | // bean.data = dataBean |
| | | // NettyClient.getInstance() |
| | | // .sendMessage(Gson().toJson(bean)) |
| | | // sysErr(dataBean.toString()) |
| | | |
| | | |
| | | val map = getMapByAny() |
| | | map["orderId"] = currentOrderId |
| | | map["orderType"] = currentOrderType |
| | | map["driverId"] = getUserId() |
| | | map["lon"] = aMapLocation!!.longitude |
| | | map["lat"] = aMapLocation!!.latitude |
| | | Log.e("sendLocation: ","发送定位参数"+Gson().toJson(map) ) |
| | | callNet1(this, Api.saveOrderPosition, map,clickAction={ |
| | | },{ |
| | | Log.e("sendLocation: ","发送定位成功" ) |
| | | },false) |
| | | logRecord(TAG, "sendLocation 发送定位参数" + Gson().toJson(map)) |
| | | callNet1(this, Api.saveOrderPosition, map, clickAction = { |
| | | logRecord(TAG, "sendLocation 发送定位成功") |
| | | }, { |
| | | logRecord(TAG, "sendLocation 发送定位失败") |
| | | }, false) |
| | | |
| | | val map1 = getMapByAny() |
| | | map1["orderId"] = currentOrderId |
| | | map1["orderType"] = currentOrderType |
| | | map1["driverId"] = getUserId() |
| | | map1["lon"] = aMapLocation!!.longitude |
| | | map1["lat"] = aMapLocation!!.latitude |
| | | map1["altitude"] = 0 |
| | | map1["directionAngle"] = 0 |
| | | |
| | | callNet1(this, Api.positionSocket, map1, clickAction = { |
| | | logRecord(TAG, "positionSocket 发送定位成功") |
| | | }, { |
| | | logRecord(TAG, "positionSocket 发送定位失败") |
| | | }, false) |
| | | } |
| | | } |
| | | |
| | | val TAG = "MyApplication" |
| | | private fun sendHeart() { |
| | | Log.e("sendLocation: ","sendHeart" ) |
| | | if (getUserId() != -1) { |
| | | Log.i(TAG, "sendHeart: userId === " + getUserId()) |
| | | logRecord(TAG, "sendHeart: userId = " + getUserId()) |
| | | val bean = HeartBean() |
| | | bean.data = HeartBean.DataBean() |
| | | NettyClient.getInstance() |
| | | .sendMessage(Gson().toJson(bean)) |
| | | NettyClient.getInstance().sendMessage(Gson().toJson(bean)) |
| | | sendLocation() |
| | | if (heartHandler == null) { |
| | | heartHandler = Handler(mainLooper) |
| | | } |
| | | heartHandler!!.removeCallbacksAndMessages(null) |
| | | testHeartNunm++ |
| | | if ((System.currentTimeMillis() - currentTime) / 1000 > 30) { |
| | | sysErr("sendHeart_websocket 重新连接") |
| | | currentTime = System.currentTimeMillis() |
| | | NettyClient.getInstance().checkNettyState() |
| | | } else { |
| | | sysErr("sendHeart_websocket 发送心跳") |
| | | // if ((System.currentTimeMillis() - currentTime) / 1000 > 3) { |
| | | // sysErr("sendHeart_websocket 重新连接") |
| | | logRecord(TAG, "sendHeart check socket status") |
| | | currentTime = System.currentTimeMillis() |
| | | val nettyState = NettyClient.getInstance().checkNettyState() |
| | | if (!nettyState) { |
| | | getOrderWhenNoSocket() |
| | | } |
| | | // } else { |
| | | // sysErr("sendHeart_websocket 发送心跳") |
| | | // } |
| | | } |
| | | heartHandler!!.postDelayed({ sendHeart() }, 5000) |
| | | } |
| | |
| | | |
| | | |
| | | companion object { |
| | | var frist=true |
| | | var frist = true |
| | | private const val TAG = "MyApplication" |
| | | public var appContext: MyApplication? = null |
| | | var mTtsManager: AudioRecoderUtils? = null |
| | |
| | | } |
| | | } |
| | | |
| | | val activities = ArrayList<Activity?>() |
| | | val activities = ArrayList<Activity?>() |
| | | private var showNum = 0 |
| | | |
| | | fun finishAll() { |
| | |
| | | return act |
| | | } |
| | | |
| | | override fun onActivityPaused(activity: Activity) { |
| | | if (BuildConfig.DEBUG) |
| | | Log.e("zmc-App", "onActivityPaused: " + activity?.javaClass?.name) |
| | | } |
| | | |
| | | override fun onActivityResumed(activity: Activity) { |
| | | if (BuildConfig.DEBUG) |
| | | Log.i("zmc-App", "onActivityResumed: " + activity?.javaClass?.name) |
| | | override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { |
| | | activities.add(activity) |
| | | Log.i("zmc-pageA", "onActivityCreated: " + activity.javaClass.name) |
| | | if (activity is MyBaseActivity) { |
| | | activity.supportFragmentManager.registerFragmentLifecycleCallbacks(object : |
| | | FragmentManager.FragmentLifecycleCallbacks() { |
| | | override fun onFragmentCreated( |
| | | fm: FragmentManager, |
| | | f: Fragment, |
| | | savedInstanceState: Bundle? |
| | | ) { |
| | | super.onFragmentCreated(fm, f, savedInstanceState) |
| | | Log.i("zmc-pageF", "onFragmentCreated: " + f.javaClass.name) |
| | | } |
| | | |
| | | override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) { |
| | | super.onFragmentDestroyed(fm, f) |
| | | Log.e("zmc-pageF", "onFragmentDestroyed: " + f.javaClass.name) |
| | | } |
| | | }, true) |
| | | } |
| | | } |
| | | |
| | | override fun onActivityStarted(activity: Activity) { |
| | | showNum++ |
| | | if (BuildConfig.DEBUG) |
| | | Log.d("zmc-App", "onActivityStarted: " + activity?.javaClass?.name) |
| | | Log.d("zmc-pageA", "onActivityStarted: " + activity.javaClass.name) |
| | | } |
| | | |
| | | override fun onActivityDestroyed(activity: Activity) { |
| | | if (BuildConfig.DEBUG) |
| | | Log.d("zmc-App", "onActivityDestroyed: " + activity?.javaClass?.name) |
| | | activities.remove(activity) |
| | | override fun onActivityResumed(activity: Activity) { |
| | | // Log.i("zmc-pageA", "onActivityResumed: " + activity.javaClass.name) |
| | | } |
| | | |
| | | override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { |
| | | if (BuildConfig.DEBUG) |
| | | Log.d("zmc-App", "onActivitySaveInstanceState: " + activity?.javaClass?.name) |
| | | override fun onActivityPaused(activity: Activity) { |
| | | // Log.e("zmc-pageA", "onActivityPaused: " + activity.javaClass.name) |
| | | } |
| | | |
| | | override fun onActivityStopped(activity: Activity) { |
| | | if (BuildConfig.DEBUG) |
| | | Log.e("zmc-App", "onActivityStopped: " + activity?.javaClass?.name) |
| | | Log.e("zmc-pageA", "onActivityStopped: " + activity.javaClass.name) |
| | | showNum-- |
| | | Handler(Looper.getMainLooper()).postDelayed({ |
| | | if (showNum == 0 && isPlayVoice) { |
| | |
| | | } |
| | | isPlayVoice = true |
| | | }, 600) |
| | | |
| | | } |
| | | |
| | | override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { |
| | | activities.add(activity) |
| | | if (BuildConfig.DEBUG) |
| | | Log.i("zmc-App", "onActivityCreated: " + activity?.javaClass?.name) |
| | | override fun onActivityDestroyed(activity: Activity) { |
| | | Log.d("zmc-pageA", "onActivityDestroyed: " + activity.javaClass.name) |
| | | activities.remove(activity) |
| | | } |
| | | |
| | | override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { |
| | | // Log.d("zmc-pageA", "onActivitySaveInstanceState: " + activity.javaClass.name) |
| | | } |
| | | |
| | | } |