package com.bytedance.sdk.openadsdk.sds.fm.ro;

import android.os.SystemClock;
import android.text.TextUtils;
import com.bytedance.sdk.openadsdk.core.dsz;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.core.settings.ku;
import com.bytedance.sdk.openadsdk.irt.fm.yz;
import com.bytedance.sdk.openadsdk.irt.lb;
import com.bytedance.sdk.openadsdk.irt.ro;
import com.bytedance.sdk.openadsdk.utils.mq;
import com.bytedance.sdk.openadsdk.utils.xgn;
import com.google.android.gms.ads.identifier.a;
import com.google.android.gms.ads.identifier.b;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class fm {
    private static volatile fm wu;
    private volatile Boolean fm = null;
    private AtomicLong ro = new AtomicLong(0);

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private int f7529lb = 0;
    private String yz = "";
    private final CountDownLatch jnr = new CountDownLatch(1);
    private volatile boolean ajl = false;
    private String wsy = "";

    private fm() {
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean lb() {
        if (this.fm == null) {
            synchronized (this) {
                try {
                    if (this.fm == null) {
                        this.fm = Boolean.valueOf(((int) ((Math.random() * 100.0d) + 1.0d)) <= lb.ro("gid_status", 100));
                        if (this.fm.booleanValue()) {
                            try {
                                this.wsy = mq.fm();
                            } catch (Throwable unused) {
                                this.wsy = "default";
                            }
                        }
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
        return this.fm.booleanValue();
    }

    public String ro() {
        if (gof.yz().pf() == 1 && !xgn.ajl()) {
            if (!this.ajl) {
                try {
                    this.jnr.await(4L, TimeUnit.SECONDS);
                } catch (InterruptedException unused) {
                } catch (Throwable th) {
                    this.ajl = true;
                    this.jnr.countDown();
                    throw th;
                }
                this.ajl = true;
                this.jnr.countDown();
            }
            return this.yz;
        }
        return this.yz;
    }

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

    /* JADX INFO: Access modifiers changed from: private */
    public static String ro(Throwable th) {
        if (th == null) {
            return "";
        }
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(th.toString());
            for (StackTraceElement stackTraceElement : th.getStackTrace()) {
                sb.append("\n\tat ");
                sb.append(stackTraceElement.toString());
            }
            return sb.toString();
        } catch (Throwable unused) {
            return "";
        }
    }

    public void fm(boolean z10) {
        if (this.f7529lb == 1 || !TextUtils.isEmpty(this.yz)) {
            return;
        }
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        if (z10 || this.ro.get() <= jElapsedRealtime) {
            this.ro.set(jElapsedRealtime + 300000);
            final long jElapsedRealtime2 = SystemClock.elapsedRealtime();
            xgn.fm(new com.bytedance.sdk.component.wu.ro.lb("pag_gaid") { // from class: com.bytedance.sdk.openadsdk.sds.fm.ro.fm.1
                /* JADX WARN: Multi-variable type inference failed */
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        a aVarA = b.a(gof.fm());
                        boolean z11 = aVarA.f7871a;
                        if (z11 != 0) {
                            fm.this.f7529lb = 1;
                            fm.this.fm(2, "lmt", SystemClock.elapsedRealtime() - jElapsedRealtime2);
                        }
                        if (z11 == 0) {
                            String id2 = aVarA.getId();
                            String str = fm.this.yz;
                            if (TextUtils.isEmpty(id2)) {
                                fm.this.fm(4, "empty gaid", SystemClock.elapsedRealtime() - jElapsedRealtime2);
                            } else {
                                fm.this.yz = id2;
                                fm.this.fm(true, SystemClock.elapsedRealtime() - jElapsedRealtime2);
                            }
                            if (!str.equals(id2)) {
                                dsz.fm();
                            }
                        }
                        if (z11 != -1) {
                            com.bytedance.sdk.openadsdk.core.lb.fm().fm("limit_ad_track", z11 ? 1 : 0);
                        }
                    } finally {
                        try {
                        } finally {
                        }
                    }
                }
            });
        }
    }

    public void fm(JSONObject jSONObject) {
        if (jSONObject == null) {
            return;
        }
        try {
            jSONObject.put("gaid", ro());
        } catch (JSONException unused) {
        }
    }

    public void fm(boolean z10, long j10) {
        fm(z10, 0, "", null, j10);
    }

    public void fm(int i10, String str, long j10) {
        fm(false, i10, str, null, j10);
    }

    public void fm(int i10, Throwable th, long j10) {
        fm(false, i10, "", th, j10);
    }

    public void fm(final boolean z10, final int i10, final String str, final Throwable th, final long j10) {
        if ((this.fm == null || this.fm.booleanValue()) && gof.fm() != null) {
            gof.jnr().fm(new ro() { // from class: com.bytedance.sdk.openadsdk.sds.fm.ro.fm.2
                @Override // com.bytedance.sdk.openadsdk.irt.ro
                public com.bytedance.sdk.openadsdk.irt.fm.lb fm() throws Exception {
                    if (!fm.this.lb()) {
                        return null;
                    }
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("session_id", fm.this.wsy);
                    jSONObject.put("is_success", z10);
                    jSONObject.put("error_code", i10);
                    jSONObject.put("error_msg", TextUtils.isEmpty(str) ? fm.ro(th) : str);
                    jSONObject.put("duration", j10);
                    jSONObject.put("has_setting", ku.ro().jm() > 0);
                    return yz.ro().fm("gid_status").ro(jSONObject.toString());
                }
            }, false);
        }
    }
}
