package com.bytedance.liko.leakdetector;

import X.0Gp;
import X.4N2;
import X.4hA;
import X.AnonymousClass7lW;
import X.AnonymousClass9EH;
import X.C149386i;
import X.C152387m;
import X.C152587o;
import X.C152687p;
import X.C152787q;
import X.C152887r;
import X.C152987s;
import X.C153087t;
import X.C18518Kc;
import X.C33198r0;
import X.C33208r1;
import X.C35318uQ;
import X.M12;
import X.SfZ;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.abmock.SettingsManager;
import com.bytedance.liko.memoryexplorer.AnalyzerEngine;
import com.bytedance.liko.memoryexplorer.MemoryConfig;
import java.io.File;

public final class LeakDetectorInstaller {
    public static final LeakDetectorInstaller INSTANCE = new LeakDetectorInstaller();
    public static Handler handler;
    public static boolean hasExcuteBackgroud;
    public static int retryCount;
    public static long trySumTime;

    public final Handler getHandler() {
        return handler;
    }

    public final boolean getHasExcuteBackgroud() {
        return hasExcuteBackgroud;
    }

    public final int getRetryCount() {
        return retryCount;
    }

    public final long getTrySumTime() {
        return trySumTime;
    }

    static {
        Covode.recordClassIndex(35367);
    }

    public final void setHandler(Handler handler2) {
        handler = handler2;
    }

    public final void setHasExcuteBackgroud(boolean z) {
        hasExcuteBackgroud = z;
    }

    public final void setRetryCount(int i) {
        retryCount = i;
    }

    public final void setTrySumTime(long j) {
        trySumTime = j;
    }

    public static Context INVOKEVIRTUAL_com_bytedance_liko_leakdetector_LeakDetectorInstaller_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(Context context) {
        Context applicationContext = context.getApplicationContext();
        if (AnonymousClass7lW.LIZJ && applicationContext == null) {
            return AnonymousClass7lW.LIZ;
        }
        return applicationContext;
    }

    public final void initConfig(Context context) {
        0Gp.LIZ(new C152687p(context));
    }

