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.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.TipDrawable; import com.mogo.utils.TipToast; import com.mogo.utils.network.NetConfig; import com.mogo.utils.storage.SharedPrefsMgr; import com.zhidao.account.sdk.AccountClientManager; import com.zhidao.account.sdk.callback.TicketInfoCallback; import com.zhidao.account.sdk.network.NetEnvironManager; import java.io.IOException; import java.security.SecureRandom; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; /** * @author congtaowang * @since 2019-12-23 *
* 描述 */ 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; } ) ); DebugConfig.setUseCustomMap( SharedPrefsMgr.getInstance( this ).getBoolean( "useCustomMap", false ) ); } /** * 忽略 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(); // 初始化toast // 初始化埋点 Analytics.getInstance().start( sApp ); Analytics.getInstance().setAppKey( "6bbe7e0e1ecd8e2f8dc336e1678a2791" ); // 0 - debug 近实时上报,积累一条埋点上报,或者积累3秒上报一次。 // 2 - 本地缓存,聚合上报,积累30条埋点上报,或者积累60秒上报一次。 AnalyticsConfig.getInstance( sApp ).setMode( DebugConfig.isDebug() ? 0 : 2 ); AnalyticsConfig.getInstance( sApp ).shouldLog( DebugConfig.isDebug() ); 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( new Interceptor() { @Override public Response intercept( Chain chain ) throws IOException { 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() ); } }