This commit is contained in:
tongchenfei
2020-10-28 15:40:26 +08:00
parent 175f018832
commit 3fb3fe93fd
10 changed files with 240 additions and 101 deletions

View File

@@ -6,11 +6,13 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.mogo.map.location.IMogoLocationListener;
import com.mogo.map.location.MogoLocation;
import com.mogo.map.navi.MogoTraffic;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.module.common.entity.CloudRoadData;
import com.mogo.module.common.entity.MogoSnapshotSetData;
@@ -31,54 +33,79 @@ import org.json.JSONObject;
* @author tongchenfei
*/
public class AdasNoticeHelper implements IMogoAdasWarnMessageCallback, IMogoLocationListener,
Handler.Callback , IMogoOnWebSocketMessageListener<MogoSnapshotSetData> {
Handler.Callback, IMogoOnWebSocketMessageListener<MogoSnapshotSetData> {
private static final String TAG = "AdasNoticeHelper";
private static final int MSG_HIDE_TRAFFIC_LIGHT_BY_OBU = 1001;
private static final int MSG_HIDE_TRAFFIC_LIGHT_BY_CLOUD = 1002;
private static final int MSG_HIDE_LIMIT_SPEED = 1003;
private static final int MSG_REFRESH_CAR_STRATEGY = 1004;
private static final long HIDE_TRAFFIC_LIGHT_DELAY = 2_000L;
private static final long HIDE_LIMIT_SPEED_DELAY = 10_000L;
private static final long STRATEGY_DELAY = 1000L;
private Context context;
private AdasNoticeReceiver adasReceiver = new AdasNoticeReceiver();
private boolean isVrMode = false;
private volatile boolean isVrMode = false;
private TextView tvSelfSpeed, tvTrafficLight, tvLimitSpeed;
private int limitSpeed;
private int currentSpeed = 0;
private String lightStatus = "G";
private String surplusTime;
private Handler handler = new Handler(this);
private View selfCar;
private boolean lightCenter = false;
public void init(Context context) {
this.context = context;
if(!lightCenter) {
selfCar = LayoutInflater.from(context).inflate(R.layout.module_ext_item_self_car, null);
tvSelfSpeed = selfCar.findViewById(R.id.tvSelfSpeed);
tvTrafficLight = selfCar.findViewById(R.id.tvTrafficLight);
tvLimitSpeed = selfCar.findViewById(R.id.tvLimitSpeed);
}
Logger.d(TAG, "init====");
}
public void initView(View root) {
tvSelfSpeed = root.findViewById(R.id.tvSelfSpeed);
tvTrafficLight = root.findViewById(R.id.tvTrafficLight);
tvLimitSpeed = root.findViewById(R.id.tvLimitSpeed);
if(lightCenter) {
tvSelfSpeed = root.findViewById(R.id.tvSelfSpeed);
tvTrafficLight = root.findViewById(R.id.tvTrafficLight);
tvLimitSpeed = root.findViewById(R.id.tvLimitSpeed);
}
}
public void enterVrMode() {
isVrMode = true;
IntentFilter filter = new IntentFilter("com.mogo.launcher.adas.app.biz");
context.registerReceiver(adasReceiver, filter);
MogoApisHandler.getInstance().getApis().getAdasControllerApi().addAdasWarnMessageCallback(this);
MogoApisHandler.getInstance().getApis().getRegisterCenterApi().registerMogoLocationListener(TAG, this);
MogoApisHandler.getInstance().getApis().getWebSocketManagerApi(context).registerOnWebSocketMessageListener(this);
// debug code
tvSelfSpeed.setVisibility(View.VISIBLE);
tvTrafficLight.setVisibility(View.VISIBLE);
tvLimitSpeed.setVisibility(View.VISIBLE);
Logger.d(TAG, "enterVrMode===" + isVrMode);
if (!isVrMode) {
isVrMode = true;
IntentFilter filter = new IntentFilter("com.mogo.launcher.adas.app.biz");
context.registerReceiver(adasReceiver, filter);
MogoApisHandler.getInstance().getApis().getAdasControllerApi().addAdasWarnMessageCallback(this);
MogoApisHandler.getInstance().getApis().getRegisterCenterApi().registerMogoLocationListener(TAG, this);
MogoApisHandler.getInstance().getApis().getWebSocketManagerApi(context).registerOnWebSocketMessageListener(this);
if(!lightCenter) {
handler.sendEmptyMessageDelayed(MSG_REFRESH_CAR_STRATEGY, STRATEGY_DELAY);
}
// debug code
// tvSelfSpeed.setVisibility(View.VISIBLE);
// tvTrafficLight.setVisibility(View.VISIBLE);
// tvLimitSpeed.setVisibility(View.VISIBLE);
}
}
public void exitVrMode() {
if(isVrMode) {
Logger.d(TAG, "退出vr模式===" + isVrMode);
if (isVrMode) {
isVrMode = false;
handler.removeMessages(MSG_REFRESH_CAR_STRATEGY);
tvSelfSpeed.setVisibility(View.GONE);
tvTrafficLight.setVisibility(View.GONE);
tvLimitSpeed.setVisibility(View.GONE);
@@ -91,7 +118,7 @@ public class AdasNoticeHelper implements IMogoAdasWarnMessageCallback, IMogoLoca
@Override
public void onReceiveData(ADASWarnMessage msg) {
Logger.d(TAG, "收到adas warn message, isVrMode: "+isVrMode+" msg: " + msg);
Logger.d(TAG, "收到adas warn message, isVrMode: " + isVrMode + " msg: " + msg);
if (!isVrMode) {
return;
}
@@ -119,20 +146,49 @@ public class AdasNoticeHelper implements IMogoAdasWarnMessageCallback, IMogoLoca
if (!isVrMode) {
return;
}
int speed = (int) (location.getSpeed() * 3.6F);
if (tvSelfSpeed != null) {
tvSelfSpeed.post(() -> {
if (tvSelfSpeed.getVisibility() == View.GONE) {
tvSelfSpeed.setVisibility(View.VISIBLE);
}
tvSelfSpeed.setText("" + speed);
});
currentSpeed = (int) (location.getSpeed() * 3.6F);
if(lightCenter) {
if (tvSelfSpeed != null) {
tvSelfSpeed.post(() -> {
if (tvSelfSpeed.getVisibility() == View.GONE) {
tvSelfSpeed.setVisibility(View.VISIBLE);
}
tvSelfSpeed.setText("" + currentSpeed);
Logger.d(TAG, "onLocationChange: " + currentSpeed);
});
}
}
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_REFRESH_CAR_STRATEGY:
// 自车速度
tvSelfSpeed.setText("" + currentSpeed);
// 红绿灯
if (tvTrafficLight.getVisibility() == View.GONE) {
tvTrafficLight.setVisibility(View.VISIBLE);
}
// todo 设置字体颜色、背景颜色、leftDrawable
switch (lightStatus) {
case "Y":
// 黄灯
break;
case "R":
// 红灯
break;
default:
// 默认绿灯
break;
}
tvTrafficLight.setText(surplusTime + "S");
MogoApisHandler.getInstance().getApis().getMapServiceApi().getMapUIController().showMyLocation(selfCar);
if (isVrMode) {
handler.sendEmptyMessageDelayed(MSG_REFRESH_CAR_STRATEGY, STRATEGY_DELAY);
}
return true;
case MSG_HIDE_LIMIT_SPEED:
tvLimitSpeed.setVisibility(View.GONE);
return true;
@@ -191,11 +247,12 @@ public class AdasNoticeHelper implements IMogoAdasWarnMessageCallback, IMogoLoca
handler.removeMessages(MSG_HIDE_TRAFFIC_LIGHT_BY_OBU);
handler.removeMessages(MSG_HIDE_TRAFFIC_LIGHT_BY_CLOUD);
drawTrafficLight(lightStatus, surplusTime);
handler.sendEmptyMessageDelayed(MSG_HIDE_TRAFFIC_LIGHT_BY_OBU, HIDE_TRAFFIC_LIGHT_DELAY);
handler.sendEmptyMessageDelayed(MSG_HIDE_TRAFFIC_LIGHT_BY_OBU,
HIDE_TRAFFIC_LIGHT_DELAY);
}
}
private void handleCloudTrafficLight(CloudRoadData roadData){
private void handleCloudTrafficLight(CloudRoadData roadData) {
if (tvTrafficLight != null && !handler.hasMessages(MSG_HIDE_TRAFFIC_LIGHT_BY_OBU)) {
handler.removeMessages(MSG_HIDE_TRAFFIC_LIGHT_BY_CLOUD);
// todo drawTrafficLight
@@ -214,7 +271,7 @@ public class AdasNoticeHelper implements IMogoAdasWarnMessageCallback, IMogoLoca
lightStatus = "G";
break;
}
int diff = (int) ((System.currentTimeMillis() - roadData.getSystemTime())/1000);
int diff = (int) ((System.currentTimeMillis() - roadData.getSystemTime()) / 1000);
int leftTime = roadData.getLightLeftTime() - diff;
drawTrafficLight(lightStatus, "" + leftTime);
handler.sendEmptyMessageDelayed(MSG_HIDE_TRAFFIC_LIGHT_BY_CLOUD,
@@ -223,25 +280,29 @@ public class AdasNoticeHelper implements IMogoAdasWarnMessageCallback, IMogoLoca
}
private void drawTrafficLight(String lightStatus, String surplusTime) {
tvTrafficLight.post(() -> {
if (tvTrafficLight.getVisibility() == View.GONE) {
tvTrafficLight.setVisibility(View.VISIBLE);
}
// todo 设置字体颜色、背景颜色、leftDrawable
switch (lightStatus) {
case "Y":
// 黄灯
break;
case "R":
//
break;
default:
// 默认绿
break;
}
tvTrafficLight.setText(surplusTime + "S");
Logger.d(TAG, "展示红绿灯信息: " + lightStatus + " time: " + surplusTime);
});
this.lightStatus = lightStatus;
this.surplusTime = surplusTime;
if(lightCenter) {
tvTrafficLight.post(() -> {
if (tvTrafficLight.getVisibility() == View.GONE) {
tvTrafficLight.setVisibility(View.VISIBLE);
}
// todo 设置字体颜色、背景颜色、leftDrawable
switch (lightStatus) {
case "Y":
//
break;
case "R":
//
break;
default:
// 默认绿灯
break;
}
tvTrafficLight.setText(surplusTime + "S");
Logger.d(TAG, "展示红绿灯信息: " + lightStatus + " time: " + surplusTime);
});
}
}
@Override
@@ -261,7 +322,7 @@ public class AdasNoticeHelper implements IMogoAdasWarnMessageCallback, IMogoLoca
if (roadData != null) {
Logger.d(TAG, "收到红绿灯数据");
handleCloudTrafficLight(roadData);
}else{
} else {
handler.sendEmptyMessage(MSG_HIDE_TRAFFIC_LIGHT_BY_CLOUD);
}
}