    public static boolean INVOKEVIRTUAL_com_bytedance_liko_leakdetector_LeakDetectorInstaller_com_ss_android_ugc_aweme_storage_FileLancet_delete(File file) {
        C33208r1 r3;
        MethodCollector.i(15969);
        try {
            if (C18518Kc.LIZIZ()) {
                r3 = (C33208r1) SettingsManager.LIZ().LIZ("storage_intercepter_key", C33208r1.class, 4N2.LIZ);
            } else {
                r3 = 4N2.LIZ;
            }
            if (C33198r0.LIZIZ(file.getAbsolutePath(), r3)) {
                C33198r0.LIZ(file, new RuntimeException(), "exception_delete_log", C33198r0.LIZ(r3));
            }
            if (C33198r0.LIZJ(file.getAbsolutePath(), r3)) {
                C33198r0.LIZ(file, new RuntimeException(), "exception_handle", C33198r0.LIZ(r3));
                MethodCollector.o(15969);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(15969);
        return delete;
    }

    public final synchronized void runAnalysis(String str) {
        MethodCollector.i(15775);
        M12.LIZ(str);
        4hA.LJFF = true;
        M12.LIZ("start Analysis:hprof path:".concat(String.valueOf(str)));
        try {
            new AnalyzerEngine().runAnalysis(new File(str), MemoryConfig.getMemoryConfig());
        } catch (Throwable th) {
            INVOKEVIRTUAL_com_bytedance_liko_leakdetector_LeakDetectorInstaller_com_ss_android_ugc_aweme_storage_FileLancet_delete(new File(str));
            M12.LIZ("runAnalysis fail! delete file " + th.toString());
        }
        4hA.LJFF = false;
        M12.LIZ("end Analysis");
        MethodCollector.o(15775);
    }

    public final void tryInstall(Context context) {
        if ("local_test".equals(AnonymousClass9EH.LJJIFFI.LJIIIZ())) {
            boolean z = true;
            try {
                C152987s.LIZ = true;
                M12.LIZ("Leak detector start install");
                if (context == null) {
                    Boolean.valueOf(true);
                } else {
                    SharedPreferences LIZ = C35318uQ.LIZ(context, "LeakDetectorSp", 0);
                    if (LIZ != null) {
                        z = LIZ.getBoolean("open_leak_detector_on_local_test", true);
                    }
                }
                C152987s.LIZ = z;
                if (!z) {
                    M12.LIZ("Leak detector is disabled");
                    return;
                }
            } catch (Throwable th) {
                M12.LIZ("SPHelper crash!\n".concat(String.valueOf(th)));
                int i = retryCount;
                retryCount = i + 1;
                if (i <= 0) {
                    new C153087t(context, Looper.getMainLooper()).sendEmptyMessageDelayed(0, 10000);
                    M12.LIZ("retry install in 10s");
                    return;
                }
                return;
            }
        }
        C152587o r1 = new C152587o(context, Looper.getMainLooper());
        handler = r1;
        r1.sendEmptyMessageDelayed(0, 10000);
    }

    public final void excuteStrategy(Context context, int i) {
        M12.LIZ(context);
        0Gp.LIZ(new C149386i(i, context));
    }

    public final void runInbackground(Context context, int i) {
        M12.LIZ(context);
        if (AnonymousClass9EH.LJJIFFI.LJI() > 0) {
            SfZ.LJIJ.LJI().LIZLLL(new C152387m(context, i));
        } else {
            excuteStrategy(context, i);
        }
    }

    public final void setLeakCanaryConfig(boolean z, int i) {
        C152787q.LIZIZ.LIZ(C152887r.LIZ(C152787q.LIZ, false, z, i, false, 0, false, false, 121));
    }

    /* JADX WARNING: Removed duplicated region for block: B:28:0x0087 A[Catch:{ all -> 0x015b }] */
    /* JADX WARNING: Removed duplicated region for block: B:44:0x00e8 A[Catch:{ all -> 0x015b }] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final void install(android.content.Context r13, int r14) {
        /*
            r12 = this;
            r2 = 0
            if (r13 == 0) goto L_0x0017
            android.content.Context r1 = INVOKEVIRTUAL_com_bytedance_liko_leakdetector_LeakDetectorInstaller_com_ss_android_ugc_aweme_lancet_launch_ApplicationContextLancet_getApplicationContext(r13)
        L_0x0007:
            boolean r0 = r1 instanceof android.app.Application
            if (r0 != 0) goto L_0x0015
        L_0x000b:
            android.app.Application r2 = (android.app.Application) r2
            if (r2 == 0) goto L_0x0171
            r0 = r14 & 1
            r3 = 0
            if (r0 == 0) goto L_0x0108
            goto L_0x0019
        L_0x0015:
            r2 = r1
            goto L_0x000b
        L_0x0017:
            r1 = r2
            goto L_0x0007
        L_0x0019:
            java.lang.String r0 = "LeakCanaryInstaller install success"
            X.M12.LIZ(r0)     // Catch:{ all -> 0x015b }
            X.87i r0 = X.C151987i.LIZ     // Catch:{ all -> 0x015b }
            r0.LIZ()     // Catch:{ all -> 0x015b }
            X.884 r0 = X.AnonymousClass884.LJFF     // Catch:{ all -> 0x015b }
            boolean r0 = r0.LIZ()     // Catch:{ all -> 0x015b }
            if (r0 == 0) goto L_0x0053
            X.87g r1 = X.AnonymousClass87R.LIZ     // Catch:{ all -> 0x015b }
            if (r1 != 0) goto L_0x004d
        L_0x002f:
            com.bytedance.liko.leakdetector.LeakDetectorInstaller r2 = INSTANCE     // Catch:{ all -> 0x015b }
            boolean r1 = X.C152987s.LJ     // Catch:{ all -> 0x015b }
            int r0 = X.C152987s.LIZIZ     // Catch:{ all -> 0x015b }
            r2.setLeakCanaryConfig(r1, r0)     // Catch:{ all -> 0x015b }
            X.87l r1 = X.C152287l.LIZIZ     // Catch:{ all -> 0x015b }
            X.882 r4 = X.C152287l.LIZ     // Catch:{ all -> 0x015b }
            r5 = 0
            r6 = 0
            r7 = 0
            r8 = 0
            r9 = 0
            r11 = 23
            X.882 r0 = X.AnonymousClass882.LIZ(r4, r5, r6, r7, r8, r9, r11)     // Catch:{ all -> 0x015b }
            r1.LIZ(r0)     // Catch:{ all -> 0x015b }
            goto L_0x0108
        L_0x004d:
            java.lang.String r0 = "LeakCanary has been installed"
            r1.LIZ(r0)     // Catch:{ all -> 0x015b }
            goto L_0x002f
        L_0x0053:
            android.content.Context r6 = r2.getApplicationContext()     // Catch:{ all -> 0x015b }
            boolean r0 = X.AnonymousClass7lW.LIZJ     // Catch:{ all -> 0x015b }
            if (r0 != 0) goto L_0x005c
            goto L_0x0061
        L_0x005c:
            if (r6 == 0) goto L_0x005f
            goto L_0x0063
        L_0x005f:
            android.app.Application r6 = X.AnonymousClass7lW.LIZ     // Catch:{ all -> 0x015b }
        L_0x0061:
            if (r6 == 0) goto L_0x0100
        L_0x0063:
            android.app.Application r6 = (android.app.Application) r6     // Catch:{ all -> 0x015b }
            X.M12.LIZ(r6)     // Catch:{ all -> 0x015b }
            X.2ox r1 = new X.2ox     // Catch:{ all -> 0x015b }
            r1.<init>()     // Catch:{ all -> 0x015b }
            X.AnonymousClass87R.LIZ = r1     // Catch:{ all -> 0x015b }
            java.lang.String r0 = "Installing AppWatcher"
            r1.LIZ(r0)     // Catch:{ all -> 0x015b }
            android.os.Looper r1 = android.os.Looper.getMainLooper()     // Catch:{ all -> 0x015b }
            java.lang.String r0 = ""
            kotlin.h.b.n.LIZ((java.lang.Object) r1, (java.lang.String) r0)     // Catch:{ all -> 0x015b }
            java.lang.Thread r1 = r1.getThread()     // Catch:{ all -> 0x015b }
            java.lang.Thread r0 = java.lang.Thread.currentThread()     // Catch:{ all -> 0x015b }
            if (r1 != r0) goto L_0x00e8
            android.app.Application r0 = X.AnonymousClass884.LIZJ     // Catch:{ all -> 0x015b }
            if (r0 != 0) goto L_0x00db
            X.AnonymousClass884.LIZJ = r6     // Catch:{ all -> 0x015b }
            X.87y r7 = X.C153587y.LIZ     // Catch:{ all -> 0x015b }
            X.88V r1 = X.AnonymousClass884.LJ     // Catch:{ all -> 0x015b }
            X.M12.LIZ(r6, r1, r7)     // Catch:{ all -> 0x015b }
            X.87n r0 = new X.87n     // Catch:{ all -> 0x015b }
            r0.<init>(r1, r7, r3)     // Catch:{ all -> 0x015b }
            X.87j r0 = r0.LIZ     // Catch:{ all -> 0x015b }
            r6.registerActivityLifecycleCallbacks(r0)     // Catch:{ all -> 0x015b }
            X.881 r5 = X.AnonymousClass881.LIZ     // Catch:{ all -> 0x015b }
            X.88V r4 = X.AnonymousClass884.LJ     // Catch:{ all -> 0x015b }
            X.M12.LIZ(r6, r4, r7)     // Catch:{ all -> 0x015b }
            java.util.ArrayList r2 = new java.util.ArrayList     // Catch:{ all -> 0x015b }
            r2.<init>()     // Catch:{ all -> 0x015b }
            int r1 = android.os.Build.VERSION.SDK_INT     // Catch:{ all -> 0x015b }
            r0 = 26
            if (r1 < r0) goto L_0x00b8
            X.88d r0 = new X.88d     // Catch:{ all -> 0x015b }
            r0.<init>(r4, r7)     // Catch:{ all -> 0x015b }
            r2.add(r0)     // Catch:{ all -> 0x015b }
        L_0x00b8:
            java.lang.String r0 = "androidx.fragment.app.Fragment"
            boolean r0 = r5.LIZ(r0)     // Catch:{ all -> 0x015b }
            if (r0 == 0) goto L_0x00c8
            X.88b r0 = new X.88b     // Catch:{ all -> 0x015b }
            r0.<init>(r4, r7)     // Catch:{ all -> 0x015b }
            r2.add(r0)     // Catch:{ all -> 0x015b }
        L_0x00c8:
            int r0 = r2.size()     // Catch:{ all -> 0x015b }
            if (r0 == 0) goto L_0x00d6
            X.87U r0 = new X.87U     // Catch:{ all -> 0x015b }
            r0.<init>(r2)     // Catch:{ all -> 0x015b }
            r6.registerActivityLifecycleCallbacks(r0)     // Catch:{ all -> 0x015b }
        L_0x00d6:
            X.LsC<android.app.Application, X.2Pr> r0 = X.AnonymousClass884.LIZIZ     // Catch:{ all -> 0x015b }
            r0.invoke(r6)     // Catch:{ all -> 0x015b }
        L_0x00db:
            X.87g r1 = X.AnonymousClass87R.LIZ     // Catch:{ all -> 0x015b }
            if (r1 != 0) goto L_0x00e1
            goto L_0x002f
        L_0x00e1:
            java.lang.String r0 = "LeakCanary install success"
            r1.LIZ(r0)     // Catch:{ all -> 0x015b }
            goto L_0x002f
        L_0x00e8:
            java.lang.UnsupportedOperationException r2 = new java.lang.UnsupportedOperationException     // Catch:{ all -> 0x015b }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x015b }
            java.lang.String r0 = "Should be called from the main thread, not "
            r1.<init>(r0)     // Catch:{ all -> 0x015b }
            java.lang.Thread r0 = java.lang.Thread.currentThread()     // Catch:{ all -> 0x015b }
            r1.append(r0)     // Catch:{ all -> 0x015b }
            java.lang.String r0 = r1.toString()     // Catch:{ all -> 0x015b }
            r2.<init>(r0)     // Catch:{ all -> 0x015b }
            throw r2     // Catch:{ all -> 0x015b }
        L_0x0100:
            X.7xU r1 = new X.7xU     // Catch:{ all -> 0x015b }
            java.lang.String r0 = "null cannot be cast to non-null type android.app.Application"
            r1.<init>(r0)     // Catch:{ all -> 0x015b }
            throw r1     // Catch:{ all -> 0x015b }
        L_0x0108:
            r0 = r14 & 2
            if (r0 == 0) goto L_0x0114
            java.lang.String r0 = "OOM catch install success"
            X.M12.LIZ(r0)     // Catch:{ all -> 0x015b }
            X.C149186g.LIZ(r13)     // Catch:{ all -> 0x015b }
        L_0x0114:
            r0 = r14 & 8
            if (r0 == 0) goto L_0x0142
            java.lang.String r0 = "MemoryPeak install success"
            X.M12.LIZ(r0)     // Catch:{ all -> 0x015b }
            X.4hA r1 = X.4hA.LJII     // Catch:{ all -> 0x015b }
            X.M12.LIZ(r13)     // Catch:{ all -> 0x015b }
            X.4hA.LIZLLL = r13     // Catch:{ all -> 0x015b }
            boolean r0 = X.4hA.LJ     // Catch:{ all -> 0x015b }
            if (r0 != 0) goto L_0x0142
            android.os.HandlerThread r0 = X.4hA.LIZIZ     // Catch:{ all -> 0x015b }
            r0.start()     // Catch:{ all -> 0x015b }
            X.86n r2 = new X.86n     // Catch:{ all -> 0x015b }
            android.os.HandlerThread r0 = X.4hA.LIZIZ     // Catch:{ all -> 0x015b }
            android.os.Looper r0 = r0.getLooper()     // Catch:{ all -> 0x015b }
            r2.<init>(r1, r0)     // Catch:{ all -> 0x015b }
            X.4hA.LIZJ = r2     // Catch:{ all -> 0x015b }
            long r0 = X.C152987s.LIZJ     // Catch:{ all -> 0x015b }
            r2.sendEmptyMessageDelayed(r3, r0)     // Catch:{ all -> 0x015b }
            r0 = 1
            X.4hA.LJ = r0     // Catch:{ all -> 0x015b }
        L_0x0142:
            com.bytedance.liko.leakdetector.LeakDetectorInstaller r0 = INSTANCE     // Catch:{ all -> 0x015b }
            r0.runInbackground(r13, r14)     // Catch:{ all -> 0x015b }
            java.lang.String r0 = "Leak detector install success"
            X.M12.LIZ(r0)     // Catch:{ all -> 0x015b }
            boolean r0 = X.2l2.LIZ     // Catch:{ all -> 0x015b }
            if (r0 == 0) goto L_0x0171
            java.lang.String r0 = "fdleak install success"
            X.M12.LIZ(r0)     // Catch:{ all -> 0x015b }
            com.bytedance.liko.leakdetector.strategy.fd.FdLeakDetector r0 = com.bytedance.liko.leakdetector.strategy.fd.FdLeakDetector.INSTANCE     // Catch:{ all -> 0x015b }
            r0.install()     // Catch:{ all -> 0x015b }
            return
        L_0x015b:
            r2 = move-exception
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "Leak detector install failed\n"
            r1.<init>(r0)
            java.lang.String r0 = android.util.Log.getStackTraceString(r2)
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            X.M12.LIZ(r0)
        L_0x0171:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.liko.leakdetector.LeakDetectorInstaller.install(android.content.Context, int):void");
    }
}
