package com.dahe.pass.base;

import android.app.Application;
import android.content.Context;

import com.dahe.mylibrary.CommonBaseLibrary;
import com.dahe.mylibrary.utils.CrashHandler;
import com.dahe.pass.net.Api;
import com.dahe.pass.oss.OssServiceUtil;
import com.dahe.pass.utils.SPUtils;
import com.taobao.sophix.SophixManager;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;

import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class App extends Application {
    public static final String TAG = "BNSDKSimpleDemo";
    public static final String APP_FOLDER_NAME = TAG;
    public static App mApplication = null;

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("测试分支合并");
        System.out.println("测试分支合并1.0.6");
        mApplication = this;
        CommonBaseLibrary.getInstance().init(this, Api.BASE_URL);
//        if (BuildConfig.DEBUG){
//            CrashHandler.getInstance().init(this);
//        }
        CrashReport.initCrashReport(getApplicationContext(), "230309184b", false);


        //预初始化
//        initPush();
        //友盟华为推送通道
//        HuaWeiRegister.register(this);
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
//        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
//        SDKInitializer.setCoordType(CoordType.BD09LL);

//        SophixManager.getInstance().queryAndLoadNewPatch();

        OssServiceUtil.getInstance().init();
    }


    public static App getApp() {
        return mApplication;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

    }

    //忽略https的证书校验
    public void handleSSLHandshake() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }};

            SSLContext sc = SSLContext.getInstance("TLS");
            // trustAllCerts信任所有的证书
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } catch (Exception ignored) {
        }
    }

    /**
     * 初始化推送SDK，在用户隐私政策协议同意后，再做初始化
     */
//    private void initPushSDK() {
//        /*
//         * 判断用户是否已同意隐私政策
//         * 当同意时，直接进行初始化；
//         * 当未同意时，待用户同意后，通过PushHelper.init(...)方法进行初始化。
//         */
//        boolean agreed = SPUtils.hasAgreePrivacyAgreement(this);
//        if (agreed && PushHelper.isMainProcess(this)) {
////        if (true) {
//            //建议在线程中执行初始化
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    PushHelper.init(getApplicationContext());
//                }
//            }).start();
//        }
//    }

    /*private void initPush(){
        //日志开关
        UMConfigure.setLogEnabled(BuildConfig.DEBUG?true:false);
        //预初始化
        PushHelper.preInit(this);
        //正式初始化
        initPushSDK();
    }*/
}
