package com.google.android.gms.common.util;

import android.content.Context;
import android.os.DropBoxManager;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.common.internal.Preconditions;
import com.tencent.matrix.trace.core.AppMethodBeat;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.annotation.concurrent.GuardedBy;

public final class CrashUtils {
    private static final String[] zzzc = new String[]{"android.", "com.android.", "dalvik.", "java.", "javax."};
    private static DropBoxManager zzzd = null;
    private static boolean zzze = false;
    private static boolean zzzf;
    private static boolean zzzg;
    private static int zzzh = -1;
    @GuardedBy("CrashUtils.class")
    private static int zzzi = 0;
    @GuardedBy("CrashUtils.class")
    private static int zzzj = 0;

    @Retention(RetentionPolicy.SOURCE)
    public @interface ErrorDialogData {
        public static final int AVG_CRASH_FREQ = 2;
        public static final int BINDER_CRASH = 268435456;
        public static final int DYNAMITE_CRASH = 536870912;
        public static final int FORCED_SHUSHED_BY_WRAPPER = 4;
        public static final int NONE = 0;
        public static final int POPUP_FREQ = 1;
        public static final int SUPPRESSED = 1073741824;
    }

    public static boolean addDynamiteErrorToDropBox(Context context, Throwable th) {
        AppMethodBeat.i(90135);
        boolean addErrorToDropBoxInternal = addErrorToDropBoxInternal(context, th, ErrorDialogData.DYNAMITE_CRASH);
        AppMethodBeat.o(90135);
        return addErrorToDropBoxInternal;
    }

    @Deprecated
    public static boolean addErrorToDropBox(Context context, Throwable th) {
        AppMethodBeat.i(90136);
        boolean addDynamiteErrorToDropBox = addDynamiteErrorToDropBox(context, th);
        AppMethodBeat.o(90136);
        return addDynamiteErrorToDropBox;
    }

    public static boolean addErrorToDropBoxInternal(Context context, String str, String str2, int i) {
        AppMethodBeat.i(90138);
        boolean zza = zza(context, str, str2, i, null);
        AppMethodBeat.o(90138);
        return zza;
    }

    public static boolean addErrorToDropBoxInternal(Context context, Throwable th, int i) {
        boolean zzdb;
        boolean z = false;
        AppMethodBeat.i(90137);
        try {
            Preconditions.checkNotNull(context);
            Preconditions.checkNotNull(th);
            if (isPackageSide()) {
                if (!zzdb()) {
                    th = zza(th);
                    if (th == null) {
                        AppMethodBeat.o(90137);
                    }
                }
                z = zza(context, Log.getStackTraceString(th), ProcessUtils.getMyProcessName(), i, th);
                AppMethodBeat.o(90137);
            } else {
                AppMethodBeat.o(90137);
            }
        } catch (Exception e) {
            try {
                zzdb = zzdb();
            } catch (Exception e2) {
                zzdb = z;
            }
            if (zzdb) {
                AppMethodBeat.o(90137);
                throw e;
            }
            AppMethodBeat.o(90137);
        }
        return z;
    }

    private static boolean isPackageSide() {
        return zzze ? zzzf : false;
    }

    public static boolean isSystemClassPrefixInternal(String str) {
        AppMethodBeat.i(90140);
        if (TextUtils.isEmpty(str)) {
            AppMethodBeat.o(90140);
            return false;
        }
        for (String startsWith : zzzc) {
            if (str.startsWith(startsWith)) {
                AppMethodBeat.o(90140);
                return true;
            }
        }
        AppMethodBeat.o(90140);
        return false;
    }

