翰林优商.apk(点击下载) / CrashUploadUtils.java


package com.nirvana.tools.crash;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Process;
import android.text.TextUtils;
import com.baidu.mobads.sdk.internal.ae;
import com.bytedance.sdk.openadsdk.downloadnew.core.TTDownloadField;
import com.kuaishou.weapon.p0.k1;
import com.nirvana.tools.core.UTSharedPreferencesHelper;
import com.umeng.umcrash.BuildConfig;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import kotlin.UByte;

/* access modifiers changed from: package-private */
public class CrashUploadUtils {
    public static final String CRASH_SDK_INFO = "CRASH_SDK_INFO";
    private static final String KEY_UNIQUE_ID = "uniqueId";

    CrashUploadUtils() {
    }

    public static String apkInDebugRelease(Context context) {
        try {
            return (context.getApplicationInfo().flags & 2) != 0 ? BuildConfig.BUILD_TYPE : "release";
        } catch (Exception unused) {
        }
    }

    private static byte[] getBytesByInputStream(InputStream inputStream) {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
        byte[] bArr = new byte[8192];
        while (true) {
            try {
                int read = bufferedInputStream.read(bArr);
                if (read <= 0) {
                    break;
                }
                bufferedOutputStream.write(bArr, 0, read);
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    bufferedOutputStream.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
                try {
                    bufferedInputStream.close();
                } catch (IOException e3) {
                    e3.printStackTrace();
                }
                return null;
            } catch (Throwable th) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
                try {
                    bufferedInputStream.close();
                } catch (IOException e5) {
                    e5.printStackTrace();
                }
                throw th;
            }
        }
        bufferedOutputStream.flush();
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        try {
            bufferedOutputStream.close();
        } catch (IOException e6) {
            e6.printStackTrace();
        }
        try {
            bufferedInputStream.close();
            return byteArray;
        } catch (IOException e7) {
            e7.printStackTrace();
            return byteArray;
        }
    }

    public static String getCurProcessName(Context context) {
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses;
        if (Build.VERSION.SDK_INT >= 28) {
            return Application.getProcessName();
        }
        try {
            return (String) Class.forName("android.app.ActivityThread").getDeclaredMethod(Build.VERSION.SDK_INT >= 18 ? "currentProcessName" : "currentPackageName", new Class[0]).invoke(null, new Object[0]);
        } catch (Exception unused) {
            int myPid = Process.myPid();
            ActivityManager activityManager = (ActivityManager) context.getSystemService(TTDownloadField.TT_ACTIVITY);
            if (activityManager == null || (runningAppProcesses = activityManager.getRunningAppProcesses()) == null) {
                return "";
            }
            for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
                if (runningAppProcessInfo.pid == myPid) {
                    return runningAppProcessInfo.processName;
                }
            }
            return "";
        }
    }

    public static String getFieldFromCpuinfo(String str) throws IOException {
        Matcher matcher;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(k1.a));
        Pattern compile = Pattern.compile(str + "\\s*:\\s*(.*)");
        do {
            try {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    matcher = compile.matcher(readLine);
                } else {
                    bufferedReader.close();
                    return "";
                }
            } catch (Exception unused) {
            } catch (Throwable th) {
                bufferedReader.close();
                throw th;
            }
        } while (!matcher.matches());
        String group = matcher.group(1);
        bufferedReader.close();
        return group;
    }

    public static String getMeminfo() {
        try {
            StringBuilder sb = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/meminfo")));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    sb.append(readLine);
                    sb.append("\n");
                } else {
                    bufferedReader.close();
                    return sb.toString();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    private static String getStringByBytes(byte[] bArr, String str) {
        try {
            return new String(bArr, str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getUniqueId(Context context) {
        String str = (String) UTSharedPreferencesHelper.get(context, CRASH_SDK_INFO, KEY_UNIQUE_ID, "");
        if (!TextUtils.isEmpty(str)) {
            return str;
        }
        String uuid = UUID.randomUUID().toString();
        UTSharedPreferencesHelper.put(context, CRASH_SDK_INFO, KEY_UNIQUE_ID, uuid);
        return uuid;
    }

    public static String getVersion(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }

    public static boolean isGzip(byte[] bArr) {
        return ((bArr[1] & UByte.MAX_VALUE) | (bArr[0] << 8)) == 8075;
    }

    public static boolean post(String str, Map<String, String> map, Map<String, File> map2) throws Exception {
        String uuid = UUID.randomUUID().toString();
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        httpURLConnection.setReadTimeout(5000);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestMethod(ae.b);
        httpURLConnection.setRequestProperty("connection", "keep-alive");
        httpURLConnection.setRequestProperty("Charsert", "UTF-8");
        httpURLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        httpURLConnection.setRequestProperty("Content-Encoding", "gzip");
        httpURLConnection.setRequestProperty("http.protocol.content-charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data" + ";boundary=" + uuid);
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append("--");
            sb.append(uuid);
            sb.append("\r\n");
            sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + "\r\n");
            sb.append("Content-Type: text/plain; charset=UTF-8\r\n");
            sb.append("\r\n");
            sb.append(entry.getValue());
            sb.append("\r\n");
        }
        DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
        dataOutputStream.write(sb.toString().getBytes());
        if (map2 != null) {
            for (Map.Entry<String, File> entry2 : map2.entrySet()) {
                StringBuilder sb2 = new StringBuilder();
                sb2.append("--");
                sb2.append(uuid);
                sb2.append("\r\n");
                sb2.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + entry2.getKey() + "\"" + "\r\n");
                sb2.append("\r\n");
                dataOutputStream.write(sb2.toString().getBytes());
                FileInputStream fileInputStream = new FileInputStream(entry2.getValue());
                GZIPInputStream gZIPInputStream = new GZIPInputStream(new BufferedInputStream(fileInputStream));
                byte[] bArr = new byte[1024];
                while (true) {
                    int read = gZIPInputStream.read(bArr);
                    if (read == -1) {
                        break;
                    }
                    dataOutputStream.write(bArr, 0, read);
                }
                fileInputStream.close();
                dataOutputStream.write("\r\n".getBytes());
            }
        }
        dataOutputStream.write(("--" + uuid + "--" + "\r\n").getBytes());
        dataOutputStream.flush();
        dataOutputStream.close();
        httpURLConnection.getResponseCode();
        byte[] bytesByInputStream = getBytesByInputStream(httpURLConnection.getInputStream());
        boolean contains = (isGzip(bytesByInputStream) ? uncompressToString(bytesByInputStream, "UTF-8") : getStringByBytes(bytesByInputStream, "UTF-8")).contains("200");
        httpURLConnection.disconnect();
        return contains;
    }

    public static String uncompressToString(byte[] bArr, String str) {
        String str2 = null;
        if (!(bArr == null || bArr.length == 0)) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
            try {
                GZIPInputStream gZIPInputStream = new GZIPInputStream(byteArrayInputStream);
                byte[] bArr2 = new byte[256];
                while (true) {
                    int read = gZIPInputStream.read(bArr2);
                    if (read < 0) {
                        break;
                    }
                    byteArrayOutputStream.write(bArr2, 0, read);
                }
                str2 = byteArrayOutputStream.toString(str);
                try {
                    byteArrayInputStream.close();
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException unused) {
                byteArrayInputStream.close();
                byteArrayOutputStream.close();
            } catch (Throwable th) {
                try {
                    byteArrayInputStream.close();
                    byteArrayOutputStream.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
                throw th;
            }
        }
        return str2;
    }
}