151 lines
4.6 KiB
Java
151 lines
4.6 KiB
Java
package com.mogo.commons;
|
|
|
|
import android.app.Application;
|
|
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.elegant.analytics.Analytics;
|
|
import com.elegant.analytics.AnalyticsConfig;
|
|
import com.elegant.analytics.IAnalyticsParamsProvider;
|
|
import com.elegant.analytics.UploadMode;
|
|
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.utils.ThreadPoolService;
|
|
import com.mogo.utils.TipToast;
|
|
import com.mogo.utils.network.NetConfig;
|
|
|
|
import java.security.SecureRandom;
|
|
import java.util.Map;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
import okhttp3.Request;
|
|
|
|
/**
|
|
* @author congtaowang
|
|
* @since 2019-12-23
|
|
* <p>
|
|
* 描述
|
|
*/
|
|
public class AbsMogoApplication extends Application {
|
|
|
|
private static final String TAG = "AbsMogoApplication";
|
|
|
|
private static Application sApp;
|
|
|
|
public static Application getApp() {
|
|
return sApp;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
sApp = this;
|
|
if ( shouldInit() ) {
|
|
init();
|
|
}
|
|
}
|
|
|
|
protected boolean shouldInit(){
|
|
return true;
|
|
}
|
|
|
|
protected void init() {
|
|
syncInit();
|
|
asyncInit();
|
|
}
|
|
|
|
private void syncInit() {
|
|
// 初始化 arouter
|
|
if ( DebugConfig.isDebug() ) {
|
|
ARouter.openDebug();
|
|
ARouter.openLog();
|
|
}
|
|
ARouter.init( sApp );
|
|
TipToast.init( this, ( ( context, message, tipDrawable ) -> {
|
|
if ( TextUtils.isEmpty( message ) ) {
|
|
return null;
|
|
}
|
|
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
|
|
* @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 ) {
|
|
}
|
|
|
|
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 );
|
|
} )
|
|
.setLoggable( DebugConfig.isDebug() );
|
|
}
|
|
}
|