CarrotChat v2.8.60.240131版本的 MD5 值为:2baf8b9f31830b86b5c5a85ae6b47e05

以下内容为反编译后的 CrashHandler.java 源代码,内容仅作参考


package com.kinggrid.iapppdf.company.common;

import ak.im.module.BleConstant;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.os.Process;
import android.util.Log;
import android.widget.Toast;
import com.huawei.hms.framework.common.ContainerUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Properties;
import java.util.TreeSet;
public class CrashHandler implements Thread.UncaughtExceptionHandler {
    public static final boolean DEBUG = true;
    public static final String TAG = "CrashHandler";
    private static CrashHandler f12004b = null;
    private static final String f12005e = "versionName";
    private static final String f = "versionCode";
    private static final String g = "STACK_TRACE";
    private static final String h = ".cr";
    private Thread.UncaughtExceptionHandler a;
    private Context f12006c;
    private String i;
    private final Properties f12007d = new Properties();
    private String j = Environment.getExternalStorageDirectory() + "/localfiles/";
    private String k = "http://192.168.0.76:8080/iWebOffice2009/OfficeServer.jsp";

    private CrashHandler() {
    }

    private String[] b(Context context) {
        return context.getFilesDir().list(new FilenameFilter() {
            @Override
            public boolean accept(File file, String str) {
                return str.endsWith(CrashHandler.h);
            }
        });
    }

    public static CrashHandler getInstance() {
        if (f12004b == null) {
            f12004b = new CrashHandler();
        }
        return f12004b;
    }

    public void collectCrashDeviceInfo(Context context) {
        Field[] declaredFields;
        String sb;
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 1);
            if (packageInfo != null) {
                StringBuilder sb2 = new StringBuilder("versionName=");
                String str = packageInfo.versionName;
                if (str == null) {
                    str = "not set";
                }
                sb2.append(str);
                sb2.append("\n");
                this.i = sb2.toString();
                this.i = String.valueOf(sb) + f + ContainerUtils.KEY_VALUE_DELIMITER + packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e2) {
            Log.e(TAG, "Error while collect package info", e2);
        }
        for (Field field : Build.class.getDeclaredFields()) {
            try {
                field.setAccessible(true);
                this.i = String.valueOf(this.i) + "\n" + field.getName() + ContainerUtils.KEY_VALUE_DELIMITER + field.get(null);
            } catch (Exception e3) {
                Log.e(TAG, "Error while collect crash info", e3);
            }
        }
    }

    public void init(Context context, String str, String str2) {
        this.f12006c = context;
        this.a = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        this.j = str;
        this.k = str2;
    }

    public void sendPreviousReportsToServer() {
        a(this.f12006c);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable th) {
        Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
        if (!a(th) && (uncaughtExceptionHandler = this.a) != null) {
            uncaughtExceptionHandler.uncaughtException(thread, th);
            return;
        }
        try {
            Thread.sleep(BleConstant.SEND_OUTTIME);
        } catch (InterruptedException e2) {
            Log.e(TAG, "Error : ", e2);
        }
        Process.killProcess(Process.myPid());
        System.exit(10);
    }

    private boolean a(Throwable th) {
        if (th == null) {
            return true;
        }
        final String localizedMessage = th.getLocalizedMessage();
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Context context = CrashHandler.this.f12006c;
                Toast.makeText(context, "程序出错啦" + localizedMessage, 1).show();
                Looper.loop();
            }
        }.start();
        collectCrashDeviceInfo(this.f12006c);
        b(th);
        a(this.f12006c);
        return true;
    }

    private String b(Throwable th) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        th.printStackTrace(printWriter);
        for (Throwable cause = th.getCause(); cause != null; cause = cause.getCause()) {
            Log.d("bb", "cause 000000000");
            cause.printStackTrace(printWriter);
        }
        stringWriter.toString();
        this.i = String.valueOf(this.i) + "\nSTACK_TRACE=" + stringWriter.toString();
        printWriter.close();
        String str = "";
        try {
            String format = new SimpleDateFormat("yyyyMMddHHmmss").format(Long.valueOf(System.currentTimeMillis()));
            str = "crash-" + String.valueOf(format) + h;
            a(this.i, str, true);
            return str;
        } catch (Exception e2) {
            Log.e(TAG, "an error occured while writing report file..." + str, e2);
            e2.printStackTrace();
            return null;
        }
    }

    private void a(Context context) {
        String[] b2 = b(context);
        if (b2 == null || b2.length <= 0) {
            return;
        }
        TreeSet treeSet = new TreeSet();
        treeSet.addAll(Arrays.asList(b2));
        Iterator it = treeSet.iterator();
        while (it.hasNext()) {
            File file = new File(new File(String.valueOf(context.getFilesDir().getAbsolutePath()) + "iAppPDF"), (String) it.next());
            a(file);
            file.delete();
        }
    }

    private void a(File file) {
        if (this.k.equals("")) {
            return;
        }
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(this.k).openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.connect();
            OutputStream outputStream = httpURLConnection.getOutputStream();
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bArr = new byte[1024];
            while (true) {
                int read = fileInputStream.read(bArr);
                if (read == -1) {
                    outputStream.close();
                    fileInputStream.close();
                    httpURLConnection.disconnect();
                    return;
                }
                outputStream.write(bArr, 0, read);
            }
        } catch (MalformedURLException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
    }

    private void a(String str, String str2, boolean z) {
        if (this.j.equals("")) {
            this.j = Environment.getExternalStorageDirectory() + "/localfiles/";
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(String.valueOf(this.j) + str2, z);
            fileOutputStream.write(str.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
    }
}