package d.f.a.r2.i3;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.lang.reflect.Field;
import java.util.Objects;

/* JADX INFO: compiled from: Cockroach.java */
/* JADX INFO: loaded from: classes.dex */
public final class c {
    public static d.f.a.r2.i3.e.c a = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static d f3228b = null;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static boolean f3229c = false;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static boolean f3230d;

    /* JADX INFO: compiled from: Cockroach.java */
    public class a implements Handler.Callback {
        public final /* synthetic */ Handler a;

        public a(Handler handler) {
            this.a = handler;
        }

        @Override // android.os.Handler.Callback
        public boolean handleMessage(Message message) {
            if (Build.VERSION.SDK_INT >= 28) {
                if (message.what != 159) {
                    return false;
                }
                try {
                    this.a.handleMessage(message);
                } catch (Throwable th) {
                    c.a.c(message);
                    c.a(th);
                }
                return true;
            }
            int i2 = message.what;
            if (i2 == 104) {
                try {
                    this.a.handleMessage(message);
                } catch (Throwable th2) {
                    c.a.b(message);
                    c.a(th2);
                }
                return true;
            }
            if (i2 == 107) {
                try {
                    this.a.handleMessage(message);
                } catch (Throwable th3) {
                    c.a.d(message);
                    c.a(th3);
                }
                return true;
            }
            if (i2 == 109) {
                try {
                    this.a.handleMessage(message);
                } catch (Throwable th4) {
                    c.a(th4);
                }
                return true;
            }
            switch (i2) {
                case 100:
                    try {
                        this.a.handleMessage(message);
                        break;
                    } catch (Throwable th5) {
                        c.a.c(message);
                        c.a(th5);
                    }
                    return true;
                case 101:
                    try {
                        this.a.handleMessage(message);
                        break;
                    } catch (Throwable th6) {
                        c.a.a(message);
                        c.a(th6);
                    }
                    return true;
                case 102:
                    try {
                        this.a.handleMessage(message);
                        break;
                    } catch (Throwable th7) {
                        c.a.a(message);
                        c.a(th7);
                    }
                    return true;
                default:
                    return false;
            }
        }
    }

    public static void a(Throwable th) {
        d dVar = f3228b;
        if (dVar == null) {
            return;
        }
        if (f3230d) {
            try {
                dVar.a(th);
                return;
            } catch (Throwable th2) {
                th2.printStackTrace();
                return;
            }
        }
        try {
            dVar.c(Looper.getMainLooper().getThread(), th);
        } catch (Throwable th3) {
            th3.printStackTrace();
        }
        d();
        throw null;
    }

    public static void b() throws Exception {
        Class<?> cls = Class.forName("android.app.ActivityThread");
        Object objInvoke = cls.getDeclaredMethod("currentActivityThread", new Class[0]).invoke(null, new Object[0]);
        Field declaredField = cls.getDeclaredField("mH");
        declaredField.setAccessible(true);
        Handler handler = (Handler) declaredField.get(objInvoke);
        Field declaredField2 = Handler.class.getDeclaredField("mCallback");
        declaredField2.setAccessible(true);
        declaredField2.set(handler, new a(handler));
    }

    public static void c(Throwable th) {
        StackTraceElement[] stackTrace;
        if (th == null || f3228b == null || (stackTrace = th.getStackTrace()) == null) {
            return;
        }
        int length = stackTrace.length;
        while (true) {
            length--;
            if (length <= -1 || stackTrace.length - length > 20) {
                return;
            }
            StackTraceElement stackTraceElement = stackTrace[length];
            if ("android.view.Choreographer".equals(stackTraceElement.getClassName()) && "Choreographer.java".equals(stackTraceElement.getFileName()) && "doFrame".equals(stackTraceElement.getMethodName())) {
                d dVar = f3228b;
                Objects.requireNonNull(dVar);
                try {
                    dVar.b(th);
                    return;
                } catch (Throwable th2) {
                    th2.printStackTrace();
                    return;
                }
            }
        }
    }

    public static void d() {
        f3230d = true;
        d dVar = f3228b;
        while (true) {
            try {
                Looper.loop();
            } catch (Throwable th) {
                c(th);
                if (f3228b != null) {
                    d dVar2 = f3228b;
                    Objects.requireNonNull(dVar2);
                    try {
                        dVar2.a(th);
                    } catch (Throwable th2) {
                        th2.printStackTrace();
                    }
                }
            }
        }
    }
}
