package com.navinfo.outdoor.api;

import android.app.Application;
import android.os.StrictMode;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.DBCookieStore;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpHeaders;
import com.navinfo.outdoor.util.SdkFolderCreate;
import com.navinfo.outdoor.util.TecentLocationUtils;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.umcrash.UMCrash;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;


public class UserApplication extends Application {
    public static UserApplication userApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        userApplication=this;
        //友盟 检测bug
        UMConfigure.init(this, "60b885bd6c421a3d97db55e6", "navinfo", UMConfigure.DEVICE_TYPE_PHONE, "");
        /**
         *设置组件化的Log开关
         *参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(true);
        String e ="测试自定义异常代码是否运行！！！！！！";
        UMCrash.generateCustomLog(e,"UmengException");

        initOkGo();


        TecentLocationUtils.getInstance(this).startLocation(this);

    }

    public static UserApplication getUserApplication() {
        return userApplication;
    }
    private void initOkGo() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //log相关
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        //log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        //设置请求头,token值
        HttpHeaders headers = new HttpHeaders();
        //headers.put("X-Access-Token", SPUtils.get(SpKey.TOKEN, "")+"");
//        LogUtils.v("X-Access-Token:" + SPUtils.get(SpKey.TOKEN, ""));
        builder.addInterceptor(loggingInterceptor);
        //自动管理cookie（或者叫session的保持），以下几种任选其一就行
        //使用sp保持cookie，如果cookie不过期，则一直有效
        //builder.cookieJar(new CookieJarImpl(new SPCookieStore(this)));
        //使用数据库保持cookie，如果cookie不过期，则一直有效
        builder.cookieJar(new CookieJarImpl(new DBCookieStore(this)));
        //使用内存保持cookie，app退出后，cookie消失
        //builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));
        //超时时间设置，默认60秒
        //全局的读取超时时间

        builder.readTimeout(0, TimeUnit.MILLISECONDS);
        //全局的写入超时时间
        builder.writeTimeout(0, TimeUnit.MILLISECONDS);
        //全局的连接超时时间
        builder.connectTimeout(0, TimeUnit.MILLISECONDS);
        OkGo.getInstance().init(this)
                .setOkHttpClient(builder.build())
                //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheMode(CacheMode.NO_CACHE)
                //全局统一缓存时间，默认永不过期，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                //设置公共请求头
                .addCommonHeaders(headers)
                //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求)，不需要可以设置为0;
                .setRetryCount(0);

        StrictMode.VmPolicy.Builder picBuilder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(picBuilder.build());
        picBuilder.detectFileUriExposure();

    }

}
