package com.bytedance.sdk.openadsdk.yz.fm;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.bytedance.sdk.component.utils.maa;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.BuildConfig;
import com.bytedance.sdk.openadsdk.core.dsz;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.core.model.lse;
import com.bytedance.sdk.openadsdk.core.settings.ku;
import com.bytedance.sdk.openadsdk.utils.DeviceUtils;
import com.bytedance.sdk.openadsdk.utils.bx;
import com.bytedance.sdk.openadsdk.utils.gzf;
import com.bytedance.sdk.openadsdk.utils.nt;
import com.bytedance.sdk.openadsdk.utils.ol;
import com.bytedance.sdk.openadsdk.utils.zan;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* JADX INFO: loaded from: classes2.dex */
public class fm {
    private static volatile fm ajl;
    private long fm;
    private final String jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private Context f7616lb;
    private int ro;
    private final boolean yz = lb();

    private fm() {
        Context contextFm = gof.fm();
        if (contextFm != null) {
            this.f7616lb = contextFm.getApplicationContext();
        }
        this.jnr = jnr();
    }

    private String ajl() {
        StringBuilder sb = new StringBuilder();
        try {
            if (gzf.ajl()) {
                sb.append("MIUI-");
            } else if (gzf.lb()) {
                sb.append("FLYME-");
            } else {
                String strFhx = gzf.fhx();
                if (gzf.fm(strFhx)) {
                    sb.append("EMUI-");
                }
                if (!TextUtils.isEmpty(strFhx)) {
                    sb.append(strFhx);
                    sb.append("-");
                }
            }
            sb.append(Build.VERSION.INCREMENTAL);
        } catch (Throwable unused) {
        }
        return sb.toString();
    }

    private String fm(int i10) {
        return i10 != 120 ? i10 != 160 ? i10 != 240 ? i10 != 320 ? i10 != 480 ? i10 != 640 ? "mdpi" : "xxxhdpi" : "xxhdpi" : "xhdpi" : "hdpi" : "mdpi" : "ldpi";
    }

    private String jnr() {
        return DeviceUtils.lb(this.f7616lb) ? "tv" : DeviceUtils.ro(this.f7616lb) ? "android_pad" : "android";
    }

    private boolean lb() {
        try {
            if (!new File("/system/bin/su").exists()) {
                if (!new File("/system/xbin/su").exists()) {
                    return false;
                }
            }
            return true;
        } catch (Exception unused) {
            return false;
        }
    }

    private int yz() {
        int rawOffset = TimeZone.getDefault().getRawOffset() / 3600000;
        if (rawOffset < -12) {
            rawOffset = -12;
        }
        if (rawOffset > 12) {
            return 12;
        }
        return rawOffset;
    }

    public List<String> ro(List<com.bytedance.sdk.openadsdk.yz.fm> list) {
        JSONArray jSONArrayOptJSONArray = (list.isEmpty() || list.get(0) == null || list.get(0).yz() == null) ? null : list.get(0).yz().optJSONArray("app_log_url_back");
        ArrayList arrayList = new ArrayList();
        if (jSONArrayOptJSONArray != null && jSONArrayOptJSONArray.length() > 0) {
            for (int i10 = 0; i10 < jSONArrayOptJSONArray.length(); i10++) {
                String strAjl = bx.ajl(jSONArrayOptJSONArray.optString(i10));
                if (!TextUtils.isEmpty(strAjl)) {
                    arrayList.add(strAjl);
                }
            }
        }
        return arrayList;
    }

