[dev_arch_opt_3.0]重构视角变换相关代码
This commit is contained in:
@@ -0,0 +1,306 @@
|
||||
package com.mogo.eagle.core.function.angle
|
||||
|
||||
import android.content.*
|
||||
import android.os.*
|
||||
import android.util.*
|
||||
import androidx.lifecycle.*
|
||||
import androidx.lifecycle.Lifecycle.Event
|
||||
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY
|
||||
import com.alibaba.android.arouter.facade.annotation.Route
|
||||
import com.mogo.eagle.core.data.config.*
|
||||
import com.mogo.eagle.core.data.constants.MogoServicePaths
|
||||
import com.mogo.eagle.core.data.map.*
|
||||
import com.mogo.eagle.core.data.map.MapRoadInfo.StopLine
|
||||
import com.mogo.eagle.core.function.api.map.angle.*
|
||||
import com.mogo.eagle.core.function.api.map.angle.Scene
|
||||
import com.mogo.eagle.core.function.call.autopilot.*
|
||||
import com.mogo.eagle.core.function.call.map.*
|
||||
import com.mogo.eagle.core.function.call.map.CallerMapRoadListenerManager.OnRoadListener
|
||||
import com.mogo.eagle.core.utilcode.kotlin.*
|
||||
import com.mogo.eagle.core.utilcode.mogo.*
|
||||
import com.mogo.eagle.core.utilcode.mogo.logger.*
|
||||
import com.zhidaoauto.map.sdk.open.tools.*
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.android.*
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.*
|
||||
|
||||
@Route(path = MogoServicePaths.PATH_VISUAL_ANGLE)
|
||||
class MoGoVisualAngleChangeProvider: IMoGoVisualAngleChangeProvider {
|
||||
|
||||
override val functionName: String
|
||||
get() = "VisualAngleChange"
|
||||
|
||||
companion object {
|
||||
const val TAG = "VisualAngleChange"
|
||||
}
|
||||
|
||||
private val triggerLocation = AtomicReference<MogoLocation>()
|
||||
|
||||
private val distanceOfCarToStopLine = AtomicReference(0.0)
|
||||
|
||||
private val travelled by lazy { AtomicReference(0.0) }
|
||||
|
||||
/**
|
||||
* 业务实体,不对外暴露
|
||||
* @param target: 目标场景
|
||||
* @param isDisplay: 是否正在展示
|
||||
* @param triggerTime: 触发时间
|
||||
*/
|
||||
private data class Record(val target: Scene, var isDisplay: Boolean = false, var triggerTime: Long): Comparable<Record> {
|
||||
override fun compareTo(other: Record): Int {
|
||||
//如果时间一样,优先级越高,越靠近堆顶
|
||||
return other.target.priority - target.priority
|
||||
}
|
||||
}
|
||||
|
||||
private val queue by lazy {
|
||||
PriorityQueue<Record>()
|
||||
}
|
||||
|
||||
private val listener = object : OnRoadListener {
|
||||
private val roadId = AtomicReference<String>()
|
||||
private val triggerRoadId = AtomicReference<String>()
|
||||
|
||||
override fun onRoadIdInfo(roadId: String) {
|
||||
this.roadId.set(roadId)
|
||||
Log.d(TAG, "-- onRoadIdInfo --: prev: ${this.triggerRoadId.get()} -> curr: $roadId")
|
||||
val loc = CallerChassisLocationGCJ02ListenerManager.getChassisLocationGCJ02()
|
||||
var triggerClose = false
|
||||
val distance = distanceOfCarToStopLine.get() + 5
|
||||
if (hasCrossRoad && distance > 0) {
|
||||
val prev = triggerLocation.get()
|
||||
if (prev != null && loc != null) {
|
||||
travelled.set(MapTools.distance(loc.longitude, loc.latitude, prev.longitude, prev.latitude) + travelled.get())
|
||||
triggerLocation.set(loc)
|
||||
}
|
||||
val oldRoadId = triggerRoadId.get()
|
||||
Log.d(TAG, "-- onRoadIdInfo --: travelled --: ${travelled.get()}")
|
||||
if ((travelled.get() > distance) && oldRoadId != null && oldRoadId != roadId) {
|
||||
distanceOfCarToStopLine.set(0.0)
|
||||
hasCrossRoad = false
|
||||
triggerRoadId.set(null)
|
||||
travelled.set(0.0)
|
||||
triggerLocation.set(null)
|
||||
Log.d(TAG, "-- onRoadIdInfo --: trigger close --")
|
||||
triggerClose = true
|
||||
}
|
||||
}
|
||||
if (triggerClose) {
|
||||
changeAngle(CrossRoad(false))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun onStopLineInfo(info: StopLine) {
|
||||
Log.d(TAG, "-- onStopLineInfo --: ${info.distanceOfCarToStopLine}")
|
||||
if (!hasCrossRoad && info.distanceOfCarToStopLine <= 30.0) {
|
||||
hasCrossRoad = true
|
||||
triggerRoadId.set(this.roadId.get())
|
||||
distanceOfCarToStopLine.set(info.distanceOfCarToStopLine)
|
||||
triggerLocation.set(CallerChassisLocationGCJ02ListenerManager.getChassisLocationGCJ02())
|
||||
changeAngle(CrossRoad(true))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun init(context: Context?) {
|
||||
if (Thread.currentThread() != Looper.getMainLooper().thread) {
|
||||
scope.launch {
|
||||
initListen(context)
|
||||
}
|
||||
} else {
|
||||
initListen(context)
|
||||
}
|
||||
}
|
||||
|
||||
private fun initListen(ctx: Context?) {
|
||||
ctx ?: return
|
||||
ctx.lifeCycleOwner.lifecycle.addObserver(object : LifecycleEventObserver {
|
||||
override fun onStateChanged(source: LifecycleOwner, event: Event) {
|
||||
if (event == ON_DESTROY) {
|
||||
CallerMapRoadListenerManager.unRegisterRoadListener("VisualAngleChange")
|
||||
}
|
||||
}
|
||||
})
|
||||
CallerMapRoadListenerManager.registerRoadListener("VisualAngleChange", listener)
|
||||
}
|
||||
|
||||
@Volatile
|
||||
private var hasCrossRoad = false
|
||||
|
||||
|
||||
private var scope: CoroutineScope = acquireScope()
|
||||
@Synchronized
|
||||
get() {
|
||||
if (field.isActive) {
|
||||
return field
|
||||
}
|
||||
val scope = acquireScope()
|
||||
field = scope
|
||||
return field
|
||||
}
|
||||
|
||||
private var defaultDelayJob: Job? = null
|
||||
|
||||
private fun acquireScope(): CoroutineScope {
|
||||
return CoroutineScope(Handler(Looper.getMainLooper()).asCoroutineDispatcher("change-visual-angle") + SupervisorJob())
|
||||
}
|
||||
|
||||
|
||||
override fun onDestroy() {
|
||||
CallerMapRoadListenerManager.unRegisterRoadListener("VisualAngleChange")
|
||||
}
|
||||
|
||||
@Volatile
|
||||
private var mLevel:Boolean = false
|
||||
|
||||
override fun updateLongSightLevel(level:Boolean){
|
||||
mLevel = level
|
||||
}
|
||||
|
||||
override fun changeAngle(scene: Scene) {
|
||||
val appIdentityMode = FunctionBuildConfig.appIdentityMode
|
||||
if (AppIdentityModeUtils.isBus(appIdentityMode) && AppIdentityModeUtils.isPassenger(appIdentityMode)) {
|
||||
return
|
||||
}
|
||||
if(mLevel){
|
||||
return
|
||||
}
|
||||
val triggerTime = SystemClock.elapsedRealtime()
|
||||
scope.launch {
|
||||
Log.d(TAG, "--- 1 ---")
|
||||
val displayed = getDisplayed()
|
||||
if (displayed == null) {
|
||||
Log.d(TAG, "--- 2 ---")
|
||||
if (scene is Turning) {
|
||||
if (!scene.open) {
|
||||
changeAngle(Default())
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
if (scene is CrossRoad) {
|
||||
if (!scene.open) {
|
||||
changeAngle(Default())
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
doRealVisualAngleChange(triggerTime, scene, null)
|
||||
} else {
|
||||
val prev = displayed.target
|
||||
Log.d(TAG, "--- 3 --- old: $prev -> cur: $scene")
|
||||
val prevTriggerTime = displayed.triggerTime
|
||||
if (scene !is Default && prev.priority > scene.priority && (prev is RoadEvent || prev is TooClose)) {
|
||||
val displayDuration = triggerTime - prevTriggerTime
|
||||
Log.d(TAG, "--- 4 ---:场景[$prev], 已展示时长: duration: $displayDuration")
|
||||
if (displayDuration < prev.displayThreshold) {
|
||||
Log.d(TAG, "--- 5 --- 场景[$prev]:仍在保护展示时长内,直接return")
|
||||
return@launch
|
||||
} else {
|
||||
Log.d(TAG, "--- 6 --- 场景[$prev]:已过保护展示时长,从展示的队列中移除,显示默认视角")
|
||||
queue -= displayed
|
||||
changeAngle(Default())
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
if (prev is Turning && scene is Turning) {
|
||||
val isOpen = scene.open
|
||||
if (!isOpen) {
|
||||
Log.d(TAG, "--- 7 --- 场景[$scene], 收到关闭通知")
|
||||
queue -= displayed
|
||||
changeAngle(Default())
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
if (prev is CrossRoad && scene is CrossRoad) {
|
||||
val isOpen = scene.open
|
||||
if (!isOpen) {
|
||||
Log.d(TAG, "--- 8 --- old: $prev -> cur: $scene")
|
||||
queue -= displayed
|
||||
changeAngle(Default())
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
if (prev.priority == scene.priority) {
|
||||
Log.d(TAG, "--- 9 --- 场景[$prev]正在展示,尚未收到关闭,优先级一致,直接return")
|
||||
return@launch
|
||||
}
|
||||
if (prev.priority > scene.priority && prev.displayThreshold < 0) {
|
||||
Log.d(TAG, "--- 10 --- 场景[$prev]正在展示,尚未收到关闭,场景,依然展示当前场景,直接return")
|
||||
return@launch
|
||||
}
|
||||
doRealVisualAngleChange(triggerTime, scene, displayed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun CoroutineScope.doRealVisualAngleChange(triggerTime: Long, target: Scene, displayed: Record? = null) {
|
||||
if (target is Default) {
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 1 ---")
|
||||
displayed?.also {
|
||||
queue -= it
|
||||
}
|
||||
defaultDelayJob?.safeCancel()
|
||||
launch {
|
||||
val delay = target.unit.toMillis(target.delay)
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 2 ---")
|
||||
delay(delay)
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 3 ---")
|
||||
doChangeAngle(Record(target, triggerTime = triggerTime))
|
||||
}.also { itx ->
|
||||
itx.invokeOnCompletion {
|
||||
if (it is CancellationException) {
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 4 ---")
|
||||
}
|
||||
}
|
||||
defaultDelayJob = itx
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 5 ---")
|
||||
defaultDelayJob?.safeCancel()
|
||||
if (displayed == null || displayed.target.priority <= target.priority) {
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 6 ---")
|
||||
displayed?.also {
|
||||
queue -= it
|
||||
}
|
||||
if (target is Turning) {
|
||||
if (!target.open) {
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 7 ---")
|
||||
changeAngle(Default())
|
||||
return
|
||||
}
|
||||
}
|
||||
if (target is CrossRoad) {
|
||||
if (!target.open) {
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 8 ---")
|
||||
changeAngle(Default())
|
||||
return
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "--- doRealVisualAngleChange --- 10 ---")
|
||||
doChangeAngle(Record(target, triggerTime = triggerTime))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(InternalCoroutinesApi::class)
|
||||
private fun doChangeAngle(record: Record) {
|
||||
val angle = record.target.angle
|
||||
CallerMapUIServiceManager.getMapUIController()?.also {
|
||||
Log.d(TAG, "--- doChangeAngle ---: ${record.target}")
|
||||
if (record.target !is Default) {
|
||||
record.isDisplay = true
|
||||
kotlinx.coroutines.internal.synchronized(queue) {
|
||||
queue += record
|
||||
}
|
||||
}
|
||||
it.changeMapVisualAngle(angle, null)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有正在展示的
|
||||
*/
|
||||
@Synchronized
|
||||
private fun getDisplayed() = queue.firstOrNull()
|
||||
}
|
||||
@@ -52,6 +52,5 @@ public class MogoMapService implements IMogoMapService {
|
||||
|
||||
@Override
|
||||
public void init(Context context) {
|
||||
CallerVisualAngleManager.INSTANCE.init(context);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user