package com.rehome.meetingbook;


import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import androidx.multidex.MultiDex;
import com.github.mikephil.charting.utils.Utils;
import com.tencent.bugly.crashreport.CrashReport;
import com.xuexiang.xui.XUI;
import com.yolanda.nohttp.NoHttp;
import com.zhy.autolayout.config.AutoLayoutConifg;
import org.litepal.LitePalApplication;


public class App extends LitePalApplication {


    private static final String APP_NAME = "com.rehome.sbcksyy";

    private String DateSave;

    public String getDateSave() {
        return DateSave;
    }

    public void setDateSave(String dateSave) {
        DateSave = dateSave;
    }

    private static App mInstance;
    public static App getInstance() {
        return mInstance;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("BaseApplication", "onCreate");
        Log.e("BaseApplication", getProcessNameByPID(getApplicationContext(), android.os.Process.myPid()));
        Log.e("isAppMainProcess", String.valueOf(isAppMainProcess()));

        mInstance = this;

        NoHttp.initialize(this);
        MultiDex.install(this);
        AutoLayoutConifg.getInstance().useDeviceSize();
        Utils.init(this);
        XUI.init(this); //初始化UI框架
        CrashReport.initCrashReport(getApplicationContext(), "ef7c97c038", true);

    }

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


    /**
     * 判断是否是主进程
     *
     * @return
     */
    public boolean isAppMainProcess() {
        try {
            int pid = android.os.Process.myPid();
            String process = getProcessNameByPID(getApplicationContext(), pid);
            return TextUtils.isEmpty(process) || APP_NAME.equalsIgnoreCase(process);
        } catch (Exception e) {
            return true;
        }
    }

    /**
     * 根据 pid 获取进程名
     *
     * @param context
     * @param pid
     * @return
     */
    public String getProcessNameByPID(Context context, int pid) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager == null) {
            return "";
        }
        for (android.app.ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo == null) {
                continue;
            }
            if (processInfo.pid == pid) {
                return processInfo.processName;
            }
        }
        return "";
    }
}
