package com.dowse.base.util;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import com.dowse.base.log.DSLog;
import java.lang.reflect.Method;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/ContextUtil.class */
public class ContextUtil {
    private static final String TAG = "ContextUtil";

    public static Application getApplication() {
        Application application = null;
        try {
            Class atClass = Class.forName("android.app.ActivityThread");
            Method currentApplicationMethod = atClass.getDeclaredMethod("currentApplication", new Class[0]);
            currentApplicationMethod.setAccessible(true);
            application = (Application) currentApplicationMethod.invoke(null, new Object[0]);
        } catch (Exception e) {
            DSLog.d(TAG, "e:" + e.toString());
        }
        if (application != null) {
            return application;
        }
        try {
            Class atClass2 = Class.forName("android.app.AppGlobals");
            Method currentApplicationMethod2 = atClass2.getDeclaredMethod("getInitialApplication", new Class[0]);
            currentApplicationMethod2.setAccessible(true);
            application = (Application) currentApplicationMethod2.invoke(null, new Object[0]);
        } catch (Exception e2) {
            DSLog.d(TAG, "e:" + e2.toString());
        }
        return application;
    }

    public static int getVersionCode(Context context, String packageName) {
        PackageManager manager = context.getPackageManager();
        long code = -1;
        try {
            PackageInfo info = manager.getPackageInfo(packageName, 0);
            code = info.getLongVersionCode();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return (int) code;
    }

    public static String getVersionName(Context context, String packageName) {
        PackageManager manager = context.getPackageManager();
        String name = "";
        try {
            PackageInfo info = manager.getPackageInfo(packageName, 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return name;
    }
}
