package com.example.writeepc.base;

import android.app.Application;

import com.example.writeepc.utils.SharedPreferencesUtils;
import com.handheld.uhfr.UHFRManager;
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.MemoryCookieStore;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpHeaders;

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

import okhttp3.OkHttpClient;


/**
 * @author: wangh
 * @description:
 * @date: 2019/06/11-13:01
 */
public class MyApplication extends Application {


    private static MyApplication application;
    private UHFRManager uhfrManager;

    public static MyApplication getApplication() {
        return application;
    }

    public UHFRManager getUhfrManager() {
        return uhfrManager;
    }

    public void setUhfrManager(UHFRManager uhfrManager) {
        this.uhfrManager = uhfrManager;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        SharedPreferencesUtils.init(getApplicationContext(), "myapp");
        okGoBase();
    }

    // OKGO 初始化
    private void okGoBase() {
        HttpHeaders headers = new HttpHeaders();
        headers.put("Content-Type", "text/xml; charset=utf-8");
        // headers.put("SOAPAction", "http://tempuri.org/RFID_BarcodBind");
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);        // log打印级别，决定了log显示的详细程度
        loggingInterceptor.setColorLevel(Level.SEVERE);                             // log颜色级别，决定了log在控制台显示的颜色
        HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();         // 方法一：信任所有证书,不安全有风险
        builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(6000, TimeUnit.MILLISECONDS);                    // 全局的读取超时时间
        builder.writeTimeout(6000, TimeUnit.MILLISECONDS);                   // 全局的写入超时时间
        builder.connectTimeout(6000, TimeUnit.MILLISECONDS);                    // 全局的连接超时时间
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));              // 使用内存保持cookie，app退出后，cookie消失
        OkGo.getInstance().init(this).setOkHttpClient(builder.build())  // 建议设置OkHttpClient，不设置将使用默认的
                .setCacheMode(CacheMode.NO_CACHE)                            // 全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)                // 全局统一缓存时间，默认永不过期，可以不传
                .setRetryCount(0)
                .addCommonHeaders(headers);


    }


}
