package com.mbridge.msdk.foundation.tools; import android.text.TextUtils; import com.mbridge.msdk.MBridgeConstans; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes4.dex */ public final class ad extends l { public static void a(String str) { try { if (TextUtils.isEmpty(str)) { return; } com.mbridge.msdk.c.g b = com.mbridge.msdk.c.h.a().b(com.mbridge.msdk.foundation.controller.c.m().k()); if (b == null) { com.mbridge.msdk.c.h.a(); b = com.mbridge.msdk.c.i.a(); } long currentTimeMillis = System.currentTimeMillis() - (b.l() * 1000); File file = new File(str); if (file.exists() && file.isDirectory()) { long currentTimeMillis2 = System.currentTimeMillis(); File[] listFiles = file.listFiles(); if (listFiles != null) { for (File file2 : listFiles) { if (file2.lastModified() + currentTimeMillis < currentTimeMillis2) { a(file2); try { File file3 = new File(str + ".zip"); if (file3.exists() && file3.isFile()) { a(file3); } } catch (Exception unused) { } } } } } } catch (Exception unused2) { } } public static void a() { Runnable runnable = new Runnable() { // from class: com.mbridge.msdk.foundation.tools.ad.1 @Override // java.lang.Runnable public final void run() { try { com.mbridge.msdk.c.g b = com.mbridge.msdk.c.h.a().b(com.mbridge.msdk.foundation.controller.c.m().k()); if (b == null) { com.mbridge.msdk.c.h.a(); b = com.mbridge.msdk.c.i.a(); } ad.a(com.mbridge.msdk.foundation.same.b.e.a(com.mbridge.msdk.foundation.same.b.c.MBRIDGE_RES_MANAGER_DIR), b.T()); ad.a(System.currentTimeMillis() - (b.l() * 1000)); } catch (Exception e) { if (MBridgeConstans.DEBUG) { e.printStackTrace(); } } } }; com.mbridge.msdk.foundation.controller.d.a(); com.mbridge.msdk.foundation.same.f.a.b().execute(runnable); } public static void b() { new Thread(new Runnable() { // from class: com.mbridge.msdk.foundation.tools.ad.2 @Override // java.lang.Runnable public final void run() { try { if (com.mbridge.msdk.foundation.controller.c.m().c() != null) { String a = com.mbridge.msdk.foundation.same.b.e.a(com.mbridge.msdk.foundation.same.b.c.MBRIDGE_VC); try { File file = new File(a); if (file.exists() && file.isDirectory()) { for (File file2 : ad.b(a)) { if (file2.exists() && file2.isFile()) { file2.delete(); } } } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e2) { if (MBridgeConstans.DEBUG) { e2.printStackTrace(); } } } }).start(); } public static File[] b(String str) { try { File file = new File(str); if (file.exists()) { return file.listFiles(); } return null; } catch (Exception unused) { return null; } } public static void a(long j) { try { Iterator it = c(new File(com.mbridge.msdk.foundation.same.b.e.a(com.mbridge.msdk.foundation.same.b.c.MBRIDGE_RES_MANAGER_DIR))).iterator(); while (it.hasNext()) { File next = it.next(); if (next.lastModified() < j && next.exists() && next.isFile()) { next.delete(); } } } catch (Throwable th) { af.b("SameFileTool", th.getMessage(), th); } } public static String a(File file) { try { if (file.isFile()) { file.delete(); return ""; } if (!file.isDirectory()) { return ""; } File[] listFiles = file.listFiles(); if (listFiles != null && listFiles.length != 0) { for (File file2 : listFiles) { a(file2); } file.delete(); return ""; } file.delete(); return ""; } catch (Exception e) { return e.getMessage(); } } private static ArrayList c(File file) { ArrayList arrayList = new ArrayList<>(); for (File file2 : file.listFiles(new FileFilter() { // from class: com.mbridge.msdk.foundation.tools.ad.3 @Override // java.io.FileFilter public final boolean accept(File file3) { return !file3.isHidden() || file3.isDirectory(); } })) { if (!file2.isDirectory()) { arrayList.add(file2); } else { arrayList.addAll(c(file2)); } } return arrayList; } public static String c(String str) { return !TextUtils.isEmpty(str) ? SameMD5.getMD5(aq.b(str.trim())) : ""; } private static long d(File file) throws Exception { long j = 0; FileInputStream fileInputStream = null; try { try { try { if (file.exists()) { FileInputStream fileInputStream2 = new FileInputStream(file); try { j = fileInputStream2.available(); fileInputStream = fileInputStream2; } catch (Exception e) { e = e; fileInputStream = fileInputStream2; e.printStackTrace(); if (fileInputStream != null) { fileInputStream.close(); } return j; } catch (Throwable th) { th = th; fileInputStream = fileInputStream2; if (fileInputStream != null) { try { fileInputStream.close(); } catch (Exception e2) { e2.printStackTrace(); } } throw th; } } else { file.createNewFile(); af.b("获取文件大小", "文件不存在!"); } } catch (Exception e3) { e3.printStackTrace(); } } catch (Exception e4) { e = e4; } if (fileInputStream != null) { fileInputStream.close(); } return j; } catch (Throwable th2) { th = th2; } } private static long e(File file) throws Exception { long d; File[] listFiles = file.listFiles(); long j = 0; if (listFiles != null) { for (int i = 0; i < listFiles.length; i++) { if (listFiles[i].isDirectory()) { d = e(listFiles[i]); } else { d = d(listFiles[i]); } j += d; } } return j; } public static List a(String str, String str2) { ArrayList arrayList = new ArrayList(); try { } catch (Exception e) { af.b("SameFileTool", e.getMessage()); } if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) { File file = new File(str + File.separator + "template_config.json"); if (file.isFile() && file.exists()) { String b = b(file); if (TextUtils.isEmpty(b)) { return arrayList; } try { JSONArray jSONArray = new JSONArray(b); for (int i = 0; i < jSONArray.length(); i++) { JSONObject jSONObject = jSONArray.getJSONObject(i); if (jSONObject != null && str2.equals(jSONObject.optString("xml_type"))) { arrayList.add(str + File.separator + jSONObject.optString("name")); if (jSONObject.has("ext_template")) { jSONObject.put("folder_dir", str); arrayList.add(jSONObject.toString()); } } } return arrayList; } catch (JSONException e2) { af.b("SameFileTool", e2.getMessage()); return arrayList; } } arrayList.add(str); } return arrayList; } /* JADX WARN: Code restructure failed: missing block: B:25:0x0042, code lost: if (r1 == null) goto L26; */ /* JADX WARN: Removed duplicated region for block: B:33:0x004f A[EXC_TOP_SPLITTER, SYNTHETIC] */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public static java.lang.String b(java.io.File r4) { /* r0 = 0 if (r4 != 0) goto L4 return r0 L4: java.io.BufferedReader r1 = new java.io.BufferedReader // Catch: java.lang.Throwable -> L3a java.io.IOException -> L3c java.io.FileReader r2 = new java.io.FileReader // Catch: java.lang.Throwable -> L3a java.io.IOException -> L3c r2.(r4) // Catch: java.lang.Throwable -> L3a java.io.IOException -> L3c r1.(r2) // Catch: java.lang.Throwable -> L3a java.io.IOException -> L3c java.lang.StringBuffer r4 = new java.lang.StringBuffer // Catch: java.lang.Throwable -> L2e java.io.IOException -> L37 r4.() // Catch: java.lang.Throwable -> L2e java.io.IOException -> L37 L13: java.lang.String r2 = r1.readLine() // Catch: java.lang.Throwable -> L2e java.io.IOException -> L31 if (r2 == 0) goto L33 java.lang.StringBuilder r3 = new java.lang.StringBuilder // Catch: java.lang.Throwable -> L2e java.io.IOException -> L31 r3.() // Catch: java.lang.Throwable -> L2e java.io.IOException -> L31 r3.append(r2) // Catch: java.lang.Throwable -> L2e java.io.IOException -> L31 java.lang.String r2 = "\n" r3.append(r2) // Catch: java.lang.Throwable -> L2e java.io.IOException -> L31 java.lang.String r2 = r3.toString() // Catch: java.lang.Throwable -> L2e java.io.IOException -> L31 r4.append(r2) // Catch: java.lang.Throwable -> L2e java.io.IOException -> L31 goto L13 L2e: r4 = move-exception r0 = r1 goto L4d L31: r2 = move-exception goto L3f L33: r1.close() // Catch: java.io.IOException -> L45 goto L45 L37: r2 = move-exception r4 = r0 goto L3f L3a: r4 = move-exception goto L4d L3c: r2 = move-exception r4 = r0 r1 = r4 L3f: r2.printStackTrace() // Catch: java.lang.Throwable -> L2e if (r1 == 0) goto L45 goto L33 L45: if (r4 == 0) goto L4c java.lang.String r4 = r4.toString() return r4 L4c: return r0 L4d: if (r0 == 0) goto L52 r0.close() // Catch: java.io.IOException -> L52 L52: throw r4 */ throw new UnsupportedOperationException("Method not decompiled: com.mbridge.msdk.foundation.tools.ad.b(java.io.File):java.lang.String"); } public static boolean d(String str) { if (com.mbridge.msdk.foundation.same.c.e.a(str)) { return false; } File file = new File(str); return file.exists() && file.isFile(); } private static void e(String str) { try { ArrayList c = c(new File(str)); Collections.sort(c, new Comparator() { // from class: com.mbridge.msdk.foundation.tools.ad.4 @Override // java.util.Comparator public final boolean equals(Object obj) { return true; } @Override // java.util.Comparator public final /* synthetic */ int compare(File file, File file2) { long lastModified = file.lastModified() - file2.lastModified(); if (lastModified > 0) { return 1; } return lastModified == 0 ? 0 : -1; } }); int size = (c.size() - 1) / 2; for (int i = 0; i < size; i++) { File file = c.get(i); if (file.exists() && file.isFile()) { file.delete(); } } } catch (Exception unused) { af.b("SameFileTool", "del memory failed"); } } /* JADX WARN: Code restructure failed: missing block: B:108:0x00ff, code lost: r12.close(); */ /* JADX WARN: Code restructure failed: missing block: B:110:0x0103, code lost: r12 = move-exception; */ /* JADX WARN: Code restructure failed: missing block: B:111:0x0104, code lost: com.mbridge.msdk.foundation.tools.af.b("SameFileTool", com.mbridge.msdk.newreward.function.common.MBridgeError.ERROR_MESSAGE_DOWNLOAD_UN_ZIP_ERROR, r12); */ /* JADX WARN: Code restructure failed: missing block: B:151:0x0129, code lost: r5.close(); */ /* JADX WARN: Code restructure failed: missing block: B:152:0x012c, code lost: if (r12 == null) goto L104; */ /* JADX WARN: Code restructure failed: missing block: B:161:0x012e, code lost: r12.close(); */ /* JADX WARN: Code restructure failed: missing block: B:163:0x0132, code lost: r12 = move-exception; */ /* JADX WARN: Code restructure failed: missing block: B:164:0x0133, code lost: com.mbridge.msdk.foundation.tools.af.b("SameFileTool", com.mbridge.msdk.newreward.function.common.MBridgeError.ERROR_MESSAGE_DOWNLOAD_UN_ZIP_ERROR, r12); */ /* JADX WARN: Code restructure failed: missing block: B:99:0x00fd, code lost: if (r12 == null) goto L80; */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public static java.lang.String b(java.lang.String r12, java.lang.String r13) { /* Method dump skipped, instructions count: 382 To view this dump add '--comments-level debug' option */ throw new UnsupportedOperationException("Method not decompiled: com.mbridge.msdk.foundation.tools.ad.b(java.lang.String, java.lang.String):java.lang.String"); } public static boolean a(byte[] bArr, File file) { FileOutputStream fileOutputStream; FileOutputStream fileOutputStream2 = null; try { try { if (file.getParentFile() != null && !file.exists()) { file.getParentFile().mkdirs(); } fileOutputStream = new FileOutputStream(file); } catch (Throwable th) { th = th; } } catch (Exception e) { e = e; } try { fileOutputStream.write(bArr); try { fileOutputStream.close(); return true; } catch (IOException e2) { e2.printStackTrace(); return true; } } catch (Exception e3) { e = e3; fileOutputStream2 = fileOutputStream; e.printStackTrace(); if (fileOutputStream2 == null) { return false; } try { fileOutputStream2.close(); return false; } catch (IOException e4) { e4.printStackTrace(); return false; } } catch (Throwable th2) { th = th2; fileOutputStream2 = fileOutputStream; if (fileOutputStream2 != null) { try { fileOutputStream2.close(); } catch (IOException e5) { e5.printStackTrace(); } } throw th; } } public static /* synthetic */ void a(String str, int i) { try { if (e(new File(str)) > i * 1048576) { e(str); } } catch (Exception e) { e.printStackTrace(); } catch (Throwable unused) { af.b("SameFileTool", "clean memory failed"); } } }