remix the log monitor
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package com.mogo.module.extensions.entrance;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
@@ -258,7 +257,7 @@ public class EntranceFragment extends MvpFragment<EntranceView, EntrancePresente
|
||||
});
|
||||
|
||||
mUploadRoadCondition.setOnLongClickListener(view -> {
|
||||
mApis.getMogoMonitorApi().showLogDebugDialog();
|
||||
mApis.getMogoMonitorApi().getMogoMonitorLog().showLogDebugDialog();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import android.widget.FrameLayout;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.alibaba.android.arouter.launcher.ARouter;
|
||||
import com.mogo.commons.context.ContextHolderUtil;
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
import com.mogo.commons.mvp.MvpActivity;
|
||||
@@ -27,7 +26,6 @@ import com.mogo.module.main.cards.MogoModulesManager;
|
||||
import com.mogo.module.main.service.MogoMainService;
|
||||
import com.mogo.module.main.windowview.FloatingViewHandler;
|
||||
import com.mogo.service.IMogoServiceApis;
|
||||
import com.mogo.service.MogoServicePaths;
|
||||
import com.mogo.service.adas.IMogoADASControlStatusChangedListener;
|
||||
import com.mogo.service.fragmentmanager.IMogoFragmentManager;
|
||||
import com.mogo.service.map.IMogoMapService;
|
||||
@@ -206,7 +204,6 @@ public class MainActivity extends MvpActivity< MainView, MainPresenter > impleme
|
||||
// 初始化MonitorModule
|
||||
// IMogoMonitorProvider monitorProvider = (IMogoMonitorProvider) ARouter.getInstance().build(MogoServicePaths.PATH_MOGO_MONITOR).navigation(this);
|
||||
// monitorProvider.resetActivityContext(this);
|
||||
mServiceApis.getMogoMonitorApi().resetActivityContext( this );
|
||||
}
|
||||
|
||||
private void initAdasControlStatusListener() {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.zhidao.mogo.module.monitor;
|
||||
|
||||
class MogoMonitorConst {
|
||||
public class MogoMonitorConst {
|
||||
|
||||
private MogoMonitorConst(){}
|
||||
|
||||
public static final String MODULE_NAME = "MogoMonitor";
|
||||
public static final String MODULE_PATH = "/monitor/api";
|
||||
|
||||
|
||||
@@ -1,28 +1,11 @@
|
||||
package com.zhidao.mogo.module.monitor;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.alibaba.android.arouter.facade.annotation.Route;
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
import com.mogo.module.common.MogoApisHandler;
|
||||
import com.mogo.service.IMogoServiceApis;
|
||||
import com.mogo.service.connection.IMogoOnMessageListener;
|
||||
import com.mogo.service.monitor.IMogoMonitorLog;
|
||||
import com.mogo.service.monitor.IMogoMonitorProvider;
|
||||
import com.mogo.utils.logger.LogLevel;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import com.mogo.utils.network.NetConfig;
|
||||
import com.mogo.utils.network.utils.GsonUtil;
|
||||
import com.zhidao.mogo.module.monitor.bean.RemoteLogPushContent;
|
||||
import com.zhidao.mogo.module.monitor.dialog.ILogDialogListener;
|
||||
import com.zhidao.mogo.module.monitor.dialog.LogDebugDialog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.zhidao.mogo.module.monitor.log.MogoLogMonitor;
|
||||
|
||||
/**
|
||||
* 应用监控模块provider
|
||||
@@ -30,153 +13,16 @@ import java.util.List;
|
||||
* @author tongchenfei
|
||||
*/
|
||||
@Route(path = MogoMonitorConst.MODULE_PATH)
|
||||
public class MogoMonitorProvider implements IMogoMonitorProvider,
|
||||
IMogoOnMessageListener<RemoteLogPushContent>, ILogDialogListener, Handler.Callback {
|
||||
private static final String TAG = MogoMonitorConst.MODULE_NAME;
|
||||
private static final int MSG_TRY_CLOSE_LOG = 1001;
|
||||
private static final String MANUAL_CATCH_PKG_NAME = "manual-catch-log";
|
||||
private Context context;
|
||||
private LogDebugDialog logDebugDialog;
|
||||
private RemoteLogPushContent manualContent = new RemoteLogPushContent(60,
|
||||
MANUAL_CATCH_PKG_NAME);
|
||||
private Handler handler = new Handler(this);
|
||||
@Override
|
||||
public void showLogDebugDialog() {
|
||||
logDebugDialog.show();
|
||||
}
|
||||
public class MogoMonitorProvider implements IMogoMonitorProvider {
|
||||
|
||||
@Override
|
||||
public void init(Context context) {
|
||||
this.context = context;
|
||||
IMogoServiceApis apis = MogoApisHandler.getInstance().getApis();
|
||||
apis.getSocketManagerApi(context).registerOnMessageListener(MogoMonitorConst.LOG_PUSH_TYPE, this);
|
||||
startRemoteCtrl();
|
||||
}
|
||||
|
||||
private void startRemoteCtrl(){
|
||||
Intent intent = new Intent("com.mogo.remotecontrol.action");
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
ComponentName comp = new ComponentName("com.mogo.remotecontrol",
|
||||
"com.mogo.remotecontrol.RemoteCtrlService");
|
||||
intent.setComponent(comp);
|
||||
context.startService(intent);
|
||||
Logger.d(TAG, "startRemoteCtrl");
|
||||
MogoLogMonitor.getInstance().init(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetActivityContext(Context context) {
|
||||
this.context = context;
|
||||
logDebugDialog = new LogDebugDialog(context);
|
||||
logDebugDialog.setDialogListener(this);
|
||||
public IMogoMonitorLog getMogoMonitorLog() {
|
||||
return MogoLogMonitor.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<RemoteLogPushContent> target() {
|
||||
return RemoteLogPushContent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMsgReceived(RemoteLogPushContent obj) {
|
||||
Logger.d(TAG, "收到push消息: " + obj);
|
||||
switch (obj.getType()) {
|
||||
case MogoMonitorConst.START_CATCH_LOG:
|
||||
if(!catchingList.contains(obj.getPkgName())){
|
||||
startCatchLog(obj);
|
||||
}
|
||||
break;
|
||||
case MogoMonitorConst.STOP_CATCH_LOG:
|
||||
stopCatchLog(obj);
|
||||
break;
|
||||
case MogoMonitorConst.LOCAL_CONFIG_OPEN_LOG:
|
||||
openLoggerLevel();
|
||||
break;
|
||||
case MogoMonitorConst.LOCAL_CONFIG_CLOSE_LOG:
|
||||
closeLoggerLevel();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> catchingList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onLogStart() {
|
||||
// 这个是通过对话框点击开始的回调
|
||||
if(catchingList.contains(MANUAL_CATCH_PKG_NAME)){
|
||||
Toast.makeText(context, "已经在抓日志了", Toast.LENGTH_LONG).show();
|
||||
}else {
|
||||
Logger.d(TAG, "开始抓取日志====");
|
||||
manualContent.setType(MogoMonitorConst.START_CATCH_LOG);
|
||||
startCatchLog(manualContent);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onLogStop() {
|
||||
// 这个是通过对话框点击结束的回调
|
||||
Logger.d(TAG,"结束抓取日志====");
|
||||
manualContent.setType(MogoMonitorConst.STOP_CATCH_LOG);
|
||||
stopCatchLog(manualContent);
|
||||
}
|
||||
|
||||
public void sendCtrlBroadcast(RemoteLogPushContent content) {
|
||||
startRemoteCtrl();
|
||||
|
||||
Intent intent = new Intent(MogoMonitorConst.BROADCAST_LOG_CTRL);
|
||||
intent.putExtra("content", GsonUtil.jsonFromObject(content));
|
||||
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
|
||||
intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
Logger.d(TAG, "sendCtrlBroadcast: " + content);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 放开Logger的限制
|
||||
*/
|
||||
private void openLoggerLevel() {
|
||||
Logger.init(LogLevel.DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据状态收紧Logger的限制
|
||||
*/
|
||||
private void closeLoggerLevel() {
|
||||
if(!catchingList.isEmpty()) {
|
||||
Logger.init(DebugConfig.isDebug() ? LogLevel.DEBUG : LogLevel.OFF);
|
||||
NetConfig.instance().setLoggable(DebugConfig.isDebug());
|
||||
}
|
||||
}
|
||||
|
||||
private void startCatchLog(RemoteLogPushContent content){
|
||||
catchingList.add(content.getPkgName());
|
||||
|
||||
long delay = content.getDuration() * 60 * 1000;
|
||||
handler.removeMessages(MSG_TRY_CLOSE_LOG);
|
||||
if (delay <= 0) {
|
||||
// 如果push 下来的delay小于等于0,那就给个默认最大值一小时
|
||||
delay = 60 * 60 * 1000;
|
||||
}
|
||||
handler.sendEmptyMessageDelayed(MSG_TRY_CLOSE_LOG, delay);
|
||||
openLoggerLevel();
|
||||
sendCtrlBroadcast(content);
|
||||
}
|
||||
|
||||
private void stopCatchLog(RemoteLogPushContent content) {
|
||||
catchingList.remove(content.getPkgName());
|
||||
if (catchingList.isEmpty()) {
|
||||
handler.removeMessages(MSG_TRY_CLOSE_LOG);
|
||||
}
|
||||
sendCtrlBroadcast(content);
|
||||
closeLoggerLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMessage(Message msg) {
|
||||
if (msg.what == MSG_TRY_CLOSE_LOG) {
|
||||
closeLoggerLevel();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
package com.zhidao.mogo.module.monitor.log;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.mogo.commons.AbsMogoApplication;
|
||||
import com.mogo.commons.context.ContextHolderUtil;
|
||||
import com.mogo.commons.debug.DebugConfig;
|
||||
import com.mogo.module.common.MogoApisHandler;
|
||||
import com.mogo.service.connection.IMogoOnMessageListener;
|
||||
import com.mogo.service.monitor.IMogoMonitorLog;
|
||||
import com.mogo.utils.logger.LogLevel;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import com.mogo.utils.network.NetConfig;
|
||||
import com.mogo.utils.network.utils.GsonUtil;
|
||||
import com.zhidao.mogo.module.monitor.MogoMonitorConst;
|
||||
import com.zhidao.mogo.module.monitor.MogoMonitorProvider;
|
||||
import com.zhidao.mogo.module.monitor.bean.RemoteLogPushContent;
|
||||
import com.zhidao.mogo.module.monitor.dialog.ILogDialogListener;
|
||||
import com.zhidao.mogo.module.monitor.dialog.LogDebugDialog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.zhidao.mogo.module.monitor.MogoMonitorConst.LOG_PUSH_TYPE;
|
||||
import static com.zhidao.mogo.module.monitor.MogoMonitorConst.MODULE_NAME;
|
||||
|
||||
public class MogoLogMonitor implements IMogoOnMessageListener<RemoteLogPushContent>, IMogoMonitorLog,
|
||||
ILogDialogListener, Handler.Callback {
|
||||
|
||||
private static final String TAG = MODULE_NAME + "-Log";
|
||||
|
||||
private static volatile MogoLogMonitor mogoLogMonitor;
|
||||
|
||||
private MogoLogMonitor() {
|
||||
|
||||
}
|
||||
|
||||
public static MogoLogMonitor getInstance() {
|
||||
if (mogoLogMonitor == null) {
|
||||
synchronized (MogoLogMonitor.class) {
|
||||
if (mogoLogMonitor == null) {
|
||||
mogoLogMonitor = new MogoLogMonitor();
|
||||
}
|
||||
}
|
||||
}
|
||||
return mogoLogMonitor;
|
||||
}
|
||||
|
||||
private static final int MSG_TRY_CLOSE_LOG = 1001;
|
||||
private static final String MANUAL_CATCH_PKG_NAME = "manual-catch-log";
|
||||
private LogDebugDialog logDebugDialog;
|
||||
private RemoteLogPushContent manualContent = new RemoteLogPushContent(60,
|
||||
MANUAL_CATCH_PKG_NAME);
|
||||
|
||||
private Handler handler = new Handler(this);
|
||||
|
||||
private List<String> catchingList = new ArrayList<>();
|
||||
|
||||
public void init(Context context) {
|
||||
MogoApisHandler.getInstance().getApis()
|
||||
.getSocketManagerApi(AbsMogoApplication.getApp().getApplicationContext())
|
||||
.registerOnMessageListener(LOG_PUSH_TYPE, this);
|
||||
startRemoteCtrl(context);
|
||||
}
|
||||
|
||||
private void startRemoteCtrl(Context context) {
|
||||
Intent intent = new Intent("com.mogo.remotecontrol.action");
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
ComponentName comp = new ComponentName("com.mogo.remotecontrol",
|
||||
"com.mogo.remotecontrol.RemoteCtrlService");
|
||||
intent.setComponent(comp);
|
||||
context.startService(intent);
|
||||
Logger.d(TAG, "startRemoteCtrl");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMessage(Message msg) {
|
||||
if (msg.what == MSG_TRY_CLOSE_LOG) {
|
||||
closeLoggerLevel();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<RemoteLogPushContent> target() {
|
||||
return RemoteLogPushContent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMsgReceived(RemoteLogPushContent obj) {
|
||||
Logger.d(TAG, "收到push消息: " + obj);
|
||||
switch (obj.getType()) {
|
||||
case MogoMonitorConst.START_CATCH_LOG:
|
||||
if (!catchingList.contains(obj.getPkgName())) {
|
||||
startCatchLog(obj);
|
||||
}
|
||||
break;
|
||||
case MogoMonitorConst.STOP_CATCH_LOG:
|
||||
stopCatchLog(obj);
|
||||
break;
|
||||
case MogoMonitorConst.LOCAL_CONFIG_OPEN_LOG:
|
||||
openLoggerLevel();
|
||||
break;
|
||||
case MogoMonitorConst.LOCAL_CONFIG_CLOSE_LOG:
|
||||
closeLoggerLevel();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLogStart() {
|
||||
// 这个是通过对话框点击开始的回调
|
||||
if (catchingList.contains(MANUAL_CATCH_PKG_NAME)) {
|
||||
Toast.makeText(ContextHolderUtil.getContext(), "已经在抓日志了", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Logger.d(TAG, "开始抓取日志====");
|
||||
manualContent.setType(MogoMonitorConst.START_CATCH_LOG);
|
||||
startCatchLog(manualContent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLogStop() {
|
||||
// 这个是通过对话框点击结束的回调
|
||||
Logger.d(TAG, "结束抓取日志====");
|
||||
manualContent.setType(MogoMonitorConst.STOP_CATCH_LOG);
|
||||
stopCatchLog(manualContent);
|
||||
}
|
||||
|
||||
public void sendCtrlBroadcast(RemoteLogPushContent content) {
|
||||
startRemoteCtrl(ContextHolderUtil.getContext());
|
||||
Intent intent = new Intent(MogoMonitorConst.BROADCAST_LOG_CTRL);
|
||||
intent.putExtra("content", GsonUtil.jsonFromObject(content));
|
||||
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
|
||||
intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
Logger.d(TAG, "sendCtrlBroadcast: " + content);
|
||||
ContextHolderUtil.getContext().sendBroadcast(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 放开Logger的限制
|
||||
*/
|
||||
private void openLoggerLevel() {
|
||||
Logger.init(LogLevel.DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据状态收紧Logger的限制
|
||||
*/
|
||||
private void closeLoggerLevel() {
|
||||
if (!catchingList.isEmpty()) {
|
||||
Logger.init(DebugConfig.isDebug() ? LogLevel.DEBUG : LogLevel.OFF);
|
||||
NetConfig.instance().setLoggable(DebugConfig.isDebug());
|
||||
}
|
||||
}
|
||||
|
||||
private void startCatchLog(RemoteLogPushContent content) {
|
||||
catchingList.add(content.getPkgName());
|
||||
|
||||
long delay = content.getDuration() * 60 * 1000;
|
||||
handler.removeMessages(MSG_TRY_CLOSE_LOG);
|
||||
if (delay <= 0) {
|
||||
// 如果push 下来的delay小于等于0,那就给个默认最大值一小时
|
||||
delay = 60 * 60 * 1000L;
|
||||
}
|
||||
handler.sendEmptyMessageDelayed(MSG_TRY_CLOSE_LOG, delay);
|
||||
openLoggerLevel();
|
||||
sendCtrlBroadcast(content);
|
||||
}
|
||||
|
||||
private void stopCatchLog(RemoteLogPushContent content) {
|
||||
catchingList.remove(content.getPkgName());
|
||||
if (catchingList.isEmpty()) {
|
||||
handler.removeMessages(MSG_TRY_CLOSE_LOG);
|
||||
}
|
||||
sendCtrlBroadcast(content);
|
||||
closeLoggerLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showLogDebugDialog() {
|
||||
if (logDebugDialog == null) {
|
||||
logDebugDialog = new LogDebugDialog(ContextHolderUtil.getContext());
|
||||
logDebugDialog.setDialogListener(this);
|
||||
}
|
||||
if (logDebugDialog.isShowing()) {
|
||||
return;
|
||||
}
|
||||
logDebugDialog.show();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user