package com.google.android.gms.internal.ads;

import android.app.Activity;
import android.content.Context;
import android.os.Looper;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.analytics.AnalyticsListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* JADX INFO: loaded from: classes2.dex */
public abstract class rg implements qg {
    public static volatile mh W;
    public float Q;
    public float R;
    public float S;
    public final DisplayMetrics V;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public MotionEvent f16118a;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public double f16127j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public double f16128k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public double f16129l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public float f16130m;

    @Nullable
    protected gh zzr;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final LinkedList f16119b = new LinkedList();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public long f16120c = 0;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public long f16121d = 0;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long f16122e = 0;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f16123f = 0;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public long f16124g = 0;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public long f16125h = 0;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public long f16126i = 0;
    public boolean T = false;
    public boolean U = false;

    public rg(Context context) {
        try {
            jg.a();
            this.V = context.getResources().getDisplayMetrics();
            if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9064s3)).booleanValue()) {
                this.zzr = new gh();
            }
        } catch (Throwable unused) {
        }
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final synchronized void a(int i10, int i11, int i12) {
        try {
            if (this.f16118a != null) {
                if (((Boolean) n7.y.f31093e.f31096c.b(ao.f8782b3)).booleanValue()) {
                    j();
                } else {
                    this.f16118a.recycle();
                }
            }
            DisplayMetrics displayMetrics = this.V;
            if (displayMetrics != null) {
                float f10 = displayMetrics.density;
                this.f16118a = MotionEvent.obtain(0L, i12, 1, i10 * f10, i11 * f10, 0.0f, 0.0f, 0, 0.0f, 0.0f, 0, 0);
            } else {
                this.f16118a = null;
            }
            this.U = false;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String b(Context context) {
        return "19";
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final void c(StackTraceElement[] stackTraceElementArr) {
        gh ghVar;
        if (!((Boolean) n7.y.f31093e.f31096c.b(ao.f9064s3)).booleanValue() || (ghVar = this.zzr) == null) {
            return;
        }
        ghVar.f11743a = new ArrayList(Arrays.asList(stackTraceElementArr));
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String d(Context context) {
        char[] cArr = ph.f15417a;
        if (Looper.myLooper() != Looper.getMainLooper()) {
            return k(context, null, 1, null, null);
        }
        throw new IllegalStateException("The caller must not be called from the UI thread.");
    }

    public abstract we e(Context context);

    public abstract we f(Context context, View view, Activity activity);

    public abstract we g(Context context, View view, Activity activity);

    public abstract oh h(MotionEvent motionEvent);

    public abstract long i(StackTraceElement[] stackTraceElementArr);

    public final void j() {
        this.f16124g = 0L;
        this.f16120c = 0L;
        this.f16121d = 0L;
        this.f16122e = 0L;
        this.f16123f = 0L;
        this.f16125h = 0L;
        this.f16126i = 0L;
        LinkedList linkedList = this.f16119b;
        if (linkedList.isEmpty()) {
            MotionEvent motionEvent = this.f16118a;
            if (motionEvent != null) {
                motionEvent.recycle();
            }
        } else {
            Iterator it = linkedList.iterator();
            while (it.hasNext()) {
                ((MotionEvent) it.next()).recycle();
            }
            linkedList.clear();
        }
        this.f16118a = null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:39:0x0086  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x008b  */
    /* JADX WARN: Removed duplicated region for block: B:49:0x00b7  */
    /* JADX WARN: Removed duplicated region for block: B:85:0x00aa A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r2v0, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r2v1 */
    /* JADX WARN: Type inference failed for: r2v14 */
    /* JADX WARN: Type inference failed for: r2v15 */
    /* JADX WARN: Type inference failed for: r2v16 */
    /* JADX WARN: Type inference failed for: r2v2 */
    /* JADX WARN: Type inference failed for: r2v8 */
    /* JADX WARN: Type inference failed for: r3v0 */
    /* JADX WARN: Type inference failed for: r3v1 */
    /* JADX WARN: Type inference failed for: r3v2 */
    /* JADX WARN: Type inference failed for: r3v3 */
    /* JADX WARN: Type inference failed for: r3v4 */
    /* JADX WARN: Type inference failed for: r3v5 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String k(Context context, String str, int i10, View view, Activity activity) {
        pg pgVar;
        String str2;
        char c6;
        boolean z10;
        Exception exc;
        we weVarE;
        int i11;
        String string;
        byte[] bArrB;
        char c10;
        int i12;
        int i13;
        ?? r22 = str;
        ?? r32 = i10;
        long jCurrentTimeMillis = System.currentTimeMillis();
        boolean zBooleanValue = ((Boolean) n7.y.f31093e.f31096c.b(ao.f8815d3)).booleanValue();
        if (zBooleanValue) {
            pgVar = W != null ? W.f14229k : null;
            str2 = "be";
        } else {
            pgVar = null;
            str2 = null;
        }
        try {
            if (r32 == 3) {
                weVarE = f(context, view, activity);
                try {
                    this.T = true;
                    i13 = 1002;
                } catch (Exception e10) {
                    exc = e10;
                    c6 = 3;
                    z10 = true;
                    if (zBooleanValue && pgVar != null) {
                        if (r32 != c6) {
                            i11 = 1003;
                            r32 = r32;
                        } else if (r32 == 2) {
                            i11 = 1009;
                            r32 = r32;
                        } else {
                            i11 = 1001;
                            r32 = z10;
                        }
                        pgVar.a(i11, -1, System.currentTimeMillis() - jCurrentTimeMillis, str2, exc);
                    }
                    long jCurrentTimeMillis2 = System.currentTimeMillis();
                    if (weVarE == null) {
                    }
                    return string;
                }
            } else if (r32 == 2) {
                weVarE = g(context, view, activity);
                i13 = AnalyticsListener.EVENT_AUDIO_DECODER_INITIALIZED;
            } else {
                weVarE = e(context);
                i13 = 1000;
            }
            if (!zBooleanValue || pgVar == null) {
                c6 = 3;
                z10 = true;
            } else {
                try {
                    c6 = 3;
                    z10 = true;
                } catch (Exception e11) {
                    e = e11;
                    c6 = 3;
                    z10 = true;
                }
                try {
                    pgVar.a(i13, -1, System.currentTimeMillis() - jCurrentTimeMillis, str2, null);
                } catch (Exception e12) {
                    e = e12;
                    exc = e;
                    if (zBooleanValue) {
                        if (r32 != c6) {
                        }
                        pgVar.a(i11, -1, System.currentTimeMillis() - jCurrentTimeMillis, str2, exc);
                    }
                }
            }
        } catch (Exception e13) {
            c6 = 3;
            z10 = true;
            exc = e13;
            weVarE = null;
        }
        long jCurrentTimeMillis22 = System.currentTimeMillis();
        try {
            if (weVarE == null) {
                try {
                    if (((pf) weVarE.e()).d(null) == 0) {
                        r22 = 2;
                        string = Integer.toString(5);
                    } else {
                        pf pfVar = (pf) weVarE.e();
                        boolean z11 = jg.f12841a;
                        try {
                            bg bgVarB = jg.b(r22, pfVar.b());
                            if (bgVarB == null) {
                                we weVarD0 = pf.D0();
                                weVarD0.h(IjkMediaMeta.AV_CH_TOP_FRONT_LEFT);
                                bArrB = jg.d(((pf) weVarD0.e()).b(), r22, z10);
                            } else {
                                bArrB = ((cg) bgVarB.e()).b();
                            }
                            string = Base64.encodeToString(bArrB, 11);
                            if (zBooleanValue && pgVar != null) {
                                if (r32 == c6) {
                                    i12 = 1006;
                                    c10 = 2;
                                } else {
                                    c10 = 2;
                                    i12 = r32 == 2 ? 1010 : 1004;
                                }
                                pgVar.a(i12, -1, System.currentTimeMillis() - jCurrentTimeMillis22, str2, null);
                                r22 = c10;
                            }
                        } catch (Exception e14) {
                            e = e14;
                            r22 = 2;
                            Exception exc2 = e;
                            string = Integer.toString(7);
                            if (zBooleanValue && pgVar != null) {
                                pgVar.a(r32 == c6 ? AnalyticsListener.EVENT_AUDIO_ENABLED : r32 == r22 ? 1011 : 1005, -1, System.currentTimeMillis() - jCurrentTimeMillis22, str2, exc2);
                            }
                        }
                    }
                } catch (Exception e15) {
                    e = e15;
                }
            }
        } catch (Exception e16) {
            e = e16;
        }
        return string;
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final synchronized void zzd(@Nullable MotionEvent motionEvent) {
        Long l10;
        try {
            if (this.T) {
                j();
                this.T = false;
            }
            int action = motionEvent.getAction();
            if (action == 0) {
                this.f16127j = 0.0d;
                this.f16128k = motionEvent.getRawX();
                this.f16129l = motionEvent.getRawY();
            } else if (action == 1 || action == 2) {
                double rawX = motionEvent.getRawX();
                double rawY = motionEvent.getRawY();
                double d10 = rawX - this.f16128k;
                double d11 = rawY - this.f16129l;
                this.f16127j += Math.sqrt((d11 * d11) + (d10 * d10));
                this.f16128k = rawX;
                this.f16129l = rawY;
            }
            int action2 = motionEvent.getAction();
            if (action2 != 0) {
                try {
                    if (action2 == 1) {
                        MotionEvent motionEventObtain = MotionEvent.obtain(motionEvent);
                        this.f16118a = motionEventObtain;
                        LinkedList linkedList = this.f16119b;
                        linkedList.add(motionEventObtain);
                        if (linkedList.size() > 6) {
                            ((MotionEvent) linkedList.remove()).recycle();
                        }
                        this.f16122e++;
                        this.f16124g = i(new Throwable().getStackTrace());
                    } else if (action2 == 2) {
                        this.f16121d += (long) (motionEvent.getHistorySize() + 1);
                        oh ohVarH = h(motionEvent);
                        Long l11 = ohVarH.f15066d;
                        if (l11 != null && ohVarH.f15069g != null) {
                            this.f16125h = l11.longValue() + ohVarH.f15069g.longValue() + this.f16125h;
                        }
                        if (this.V != null && (l10 = ohVarH.f15067e) != null && ohVarH.f15070h != null) {
                            this.f16126i = l10.longValue() + ohVarH.f15070h.longValue() + this.f16126i;
                        }
                    } else if (action2 == 3) {
                        this.f16123f++;
                    }
                } catch (zzbaa unused) {
                }
            } else {
                this.f16130m = motionEvent.getX();
                this.Q = motionEvent.getY();
                this.R = motionEvent.getRawX();
                this.S = motionEvent.getRawY();
                this.f16120c++;
            }
            this.U = true;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String zzf(Context context, @Nullable String str, @Nullable View view, @Nullable Activity activity) {
        return k(context, str, 3, view, activity);
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String zzg(Context context, @Nullable String str, @Nullable View view) {
        return k(context, str, 3, view, null);
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String zzj(Context context, @Nullable View view, @Nullable Activity activity) {
        return k(context, null, 2, view, activity);
    }
}
