[dev_arch_opt_3.0] 优化魔方

This commit is contained in:
lixiaopeng
2023-02-21 18:33:27 +08:00
parent b192238894
commit 1b1724668c
6 changed files with 275 additions and 227 deletions

View File

@@ -7,18 +7,33 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.view.KeyEvent
import com.mogo.commons.context.ContextHolderUtil
import com.mogo.eagle.core.data.mofang.MfConstants
import com.mogo.eagle.core.function.api.mofang.IMoGoMoFangListener
import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotControlManager.sendOperatorChangeLaneLeft
import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotControlManager.sendOperatorChangeLaneRight
import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotControlManager.sendOperatorSetAcceleratedSpeed
import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotControlManager.sendOperatorSetHorn
import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotControlManager.startAutoPilot
import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotStatusListenerManager.getAutoPilotStatusInfo
import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsListenerManager.invokeMoFangStatus
import com.mogo.eagle.core.function.call.mofang.CallerMofangListenerManager
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger.d
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger.e
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.Companion.M_F
import com.mogo.eagle.core.utilcode.mogo.storage.SharedPrefsMgr
import com.mogo.eagle.core.utilcode.util.ToastUtils
import com.mogo.eagle.core.utilcode.util.UiThreadHandler
import java.util.*
/**
* 魔方连接状态和设备管理
*/
@SuppressLint("StaticFieldLeak")
class MoFangManager private constructor() {
class MoFangManager private constructor() : IMoGoMoFangListener{
companion object {
val moFangManager: MoFangManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
@@ -31,6 +46,29 @@ class MoFangManager private constructor() {
private lateinit var mBluetoothAdapter: BluetoothAdapter
private var isMfConnect: Boolean = false //添加状态判断
private val isShowToast = false //toast 控制,自测使用
private var startPressTime: Long = 0 //开始按键时间
private var isPressEnd = false //按键是否结束
@Volatile
private var isCombinationKey = 0 //是否是组合按键 1单击2长按3组合
private var pressADownTime: Long = 0
private var pressAUpTime: Long = 0
private var pressBDownTime: Long = 0
private var pressBUpTime: Long = 0
private var pressCDownTime: Long = 0
private var pressCUpTime: Long = 0
private var pressDDownTime: Long = 0
private var pressDUpTime: Long = 0
private var pressEDownTime: Long = 0
private var pressEUpTime: Long = 0
private val clickTime = 300 //单击
private val clickTimeInterval = 330
private val longPressTime = 670
private val longPressTimeInterval = 700
private var timerHorn: Timer? = null
private var timerAcc: Timer? = null
fun init(context: Context) {
mContext = context
@@ -41,6 +79,12 @@ class MoFangManager private constructor() {
}
mBluetoothAdapter.startDiscovery()
showBondedDevice(mBluetoothAdapter)
CallerMofangListenerManager.addListener(TAG, this)
}
fun release() {
CallerMofangListenerManager.removeListener(TAG)
}
/**
@@ -89,7 +133,7 @@ class MoFangManager private constructor() {
private val bluetoothMonitorReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
BluetoothAdapter.ACTION_STATE_CHANGED -> { //中间状态 TODO
BluetoothAdapter.ACTION_STATE_CHANGED -> { //中间状态
CallerLogger.d("$M_F${TAG}","bluetoothMonitorReceiver ACTION_STATE_CHANGED action = ${intent.action}")
}
@@ -115,5 +159,190 @@ class MoFangManager private constructor() {
}
}
/**
* 魔方按键处理
*/
override fun onMofangHandle(keyCode: Int, action: Int): Boolean {
val bluetoothName = SharedPrefsMgr.getInstance(ContextHolderUtil.getContext()).getString(MfConstants.BLUETOOTH_NAME)
if (bluetoothName == "MINI_KEYBOARD") {
if (!isPressEnd) {
isPressEnd = true
startPressTime = System.currentTimeMillis()
}
e(M_F + "MoFangManager",
"dispatchKeyEvent ------ bluetoothName = $bluetoothName ---code = $keyCode -----action = $action ")
if (keyCode == KeyEvent.KEYCODE_A) { //单击 -1长按无操作AB组合-2
if (action == KeyEvent.ACTION_DOWN) {
pressADownTime = System.currentTimeMillis()
d(M_F + "MoFangManager",
"dispatchKeyEvent A down pressADownTime = " + pressADownTime + "---" + (pressADownTime - startPressTime) + "----isCombinationKey = " + isCombinationKey + "--pressBDownTime = " + pressBDownTime)
if (pressADownTime - startPressTime in (clickTimeInterval + 1) until longPressTime && pressBDownTime > 0) {
if (isShowToast) {
ToastUtils.showShort("方块 A 按AB组合 +1 ")
}
sendAcc(true, +1.0)
isCombinationKey = 3
}
if (isCombinationKey != 3 && isCombinationKey != 1) {
if (pressADownTime - startPressTime > longPressTimeInterval) {
if (isShowToast) {
ToastUtils.showShort("方块 长按A -2 ")
}
sendAcc(true, -2.0)
isCombinationKey = 2
}
}
} else if (action == KeyEvent.ACTION_UP) {
pressAUpTime = System.currentTimeMillis()
d(M_F + "MoFangManager",
"dispatchKeyEvent A up pressAUpTime = " + pressAUpTime + "---" + (pressAUpTime - startPressTime) + "--pressBDownTime = " + pressBDownTime + "---isCombinationKey = $isCombinationKey")
if (pressAUpTime - startPressTime < clickTime && isCombinationKey != 3) {
isCombinationKey = 1
if (isShowToast) {
ToastUtils.showShort("方块 单击A -1 ")
}
sendAcc(true, -1.0)
}
pressADownTime = 0
isPressEnd = false
UiThreadHandler.postDelayed({ isCombinationKey = 0 }, 300)
}
} else if (keyCode == KeyEvent.KEYCODE_B) { //单击复原,长按+1AB组合-2
if (action == KeyEvent.ACTION_DOWN) {
pressBDownTime = System.currentTimeMillis()
d(M_F + "MoFangManager",
"dispatchKeyEvent B down pressBDownTime = " + pressBDownTime + "--差-" + (pressBDownTime - startPressTime) + "---isCombinationKey = " + isCombinationKey + "--pressADownTime = " + pressADownTime
)
if (pressBDownTime - startPressTime > clickTimeInterval && pressBDownTime - startPressTime < longPressTime && pressADownTime > 0) {
if (isShowToast) {
ToastUtils.showShort("方块 B 按AB组合 +1 ")
}
sendAcc(true, +1.0)
isCombinationKey = 3
}
if (isCombinationKey != 3 && isCombinationKey != 1) {
if (pressBDownTime - startPressTime > longPressTimeInterval) {
if (isShowToast) {
ToastUtils.showShort("方块 长按B 无操作 ")
}
isCombinationKey = 2
}
}
} else if (action == KeyEvent.ACTION_UP) {
pressBUpTime = System.currentTimeMillis()
d(M_F + "MoFangManager",
"dispatchKeyEvent B up pressBUpTime = " + pressBUpTime + "--差-" + (pressBUpTime - startPressTime) + "--pressADownTime = " + pressADownTime + "----isCombinationKey = $isCombinationKey")
if (pressBUpTime - startPressTime < clickTime && isCombinationKey != 3) {
if (isShowToast) {
ToastUtils.showShort("方块 单击B 0 ")
}
sendAcc(false, 0.0)
isCombinationKey = 1
}
pressBDownTime = 0
isPressEnd = false
UiThreadHandler.postDelayed({ isCombinationKey = 0 }, 300)
}
} else if (keyCode == KeyEvent.KEYCODE_C) { //单击左变道,长按无操作
if (action == KeyEvent.ACTION_DOWN) {
pressCDownTime = System.currentTimeMillis()
d(M_F + "MoFangManager",
"dispatchKeyEvent 方块 长按C 无操作 time dif = " + (pressCDownTime - startPressTime))
if (pressCDownTime - startPressTime > longPressTimeInterval) {
if (isShowToast) {
ToastUtils.showShort("方块 长按C 无操作 ")
}
}
} else if (action == KeyEvent.ACTION_UP) {
pressCUpTime = System.currentTimeMillis()
isPressEnd = false
d(M_F + "MoFangManager",
"dispatchKeyEvent 方块 单击C ← 向左变道 time dif = " + (pressCUpTime - startPressTime))
if (pressCUpTime - startPressTime < clickTime) {
if (isShowToast) {
ToastUtils.showShort("方块 单击C ← 向左变道 ")
}
sendOperatorChangeLaneLeft()
}
}
} else if (keyCode == KeyEvent.KEYCODE_D) { //单击向右变道,双击无操作
if (action == KeyEvent.ACTION_DOWN) {
pressDDownTime = System.currentTimeMillis()
d(M_F + "MoFangManager",
"dispatchKeyEvent 方块 长按D 无操作 time dif = " + (pressDDownTime - startPressTime))
if (pressDDownTime - startPressTime > longPressTimeInterval) {
if (isShowToast) {
ToastUtils.showShort("方块 长按D 无操作 ")
}
}
} else if (action == KeyEvent.ACTION_UP) {
pressDUpTime = System.currentTimeMillis()
isPressEnd = false
d(M_F + "MoFangManager",
"dispatchKeyEvent 方块 单击D → 向右变道 time dif = " + (pressDUpTime - startPressTime))
if (pressDUpTime - startPressTime < clickTime) {
if (isShowToast) {
ToastUtils.showShort("方块 单击D → 向右变道 ")
}
sendOperatorChangeLaneRight()
}
}
} else if (keyCode == KeyEvent.KEYCODE_E) { //单击启动自驾,长按鸣笛
if (action == KeyEvent.ACTION_DOWN) {
pressEDownTime = System.currentTimeMillis()
d(M_F + "MoFangManager",
"dispatchKeyEvent 方块 长按E 鸣笛 time dif = " + (pressEDownTime - startPressTime))
if (pressEDownTime - startPressTime > longPressTimeInterval) {
if (isShowToast) {
ToastUtils.showShort("方块 长按E 鸣笛 ")
}
sendOperatorSetHorn(1.0)
if (timerHorn == null) {
timerHorn = Timer()
}
timerHorn!!.schedule(object : TimerTask() {
override fun run() {
sendOperatorSetHorn(2.0)
timerHorn = null
}
}, 500)
}
} else if (action == KeyEvent.ACTION_UP) {
pressEUpTime = System.currentTimeMillis()
isPressEnd = false
d(M_F + "MoFangManager",
"方块 单击E 开启自动驾驶 time dif = " + (pressEUpTime - startPressTime))
if (pressEUpTime - startPressTime < clickTime) {
if (isShowToast) {
ToastUtils.showShort("方块 单击E 开启自动驾驶 ")
}
startAutoPilot(getAutoPilotStatusInfo().autopilotControlParameters)
}
}
}
}
return true
}
@Synchronized
private fun sendAcc(isSend: Boolean, acc: Double) {
if (isSend) {
if (timerAcc == null) {
timerAcc = Timer()
timerAcc!!.schedule(object : TimerTask() {
override fun run() {
sendOperatorSetAcceleratedSpeed(acc)
}
}, 0, 500)
}
} else {
if (timerAcc != null) {
timerAcc!!.cancel()
timerAcc = null
}
sendOperatorSetAcceleratedSpeed(acc)
}
}
}