package com.sigmob.sdk.base.common; import android.app.DownloadManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.text.TextUtils; import android.webkit.MimeTypeMap; import android.widget.Toast; import com.czhj.sdk.common.ClientMetadata; import com.czhj.sdk.common.Constants; import com.czhj.sdk.common.network.Networking; import com.czhj.sdk.common.utils.FileUtil; import com.czhj.sdk.common.utils.Md5Util; import com.czhj.sdk.logger.SigmobLog; import com.kuaishou.weapon.p0.h; import com.mbridge.msdk.foundation.entity.CampaignEx; import com.sigmob.sdk.SigmobFileProvider; import com.sigmob.sdk.b; import com.sigmob.sdk.base.common.x; import com.sigmob.sdk.base.i; import com.sigmob.sdk.base.models.BaseAdUnit; import com.sigmob.sdk.base.models.IntentActions; import com.sigmob.sdk.base.mta.PointCategory; import com.sigmob.sdk.base.mta.PointEntitySigmob; import com.sigmob.sdk.base.network.e; import com.ss.android.downloadad.api.constant.AdBaseConstants; import com.umeng.analytics.pro.aq; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; public class l { private static Map<String, String> a = new Hashtable(); private static Set<a> b = new CopyOnWriteArraySet(); /* access modifiers changed from: private */ public static class a { String a; String b; long c; public a(String str, String str2, long j) { this.a = str; this.b = str2; this.c = j; } public String a() { return this.a; } public String b() { return this.b; } public long c() { return this.c; } } private static long a(String str, String str2) { if (str != null) { try { if (!str.isEmpty()) { String trim = str.trim(); if (!trim.startsWith(Constants.HTTP)) { trim = "http://" + trim; } DownloadManager.Request request = new DownloadManager.Request(Uri.parse(trim)); File file = new File(str2); request.setTitle(file.getName()); request.setAllowedNetworkTypes(3); request.setAllowedOverRoaming(true); request.setVisibleInDownloadsUi(true); request.setNotificationVisibility(1); File parentFile = file.getParentFile(); if (parentFile.exists()) { if (!parentFile.isDirectory()) { parentFile.delete(); if (!parentFile.mkdirs()) { return -1; } } } else if (!parentFile.mkdirs()) { return -1; } request.addRequestHeader("User-Agent", Networking.getUserAgent()); request.setDestinationUri(Uri.fromFile(file)); return ((DownloadManager) b.b().getSystemService("download")).enqueue(request); } } catch (Throwable th) { SigmobLog.e(th.getMessage()); } } return -1; } public static String a(File file) { String name = file.getName(); return MimeTypeMap.getSingleton().getMimeTypeFromExtension(name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase()); } private static String a(List<String> list, String str) { String str2; StringBuilder sb; if (!list.contains(str)) { return str; } Integer valueOf = Integer.valueOf(str.lastIndexOf(".")); String str3 = ""; if (valueOf.intValue() != -1) { str3 = str.substring(0, valueOf.intValue()); str2 = str.substring(valueOf.intValue()); } else { str2 = str3; } Integer valueOf2 = Integer.valueOf(str3.lastIndexOf("(")); if (valueOf2.intValue() == -1) { sb = new StringBuilder(); } else { Integer valueOf3 = Integer.valueOf(str3.indexOf(")", valueOf2.intValue())); if (valueOf3.intValue() == -1) { sb = new StringBuilder(); } else if (valueOf3.intValue() < str3.length() - 1) { sb = new StringBuilder(); } else { try { Integer valueOf4 = Integer.valueOf(Integer.parseInt(str3.substring(valueOf2.intValue() + 1, valueOf3.intValue())) + 1); return a(list, str3.substring(0, valueOf2.intValue()) + "(" + valueOf4 + ")" + str2); } catch (Throwable unused) { sb = new StringBuilder(); } } } sb.append(str3); sb.append("(1)"); sb.append(str2); return a(list, sb.toString()); } /* JADX WARNING: Removed duplicated region for block: B:21:0x0053 A[DONT_GENERATE] */ public static HashMap<String, String> a(DownloadManager downloadManager) { Cursor cursor; Throwable th; try { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterByStatus(3); cursor = downloadManager.query(query); try { if (!cursor.moveToFirst()) { cursor.close(); if (cursor != null) { cursor.close(); } return null; } HashMap<String, String> hashMap = new HashMap<>(); String string = cursor.getString(cursor.getColumnIndex(CampaignEx.JSON_KEY_TITLE)); String string2 = cursor.getString(cursor.getColumnIndex(aq.d)); if (!TextUtils.isEmpty(string)) { hashMap.put(string, string2); } cursor.moveToNext(); cursor.close(); return hashMap; } catch (Throwable th2) { th = th2; try { SigmobLog.e(th.getMessage()); return null; } finally { if (cursor != null) { cursor.close(); } } } } catch (Throwable th3) { th = th3; cursor = null; SigmobLog.e(th.getMessage()); return null; } } /* JADX WARNING: Removed duplicated region for block: B:18:0x0072 A[DONT_GENERATE] */ public static Map<String, Object> a(DownloadManager downloadManager, long j) { Throwable th; Cursor cursor; try { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(j); Uri uriForDownloadedFile = downloadManager.getUriForDownloadedFile(j); cursor = downloadManager.query(query); try { if (!cursor.moveToFirst()) { cursor.close(); if (cursor != null) { cursor.close(); } return null; } int i = cursor.getInt(cursor.getColumnIndex("status")); int i2 = cursor.getInt(cursor.getColumnIndex("reason")); String string = cursor.getString(cursor.getColumnIndex("local_uri")); HashMap hashMap = new HashMap(); hashMap.put("fileName", string); hashMap.put("status", Integer.valueOf(i)); hashMap.put("uri", uriForDownloadedFile); hashMap.put("reason", Integer.valueOf(i2)); cursor.close(); return hashMap; } catch (Throwable th2) { th = th2; try { SigmobLog.e(th.getMessage()); return null; } finally { if (cursor != null) { cursor.close(); } } } } catch (Throwable th3) { th = th3; cursor = null; SigmobLog.e(th.getMessage()); return null; } } public static void a(Context context, String str, BaseAdUnit baseAdUnit) { File file = new File(str); SigmobLog.i("installAPK:" + file.getAbsolutePath() + ":" + file.exists()); Intent intent = new Intent(); intent.addFlags(268435456); intent.setAction("android.intent.action.VIEW"); try { Toast.makeText(context, "开始安装", 1).show(); } catch (Throwable th) { SigmobLog.e(th.getMessage()); } if (Build.VERSION.SDK_INT < 24 || context.getApplicationInfo().targetSdkVersion < 24) { intent.setDataAndType(Uri.fromFile(file), a(file)); } else { try { Context b2 = b.b(); Uri uriForFile = SigmobFileProvider.getUriForFile(b2, b.b().getPackageName() + ".sigprovider", file); if (uriForFile != null) { intent.addFlags(1); intent.setDataAndType(uriForFile, AdBaseConstants.MIME_APK); } } catch (Exception e) { SigmobLog.e(e.getMessage()); } } context.startActivity(intent); e.a(baseAdUnit, a.AD_INSTALL_START); HashMap hashMap = new HashMap(); hashMap.put("result", "1"); BaseBroadcastReceiver.a(context, baseAdUnit.getUuid(), hashMap, IntentActions.ACTION_INTERSTITIAL_INSTALL_START); PackageInfo packageInfoWithUri = ClientMetadata.getPackageInfoWithUri(context, str); if (packageInfoWithUri != null) { String f = com.sigmob.sdk.base.utils.b.f(); FileUtil.writeToCache(baseAdUnit, new File(f, packageInfoWithUri.packageName + ".log").getAbsolutePath()); } x.a(baseAdUnit, PointCategory.APP_INSTALL_START, packageInfoWithUri, "1"); } public static void a(final String str, BaseAdUnit baseAdUnit) { Context b2 = b.b(); try { SigmobLog.i("download apk:" + str); d.b(baseAdUnit); String a2 = m.a(str); if (TextUtils.isEmpty(a2) && !TextUtils.isEmpty(baseAdUnit.getProductId())) { a2 = baseAdUnit.getProductId() + ".apk"; } a(a2, str, baseAdUnit); } catch (Throwable th) { SigmobLog.e(th.getMessage()); } } /* JADX WARNING: Can't wrap try/catch for region: R(5:23|24|25|26|74) */ /* JADX WARNING: Code restructure failed: missing block: B:75:?, code lost: return; */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Missing exception handler attribute for start block: B:25:0x00bd */ private static void a(String str, final String str2, BaseAdUnit baseAdUnit) { HashMap hashMap; String uuid; String str3; Context b2 = b.b(); try { String str4 = TextUtils.isEmpty(str) ? Md5Util.md5(baseAdUnit.getLanding_page()) + ".apk" : str; final File file = new File(com.sigmob.sdk.base.utils.b.a(b2), str4); Long c = c(-1, file.getAbsolutePath()); if (c == null || c.longValue() <= 0) { HashMap<String, String> a2 = a((DownloadManager) b2.getSystemService("download")); if (a2 != null) { for (String str5 : a2.keySet()) { if (!TextUtils.isEmpty(str5) && str5.equals(str4)) { Toast.makeText(b2, str4 + "当前正在努力下载,请稍等", 1).show(); String str6 = a2.get(str5); baseAdUnit.setDownloadId(Long.parseLong(str6)); HashMap hashMap2 = new HashMap(); hashMap2.put("result", "1"); hashMap2.put("downloadId", str6); BaseBroadcastReceiver.a(b2, baseAdUnit.getUuid(), hashMap2, IntentActions.ACTION_INTERSTITIAL_DOWNLOAD_START); return; } } } boolean containsKey = a.containsKey(baseAdUnit.getUuid()); if (file.exists() && !containsKey && !a(b2, file.getAbsolutePath())) { SigmobLog.i("cacheFile deleteFile:" + FileUtil.deleteFile(file.getAbsolutePath())); } SigmobLog.i(str4 + " exists:" + file.exists() + " containsKey:" + containsKey); if (file.exists()) { if (containsKey) { a(b2, file.getAbsolutePath(), baseAdUnit); return; } long lastModified = file.lastModified(); int M = i.a().M(); if (M == 0) { str3 = "default deleteFile:" + FileUtil.deleteFile(file.getAbsolutePath()); } else if (System.currentTimeMillis() - lastModified > ((long) (M * 1000))) { str3 = "timeOut deleteFile:" + FileUtil.deleteFile(file.getAbsolutePath()); } else { a(b2, file.getAbsolutePath(), baseAdUnit); return; } SigmobLog.i(str3); } try { Toast.makeText(b2, "下载开始", 1).show(); } catch (Throwable th) { SigmobLog.e(th.getMessage()); } a.put(baseAdUnit.getUuid(), str4); long a3 = a(str2, file.getAbsolutePath()); if (a3 != -1) { a(a3, file.getAbsolutePath()); File file2 = new File(com.sigmob.sdk.base.utils.b.f(), a3 + ".log"); baseAdUnit.setDownloadId(a3); FileUtil.writeToCache(baseAdUnit, file2.getAbsolutePath()); x.a(PointCategory.DOWNLOAD_START, "1", baseAdUnit, new x.a() { /* class com.sigmob.sdk.base.common.l.AnonymousClass1 */ @Override // com.sigmob.sdk.base.common.x.a public void a(Object obj) { if (obj instanceof PointEntitySigmob) { PointEntitySigmob pointEntitySigmob = (PointEntitySigmob) obj; pointEntitySigmob.setFinal_url(str2); HashMap hashMap = new HashMap(); hashMap.put("apkfile", file.getAbsolutePath()); hashMap.put("apkurl", str2); pointEntitySigmob.setOptions(hashMap); } } }); hashMap = new HashMap(); hashMap.put("result", "1"); hashMap.put("downloadId", Long.valueOf(a3)); uuid = baseAdUnit.getUuid(); } else { x.a(PointCategory.DOWNLOAD_START, "0", baseAdUnit, new x.a() { /* class com.sigmob.sdk.base.common.l.AnonymousClass2 */ @Override // com.sigmob.sdk.base.common.x.a public void a(Object obj) { if (obj instanceof PointEntitySigmob) { PointEntitySigmob pointEntitySigmob = (PointEntitySigmob) obj; pointEntitySigmob.setFinal_url(str2); HashMap hashMap = new HashMap(); hashMap.put("apkfile", file.getAbsolutePath()); hashMap.put("apkurl", str2); pointEntitySigmob.setOptions(hashMap); } } }); if (!ClientMetadata.isPermissionGranted(b2, h.j)) { Toast.makeText(b2, "请先给予应用 WRITE_EXTERNAL_STORAGE 权限", 1).show(); x.a(PointCategory.DOWNLOAD_FAILED, 1, " No permission Manifest.permission.WRITE_EXTERNAL_STORAGE ", baseAdUnit); } else { x.a(PointCategory.DOWNLOAD_FAILED, 2, "download Directory not exist", baseAdUnit); } hashMap = new HashMap(); hashMap.put("result", "0"); hashMap.put("downloadId", -1); uuid = baseAdUnit.getUuid(); } BaseBroadcastReceiver.a(b2, uuid, hashMap, IntentActions.ACTION_INTERSTITIAL_DOWNLOAD_START); e.a(baseAdUnit, a.AD_DOWNLOAD_START); return; } try { Toast.makeText(b2, str4 + "当前正在努力下载,请稍等", 1).show(); baseAdUnit.setDownloadId(c.longValue()); } catch (Throwable unused) { } } catch (Throwable unused2) { } } public static boolean a(long j, String str) { if (TextUtils.isEmpty(str)) { return false; } b.add(new a(str, null, j)); return true; } public static boolean a(Context context, String str) { try { return context.getPackageManager().getPackageArchiveInfo(str, 1) != null; } catch (Exception unused) { return false; } } public static boolean a(String str) { if (TextUtils.isEmpty(str)) { return false; } DownloadManager downloadManager = (DownloadManager) b.b().getSystemService("download"); Cursor cursor = null; try { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterByStatus(3); Cursor query2 = downloadManager.query(query); if (!query2.moveToFirst()) { query2.close(); if (query2 != null) { query2.close(); } return false; } new ArrayList(); do { String string = query2.getString(query2.getColumnIndex("uri")); if (!TextUtils.isEmpty(string) && str.equalsIgnoreCase(string)) { if (query2 != null) { query2.close(); } return true; } } while (query2.moveToNext()); query2.close(); return false; } catch (Throwable th) { if (0 != 0) { cursor.close(); } throw th; } } public static long[] a(Context context, Long l) { long[] jArr = {-1, -1, 0}; if (l == null || l.longValue() < 0) { return jArr; } Cursor cursor = null; try { cursor = ((DownloadManager) context.getSystemService("download")).query(new DownloadManager.Query().setFilterById(l.longValue())); if (cursor != null && cursor.moveToFirst()) { jArr[0] = (long) cursor.getInt(cursor.getColumnIndexOrThrow("bytes_so_far")); jArr[1] = (long) cursor.getInt(cursor.getColumnIndexOrThrow("total_size")); jArr[2] = (long) cursor.getInt(cursor.getColumnIndex("status")); } return jArr; } finally { if (cursor != null) { cursor.close(); } } } /* JADX WARNING: Removed duplicated region for block: B:3:0x000c */ public static void b(long j, String str) { for (a aVar : b) { if (aVar.c == j || aVar.a.equalsIgnoreCase(str)) { b.remove(aVar); return; } while (r0.hasNext()) { } } } /* JADX WARNING: Removed duplicated region for block: B:3:0x000c */ public static Long c(long j, String str) { for (a aVar : b) { if (aVar.c == j || aVar.a.equalsIgnoreCase(str)) { return Long.valueOf(aVar.c); } while (r0.hasNext()) { } } return null; } }