|
|
@ -78,6 +78,7 @@ public class ContainerHandler extends Handler { |
|
|
public static final int INIT_JXB2 = 11; //设置机械臂
|
|
|
public static final int INIT_JXB2 = 11; //设置机械臂
|
|
|
public int goMemoryTime =0; |
|
|
public int goMemoryTime =0; |
|
|
private boolean isSetOver = false; //是否设置过开机时间了,默认没有设置过
|
|
|
private boolean isSetOver = false; //是否设置过开机时间了,默认没有设置过
|
|
|
|
|
|
private boolean lastHdmiEnabled = true;//上次HDMI信号状态,用于检测off→on恢复以解除MQTT主动停止标志
|
|
|
|
|
|
|
|
|
private WeakReference<ContainerService> weakReference; |
|
|
private WeakReference<ContainerService> weakReference; |
|
|
|
|
|
|
|
|
@ -242,23 +243,85 @@ public class ContainerHandler extends Handler { |
|
|
// 获取 ActivityManager 服务
|
|
|
// 获取 ActivityManager 服务
|
|
|
ActivityManager activityManager = (ActivityManager) MyApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE); |
|
|
ActivityManager activityManager = (ActivityManager) MyApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE); |
|
|
|
|
|
|
|
|
final Debug.MemoryInfo[] memInfo = activityManager.getProcessMemoryInfo(new int[]{android.os.Process.myPid()}); |
|
|
|
|
|
final int totalPss = memInfo[0].getTotalPss(); |
|
|
|
|
|
|
|
|
//部分定制ROM(本机rk3399 eng)下getTotalPss恒返回0,改用/proc/self/status的VmRSS作为可靠的主进程内存来源
|
|
|
|
|
|
int rssMb = readProcRssMb(); |
|
|
|
|
|
LoggerUtil.e("getMemory()","运行内存(RSS):"+rssMb+"MB"); |
|
|
|
|
|
|
|
|
LoggerUtil.e("getMemory()","运行内存:"+totalPss/1024); |
|
|
|
|
|
|
|
|
|
|
|
// Long cpuUsage = getCpuUsage();
|
|
|
|
|
|
// if(cpuUsage>50){
|
|
|
|
|
|
// LoggerUtil.e("ContainerHandler","cpu使用率:"+cpuUsage+"%");
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
//检测WebView渲染进程的内存和显存(WebView渲染器运行在独立进程,OOM会导致渲染进程崩溃)
|
|
|
|
|
|
String deviceType = DeviceData.getDeviceInfo(DeviceData.HINT_DEVICE_TYPE); |
|
|
|
|
|
if ("导视".equals(deviceType) || "水牌".equals(deviceType)) { |
|
|
|
|
|
checkWebViewMemory(activityManager); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if(totalPss/1024>1200){ |
|
|
|
|
|
//内存超过了1G会出现卡顿,内存溢出问题。重启设备。
|
|
|
|
|
|
|
|
|
if(rssMb>2000){ |
|
|
|
|
|
//内存超过了2.4G会出现卡顿,内存溢出问题。重启软件。
|
|
|
LoggerUtil.e("getMemory()","内存溢出重启软件"); |
|
|
LoggerUtil.e("getMemory()","内存溢出重启软件"); |
|
|
EventBus.getDefault().post(new MessageEvent(Constant.ACTION_RESTART_APP)); |
|
|
EventBus.getDefault().post(new MessageEvent(Constant.ACTION_RESTART_APP)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 读取本进程 /proc/self/status 的 VmRSS(物理内存占用),单位MB,失败返回0。 |
|
|
|
|
|
* 用于替代部分定制ROM下恒返回0的 getProcessMemoryInfo().getTotalPss()。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private int readProcRssMb(){ |
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader("/proc/self/status"))) { |
|
|
|
|
|
String line; |
|
|
|
|
|
while ((line = reader.readLine()) != null) { |
|
|
|
|
|
if (line.startsWith("VmRSS:")) { |
|
|
|
|
|
//形如: VmRSS: 123456 kB
|
|
|
|
|
|
String[] parts = line.trim().split("\\s+"); |
|
|
|
|
|
return Integer.parseInt(parts[1]) / 1024; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Throwable t) { |
|
|
|
|
|
LoggerUtil.e("readProcRssMb", StringUtil.getThrowableStr(t)); |
|
|
|
|
|
} |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 检测WebView渲染进程的内存和显存 |
|
|
|
|
|
* WebView渲染器运行在独立的沙箱进程(sandboxed_process)中,其内存不计入主进程PSS, |
|
|
|
|
|
* 渲染进程显存/内存过高会导致渲染进程被系统杀死(onRenderProcessGone),表现为app莫名重启。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private void checkWebViewMemory(ActivityManager activityManager){ |
|
|
|
|
|
try { |
|
|
|
|
|
List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses(); |
|
|
|
|
|
if(processes == null) return; |
|
|
|
|
|
String pkg = MyApplication.getInstance().getPackageName(); |
|
|
|
|
|
for(ActivityManager.RunningAppProcessInfo info : processes){ |
|
|
|
|
|
if(info.processName == null) continue; |
|
|
|
|
|
//只统计本应用的WebView渲染/沙箱进程
|
|
|
|
|
|
boolean isWebViewProcess = info.processName.contains("sandboxed_process") |
|
|
|
|
|
|| info.processName.contains("webview"); |
|
|
|
|
|
if(!isWebViewProcess || !info.processName.startsWith(pkg)) continue; |
|
|
|
|
|
|
|
|
|
|
|
Debug.MemoryInfo[] wvMem = activityManager.getProcessMemoryInfo(new int[]{info.pid}); |
|
|
|
|
|
if(wvMem == null || wvMem.length == 0) continue; |
|
|
|
|
|
int wvPss = wvMem[0].getTotalPss(); |
|
|
|
|
|
int wvGraphics = parseMemoryStat(wvMem[0], "summary.graphics"); |
|
|
|
|
|
LoggerUtil.e("getMemory()","WebView渲染进程["+info.processName+"]内存:" |
|
|
|
|
|
+ wvPss/1024 + "MB,显存:" + wvGraphics/1024 + "MB"); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Throwable t){ |
|
|
|
|
|
LoggerUtil.e("checkWebViewMemory", StringUtil.getThrowableStr(t)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 读取MemoryInfo的指定统计项(如summary.graphics),单位KB,解析失败返回0 |
|
|
|
|
|
*/ |
|
|
|
|
|
private int parseMemoryStat(Debug.MemoryInfo memInfo, String key){ |
|
|
|
|
|
try { |
|
|
|
|
|
String value = memInfo.getMemoryStat(key); |
|
|
|
|
|
if(value == null) return 0; |
|
|
|
|
|
return Integer.parseInt(value); |
|
|
|
|
|
} catch (Throwable t){ |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private long mLastCpuTime; |
|
|
private long mLastCpuTime; |
|
|
private long mLastAppCpuTime; |
|
|
private long mLastAppCpuTime; |
|
|
|
|
|
|
|
|
@ -315,18 +378,24 @@ public class ContainerHandler extends Handler { |
|
|
try { |
|
|
try { |
|
|
if(Constant.androidBoardType.equals("xwst") && Constant.screenType.equals("HDMI")){ |
|
|
if(Constant.androidBoardType.equals("xwst") && Constant.screenType.equals("HDMI")){ |
|
|
//欣威视通假关机
|
|
|
//欣威视通假关机
|
|
|
LoggerUtil.e("mqttState","HDMI结果:"+RootCmdUtil.HDMIEnabled()+",mqttState结果:"+Constant.mqttState); |
|
|
|
|
|
if(!RootCmdUtil.HDMIEnabled()){ |
|
|
|
|
|
|
|
|
boolean hdmiOn = RootCmdUtil.HDMIEnabled(); |
|
|
|
|
|
LoggerUtil.e("mqttState","HDMI结果:"+hdmiOn+",mqttState结果:"+Constant.mqttState); |
|
|
|
|
|
boolean isService = DeviceUtil.isServiceRunning(MyApplication.getInstance(), "MQTTService"); |
|
|
|
|
|
if(!hdmiOn){ |
|
|
//HDMI无信号
|
|
|
//HDMI无信号
|
|
|
if(Constant.mqttState.equals("on")){ |
|
|
|
|
|
|
|
|
if(isService){ |
|
|
LoggerUtil.e("mqttState","HDMI无信号关闭MQTTService"); |
|
|
LoggerUtil.e("mqttState","HDMI无信号关闭MQTTService"); |
|
|
MyApplication.getInstance().stopService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
|
|
|
|
|
|
MQTTService.stopAndDisableReconnect(MyApplication.getInstance()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
}else { |
|
|
}else { |
|
|
//HDMI有信号
|
|
|
//HDMI有信号
|
|
|
boolean isService = DeviceUtil.isServiceRunning(MyApplication.getInstance(), "MQTTService"); |
|
|
|
|
|
|
|
|
if(!lastHdmiEnabled){ |
|
|
|
|
|
//HDMI信号off→on恢复,解除主动停止标志,允许重新拉起MQTT
|
|
|
|
|
|
MQTTService.manualStopped = false; |
|
|
|
|
|
} |
|
|
LoggerUtil.e("mqttState","MQTTService:"+isService); |
|
|
LoggerUtil.e("mqttState","MQTTService:"+isService); |
|
|
|
|
|
//主动停止(stopService)后不自动拉起,直到HDMI/开屏恢复或重新启动服务
|
|
|
|
|
|
if(!MQTTService.manualStopped){ |
|
|
if(Constant.mqttState.equals("off") || !isService){ |
|
|
if(Constant.mqttState.equals("off") || !isService){ |
|
|
LoggerUtil.e("mqttState","HDMI有信号开启MQTTService"); |
|
|
LoggerUtil.e("mqttState","HDMI有信号开启MQTTService"); |
|
|
MyApplication.getInstance().startService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
MyApplication.getInstance().startService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
@ -334,6 +403,8 @@ public class ContainerHandler extends Handler { |
|
|
EventBus.getDefault().post(new MessageEvent(Constant.ACTION_MQTT_STATE));//通知mqtt状态判断
|
|
|
EventBus.getDefault().post(new MessageEvent(Constant.ACTION_MQTT_STATE));//通知mqtt状态判断
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
lastHdmiEnabled = hdmiOn; |
|
|
}else if(Constant.androidBoardType.equals("xwst2") && Constant.screenType.equals("HDMI")){ |
|
|
}else if(Constant.androidBoardType.equals("xwst2") && Constant.screenType.equals("HDMI")){ |
|
|
|
|
|
|
|
|
RootCmdUtil.checkHDMIEnabled(new HdmiStatusCallback() { |
|
|
RootCmdUtil.checkHDMIEnabled(new HdmiStatusCallback() { |
|
|
@ -344,12 +415,18 @@ public class ContainerHandler extends Handler { |
|
|
//HDMI无信号
|
|
|
//HDMI无信号
|
|
|
if(Constant.mqttState.equals("on")){ |
|
|
if(Constant.mqttState.equals("on")){ |
|
|
LoggerUtil.e("mqttState","HDMI无信号关闭MQTTService"); |
|
|
LoggerUtil.e("mqttState","HDMI无信号关闭MQTTService"); |
|
|
MyApplication.getInstance().stopService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
|
|
|
|
|
|
MQTTService.stopAndDisableReconnect(MyApplication.getInstance()); |
|
|
} |
|
|
} |
|
|
}else { |
|
|
}else { |
|
|
//HDMI有信号
|
|
|
//HDMI有信号
|
|
|
|
|
|
if(!lastHdmiEnabled){ |
|
|
|
|
|
//HDMI信号off→on恢复,解除主动停止标志,允许重新拉起MQTT
|
|
|
|
|
|
MQTTService.manualStopped = false; |
|
|
|
|
|
} |
|
|
boolean isService = DeviceUtil.isServiceRunning(MyApplication.getInstance(), "MQTTService"); |
|
|
boolean isService = DeviceUtil.isServiceRunning(MyApplication.getInstance(), "MQTTService"); |
|
|
LoggerUtil.e("mqttState","MQTTService:"+isService); |
|
|
LoggerUtil.e("mqttState","MQTTService:"+isService); |
|
|
|
|
|
//主动停止(stopService)后不自动拉起,直到HDMI/开屏恢复或重新启动服务
|
|
|
|
|
|
if(!MQTTService.manualStopped){ |
|
|
if(Constant.mqttState.equals("off") || !isService){ |
|
|
if(Constant.mqttState.equals("off") || !isService){ |
|
|
LoggerUtil.e("mqttState","HDMI有信号开启MQTTService"); |
|
|
LoggerUtil.e("mqttState","HDMI有信号开启MQTTService"); |
|
|
MyApplication.getInstance().startService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
MyApplication.getInstance().startService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
@ -358,8 +435,12 @@ public class ContainerHandler extends Handler { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
lastHdmiEnabled = isEnabled; |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
}else { |
|
|
}else { |
|
|
|
|
|
//非HDMI板:主动停止(stopService)后不自动拉起,待开屏/重新启动服务(onCreate清除标志)时恢复
|
|
|
|
|
|
if(!MQTTService.manualStopped){ |
|
|
if(Constant.mqttState.equals("off")){ |
|
|
if(Constant.mqttState.equals("off")){ |
|
|
LoggerUtil.e("mqttState","MQTT被关闭了,开启MQTTService"); |
|
|
LoggerUtil.e("mqttState","MQTT被关闭了,开启MQTTService"); |
|
|
MyApplication.getInstance().startService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
MyApplication.getInstance().startService(new Intent(MyApplication.getInstance(),MQTTService.class)); |
|
|
@ -367,6 +448,7 @@ public class ContainerHandler extends Handler { |
|
|
EventBus.getDefault().post(new MessageEvent(Constant.ACTION_MQTT_STATE));//通知mqtt状态判断
|
|
|
EventBus.getDefault().post(new MessageEvent(Constant.ACTION_MQTT_STATE));//通知mqtt状态判断
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
}catch (Exception e){ |
|
|
}catch (Exception e){ |
|
|
LoggerUtil.e("mqttState", "判断MQTT状态失败", e); |
|
|
LoggerUtil.e("mqttState", "判断MQTT状态失败", e); |
|
|
|