205 lines
6.9 KiB
Java
205 lines
6.9 KiB
Java
package com.mogo.commons;
|
||
|
||
import android.app.Application;
|
||
import android.content.Context;
|
||
import android.text.TextUtils;
|
||
import android.view.ContextThemeWrapper;
|
||
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.amap.api.navi.AMapNaviView;
|
||
import com.android.internal.policy.MyPhoneLayoutInflater;
|
||
import com.mogo.commons.analytics.AnalyticsUtils;
|
||
import com.mogo.commons.debug.DebugConfig;
|
||
import com.mogo.commons.device.Devices;
|
||
import com.mogo.commons.network.AllAllowedHostnameVerifier;
|
||
import com.mogo.commons.network.Constants;
|
||
import com.mogo.commons.network.ParamsUtil;
|
||
import com.mogo.commons.network.X509TrustManagerImpl;
|
||
import com.mogo.commons.storage.SpStorage;
|
||
import com.mogo.httpdns.HttpDnsConst;
|
||
import com.mogo.httpdns.IMogoHttpDns;
|
||
import com.mogo.httpdns.MogoHttpDnsHandler;
|
||
import com.mogo.utils.ThreadPoolService;
|
||
import com.mogo.utils.TipDrawable;
|
||
import com.mogo.utils.TipToast;
|
||
import com.mogo.utils.logger.Logger;
|
||
import com.mogo.utils.httpdns.HttpSimpleLocation;
|
||
import com.mogo.utils.network.NetConfig;
|
||
|
||
import java.security.SecureRandom;
|
||
|
||
import javax.net.ssl.SSLContext;
|
||
import javax.net.ssl.TrustManager;
|
||
|
||
import okhttp3.Request;
|
||
|
||
/**
|
||
* @author congtaowang
|
||
* @since 2019-12-23
|
||
* <p>
|
||
* 描述
|
||
*/
|
||
public abstract class AbsMogoApplication extends Application {
|
||
|
||
private static final String TAG = "AbsMogoApplication";
|
||
|
||
// 没有换肤功能的LayoutInflater
|
||
private static LayoutInflater mLayoutInflaterNoSkin;
|
||
|
||
public static LayoutInflater getLayoutInflaterNoSkin() {
|
||
return mLayoutInflaterNoSkin;
|
||
}
|
||
|
||
private static Application sApp;
|
||
|
||
public static Application getApp() {
|
||
return sApp;
|
||
}
|
||
|
||
public static AMapNaviView aMapNaviView;
|
||
|
||
public static AMapNaviView getMapNaviView() {
|
||
return aMapNaviView;
|
||
}
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
aMapNaviView = new AMapNaviView(this);
|
||
aMapNaviView.onCreate(null);
|
||
// 在设置皮肤布局填充器之前进行克隆一个出来
|
||
mLayoutInflaterNoSkin = LayoutInflater.from(new ContextThemeWrapper(this, R.style.Theme_AppCompat)).cloneInContext(new ContextThemeWrapper(this, R.style.Theme_AppCompat));
|
||
sApp = this;
|
||
initARouter();
|
||
if ( shouldInit() ) {
|
||
init();
|
||
}
|
||
}
|
||
|
||
private void initARouter() {
|
||
ARouter.init( sApp );
|
||
// 初始化 arouter
|
||
if ( DebugConfig.isDebug() ) {
|
||
ARouter.openDebug();
|
||
ARouter.openLog();
|
||
}
|
||
}
|
||
|
||
protected boolean shouldInit() {
|
||
return true;
|
||
}
|
||
|
||
protected void init() {
|
||
syncInit();
|
||
asyncInit();
|
||
}
|
||
|
||
private void syncInit() {
|
||
MogoHttpDnsHandler.getHttpDnsApi().init(this, this::getCurrentLocation);
|
||
TipToast.init( this, ( ( context, message, tipDrawable ) -> {
|
||
if ( TextUtils.isEmpty( message ) ) {
|
||
return null;
|
||
}
|
||
try {
|
||
View contentView = generateToastView( context, message, tipDrawable );
|
||
return contentView;
|
||
} catch ( Exception e ) {
|
||
}
|
||
return null;
|
||
} ) );
|
||
}
|
||
|
||
protected HttpSimpleLocation getCurrentLocation(){
|
||
return null;
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
/**
|
||
* 忽略 https 验证
|
||
*
|
||
* @return {@link SSLContext}
|
||
* @throws Exception
|
||
*/
|
||
private static SSLContext getSslContext() throws Exception {
|
||
SSLContext sc = null;
|
||
sc = SSLContext.getInstance( "SSL" );
|
||
sc.init( null, new TrustManager[]{new X509TrustManagerImpl()}, new SecureRandom() );
|
||
return sc;
|
||
}
|
||
|
||
private void asyncInit() {
|
||
ThreadPoolService.execute( () -> {
|
||
initNetConfig();
|
||
// 初始化埋点
|
||
AnalyticsUtils.init(sApp);
|
||
Devices.init( getApp() );
|
||
Devices.checkBindState();
|
||
asyncInitImpl();
|
||
} );
|
||
}
|
||
|
||
protected void asyncInitImpl() {
|
||
|
||
}
|
||
|
||
private static void initNetConfig() {
|
||
|
||
try {
|
||
SSLContext sc = getSslContext();
|
||
NetConfig.instance().setSslContext( sc );
|
||
} catch ( Exception e ) {
|
||
}
|
||
|
||
IMogoHttpDns dns = MogoHttpDnsHandler.getHttpDnsApi();
|
||
if(dns == null){
|
||
Logger.d(TAG,"dns is null");
|
||
}
|
||
NetConfig.instance().setSignaturePrefix( Constants.SIGN_PREFIX )
|
||
.setPublicParams( ParamsUtil.getStaticParams() )
|
||
.setHostnameVerifier( new AllAllowedHostnameVerifier() )
|
||
.addNetworkInterceptor( chain -> {
|
||
Request original = chain.request();
|
||
Request request = original.newBuilder()
|
||
.header( "token", SpStorage.getTicket() )
|
||
.method( original.method(), original.body() )
|
||
.build();
|
||
return chain.proceed( request );
|
||
} )
|
||
// 增加域名->域名的转换方式,暂时去掉httpdns方式
|
||
.addInterceptor(chain -> {
|
||
Request request = chain.request();
|
||
String path = request.url().encodedPath();
|
||
String host = "http://" + dns.syncGetHttpDns(request.url().host().replace("http://", "").replace("https://", ""), HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP, true);
|
||
String url = host + path;
|
||
Logger.d("DomainExchange", "oriHost: " + request.url().host() + " newHost: " + host+" \r\n newUrl: "+url);
|
||
return chain.proceed(request.newBuilder().url(url).build());
|
||
})
|
||
.setHttpDns( null )
|
||
.setLoggable( DebugConfig.isDebug() );
|
||
}
|
||
}
|