package com.fangxinxing.xxfxx.ui.main import android.graphics.Color import android.os.Bundle import android.os.Handler import android.os.Looper import android.view.View import cn.sinata.xldutils.utils.Utils import com.amap.api.location.AMapLocationClient import com.amap.api.location.AMapLocationListener import com.amap.api.maps.AMap import com.amap.api.maps.AMapUtils import com.amap.api.maps.model.BitmapDescriptorFactory import com.amap.api.maps.model.LatLng import com.amap.api.maps.model.Marker import com.amap.api.maps.model.MarkerOptions import com.amap.api.maps.model.Polyline import com.fangxinxing.xxfxx.R import com.fangxinxing.xxfxx.base.BaseEvent import com.fangxinxing.xxfxx.base.MyApplication import com.fangxinxing.xxfxx.base.MyBaseActivity import com.fangxinxing.xxfxx.base.gaode.AMapKit import com.fangxinxing.xxfxx.base.gaode.AMapKit.addMarker import com.fangxinxing.xxfxx.base.gaode.AMapKit.getIcon import com.fangxinxing.xxfxx.base.gaode.AMapKit.getTimeType import com.fangxinxing.xxfxx.base.gaode.AMapKit.initMap import com.fangxinxing.xxfxx.base.gaode.AMapKit.moveCamera import com.fangxinxing.xxfxx.bean.DestinationInfo import com.fangxinxing.xxfxx.bean.OrderBean import com.fangxinxing.xxfxx.bean.OrderSimpleData import com.fangxinxing.xxfxx.bean.PassengersLocationInfo import com.fangxinxing.xxfxx.netUtls.Api import com.fangxinxing.xxfxx.netUtls.callNet import com.fangxinxing.xxfxx.netUtls.createView import com.fangxinxing.xxfxx.netUtls.getMapByAny import com.fangxinxing.xxfxx.ui.DialogUtil import com.fangxinxing.xxfxx.ui.main.major.MajorSureMoneyActivity import com.fangxinxing.xxfxx.utils.DateUtil import com.fangxinxing.xxfxx.utils.MyUtils import com.fangxinxing.xxfxx.utils.clickDelay import com.fangxinxing.xxfxx.utils.gone import com.fangxinxing.xxfxx.utils.setColorBuild import com.fangxinxing.xxfxx.utils.visible import com.google.gson.Gson import com.trello.rxlifecycle3.android.ActivityEvent import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers import kotlinx.android.synthetic.main.activity_trip.iv_move import kotlinx.android.synthetic.main.activity_trip.iv_police import kotlinx.android.synthetic.main.activity_trip.iv_to_gd import kotlinx.android.synthetic.main.activity_trip.map_view import kotlinx.android.synthetic.main.activity_trip.slide_btn import kotlinx.android.synthetic.main.dialog_select_pay_type.view.tv_cancel import kotlinx.android.synthetic.main.dialog_select_pay_type.view.tv_ok import kotlinx.android.synthetic.main.dialog_select_pay_type.view.tv_select_ok import kotlinx.android.synthetic.main.dialog_select_pay_type.view.tv_select_or import kotlinx.android.synthetic.main.include_order_top.iv_phone import kotlinx.android.synthetic.main.include_order_top.tv_car_num import kotlinx.android.synthetic.main.include_order_top.tv_end_address import kotlinx.android.synthetic.main.include_order_top.tv_name import kotlinx.android.synthetic.main.include_order_top.tv_phone import kotlinx.android.synthetic.main.include_order_top.tv_red_money import kotlinx.android.synthetic.main.include_order_top.tv_start_address import kotlinx.android.synthetic.main.include_order_top.tv_time import kotlinx.android.synthetic.main.item_map_market.view.iv_img import kotlinx.android.synthetic.main.item_map_market.view.tv_hint import org.greenrobot.eventbus.EventBus import org.jetbrains.anko.startActivity import org.jetbrains.anko.toast import java.util.concurrent.TimeUnit class TripActivity : MyBaseActivity() { /*** * // 流程操作状态(3=开始出发预约点,4=到达预约点,5=开始服务,6=服务结束) * 页面一共4个marker 1:预约地点 2:起点 3终点 4:司机点 */ val orderType by lazy { intent.getStringExtra("orderType") } val orderId by lazy { intent.getStringExtra("orderId") } var isTimeStart = false //限制计时器 var makeMarker: Marker? = null //乘客位置marker var passengersMarker: Marker? = null var startMarker: Marker? = null var endMarker: Marker? = null var carMarker: Marker? = null var markerLine: Polyline? = null val gson by lazy { Gson() } lateinit var aMap: AMap lateinit var orderBean: OrderBean var mLocationClient: AMapLocationClient? = null //定位服务,退出页面需要销毁 override fun setContentView() { setContentView(R.layout.activity_trip) } override fun initView() { setTitleText("准备出发-去接客户-等待客户-服务中") tv_Right.text = "申请改派" callOrder() initMineLocation() onclick() addSockectCancel() } override fun onPermissionsDenied(requestCode: Int, perms: MutableList) { super.onPermissionsDenied(requestCode, perms) if (requestCode == 1006) { onBackPressed() } } private fun addSockectCancel() { checkOrderStatue() MyApplication.addOrderView(socketListener) } private val socketListener = object : MyApplication.OrderStatueView { override fun orderInfo(data: OrderSimpleData?) { data?.let { when (it.orderType) { 1, 2, 3 -> { if (it.status == 10 || it.status == 12) { Handler(Looper.getMainLooper()).post { toast("订单已被取消") MyApplication.getTTsManager().setVideoText("订单已被取消") AMapKit.exitNavi() MyApplication.currentOrderType = "" MyApplication.currentOrderId = "" MyApplication.finishRecord(orderId, orderType) finish() } } } } } } } var handler: Handler? = null fun checkOrderStatue() { var map = getMapByAny() map["orderId"] = orderId map["orderType"] = orderType callNet(false, Api.orderStateSocket, map) { val orderSimpleData = Gson().fromJson( it, OrderSimpleData::class.java ) orderSimpleData?.let { if (it.status == 10 || it.status == 12) { Handler(Looper.getMainLooper()).post { toast("订单已被取消") MyApplication.getTTsManager().setVideoText("订单已被取消") AMapKit.exitNavi() MyApplication.currentOrderType = "" MyApplication.currentOrderId = "" MyApplication.finishRecord(orderId, orderType) finish() } } } } handler?.postDelayed({ checkOrderStatue() }, 3000) } override fun onBackPressed() { EventBus.getDefault().post( BaseEvent( BaseEvent.UPDATA_MAIN_CAR ) ) super.onBackPressed() } override fun finish() { EventBus.getDefault().post( BaseEvent( BaseEvent.UPDATA_MAIN_CAR ) ) super.finish() } private fun onclick() { tv_Right.clickDelay { DialogUtil.getDelAndSureDialog(this, "您是否要进行改派?", {}, { var map = getMapByAny() map["orderId"] = orderId map["orderType"] = orderType map["reason"] = "" map["payType"] = 3 callNet(Api.reassign, map) { toast("改派申请已提交") startActivity( "orderId" to orderId, "orderType" to orderType ) } }) } iv_move.setOnClickListener { orderBean.let { when (it.data.orderState) { 2, 3 -> { moveCamera(aMap, makeMarker!!.position, carMarker!!.position) } 4 -> { moveCamera(aMap, carMarker!!.position) } 5, 6 -> { if (endMarker == null) { moveCamera(aMap, carMarker!!.position) } else { moveCamera(aMap, carMarker!!.position, endMarker!!.position) } } } } } iv_police.clickDelay { orderBean.let { MyUtils.getInstans().CallPhone(this, it.data.emergencyCall) } } iv_to_gd.clickDelay { orderBean.let { when (it.data.orderState) { 2, 3 -> { AMapKit.startNavi( carMarker!!.position.latitude, carMarker!!.position.longitude, makeMarker!!.position.latitude, makeMarker!!.position.longitude, this ) } 4 -> { //showToast("没有终点,无法导航") } 5, 6 -> { if (endMarker == null) { //toast("暂无终点") } else { AMapKit.startNavi( carMarker!!.position.latitude, carMarker!!.position.longitude, endMarker!!.position.latitude, endMarker!!.position.longitude, this ) } } else -> { } } } } slide_btn.onSwipeListener = { orderBean.let { when (it.data.orderState) { 2, 3 -> { var timeByLo = (DateUtil.dateToStamp( DateUtil.TYPE0, it.data.travelTime ) - System.currentTimeMillis()) / 1000L if (it.data.orderState == 2 && it.data.reservation == 2 && timeByLo > 1800L) {//判断预约单时间滑动是否有效 toast("只能在预约时间的半小时内出行") return@let } it.data.orderState++ callStatue(it.data.orderState, { if (it.data.orderState == 3) iv_to_gd.callOnClick() if (it.data.orderState == 5) iv_to_gd.callOnClick() showStatueMapUI(it) }) { it.data.orderState-- } } 4 -> { var timeByLo = (DateUtil.dateToStamp( DateUtil.TYPE0, it.data.travelTime ) - System.currentTimeMillis()) / 1000L if (it.data.orderState == 2 && it.data.reservation == 2 && timeByLo > 1800L) {//判断预约单时间滑动是否有效 toast("只能在预约时间的半小时内出行") return@let } //手机后4位数字 val phoneLast4Number = it.data.realPhone.substring(it.data.realPhone.length - 4) DialogUtil.getInputNumberDialog( this, phoneLast4Number.substring(0, 2), 4, ) { value -> if (value != phoneLast4Number) { toast("手机号错误") return@getInputNumberDialog } it.data.orderState++ callStatue(it.data.orderState, { if (it.data.orderState == 3) iv_to_gd.callOnClick() if (it.data.orderState == 5) iv_to_gd.callOnClick() showStatueMapUI(it) }) { it.data.orderState-- } } } 5, 6 -> { if (it.data.orderState == 5 && !it.data.hadEffectiveEndPoint()) { startActivity() } else { it.data.orderState = 6 when (orderType) { "2" -> { showMoneyTypeDialog() } "1" -> { callStatue(6, { startActivity( "orderId" to orderId, "orderType" to orderType ) finish() }) { } } } } } } } } iv_phone.clickDelay { orderBean.let { Utils.callPhone(this, it.data.phone) } } tv_phone.clickDelay { orderBean.let { Utils.callPhone(this, it.data.contactPersonPhone) } } } private fun callStatue(i: Int, click: () -> Unit, clickFail: () -> Unit) { var map = getMapByAny() map["orderId"] = orderId map["orderType"] = orderType map["state"] = i map["lat"] = MyApplication.getLocation().latitude map["lon"] = MyApplication.getLocation().longitude callNet(Api.process, map, { click() }) { clickFail() } } private fun showMoneyTypeDialog() { var pop = DialogUtil.getPopupwindow(this, R.layout.dialog_select_pay_type) var payType = 2 //支付方式(1=OK平台收款,2=其他方式收款) pop.showDown(window.decorView) pop.contentView.tv_cancel.setOnClickListener { pop.dismiss() } pop.contentView.tv_ok.setOnClickListener { pop.dismiss() if (payType == 1) { callStatue(6, { startActivity( "orderId" to orderId, "orderType" to orderType ) finish() }) { } } else { DialogUtil.getDelAndSureDialog(this, "请确定是否收款?", {}, { pop.dismiss() val map = getMapByAny() map["orderId"] = orderId map["orderType"] = orderType map["type"] = payType callStatue(6, { callNet(Api.confirmFees, map) { finish() startActivity( "orderId" to orderId, "orderType" to orderType ) } }) { } }) } } pop.contentView.tv_select_ok.setOnClickListener { pop.contentView.tv_select_ok.setTextColor(Color.WHITE) pop.contentView.tv_select_ok.setBackgroundResource(R.drawable.bg_blue_7dp) pop.contentView.tv_select_or.setTextColor(Color.parseColor("#2A6BE4")) pop.contentView.tv_select_or.setBackgroundResource(R.drawable.bg_blue_line_7dp) payType = 1 } pop.contentView.tv_select_or.setOnClickListener { pop.contentView.tv_select_or.setTextColor(Color.WHITE) pop.contentView.tv_select_or.setBackgroundResource(R.drawable.bg_blue_7dp) pop.contentView.tv_select_ok.setTextColor(Color.parseColor("#2A6BE4")) pop.contentView.tv_select_ok.setBackgroundResource(R.drawable.bg_blue_line_7dp) payType = 2 } } /*** * 定位时司机与线路的改变 */ private fun initMineLocation() { mLocationClient = AMapKit.initLocation(this, AMapLocationListener { it?.let { MyApplication.aMapLocation = it carMarker!!.position = LatLng(it.latitude, it.longitude) } orderBean.let { when (it.data.orderState) { 2, 3 -> { changeCarThree(false) } 5, 6 -> { changeCarFive(false) } } } }) } private fun changeCarThree(isMove: Boolean) { if (makeMarker != null && carMarker != null) { AMapKit.initRouteLine( this, makeMarker!!.position, carMarker!!.position, object : (MutableList, Float, Long) -> Unit { override fun invoke(p1: MutableList, p2: Float, p3: Long) { var view = creatCarView("距预约点" + AMapKit.getTance(p2)) carMarker!!.setIcon(getIcon(view)) addline(p1) if (isMove) moveCamera(aMap, makeMarker!!.position, carMarker!!.position) } }) } } private fun changeCarFive(isMove: Boolean) { if (carMarker != null && endMarker != null) { AMapKit.initRouteLine( this, carMarker!!.position, endMarker!!.position, object : (MutableList, Float, Long) -> Unit { override fun invoke(p1: MutableList, p2: Float, p3: Long) { addline(p1) var view = creatCarView("剩余" + AMapKit.getTance(p2) + "\n" + "预计还需" + p3.toInt() + "分钟") carMarker!!.setIcon(getIcon(view)) if (isMove) { moveCamera(aMap, carMarker!!.position, endMarker!!.position) } } }) } else { var view = creatCarView() carMarker?.setIcon(getIcon(view)) if (isMove) { moveCamera(aMap, carMarker!!.position) } } } private fun callOrder() { MyApplication.currentOrderId = orderId MyApplication.currentOrderType = orderType var map = getMapByAny() map["orderId"] = orderId map["orderType"] = orderType callNet(Api.queryOrderInfo, map) { orderBean = gson.fromJson(it, OrderBean::class.java) showUi(orderBean) } } private fun showUi(orderBean: OrderBean?) { orderBean?.let { tv_name.text = it.data.realPhone if (!it.data.contactPersonPhone.isNullOrEmpty()) { tv_phone.visible() tv_phone.text = "备用联系人" } iv_phone.text = "乘车人" iv_phone.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_phone_green, 0, 0, 0) tv_car_num.text = it.data.historyNum.toString() + "次乘车" tv_time.text = it.data.travelTime_ tv_start_address.text = it.data.startAddress tv_end_address.text = it.data.endAddress // tv_end_address.visibility = // if (it.data.endAddress.isNullOrEmpty()) View.GONE else View.VISIBLE if (orderType == "1") { tv_red_money.gone() } tv_red_money.text = it.data.tipMoney.toString() + "元红包" showStatueMapUI(it) } } private fun getAllMark(it: OrderBean) { if (makeMarker == null) { val view = createView(R.layout.item_map_market_make, this) view.iv_img.setImageResource(R.mipmap.trip_yuyue) makeMarker = addMarker(aMap, it.data.startLat, it.data.startLon, view, "") } makeMarker!!.isVisible = false if (startMarker == null) { val view = createView(R.layout.item_map_market, this) view.iv_img.setImageResource(R.mipmap.starting_point) startMarker = addMarker(aMap, it.data.startLat, it.data.startLon, view, "") } startMarker!!.isVisible = false if (endMarker == null) { val view = createView(R.layout.item_map_market, this) view.iv_img.setImageResource(R.mipmap.end_point) endMarker = addMarker(aMap, it.data.endLat, it.data.endLon, view, "") } endMarker?.isVisible = false if (carMarker == null) { val carView = creatCarView("") carMarker = addMarker( aMap, MyApplication.getLocation().latitude, MyApplication.getLocation().longitude, carView, "" ) } carMarker!!.isVisible = false if (markerLine != null) { markerLine!!.isVisible = false } } fun creatCarView(content: String): View { val carView = createView(R.layout.item_map_market, this) carView.iv_img.setImageResource(R.mipmap.car_trip) carView.tv_hint.visible() carView.tv_hint.text = content return carView } fun creatCarView(): View { val carView = createView(R.layout.item_map_market, this) carView.iv_img.setImageResource(R.mipmap.car_trip) carView.tv_hint.gone() return carView } fun creatCarViewHint(content: String): View { val carView = createView(R.layout.item_map_market, this) carView.iv_img.setImageResource(R.mipmap.car_trip) carView.tv_hint.visible() carView.tv_hint.setColorBuild(this, content, R.color.main_yellow, 4, content.length) return carView } // 流程操作状态(3=开始出发预约点,4=到达预约点,5=开始服务,6=服务结束) private fun showStatueMapUI(it: OrderBean) { if (it.data.orderState in 3..5) { MyApplication.startRecord(this, orderId, orderType) } getAllMark(it) tv_Right.visible() when (it.data.orderState) { 2, 3 -> { if (it.data.reassign == 1) { tv_Right.gone() } iv_to_gd.visible() makeMarker?.isVisible = true carMarker?.isVisible = true changeCarThree(true) closeTimeStart() if (it.data.orderState == 2) { slide_btn.changeButtonText("出发前往预约地点") setTitleText("准备出发") } else { slide_btn.changeButtonText("到达预约地点") setTitleText("去接客户") } } 4 -> { iv_to_gd.gone() if (it.data.reassign == 1) { tv_Right.gone() } closeLine() carMarker?.isVisible = true startTimeStart( ((System.currentTimeMillis() - DateUtil.dateToStamp( DateUtil.TYPE0, it.data.waitTime )) / 1000).toInt() ) moveCamera(aMap, carMarker!!.position) setTitleText("等待客户") slide_btn.changeButtonText("乘客已上车") } 5, 6 -> { if (it.data.orderState == 5 && !it.data.hadEffectiveEndPoint()) { slide_btn.changeButtonText("填写目的地") } else { slide_btn.changeButtonText("送达该乘客") } closeTimeStart() iv_to_gd.visible() tv_Right.gone() startMarker?.isVisible = true endMarker?.isVisible = true carMarker?.isVisible = true changeCarFive(true) setTitleText("服务中") } } } private fun startTimeStart(waitTime: Int) { if (!isTimeStart) { isTimeStart = true refreshTime(waitTime) { var view = creatCarViewHint("您已等待" + getTimeType(it)) carMarker!!.setIcon(getIcon(view)) } } } private var timeDisposable: Disposable? = null private fun refreshTime(waitTime: Int, click: (num: Int) -> Unit) { if (null != timeDisposable && !timeDisposable!!.isDisposed) { timeDisposable!!.dispose() } timeDisposable = Observable .interval(1, TimeUnit.SECONDS) .take(100000000) .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .compose(bindUntilEvent(ActivityEvent.DESTROY)) .subscribe { aLong -> var num = waitTime + aLong click(num.toInt()) } } private fun closeTimeStart() { if (timeDisposable != null) { timeDisposable!!.dispose() } } override fun onDestroy() { super.onDestroy() if (handler != null) { handler?.removeCallbacksAndMessages(null) } closeTimeStart() AMapKit.stopLocation(mLocationClient) MyApplication.removeOrderView(socketListener) } private fun addline(allLine: MutableList) { closeLine() markerLine = AMapKit.drawLine(this, aMap, allLine) } fun closeLine() { if (markerLine != null) { markerLine!!.remove() } } override fun setOnclick() { } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) aMap = initMap(savedInstanceState, map_view) } //刷新乘客位置 private fun refreshPassengersLocation(latLng: LatLng) { if (!::orderBean.isInitialized) { return } if (passengersMarker == null) { passengersMarker = aMap.addMarker( MarkerOptions().anchor(0.5f, 0.5f) .position(latLng) .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_passenger_location)) ) } else { passengersMarker?.apply { position = latLng } } passengersMarker?.isVisible = AMapUtils.calculateLineDistance( orderBean.data.startLatLng, latLng ) <= 500 && orderBean.data.orderState in 3..4 } //显示设置乘客目的地弹窗 private fun showSetPassengerDestinationDialog(item: DestinationInfo) { DialogUtil.getSetPassengerDestinationDialog(this, item) { val map = getMapByAny() map["orderId"] = orderId map["endLat"] = item.lat map["endLon"] = item.lng map["endAddress"] = item.address callNet(Api.setDestination, map) { callOrder() } } } override fun onEventMainThread(event: BaseEvent?) { super.onEventMainThread(event) when (event!!.code) { BaseEvent.REAGASSIGN_SUCCESS -> { if (event.msg == orderId) { MyApplication.currentOrderId = "" MyApplication.currentOrderType = "" MyApplication.finishRecord(orderId, orderType) finish() } } BaseEvent.BACK_CAR_OVER -> { finish() } BaseEvent.REASSIGN_END_CANCEL -> { if (event.msg == orderId) { toast("订单已被取消") MyApplication.currentOrderId = "" MyApplication.currentOrderType = "" MyApplication.finishRecord(orderId, orderType) finish() } } BaseEvent.PASSENGERS_LOCATION_REFRESH -> { val info = gson.fromJson(event.msg, PassengersLocationInfo::class.java) if (info.orderId != orderId || info.orderType != orderType) { return } refreshPassengersLocation(info.getLatLng()) } BaseEvent.SELECT_ADDRESS -> { val info = gson.fromJson(event.msg, DestinationInfo::class.java) showSetPassengerDestinationDialog(info) } } } }