Files
MoGoEagleEye/foudations/mogo-commons/src/main/java/com/mogo/commons/AbsMogoApplication.java
donghongyu 81e76f2dd4 [Upload]
整理代码控制日志打印

Signed-off-by: donghongyu <donghongyu@zhidaoauto.com>
2022-01-27 18:21:22 +08:00

187 lines
5.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.mogo.commons;
import static com.mogo.aicloud.services.httpdns.HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP;
import static com.mogo.commons.debug.DebugConfig.CAR_MACHINE_TYPE_SELF_INNOVATE;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mogo.aicloud.services.httpdns.IMogoHttpDns;
import com.mogo.aicloud.services.httpdns.MogoHttpDnsHandler;
import com.mogo.commons.analytics.AnalyticsUtils;
import com.mogo.commons.crash.FinalizeCrashFixer;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.commons.device.Devices;
import com.mogo.commons.network.NetConfigUtils;
import com.mogo.eagle.core.utilcode.mogo.logger.Logger;
import com.mogo.eagle.core.utilcode.mogo.toast.TipDrawable;
import com.mogo.eagle.core.utilcode.mogo.toast.TipToast;
import com.mogo.eagle.core.utilcode.util.AppStateManager;
import com.mogo.eagle.core.utilcode.util.AppUtils;
import com.mogo.eagle.core.utilcode.util.CleanUtils;
import com.mogo.eagle.core.utilcode.util.ThreadPoolService;
import com.mogo.eagle.core.utilcode.util.Utils;
/**
* @author congtaowang
* @since 2019-12-23
* <p>
* 必须继承并实现列面的方法,这里完成了鹰眼基础服务的初始化操作
*/
public abstract class AbsMogoApplication extends Application {
private static final String TAG = "AbsMogoApplication";
private static Application sApp;
public static Application getApp() {
return sApp;
}
private static IMogoHttpDns sApis;
@Override
public void onCreate() {
super.onCreate();
AppStateManager.INSTANCE.init(this);
sApp = this;
FinalizeCrashFixer.fix();
initARouter();
Utils.init(this);
if (shouldInit()) {
initHttpDns();
}
}
/**
* 初始化跨模块框架 ARouter
*/
private void initARouter() {
try {
ARouter.init(sApp);
// 初始化 arouter
if (DebugConfig.isDebug()) {
ARouter.openDebug();
ARouter.openLog();
}
} catch (Exception e) {
e.printStackTrace();
// 由于ARouter会在SP_AROUTER_CACHE.xml缓存路由表如果出现了被删除的情况会报错这里清除下就好了
CleanUtils.cleanInternalSp();
// 重启应用
AppUtils.relaunchApp();
}
}
protected boolean shouldInit() {
return true;
}
/**
* 初始化 HttpDNS ,这里会通过一个接口获取所有鹰眼中使用的微服务域名以及端口号
* 后续的网络请求会通过 HttpDnsInterceptor 进行拦截替换
*/
protected void initHttpDns() {
if (sApis == null) {
sApis = MogoHttpDnsHandler.getHttpDnsApi();
}
}
/**
* 初始化 自定义样式e饿
*/
protected void initTipToast() {
TipToast.init(this, ((context, message, tipDrawable) -> {
if (TextUtils.isEmpty(message)) {
return null;
}
try {
View contentView = generateToastView(context, message, tipDrawable);
return contentView;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}));
}
/**
* 初始化自定义 Toast View
*/
private View generateToastView(Context context, String message, TipDrawable tipDrawable) {
View contentView;
if (tipDrawable == null) {
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast, null);
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
txt.setText(message);
} else {
// 有图片,使用带图片的布局,当前只实现了左侧图片
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast_with_left_drawable, null);
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
ImageView img = contentView.findViewById(R.id.module_commons_toast_left_drawable);
img.setImageDrawable(tipDrawable.getDrawable());
ViewGroup.LayoutParams params = img.getLayoutParams();
params.width = tipDrawable.getWidth();
params.height = tipDrawable.getHeight();
txt.setText(message);
}
return contentView;
}
/**
* 异步初始化
*/
protected void asyncInit() {
ThreadPoolService.execute(() -> {
//初始化网络配置
NetConfigUtils.init();
// 初始化埋点
AnalyticsUtils.init(sApp);
if (DebugConfig.getCarMachineType() == CAR_MACHINE_TYPE_SELF_INNOVATE) {
Devices.init(getApp());
Devices.checkBindState();
}
asyncInitImpl();
});
}
/**
* 异步厨初始化
*/
protected void asyncInitImpl() {
}
// 缓存IP地址
private String cacheIp = null;
/**
* 请求获取最新的 DNS 微服务 域名信息
*/
protected void registerSocketHttpDnsTTL(String host) {
sApis.addressChangedListener(map -> {
Logger.d("TEST-SOCKET", "ttl callBack ,ready to getCache Dns IP");
String dnsCacheIp = sApis.getCachedHttpDnsIps(host, HTTP_DNS_ADDRESS_TYPE_HTTP);
if (dnsCacheIp == null) {
return;
}
Logger.d("TEST-SOCKET", "获取缓存Dns IP : " + dnsCacheIp + " , 原缓存 IP " + cacheIp);
if (!dnsCacheIp.equals(cacheIp)) {
socketTTL();
this.cacheIp = dnsCacheIp;
}
});
}
protected abstract void socketTTL();
}