package com.bytedance.sysoptimizer;

import X.3GY;
import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.DeadSystemException;
import android.text.TextUtils;
import android.util.Pair;
import com.bytedance.covode.number.Covode;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public class LaunchActivityOptimizer {
    public static boolean initialized;
    public static Activity mActivity;
    public static CopyOnWriteArrayList<ExceptionCatcher> sExceptionCatchers = new CopyOnWriteArrayList<>();

    static {
        Covode.recordClassIndex(38406);
    }

    public static class LaunchActivityLifecycleCallback implements Application.ActivityLifecycleCallbacks {
        static {
            Covode.recordClassIndex(38409);
        }

        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        }

        public void onActivityStarted(Activity activity) {
        }

        public void onActivityStopped(Activity activity) {
        }

        public void onActivityDestroyed(Activity activity) {
            if (Build.VERSION.SDK_INT <= 28) {
                LaunchActivityOptimizer.mActivity = null;
            }
        }

        public void onActivityPaused(Activity activity) {
            if (Build.VERSION.SDK_INT <= 28) {
                LaunchActivityOptimizer.mActivity = null;
            }
        }

        public void onActivityResumed(Activity activity) {
            if (Build.VERSION.SDK_INT <= 28) {
                LaunchActivityOptimizer.mActivity = activity;
                LaunchActivityOptimizer.setStartedActivity(activity);
            }
        }

        public void onActivityCreated(Activity activity, Bundle bundle) {
            if (Build.VERSION.SDK_INT <= 28) {
                LaunchActivityOptimizer.mActivity = activity;
                LaunchActivityOptimizer.hookAMS(true);
            }
        }
    }

    public static void unRegisterExceptionCatcher(ExceptionCatcher exceptionCatcher) {
        if (exceptionCatcher != null) {
            sExceptionCatchers.remove(exceptionCatcher);
        }
    }

    public static class DeadException implements ExceptionCatcher {
        static {
            Covode.recordClassIndex(38408);
        }

        public boolean registerCondition() {
            return true;
        }

        public boolean analysis(Throwable th) {
            if (!(th instanceof InvocationTargetException)) {
                return false;
            }
            InvocationTargetException invocationTargetException = (InvocationTargetException) th;
            if ((invocationTargetException.getTargetException() instanceof DeadObjectException) || (invocationTargetException.getTargetException() instanceof DeadSystemException)) {
                return true;
            }
            return false;
        }
    }

    public static void registerExceptionCatcher(ExceptionCatcher exceptionCatcher) {
        if (exceptionCatcher != null && exceptionCatcher.registerCondition() && !sExceptionCatchers.contains(exceptionCatcher)) {
            sExceptionCatchers.add(exceptionCatcher);
        }
    }

    public static void setStartedActivity(Activity activity) {
        Field declaredField = getDeclaredField(activity, "mStartedActivity");
        if (declaredField != null) {
            try {
                declaredField.setAccessible(true);
                declaredField.set(activity, true);
            } catch (Exception unused) {
            }
        }
    }

    public static void hookAMS(boolean z) {
        Field declaredField;
        if (!initialized) {
            initialized = true;
            HackHelper.init();
            if (Build.VERSION.SDK_INT <= 28) {
                try {
                    if (Build.VERSION.SDK_INT < 26) {
                        declaredField = Class.forName("android.app.ActivityManagerNative").getDeclaredField("gDefault");
                    } else {
                        declaredField = Class.forName("android.app.ActivityManager").getDeclaredField("IActivityManagerSingleton");
                    }
                    declaredField.setAccessible(true);
                    Object obj = declaredField.get((Object) null);
                    Field declaredField2 = Class.forName("android.util.Singleton").getDeclaredField("mInstance");
                    declaredField2.setAccessible(true);
                    final Object obj2 = declaredField2.get(obj);
                    if (obj2 != null) {
                        if (z) {
                            Class<?> cls = Class.forName("android.app.IActivityManager");
                            if (cls != null) {
                                declaredField2.set(obj, Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{cls}, new InvocationHandler() {
                                    static {
                                        Covode.recordClassIndex(38407);
                                    }

                                    public static Object com_bytedance_sysoptimizer_LaunchActivityOptimizer$1_java_lang_reflect_Method_invoke(Method method, Object obj, Object[] objArr) {
                                        3GY.LIZ(110000);
                                        Method method2 = method;
                                        Pair LIZ = 3GY.LIZ((Object) null, method2, new Object[]{obj, objArr}, 110000, "java.lang.Object", true, 563806846, 2);
                                        return ((Boolean) LIZ.first).booleanValue() ? LIZ.second : method2.invoke(obj, objArr);
                                    }

                                    /* JADX WARNING: Missing exception handler attribute for start block: B:31:0x0065 */
                                    /* Code decompiled incorrectly, please refer to instructions dump. */
                                    public final java.lang.Object invoke(java.lang.Object r5, java.lang.reflect.Method r6, java.lang.Object[] r7) {
                                        /*
                                            r4 = this;
                                            java.lang.String r1 = r6.getName()
                                            java.lang.String r0 = "reportSizeConfigurations"
                                            boolean r0 = r1.contains(r0)
                                            r3 = 0
                                            r2 = 24
                                            if (r0 == 0) goto L_0x0024
                                            int r0 = android.os.Build.VERSION.SDK_INT
                                            if (r0 < r2) goto L_0x0024
                                            java.lang.Object r0 = r4     // Catch:{ InvocationTargetException -> 0x001a }
                                            java.lang.Object r0 = com_bytedance_sysoptimizer_LaunchActivityOptimizer$1_java_lang_reflect_Method_invoke(r6, r0, r7)     // Catch:{ InvocationTargetException -> 0x001a }
                                            return r0
                                        L_0x001a:
                                            r0 = move-exception
                                            java.lang.Throwable r0 = r0.getTargetException()
                                            boolean r0 = r0 instanceof java.lang.IllegalArgumentException
                                            if (r0 == 0) goto L_0x0024
                                            return r3
                                        L_0x0024:
                                            java.lang.String r1 = r6.getName()
                                            java.lang.String r0 = "isTopOfTask"
                                            boolean r0 = r1.contains(r0)
                                            if (r0 == 0) goto L_0x004a
                                            int r0 = android.os.Build.VERSION.SDK_INT
                                            if (r0 < r2) goto L_0x004a
                                            java.lang.Object r0 = r4     // Catch:{ InvocationTargetException -> 0x003b }
                                            java.lang.Object r0 = com_bytedance_sysoptimizer_LaunchActivityOptimizer$1_java_lang_reflect_Method_invoke(r6, r0, r7)     // Catch:{ InvocationTargetException -> 0x003b }
                                            return r0
                                        L_0x003b:
                                            r0 = move-exception
                                            java.lang.Throwable r0 = r0.getTargetException()
                                            boolean r0 = r0 instanceof java.lang.IllegalArgumentException
                                            if (r0 == 0) goto L_0x004a
                                            r0 = 0
                                            java.lang.Boolean r0 = java.lang.Boolean.valueOf(r0)
                                            return r0
                                        L_0x004a:
                                            java.lang.String r1 = r6.getName()
                                            java.lang.String r0 = "willActivityBeVisible"
                                            boolean r0 = r1.contains(r0)
                                            if (r0 == 0) goto L_0x0065
                                            android.app.Activity r0 = com.bytedance.sysoptimizer.LaunchActivityOptimizer.mActivity     // Catch:{ IllegalArgumentException -> 0x005c, all -> 0x0065 }
                                            r0.isImmersive()     // Catch:{ IllegalArgumentException -> 0x005c, all -> 0x0065 }
                                            goto L_0x0065
                                        L_0x005c:
                                            android.app.Activity r0 = com.bytedance.sysoptimizer.LaunchActivityOptimizer.mActivity
                                            if (r0 == 0) goto L_0x0065
                                            android.app.Activity r0 = com.bytedance.sysoptimizer.LaunchActivityOptimizer.mActivity
                                            r0.finish()
                                        L_0x0065:
                                            java.lang.Object r0 = r4     // Catch:{ all -> 0x006c }
                                            java.lang.Object r0 = com_bytedance_sysoptimizer_LaunchActivityOptimizer$1_java_lang_reflect_Method_invoke(r6, r0, r7)     // Catch:{ all -> 0x006c }
                                            return r0
                                        L_0x006c:
                                            r2 = move-exception
                                            java.util.concurrent.CopyOnWriteArrayList<com.bytedance.sysoptimizer.ExceptionCatcher> r0 = com.bytedance.sysoptimizer.LaunchActivityOptimizer.sExceptionCatchers
                                            java.util.Iterator r1 = r0.iterator()
                                        L_0x0073:
                                            boolean r0 = r1.hasNext()
                                            if (r0 == 0) goto L_0x0086
                                            java.lang.Object r0 = r1.next()
                                            com.bytedance.sysoptimizer.ExceptionCatcher r0 = (com.bytedance.sysoptimizer.ExceptionCatcher) r0
                                            boolean r0 = r0.analysis(r2)
                                            if (r0 == 0) goto L_0x0073
                                            return r3
                                        L_0x0086:
                                            java.lang.Throwable r0 = com.bytedance.sysoptimizer.LaunchActivityOptimizer.getSafeThrowable(r2)
                                            throw r0
                                        */
                                        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sysoptimizer.LaunchActivityOptimizer.AnonymousClass1.invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]):java.lang.Object");
                                    }
                                }));
                                return;
                            }
                            return;
                        }
                        declaredField2.set(obj, obj);
                    }
                } catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException unused) {
                }
            }
        }
    }

    public static Throwable getSafeThrowable(Throwable th) {
        RuntimeException runtimeException;
        try {
            StackTraceElement[] stackTrace = th.getStackTrace();
            if (th instanceof InvocationTargetException) {
                InvocationTargetException invocationTargetException = (InvocationTargetException) th;
                if (invocationTargetException.getTargetException() != null) {
                    th = invocationTargetException.getTargetException();
                    stackTrace = th.getStackTrace();
                } else {
                    runtimeException = new RuntimeException();
                    th = runtimeException;
                }
            } else if (th instanceof UndeclaredThrowableException) {
                UndeclaredThrowableException undeclaredThrowableException = (UndeclaredThrowableException) th;
                if (undeclaredThrowableException.getUndeclaredThrowable() != null) {
                    th = undeclaredThrowableException.getUndeclaredThrowable();
                    stackTrace = th.getStackTrace();
                } else {
                    runtimeException = new RuntimeException();
                    th = runtimeException;
                }
            }
            ArrayList arrayList = new ArrayList();
            for (StackTraceElement stackTraceElement : stackTrace) {
                if (stackTraceElement != null) {
                    String className = stackTraceElement.getClassName();
                    if (TextUtils.isEmpty(className) || (!className.contains("com.ss.android.ugc.aweme.utils.PreventServerSideCrashes") && !className.equals(Proxy.class.getName()) && !className.equals(Method.class.getName()))) {
                        arrayList.add(stackTraceElement);
                    }
                }
            }
            if (arrayList.isEmpty()) {
                return th;
            }
            int size = arrayList.size();
            StackTraceElement[] stackTraceElementArr = new StackTraceElement[size];
            for (int i = 0; i < size; i++) {
                stackTraceElementArr[i] = (StackTraceElement) arrayList.get(i);
            }
            th.setStackTrace(stackTraceElementArr);
            return th;
        } catch (Throwable unused) {
        }
    }

    public static Field getDeclaredField(Object obj, String str) {
        if (obj != null && !TextUtils.isEmpty(str)) {
            for (Class cls = obj.getClass(); cls != Object.class; cls = cls.getSuperclass()) {
                Field field = HackHelper.getField(cls, str);
                if (field != null) {
                    return field;
                }
            }
        }
        return null;
    }
}