    @VisibleForTesting
    public static synchronized void setTestVariables(DropBoxManager dropBoxManager, boolean z, boolean z2, int i) {
        synchronized (CrashUtils.class) {
            zzze = true;
            zzzd = dropBoxManager;
            zzzg = z;
            zzzf = z2;
            zzzh = i;
            zzzi = 0;
            zzzj = 0;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:69:0x01b2 A:{SYNTHETIC, Splitter:B:69:0x01b2} */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x01ad A:{Splitter:B:42:0x016b, ExcHandler: all (r0_43 'th' java.lang.Throwable)} */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x01ad A:{Splitter:B:42:0x016b, ExcHandler: all (r0_43 'th' java.lang.Throwable)} */
    /* JADX WARNING: Missing block: B:66:0x01ad, code skipped:
            r0 = move-exception;
     */
    /* JADX WARNING: Missing block: B:67:0x01ae, code skipped:
            r2 = r0;
            r3 = null;
     */
    /* JADX WARNING: Missing block: B:70:?, code skipped:
            r3.close();
     */
    @com.google.android.gms.common.util.VisibleForTesting
    private static synchronized java.lang.String zza(android.content.Context r8, java.lang.String r9, java.lang.String r10, int r11) {
        /*
        r2 = 0;
        r4 = com.google.android.gms.common.util.CrashUtils.class;
        monitor-enter(r4);
        r0 = 90142; // 0x1601e float:1.26316E-40 double:4.4536E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r0);	 Catch:{ all -> 0x01bc }
        r3 = new java.lang.StringBuilder;	 Catch:{ all -> 0x01bc }
        r0 = 1024; // 0x400 float:1.435E-42 double:5.06E-321;
        r3.<init>(r0);	 Catch:{ all -> 0x01bc }
        r0 = "Process: ";
        r0 = r3.append(r0);	 Catch:{ all -> 0x01bc }
        r1 = com.google.android.gms.common.util.Strings.nullToEmpty(r10);	 Catch:{ all -> 0x01bc }
        r0 = r0.append(r1);	 Catch:{ all -> 0x01bc }
        r1 = "\n";
        r0.append(r1);	 Catch:{ all -> 0x01bc }
        r0 = "Package: com.google.android.gms";
        r3.append(r0);	 Catch:{ all -> 0x01bc }
        r1 = 12451009; // 0xbdfcc1 float:1.744758E-38 double:6.151616E-317;
        r0 = "12.4.51 (020308-{{cl}})";
        r5 = zzdb();	 Catch:{ all -> 0x01bc }
        if (r5 == 0) goto L_0x004d;
    L_0x0038:
        r5 = com.google.android.gms.common.wrappers.Wrappers.packageManager(r8);	 Catch:{ Exception -> 0x01ce }
        r6 = r8.getPackageName();	 Catch:{ Exception -> 0x01ce }
        r7 = 0;
        r5 = r5.getPackageInfo(r6, r7);	 Catch:{ Exception -> 0x01ce }
        r1 = r5.versionCode;	 Catch:{ Exception -> 0x01ce }
        r6 = r5.versionName;	 Catch:{ Exception -> 0x01ce }
        if (r6 == 0) goto L_0x004d;
    L_0x004b:
        r0 = r5.versionName;	 Catch:{ Exception -> 0x01ce }
    L_0x004d:
        r5 = " v";
        r5 = r3.append(r5);	 Catch:{ all -> 0x01bc }
        r5.append(r1);	 Catch:{ all -> 0x01bc }
        r1 = android.text.TextUtils.isEmpty(r0);	 Catch:{ all -> 0x01bc }
        if (r1 != 0) goto L_0x009f;
    L_0x005d:
        r1 = "(";
        r1 = r0.contains(r1);	 Catch:{ all -> 0x01bc }
        if (r1 == 0) goto L_0x008e;
    L_0x0066:
        r1 = ")";
        r1 = r0.contains(r1);	 Catch:{ all -> 0x01bc }
        if (r1 != 0) goto L_0x008e;
    L_0x006f:
        r1 = "-";
        r1 = r0.endsWith(r1);	 Catch:{ all -> 0x01bc }
        if (r1 == 0) goto L_0x0083;
    L_0x0078:
        r0 = java.lang.String.valueOf(r0);	 Catch:{ all -> 0x01bc }
        r1 = "111111111";
        r0 = r0.concat(r1);	 Catch:{ all -> 0x01bc }
    L_0x0083:
        r0 = java.lang.String.valueOf(r0);	 Catch:{ all -> 0x01bc }
        r1 = ")";
        r0 = r0.concat(r1);	 Catch:{ all -> 0x01bc }
    L_0x008e:
        r1 = " (";
        r1 = r3.append(r1);	 Catch:{ all -> 0x01bc }
        r0 = r1.append(r0);	 Catch:{ all -> 0x01bc }
        r1 = ")";
        r0.append(r1);	 Catch:{ all -> 0x01bc }
    L_0x009f:
        r0 = "\n";
        r3.append(r0);	 Catch:{ all -> 0x01bc }
        r0 = "Build: ";
        r0 = r3.append(r0);	 Catch:{ all -> 0x01bc }
        r1 = android.os.Build.FINGERPRINT;	 Catch:{ all -> 0x01bc }
        r0 = r0.append(r1);	 Catch:{ all -> 0x01bc }
        r1 = "\n";
        r0.append(r1);	 Catch:{ all -> 0x01bc }
        r0 = android.os.Debug.isDebuggerConnected();	 Catch:{ all -> 0x01bc }
        if (r0 == 0) goto L_0x00c4;
    L_0x00be:
        r0 = "Debugger: Connected\n";
        r3.append(r0);	 Catch:{ all -> 0x01bc }
    L_0x00c4:
        if (r11 == 0) goto L_0x00d7;
    L_0x00c6:
        r0 = "DD-EDD: ";
        r0 = r3.append(r0);	 Catch:{ all -> 0x01bc }
        r0 = r0.append(r11);	 Catch:{ all -> 0x01bc }
        r1 = "\n";
        r0.append(r1);	 Catch:{ all -> 0x01bc }
    L_0x00d7:
        r0 = "\n";
        r3.append(r0);	 Catch:{ all -> 0x01bc }
        r0 = android.text.TextUtils.isEmpty(r9);	 Catch:{ all -> 0x01bc }
        if (r0 != 0) goto L_0x00e6;
    L_0x00e3:
        r3.append(r9);	 Catch:{ all -> 0x01bc }
    L_0x00e6:
        r0 = zzdb();	 Catch:{ all -> 0x01bc }
        if (r0 == 0) goto L_0x01d1;
    L_0x00ec:
        r0 = "logcat_for_system_app_crash";
        r1 = zzzh;	 Catch:{ all -> 0x01bc }
        if (r1 < 0) goto L_0x019c;
    L_0x00f3:
        r0 = zzzh;	 Catch:{ all -> 0x01bc }
    L_0x00f5:
        if (r0 <= 0) goto L_0x0190;
    L_0x00f7:
        r1 = "\n";
        r3.append(r1);	 Catch:{ all -> 0x01bc }
        r1 = 0;
        r2 = new java.lang.ProcessBuilder;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r5 = 13;
        r5 = new java.lang.String[r5];	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 0;
        r7 = "/system/bin/logcat";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 1;
        r7 = "-v";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 2;
        r7 = "time";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 3;
        r7 = "-b";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 4;
        r7 = "events";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 5;
        r7 = "-b";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 6;
        r7 = "system";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 7;
        r7 = "-b";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 8;
        r7 = "main";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 9;
        r7 = "-b";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 10;
        r7 = "crash";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 11;
        r7 = "-t";
        r5[r6] = r7;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r6 = 12;
        r0 = java.lang.String.valueOf(r0);	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r5[r6] = r0;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r2.<init>(r5);	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r0 = 1;
        r0 = r2.redirectErrorStream(r0);	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r2 = r0.start();	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r0 = r2.getOutputStream();	 Catch:{ IOException -> 0x01cc, all -> 0x01ad }
        r0.close();	 Catch:{ IOException -> 0x01cc, all -> 0x01ad }
    L_0x016b:
        r0 = r2.getErrorStream();	 Catch:{ IOException -> 0x01ca, all -> 0x01ad }
        r0.close();	 Catch:{ IOException -> 0x01ca, all -> 0x01ad }
    L_0x0172:
        r0 = new java.io.InputStreamReader;	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r2 = r2.getInputStream();	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r0.<init>(r2);	 Catch:{ IOException -> 0x01c7, all -> 0x01ad }
        r1 = 8192; // 0x2000 float:1.14794E-41 double:4.0474E-320;
        r1 = new char[r1];	 Catch:{ IOException -> 0x018a, all -> 0x01c3 }
    L_0x017f:
        r2 = r0.read(r1);	 Catch:{ IOException -> 0x018a, all -> 0x01c3 }
        if (r2 <= 0) goto L_0x01a7;
    L_0x0185:
        r5 = 0;
        r3.append(r1, r5, r2);	 Catch:{ IOException -> 0x018a, all -> 0x01c3 }
        goto L_0x017f;
    L_0x018a:
        r1 = move-exception;
    L_0x018b:
        if (r0 == 0) goto L_0x0190;
    L_0x018d:
        r0.close();	 Catch:{ IOException -> 0x01bf }
    L_0x0190:
        r0 = r3.toString();	 Catch:{ all -> 0x01bc }
        r1 = 90142; // 0x1601e float:1.26316E-40 double:4.4536E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r1);	 Catch:{ all -> 0x01bc }
        monitor-exit(r4);
        return r0;
    L_0x019c:
        r1 = r8.getContentResolver();	 Catch:{ all -> 0x01bc }
        r2 = 0;
        r0 = android.provider.Settings.Secure.getInt(r1, r0, r2);	 Catch:{ all -> 0x01bc }
        goto L_0x00f5;
    L_0x01a7:
        r0.close();	 Catch:{ IOException -> 0x01ab }
        goto L_0x0190;
    L_0x01ab:
        r0 = move-exception;
        goto L_0x0190;
    L_0x01ad:
        r0 = move-exception;
        r2 = r0;
        r3 = r1;
    L_0x01b0:
        if (r3 == 0) goto L_0x01b5;
    L_0x01b2:
        r3.close();	 Catch:{ IOException -> 0x01c1 }
    L_0x01b5:
        r0 = 90142; // 0x1601e float:1.26316E-40 double:4.4536E-319;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r0);	 Catch:{ all -> 0x01bc }
        throw r2;	 Catch:{ all -> 0x01bc }
    L_0x01bc:
        r0 = move-exception;
        monitor-exit(r4);
        throw r0;
    L_0x01bf:
        r0 = move-exception;
        goto L_0x0190;
    L_0x01c1:
        r0 = move-exception;
        goto L_0x01b5;
    L_0x01c3:
        r1 = move-exception;
        r2 = r1;
        r3 = r0;
        goto L_0x01b0;
    L_0x01c7:
        r0 = move-exception;
        r0 = r1;
        goto L_0x018b;
    L_0x01ca:
        r0 = move-exception;
        goto L_0x0172;
    L_0x01cc:
        r0 = move-exception;
        goto L_0x016b;
    L_0x01ce:
        r5 = move-exception;
        goto L_0x004d;
    L_0x01d1:
        r0 = r2;
        goto L_0x00f5;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.android.gms.common.util.CrashUtils.zza(android.content.Context, java.lang.String, java.lang.String, int):java.lang.String");
    }

    @VisibleForTesting
    private static synchronized Throwable zza(Throwable th) {
        Throwable th2;
        synchronized (CrashUtils.class) {
            AppMethodBeat.i(90141);
            LinkedList linkedList = new LinkedList();
            Object th3;
            while (th3 != null) {
                linkedList.push(th3);
                th3 = th3.getCause();
            }
            Throwable th4 = null;
            int i = 0;
            while (!linkedList.isEmpty()) {
                th2 = (Throwable) linkedList.pop();
                StackTraceElement[] stackTrace = th2.getStackTrace();
                ArrayList arrayList = new ArrayList();
                arrayList.add(new StackTraceElement(th2.getClass().getName(), "<filtered>", "<filtered>", 1));
                int i2 = i;
                for (Object obj : stackTrace) {
                    Object obj2;
                    String className = obj2.getClassName();
                    String fileName = obj2.getFileName();
                    i = (TextUtils.isEmpty(fileName) || !fileName.startsWith(":com.google.android.gms")) ? 0 : 1;
                    i2 |= i;
                    if (i == 0 && !isSystemClassPrefixInternal(className)) {
                        obj2 = new StackTraceElement("<filtered>", "<filtered>", "<filtered>", 1);
                    }
                    arrayList.add(obj2);
                }
                th4 = th4 == null ? new Throwable("<filtered>") : new Throwable("<filtered>", th4);
                th4.setStackTrace((StackTraceElement[]) arrayList.toArray(new StackTraceElement[0]));
                i = i2;
            }
            if (i == 0) {
                th2 = null;
                AppMethodBeat.o(90141);
            } else {
                AppMethodBeat.o(90141);
                th2 = th4;
            }
        }
        return th2;
    }

    private static synchronized boolean zza(Context context, String str, String str2, int i, Throwable th) {
        boolean z;
        synchronized (CrashUtils.class) {
            AppMethodBeat.i(90139);
            Preconditions.checkNotNull(context);
            if (!isPackageSide() || Strings.isEmptyOrWhitespace(str)) {
                AppMethodBeat.o(90139);
                z = false;
            } else {
                int hashCode = str.hashCode();
                int hashCode2 = th == null ? zzzj : th.hashCode();
                if (zzzi == hashCode && zzzj == hashCode2) {
                    AppMethodBeat.o(90139);
                    z = false;
                } else {
                    zzzi = hashCode;
                    zzzj = hashCode2;
                    DropBoxManager dropBoxManager = zzzd != null ? zzzd : (DropBoxManager) context.getSystemService("dropbox");
                    if (dropBoxManager == null || !dropBoxManager.isTagEnabled("system_app_crash")) {
                        AppMethodBeat.o(90139);
                        z = false;
                    } else {
                        dropBoxManager.addText("system_app_crash", zza(context, str, str2, i));
                        z = true;
                        AppMethodBeat.o(90139);
                    }
                }
            }
        }
        return z;
    }

    private static boolean zzdb() {
        return zzze ? zzzg : false;
    }
}
