package wg;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import wg.j;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes4.dex */
public class i implements j {
    private Context t() {
        return i0.f().a().a();
    }

    private PackageInfo u() {
        Context t10 = t();
        if (t10 == null) {
            return null;
        }
        try {
            PackageManager packageManager = t10.getPackageManager();
            if (packageManager == null) {
                return null;
            }
            return packageManager.getPackageInfo(t10.getPackageName(), 0);
        } catch (Exception e10) {
            t.a("Services", "DeviceInfoService", String.format("PackageManager couldn't find application version (%s)", e10.getLocalizedMessage()), new Object[0]);
            return null;
        }
    }

    private boolean v(String str) {
        return str == null || str.trim().isEmpty();
    }

    @Override // wg.j
    public j.b a() {
        Resources resources;
        Context t10 = t();
        if (t10 == null || (resources = t10.getResources()) == null) {
            return null;
        }
        return new k(resources.getDisplayMetrics());
    }

    @Override // wg.j
    public String b() {
        return "Android";
    }

    @Override // wg.j
    public File c() {
        Context t10 = t();
        if (t10 == null) {
            return null;
        }
        return t10.getCacheDir();
    }

    @Override // wg.j
    public String d() {
        return Build.MANUFACTURER;
    }

    @Override // wg.j
    public String e() {
        ApplicationInfo applicationInfo;
        Context t10 = t();
        if (t10 == null) {
            return null;
        }
        try {
            PackageManager packageManager = t10.getPackageManager();
            if (packageManager == null || (applicationInfo = packageManager.getApplicationInfo(t10.getPackageName(), 0)) == null) {
                return null;
            }
            return (String) packageManager.getApplicationLabel(applicationInfo);
        } catch (Exception e10) {
            t.a("Services", "DeviceInfoService", String.format("PackageManager couldn't find application name (%s)", e10), new Object[0]);
            return null;
        }
    }

    @Override // wg.j
    public String f() {
        return Build.VERSION.RELEASE;
    }

    @Override // wg.j
    public String g() {
        Context t10 = t();
        if (t10 == null) {
            return null;
        }
        return t10.getPackageName();
    }

    @Override // wg.j
    public String h() {
        return Build.MODEL;
    }

    @Override // wg.j
    public String i() {
        return Build.ID;
    }

    @Override // wg.j
    public String j() {
        PackageInfo u10 = u();
        if (u10 == null) {
            return null;
        }
        Locale locale = Locale.US;
        int i10 = 0;
        try {
            i10 = (int) ((Long) u10.getClass().getDeclaredMethod("getLongVersionCode", new Class[0]).invoke(u10, new Object[0])).longValue();
        } catch (Exception e10) {
            t.a("Services", "DeviceInfoService", String.format("Failed to get app version code, (%s)", e10), new Object[0]);
        }
        if (i10 > 0) {
            return String.format(locale, "%d", Integer.valueOf(i10));
        }
        return null;
    }

    @Override // wg.j
    public InputStream k(String str) {
        Context t10 = t();
        if (v(str) || t10 == null) {
            return null;
        }
        Resources resources = t10.getResources();
        if (resources == null) {
            t.a("Services", "DeviceInfoService", String.format("%s (Resources), unable to read (%s) from the the assets folder.", "Unexpected Null Value", str), new Object[0]);
            return null;
        }
        AssetManager assets = resources.getAssets();
        if (assets == null) {
            t.a("Services", "DeviceInfoService", String.format("%s (AssetManager), unable to read (%s) from the the assets folder.", "Unexpected Null Value", str), new Object[0]);
            return null;
        }
        try {
            return assets.open(str);
        } catch (IOException e10) {
            t.a("Services", "DeviceInfoService", String.format("Unable to read (%s) from the the assets folder. (%s)", str, e10), new Object[0]);
            return null;
        }
    }

    @Override // wg.j
    public String l() {
        PackageInfo u10 = u();
        if (u10 != null) {
            return u10.versionName;
        }
        return null;
    }

    @Override // wg.j
    public String m(String str) {
        Context t10 = t();
        if (v(str) || t10 == null) {
            return null;
        }
        PackageManager packageManager = t10.getPackageManager();
        if (packageManager == null) {
            t.a("Services", "DeviceInfoService", String.format("%s (Package Manager), unable to read property for key (%s).", "Unexpected Null Value", str), new Object[0]);
            return null;
        }
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(t10.getPackageName(), 128);
            if (applicationInfo == null) {
                t.a("Services", "DeviceInfoService", String.format("%s (Application info), unable to read property for key (%s).", "Unexpected Null Value", str), new Object[0]);
                return null;
            }
            Bundle bundle = applicationInfo.metaData;
            if (bundle != null) {
                return bundle.getString(str);
            }
            t.a("Services", "DeviceInfoService", String.format("%s (ApplicationInfo's metaData), unable to read property for key (%s).", "Unexpected Null Value", str), new Object[0]);
            return null;
        } catch (Exception e10) {
            t.a("Services", "DeviceInfoService", String.format("Unable to read property for key (%s). Exception - (%s)", str, e10), new Object[0]);
            return null;
        }
    }

    @Override // wg.j
    public String n() {
        String str = b() + " " + f();
        if (v(str)) {
            str = "unknown";
        }
        String q10 = q();
        if (v(q10)) {
            q10 = "unknown";
        }
        return String.format("Mozilla/5.0 (Linux; U; %s; %s; %s Build/%s)", str, q10, v(h()) ? "unknown" : h(), v(i()) ? "unknown" : i());
    }

    @Override // wg.j
    public Locale o() {
        Resources resources;
        Configuration configuration;
        Context t10 = t();
        if (t10 == null || (resources = t10.getResources()) == null || (configuration = resources.getConfiguration()) == null) {
            return null;
        }
        return configuration.getLocales().get(0);
    }

    @Override // wg.j
    public j.a p() {
        Resources resources;
        Context t10 = t();
        if (t10 != null && (resources = t10.getResources()) != null) {
            if ((resources.getConfiguration().uiMode & 15) == 6) {
                return j.a.WATCH;
            }
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            float f10 = displayMetrics.heightPixels / displayMetrics.ydpi;
            float f11 = displayMetrics.widthPixels / displayMetrics.xdpi;
            return Math.sqrt((double) ((f11 * f11) + (f10 * f10))) >= 6.5d ? j.a.TABLET : j.a.PHONE;
        }
        return j.a.UNKNOWN;
    }

    @Override // wg.j
    public String q() {
        Locale o10 = o();
        if (o10 == null) {
            o10 = Locale.US;
        }
        String language = o10.getLanguage();
        String country = o10.getCountry();
        if (country.isEmpty()) {
            return language;
        }
        return language + "-" + country;
    }

    @Override // wg.j
    public String r() {
        return "Application";
    }

    @Override // wg.j
    public String s() {
        TelephonyManager telephonyManager;
        Context t10 = t();
        if (t10 == null || (telephonyManager = (TelephonyManager) t10.getSystemService("phone")) == null) {
            return null;
        }
        return telephonyManager.getNetworkOperatorName();
    }
}