    public static fm fm() {
        if (ajl == null) {
            synchronized (fm.class) {
                try {
                    if (ajl == null) {
                        ajl = new fm();
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        return ajl;
    }

    public String fm(List<com.bytedance.sdk.openadsdk.yz.fm> list) {
        return bx.jnr((list.size() <= 0 || list.get(0) == null || list.get(0).yz() == null) ? "" : list.get(0).yz().optString("app_log_url"));
    }

    private void fm(com.bytedance.sdk.openadsdk.yz.fm fmVar) {
        if (fmVar == null) {
            return;
        }
        try {
            String strOptString = fmVar.yz().optJSONObject("params").optString("log_extra", "");
            long jFm = lse.fm(strOptString);
            int iYz = lse.yz(strOptString);
            if (jFm == 0) {
                jFm = this.fm;
            }
            this.fm = jFm;
            if (iYz == 0) {
                iYz = this.ro;
            }
            this.ro = iYz;
        } catch (Exception e10) {
            wey.lb("AdLogParamsGenerate", "getInfoFromLogExtra exception", e10.getMessage());
        }
    }

    @SuppressLint({"HardwareIds"})
    public JSONObject ro() {
        Context contextFm;
        JSONObject jSONObject = new JSONObject();
        try {
            if (this.f7616lb == null && (contextFm = gof.fm()) != null) {
                this.f7616lb = contextFm.getApplicationContext();
            }
            jSONObject.put("ua", bx.yz());
            jSONObject.put("ad_sdk_version", BuildConfig.VERSION_NAME);
            jSONObject.put("app_version", bx.wu());
            jSONObject.put("sim_op", fm(this.f7616lb));
            jSONObject.put("root", this.yz ? 1 : 0);
            jSONObject.put("timezone", yz());
            jSONObject.put("access", nt.fm(this.f7616lb));
            jSONObject.put("os", "Android");
            jSONObject.put("os_version", Build.VERSION.RELEASE);
            jSONObject.put("os_api", Build.VERSION.SDK_INT);
            jSONObject.put("device_type", this.jnr);
            jSONObject.put("device_model", Build.MODEL);
            jSONObject.put("device_brand", Build.BRAND);
            jSONObject.put("device_manufacturer", Build.MANUFACTURER);
            jSONObject.put(IjkMediaMeta.IJKM_KEY_LANGUAGE, Locale.getDefault().getLanguage());
            jSONObject.put("resolution", zan.jnr(this.f7616lb) + "x" + zan.lb(this.f7616lb));
            jSONObject.put("display_density", fm(zan.vt(this.f7616lb)));
            jSONObject.put("density_dpi", zan.vt(this.f7616lb));
            jSONObject.put("aid", "1371");
            jSONObject.put("device_id", dsz.fm(this.f7616lb));
            jSONObject.put("rom", ajl());
            jSONObject.put("cpu_abi", Build.CPU_ABI);
            jSONObject.put("ut", this.ro);
            jSONObject.put("uid", this.fm);
            jSONObject.put("google_aid", com.bytedance.sdk.openadsdk.sds.fm.ro.fm.fm().ro());
            jSONObject.put("locale_language", DeviceUtils.yz());
            jSONObject.put("screen_bright", Math.ceil(DeviceUtils.jnr() * 10.0f) / 10.0d);
            jSONObject.put("is_screen_off", !DeviceUtils.ro() ? 1 : 0);
            ku kuVarYz = gof.yz();
            Context context = this.f7616lb;
            if (context != null) {
                jSONObject.put("force_language", maa.fm(context, "tt_choose_language"));
            }
            if (kuVarYz.gzf("mnc")) {
                jSONObject.put("mnc", ol.lb());
            }
            if (kuVarYz.gzf("mcc")) {
                jSONObject.put("mcc", ol.ro());
            }
        } catch (Exception unused) {
        }
        return jSONObject;
    }

    public JSONObject fm(List<com.bytedance.sdk.openadsdk.yz.fm> list, long j10, JSONObject jSONObject) {
        JSONObject jSONObject2 = new JSONObject();
        try {
            fm(list.get(0));
            jSONObject2.put("header", jSONObject);
            JSONArray jSONArray = new JSONArray();
            Iterator<com.bytedance.sdk.openadsdk.yz.fm> it = list.iterator();
            while (it.hasNext()) {
                jSONArray.put(it.next().jnr());
            }
            jSONObject2.put("event_v3", jSONArray);
            jSONObject2.put("magic_tag", "ss_app_log");
            jSONObject2.put("_gen_time", j10);
            jSONObject2.put("local_time", j10 / 1000);
        } catch (JSONException unused) {
        }
        return jSONObject2;
    }

    public List<com.bytedance.sdk.openadsdk.yz.fm> fm(List<com.bytedance.sdk.openadsdk.yz.fm> list, int i10) {
        ArrayList arrayList = new ArrayList();
        for (com.bytedance.sdk.openadsdk.yz.fm fmVar : list) {
            try {
                JSONObject jSONObject = new JSONObject();
                JSONObject jSONObjectYz = fmVar.yz();
                jSONObject.putOpt("event", jSONObjectYz.optString("label"));
                long jOptLong = jSONObjectYz.optLong("event_ts", System.currentTimeMillis());
                jSONObject.putOpt("local_time_ms", Long.valueOf(jOptLong));
                jSONObject.putOpt("datetime", com.bytedance.sdk.openadsdk.yz.ajl.f7595lb.format(new Date(jOptLong)));
                JSONObject jSONObject2 = new JSONObject();
                Iterator<String> itKeys = jSONObjectYz.keys();
                while (itKeys.hasNext()) {
                    String next = itKeys.next();
                    if (!TextUtils.equals(next, "label")) {
                        jSONObject2.putOpt(next, jSONObjectYz.opt(next));
                    }
                }
                if (i10 > 0 && fmVar.lb() == 3) {
                    jSONObject2.put("_ad_staging_flag", i10);
                }
                jSONObject.putOpt("params", jSONObject2);
                arrayList.add(new com.bytedance.sdk.openadsdk.yz.ajl(fmVar.fm, jSONObject));
            } catch (Exception e10) {
                wey.lb("AdLogParamsGenerate", e10.getMessage());
            }
        }
        return arrayList;
    }

    private String fm(Context context) {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
            return telephonyManager == null ? "" : telephonyManager.getSimOperator();
        } catch (Throwable unused) {
            return "";
        }
    }
}
