package com.netease.nimlib.d.g; import android.content.Context; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; import android.os.Build; import android.util.Log; import androidx.annotation.Nullable; import com.huawei.hms.framework.common.hianalytics.HianalyticsBaseData; import com.huawei.hms.support.hianalytics.HiAnalyticsConstant; import com.netease.nim.uikit.business.contact.core.model.ContactGroupStrategy; import com.netease.nimlib.jsbridge.interfaces.IJavaReplyToJsFileInfo; import com.netease.nimlib.net.a.d.b; import com.netease.nimlib.s.b.b; import com.netease.nimlib.s.b.c; import com.netease.nimlib.s.j; import com.vivo.push.PushClientConstants; import com.xiaomi.mipush.sdk.Constants; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Semaphore; import org.json.HTTP; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* compiled from: IMLogManager */ public class a { private static final String a = c.b(b.TYPE_LOG); private static final String b = com.netease.nimlib.push.b.c(); private static volatile int c = 0; /* renamed from: d reason: collision with root package name */ private static volatile a f1701d = null; private final Semaphore e = new Semaphore(1); /* access modifiers changed from: private */ /* renamed from: com.netease.nimlib.d.g.a$a reason: collision with other inner class name */ /* compiled from: IMLogManager */ public static class C0089a { private final short a; private final String b; private final long c; /* renamed from: d reason: collision with root package name */ private final String f1702d; private final String e; private int f; C0089a(short s, byte b2, byte b3) { this(s, com.netease.nimlib.c.n(), System.currentTimeMillis(), "" + ((int) b2), "" + ((int) b3), 1); } /* access modifiers changed from: package-private */ public int a() { return this.f; } /* access modifiers changed from: package-private */ public String b() { JSONObject jSONObject = new JSONObject(); try { jSONObject.put("errorCode", (int) this.a); jSONObject.put("accid", this.b); jSONObject.put("timestamp", this.c); jSONObject.put("module", this.f1702d); jSONObject.put(IJavaReplyToJsFileInfo.RESPONSE_FILE_INFO, this.e); jSONObject.put("count", this.f); } catch (JSONException e2) { e2.printStackTrace(); } return jSONObject.toString(); } /* access modifiers changed from: package-private */ public String c() { JSONObject jSONObject = new JSONObject(); try { jSONObject.put("errorCode", (int) this.a); jSONObject.put("accid", this.b); jSONObject.put("module", this.f1702d); jSONObject.put(IJavaReplyToJsFileInfo.RESPONSE_FILE_INFO, this.e); return jSONObject.toString(); } catch (JSONException e2) { e2.printStackTrace(); return null; } } /* access modifiers changed from: package-private */ public void a(int i) { this.f = i; } static C0089a a(JSONObject jSONObject) { return new C0089a((short) jSONObject.optInt("errorCode"), jSONObject.optString("accid"), jSONObject.optLong("timestamp"), jSONObject.optString("module"), jSONObject.optString(IJavaReplyToJsFileInfo.RESPONSE_FILE_INFO), jSONObject.optInt("count")); } C0089a(short s, String str, long j, String str2, String str3, int i) { this.a = s; this.b = str == null ? "" : str; this.c = j; this.f1702d = str2 == null ? "" : str2; this.e = str3 == null ? "" : str3; this.f = i; } } private a() { e(); try { d(); } catch (IOException e2) { e2.printStackTrace(); } } public static a a() { if (f1701d == null) { synchronized (a.class) { if (f1701d == null) { f1701d = new a(); return f1701d; } } } return f1701d; } private static String b() { String str = com.netease.nimlib.t.a.c() + ContactGroupStrategy.GROUP_SHARP + Build.BOARD + ContactGroupStrategy.GROUP_SHARP + com.netease.nimlib.t.a.b(); String c2 = c(); if (c2 == null || c2.isEmpty()) { return str; } return str + ContactGroupStrategy.GROUP_SHARP + c2; } private static String c() { UsbManager usbManager; HashMap<String, UsbDevice> hashMap; Context e2 = com.netease.nimlib.c.e(); if (e2 == null || (usbManager = (UsbManager) e2.getSystemService("usb")) == null) { return ""; } UsbDevice usbDevice = null; try { hashMap = usbManager.getDeviceList(); } catch (Exception e3) { e3.printStackTrace(); com.netease.nimlib.l.b.b.a.e("IMLogManager", "getDeviceList Exception, " + e3); hashMap = null; } if (hashMap == null || hashMap.size() == 0) { return ""; } for (UsbDevice usbDevice2 : hashMap.values()) { int deviceClass = usbDevice2.getDeviceClass(); if (deviceClass == 239 || deviceClass == 14) { int interfaceCount = usbDevice2.getInterfaceCount(); int i = 0; while (true) { if (i >= interfaceCount) { break; } else if (usbDevice2.getInterface(i).getInterfaceClass() == 14) { usbDevice = usbDevice2; break; } else { i++; } } } } if (usbDevice == null) { return ""; } return usbDevice.getVendorId() + Constants.COLON_SEPARATOR + usbDevice.getProductId(); } private File d() throws IOException { String str = a; File file = new File(str, "IMLogMsg.log"); if (!file.exists()) { com.netease.nimlib.l.b.a.a.b(str, "IMLogMsg.log"); file.createNewFile(); file.setReadable(true); file.setWritable(true); } return file; } private void e() { File file = new File(a, "JsonBody.log"); if (file.exists()) { file.delete(); } } private void f() { com.netease.nimlib.l.a.a(); } private void g() { if (!k() && this.e.tryAcquire()) { com.netease.nimlib.net.a.d.b.a().a(com.netease.nimlib.c.e()); com.netease.nimlib.net.a.d.b.a().a("http://statistic.live.126.net/statics/report/common/form", h(), i(), new h(this)); } } private Map<String, String> h() { HashMap hashMap = new HashMap(); hashMap.put("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryyEePCbPKpBvrs9OE"); return hashMap; } private byte[] i() { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); StringBuilder sb = new StringBuilder(); sb.append("------WebKitFormBoundaryyEePCbPKpBvrs9OE"); sb.append(HTTP.CRLF); sb.append("Content-Disposition: form-data; name=\"reporterHeader\""); sb.append(HTTP.CRLF); sb.append(HTTP.CRLF); String a2 = com.netease.nimlib.l.a.a(true); File file = new File(a2); JSONObject jSONObject = new JSONObject(); try { jSONObject.put("common", new JSONObject().put(HianalyticsBaseData.SDK_TYPE, "IM").put("device_id", b)).put("event", new JSONObject().put("logReport", new JSONObject().put("fileMD5", j.b(a2)).put("errorList", m()))); } catch (JSONException e2) { e2.printStackTrace(); } catch (Throwable th) { sb.append(jSONObject.toString()); sb.append(HTTP.CRLF); throw th; } sb.append(jSONObject.toString()); sb.append(HTTP.CRLF); sb.append("------WebKitFormBoundaryyEePCbPKpBvrs9OE"); sb.append(HTTP.CRLF); sb.append("Content-Disposition: form-data; name=\"logReport\"; filename=\""); sb.append(l()); sb.append("\""); sb.append(HTTP.CRLF); sb.append("Content-Type: application/octet-stream"); sb.append(HTTP.CRLF); sb.append(HTTP.CRLF); byte[] bArr = null; try { byteArrayOutputStream.write(sb.toString().getBytes()); FileInputStream fileInputStream = new FileInputStream(file); byte[] bArr2 = new byte[4096]; while (fileInputStream.read(bArr2) != -1) { byteArrayOutputStream.write(bArr2); } byteArrayOutputStream.write("\r\n------WebKitFormBoundaryyEePCbPKpBvrs9OE--".getBytes()); bArr = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); fileInputStream.close(); } catch (IOException e3) { e3.printStackTrace(); } file.delete(); return bArr == null ? new byte[0] : bArr; } private ConcurrentHashMap<String, C0089a> j() { JSONArray m = m(); ConcurrentHashMap<String, C0089a> concurrentHashMap = new ConcurrentHashMap<>(); for (int i = 0; i < m.length(); i++) { Object opt = m.opt(i); if (opt instanceof JSONObject) { C0089a a2 = C0089a.a((JSONObject) opt); concurrentHashMap.put(a2.c(), a2); } } return concurrentHashMap; } private boolean k() { return m().length() == 0; } private String l() { return "logs_" + new SimpleDateFormat("yyyyMMddhhmm", Locale.CHINA).format(new Date()) + ".zip"; } private JSONArray m() { try { BufferedReader bufferedReader = new BufferedReader(new FileReader(d())); StringBuilder sb = new StringBuilder(); for (String readLine = bufferedReader.readLine(); readLine != null; readLine = bufferedReader.readLine()) { sb.append(readLine); } String sb2 = sb.toString(); return sb2.isEmpty() ? new JSONArray() : new JSONArray(sb2); } catch (FileNotFoundException e2) { e2.printStackTrace(); Log.e("@CJL/文件不存在", e2.getMessage()); return new JSONArray(); } catch (IOException e3) { e3.printStackTrace(); Log.e("@CJL/读写异常", e3.getMessage()); return new JSONArray(); } catch (JSONException e4) { e4.printStackTrace(); Log.e("@CJL/字符串转JSONArray异常", e4.getMessage()); return new JSONArray(); } } /* access modifiers changed from: private */ public /* synthetic */ void b(String str, int i, Throwable th) { StringBuilder sb = new StringBuilder(); sb.append("on register device result: "); sb.append(i); sb.append("resp="); if (str == null) { str = ""; } sb.append(str); Log.i("IMLogManager", sb.toString()); if (i == 200) { g(); } } private boolean b(short s) { if (!com.netease.nimlib.c.j().reportImLog) { return false; } return s == 408 || s == 415 || s == 500; } public void a(@Nullable b.a aVar) { if (c == 0) { c = 1; JSONObject jSONObject = new JSONObject(); try { jSONObject.put("common", new JSONObject().put(HianalyticsBaseData.SDK_TYPE, "IM").put("device_id", b)).put("event", new JSONObject().put("deviceinfo", new JSONObject().put("app_key", com.netease.nimlib.c.h()).put(HiAnalyticsConstant.BI_KEY_SDK_VER, "8.11.5").put("platform", "Android").put("compat_id", b()).put("os_ver", Build.VERSION.RELEASE).put("manufacturer", com.netease.nimlib.t.a.c()).put("model", com.netease.nimlib.t.a.b()).put(PushClientConstants.TAG_PKG_NAME, com.netease.nimlib.c.f()).put("appName", com.netease.nimlib.c.g()))); } catch (JSONException e2) { e2.printStackTrace(); } HashMap hashMap = new HashMap(); hashMap.put("Content-Type", "application/json"); com.netease.nimlib.net.a.d.b.a().a(com.netease.nimlib.c.e()); com.netease.nimlib.net.a.d.b.a().a("http://statistic.live.126.net/statics/report/common/form", hashMap, jSONObject.toString(), new f(jSONObject, aVar)); } } /* access modifiers changed from: private */ public static /* synthetic */ void a(JSONObject jSONObject, b.a aVar, String str, int i, Throwable th) { if (i == 200) { c = 2; com.netease.nimlib.l.b.b.a.c("IMLogManager", "register device info succeed, " + jSONObject); } else { c = 0; } if (aVar != null) { aVar.onResponse(str, i, th); } } public void a(short s) { if (s == 200 && com.netease.nimlib.c.j().reportImLog) { int i = c; if (i == 0) { a(new g(this)); } else if (i == 2) { g(); } } } /* access modifiers changed from: private */ public /* synthetic */ void a(String str, int i, Throwable th) { if (i == 200) { f(); } this.e.release(); } public void a(com.netease.nimlib.d.e.a aVar) { a(aVar.r(), aVar.j().i(), aVar.j().j()); } public void a(short s, byte b2, byte b3) { if (b(s)) { ConcurrentHashMap<String, C0089a> j = j(); C0089a aVar = new C0089a(s, b2, b3); String c2 = aVar.c(); C0089a aVar2 = j.get(aVar.c()); if (aVar2 == null) { j.put(c2, aVar); } else { aVar.a(aVar2.a() + aVar.a()); j.replace(c2, aVar); } a(j); } } private boolean a(ConcurrentHashMap<String, C0089a> concurrentHashMap) { try { File d2 = d(); JSONArray jSONArray = new JSONArray(); for (String str : concurrentHashMap.keySet()) { C0089a aVar = concurrentHashMap.get(str); if (aVar != null) { jSONArray.put(new JSONObject(aVar.b())); } } d2.setWritable(true); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(d2)); outputStreamWriter.write(jSONArray.toString()); outputStreamWriter.flush(); outputStreamWriter.close(); return true; } catch (IOException e2) { e2.printStackTrace(); return false; } catch (JSONException e3) { e3.printStackTrace(); return false; } } }