package com.ttstd.controller.base;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.tencent.mmkv.MMKV;
import com.ttstd.controller.utils.TokenStore;

public class BaseApplication extends Application {
    private static final String TAG = "BaseApplication";

    /**
     * ViewModel中因为经常旋转导致弱引用为空
     */
    @SuppressLint("StaticFieldLeak")
    private static Context mAppContext;

    public static Context getContext() {
        return mAppContext;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate: ");
        mAppContext = getApplicationContext();

        init();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    private void init() {
        Log.e(TAG, "init: ");
        String rootDir = MMKV.initialize(this);
        Log.e(TAG, "mmkv root: " + rootDir);

        // 令牌安全存储初始化，供 OkHttp 拦截器与仓库层全局访问
        TokenStore.init(this);
    }

}
