package cc.winboll.studio.libappbase;

/**
 * @Author ZhanGSKen<zhangsken@qq.com>
 * @Date 2025/01/05 10:10:23
 * @Describe 全局应用类
 */
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import cc.winboll.studio.libappbase.GlobalApplication;

public class GlobalApplication extends Application {

    public static final String TAG = "GlobalApplication";
	
    // 应用是否处于调试状态
    volatile static boolean isDebuging = false;
    
    public static void setIsDebuging(boolean isDebuging) {
        GlobalApplication.isDebuging = isDebuging;
    }
    
    public static void saveDebugStatus(Context context) {
        APPModel.saveBeanToFile(getAPPModelFilePath(context), new APPModel(GlobalApplication.isDebuging));
    }
	
    static String getAPPModelFilePath(Context context) {
        return context.getDataDir().getPath() + "/APPModel.json";
    }

    public static boolean isDebuging() {
        return isDebuging;
    }

    @Override
    public void onCreate() {
        super.onCreate();
		
        setIsDebuging(true);
        // 添加日志模块
        LogUtils.init(this);
        // 设置应用异常处理窗口
        CrashHandler.init(this);
        // 初始化 Toast 框架
        ToastUtils.init(this);

        // 应用保存的调试标志
        APPModel appModel = APPModel.loadBeanFromFile(getAPPModelFilePath(this), APPModel.class);
        if (appModel == null) {
            setIsDebuging(false);
            saveDebugStatus(this);
        } else {
            setIsDebuging(appModel.isDebuging());
        }
    }
	
	public static String getAppName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(
                context.getPackageName(), 0);
            return (String) packageManager.getApplicationLabel(applicationInfo);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
