package com.vungle.ads.internal.load; import android.content.Context; import android.webkit.URLUtil; import androidx.annotation.WorkerThread; import com.vungle.ads.AnalyticsClient; import com.vungle.ads.AssetDownloadError; import com.vungle.ads.InternalError; import com.vungle.ads.MraidJsError; import com.vungle.ads.NativeAdInternal; import com.vungle.ads.ServiceLocator; import com.vungle.ads.SingleValueMetric; import com.vungle.ads.TimeIntervalMetric; import com.vungle.ads.VungleError; import com.vungle.ads.internal.ConfigManager; import com.vungle.ads.internal.Constants; import com.vungle.ads.internal.downloader.AssetDownloadListener; import com.vungle.ads.internal.downloader.DownloadRequest; import com.vungle.ads.internal.downloader.Downloader; import com.vungle.ads.internal.executor.Executors; import com.vungle.ads.internal.load.MraidJsLoader; import com.vungle.ads.internal.model.AdAsset; import com.vungle.ads.internal.model.AdPayload; import com.vungle.ads.internal.model.ConfigPayload; import com.vungle.ads.internal.network.TpatSender; import com.vungle.ads.internal.network.VungleApiClient; import com.vungle.ads.internal.omsdk.OMInjector; import com.vungle.ads.internal.protos.Sdk; import com.vungle.ads.internal.signals.SignalManager; import com.vungle.ads.internal.util.FileUtility; import com.vungle.ads.internal.util.Logger; import com.vungle.ads.internal.util.PathProvider; import com.vungle.ads.internal.util.UnzipUtility; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import kotlin.Lazy; import kotlin.LazyKt__LazyJVMKt; import kotlin.LazyThreadSafetyMode; import kotlin.io.FilesKt__UtilsKt; import kotlin.jvm.functions.Function0; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.text.StringsKt__StringsJVMKt; /* loaded from: classes4.dex */ public abstract class BaseAdLoader { public static final Companion Companion = new Companion(null); private static final String DOWNLOADED_FILE_NOT_FOUND = "Downloaded file not found!"; private static final String TAG = "BaseAdLoader"; private final List adAssets; private AdLoaderCallback adLoaderCallback; private final AdRequest adRequest; private AdPayload advertisement; private TimeIntervalMetric assetDownloadDurationMetric; private final Context context; private final AtomicLong downloadCount; private final AtomicLong downloadRequiredCount; private final Downloader downloader; private AtomicBoolean fullyDownloaded; private SingleValueMetric mainVideoSizeMetric; private AtomicBoolean notifyFailed; private AtomicBoolean notifySuccess; private final OMInjector omInjector; private final PathProvider pathProvider; private AtomicBoolean requiredAssetDownloaded; private final Executors sdkExecutors; private SingleValueMetric templateHtmlSizeMetric; private SingleValueMetric templateSizeMetric; private final VungleApiClient vungleApiClient; public final AdRequest getAdRequest() { return this.adRequest; } public final AdPayload getAdvertisement$vungle_ads_release() { return this.advertisement; } public final Context getContext() { return this.context; } public final PathProvider getPathProvider() { return this.pathProvider; } public final Executors getSdkExecutors() { return this.sdkExecutors; } public final VungleApiClient getVungleApiClient() { return this.vungleApiClient; } public abstract void onAdLoadReady(); public abstract void requestAd(); public final void setAdvertisement$vungle_ads_release(AdPayload adPayload) { this.advertisement = adPayload; } public BaseAdLoader(Context context, VungleApiClient vungleApiClient, Executors sdkExecutors, OMInjector omInjector, Downloader downloader, PathProvider pathProvider, AdRequest adRequest) { Intrinsics.checkNotNullParameter(context, "context"); Intrinsics.checkNotNullParameter(vungleApiClient, "vungleApiClient"); Intrinsics.checkNotNullParameter(sdkExecutors, "sdkExecutors"); Intrinsics.checkNotNullParameter(omInjector, "omInjector"); Intrinsics.checkNotNullParameter(downloader, "downloader"); Intrinsics.checkNotNullParameter(pathProvider, "pathProvider"); Intrinsics.checkNotNullParameter(adRequest, "adRequest"); this.context = context; this.vungleApiClient = vungleApiClient; this.sdkExecutors = sdkExecutors; this.omInjector = omInjector; this.downloader = downloader; this.pathProvider = pathProvider; this.adRequest = adRequest; this.downloadCount = new AtomicLong(0L); this.downloadRequiredCount = new AtomicLong(0L); this.notifySuccess = new AtomicBoolean(false); this.notifyFailed = new AtomicBoolean(false); this.adAssets = new ArrayList(); this.fullyDownloaded = new AtomicBoolean(true); this.requiredAssetDownloaded = new AtomicBoolean(true); this.mainVideoSizeMetric = new SingleValueMetric(Sdk.SDKMetric.SDKMetricType.ASSET_FILE_SIZE); this.templateSizeMetric = new SingleValueMetric(Sdk.SDKMetric.SDKMetricType.TEMPLATE_ZIP_SIZE); this.templateHtmlSizeMetric = new SingleValueMetric(Sdk.SDKMetric.SDKMetricType.TEMPLATE_HTML_SIZE); this.assetDownloadDurationMetric = new TimeIntervalMetric(Sdk.SDKMetric.SDKMetricType.ASSET_DOWNLOAD_DURATION_MS); } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } private final AssetDownloadListener getAssetDownloadListener() { return new BaseAdLoader$assetDownloadListener$1(this); } public final void loadAd(AdLoaderCallback adLoaderCallback) { Intrinsics.checkNotNullParameter(adLoaderCallback, "adLoaderCallback"); this.adLoaderCallback = adLoaderCallback; this.sdkExecutors.getBackgroundExecutor().execute(new Runnable() { // from class: com.vungle.ads.internal.load.BaseAdLoader$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { BaseAdLoader.m3892loadAd$lambda0(BaseAdLoader.this); } }); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: loadAd$lambda-0, reason: not valid java name */ public static final void m3892loadAd$lambda0(BaseAdLoader this$0) { Intrinsics.checkNotNullParameter(this$0, "this$0"); AnalyticsClient.INSTANCE.logMetric$vungle_ads_release(Sdk.SDKMetric.SDKMetricType.LOAD_AD_API, (r15 & 2) != 0 ? 0L : 0L, (r15 & 4) != 0 ? null : this$0.adRequest.getPlacement().getReferenceId(), (r15 & 8) != 0 ? null : null, (r15 & 16) != 0 ? null : null, (r15 & 32) == 0 ? null : null); this$0.requestAd(); } public final void cancel() { this.downloader.cancelAll(); } /* JADX INFO: Access modifiers changed from: private */ public final void downloadAssets(AdPayload adPayload) { this.assetDownloadDurationMetric.markStart(); this.downloadCount.set(this.adAssets.size()); AtomicLong atomicLong = this.downloadRequiredCount; List list = this.adAssets; ArrayList arrayList = new ArrayList(); for (Object obj : list) { if (((AdAsset) obj).isRequired()) { arrayList.add(obj); } } atomicLong.set(arrayList.size()); for (AdAsset adAsset : this.adAssets) { DownloadRequest downloadRequest = new DownloadRequest(getAssetPriority(adAsset), adAsset, this.adRequest.getPlacement().getReferenceId(), adPayload.getCreativeId(), adPayload.eventId()); if (downloadRequest.isTemplate()) { downloadRequest.startRecord(); } this.downloader.download(downloadRequest, getAssetDownloadListener()); } } public final void onAdLoadFailed(VungleError error) { AdLoaderCallback adLoaderCallback; Intrinsics.checkNotNullParameter(error, "error"); if (this.notifySuccess.get() || !this.notifyFailed.compareAndSet(false, true) || (adLoaderCallback = this.adLoaderCallback) == null) { return; } adLoaderCallback.onFailure(error); } /* JADX INFO: Access modifiers changed from: private */ public final void onAdReady() { AdPayload adPayload = this.advertisement; if (adPayload == null || this.notifyFailed.get() || !this.notifySuccess.compareAndSet(false, true)) { return; } onAdLoadReady(); AdLoaderCallback adLoaderCallback = this.adLoaderCallback; if (adLoaderCallback != null) { adLoaderCallback.onSuccess(adPayload); } } private final boolean isUrlValid(String str) { return (str == null || str.length() == 0 || (!URLUtil.isHttpsUrl(str) && !URLUtil.isHttpUrl(str))) ? false : true; } private final boolean fileIsValid(File file, AdAsset adAsset) { return file.exists() && file.length() == adAsset.getFileSize(); } private final boolean unzipFile(AdPayload adPayload, File file, File file2) { final ArrayList arrayList = new ArrayList(); for (AdAsset adAsset : this.adAssets) { if (adAsset.getFileType() == AdAsset.FileType.ASSET) { arrayList.add(adAsset.getLocalPath()); } } try { UnzipUtility unzipUtility = UnzipUtility.INSTANCE; String path = file.getPath(); String path2 = file2.getPath(); Intrinsics.checkNotNullExpressionValue(path2, "destinationDir.path"); unzipUtility.unzip(path, path2, new UnzipUtility.Filter() { // from class: com.vungle.ads.internal.load.BaseAdLoader$unzipFile$1 @Override // com.vungle.ads.internal.util.UnzipUtility.Filter public boolean matches(String str) { if (str != null && str.length() != 0) { File file3 = new File(str); Iterator it = arrayList.iterator(); while (it.hasNext()) { File file4 = new File(it.next()); if (Intrinsics.areEqual(file4, file3)) { return false; } String path3 = file3.getPath(); Intrinsics.checkNotNullExpressionValue(path3, "toExtract.path"); if (StringsKt__StringsJVMKt.startsWith$default(path3, file4.getPath() + File.separator, false, 2, null)) { return false; } } } return true; } }); if (!new File(file2.getPath(), Constants.AD_INDEX_FILE_NAME).exists()) { AnalyticsClient.INSTANCE.logError$vungle_ads_release(115, "Failed to retrieve indexFileUrl from the Ad.", this.adRequest.getPlacement().getReferenceId(), adPayload.getCreativeId(), adPayload.eventId()); return false; } FileUtility.delete(file); return true; } catch (Exception e) { AnalyticsClient.INSTANCE.logError$vungle_ads_release(109, "Unzip failed: " + e.getMessage(), this.adRequest.getPlacement().getReferenceId(), adPayload.getCreativeId(), adPayload.eventId()); return false; } } private final File getDestinationDir(AdPayload adPayload) { return this.pathProvider.getDownloadsDirForAd(adPayload.eventId()); } private final boolean injectMraidJS(File file) { try { File file2 = new File(file.getPath(), Constants.AD_MRAID_JS_FILE_NAME); File file3 = new File(this.pathProvider.getJsAssetDir(ConfigManager.INSTANCE.getMraidJsVersion()), Constants.MRAID_JS_FILE_NAME); if (!file3.exists()) { return true; } FilesKt__UtilsKt.copyTo$default(file3, file2, true, 0, 4, null); return true; } catch (Exception e) { Logger.Companion.e(TAG, "Failed to inject mraid.js: " + e.getMessage()); return false; } } /* JADX INFO: Access modifiers changed from: private */ public final boolean processVmTemplate(AdAsset adAsset, AdPayload adPayload) { if (adPayload == null || adAsset.getStatus() != AdAsset.Status.DOWNLOAD_SUCCESS || adAsset.getLocalPath().length() == 0) { return false; } File file = new File(adAsset.getLocalPath()); if (!fileIsValid(file, adAsset)) { return false; } File destinationDir = getDestinationDir(adPayload); if (destinationDir == null || !destinationDir.isDirectory()) { Logger.Companion.e(TAG, "Unable to access Destination Directory"); return false; } if (adAsset.getFileType() == AdAsset.FileType.ZIP && !unzipFile(adPayload, file, destinationDir)) { return false; } if (adPayload.omEnabled()) { try { this.omInjector.injectJsFiles(destinationDir); } catch (Exception e) { Logger.Companion.e(TAG, "Failed to inject OMSDK: " + e.getMessage()); return false; } } boolean injectMraidJS = injectMraidJS(destinationDir); FileUtility.printDirectoryTree(destinationDir); return injectMraidJS; } @WorkerThread public void onDownloadCompleted(AdRequest request, String str) { Intrinsics.checkNotNullParameter(request, "request"); Logger.Companion.d(TAG, "download completed " + request); AdPayload adPayload = this.advertisement; if (adPayload != null) { adPayload.setAssetFullyDownloaded(); } onAdReady(); this.assetDownloadDurationMetric.markEnd(); AdPayload adPayload2 = this.advertisement; String placementId = adPayload2 != null ? adPayload2.placementId() : null; AdPayload adPayload3 = this.advertisement; String creativeId = adPayload3 != null ? adPayload3.getCreativeId() : null; AdPayload adPayload4 = this.advertisement; AnalyticsClient.logMetric$vungle_ads_release$default(AnalyticsClient.INSTANCE, this.assetDownloadDurationMetric, placementId, creativeId, adPayload4 != null ? adPayload4.eventId() : null, (String) null, 16, (Object) null); } public static /* synthetic */ void handleAdMetaData$vungle_ads_release$default(BaseAdLoader baseAdLoader, AdPayload adPayload, SingleValueMetric singleValueMetric, int i, Object obj) { if (obj != null) { throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: handleAdMetaData"); } if ((i & 2) != 0) { singleValueMetric = null; } baseAdLoader.handleAdMetaData$vungle_ads_release(adPayload, singleValueMetric); } public final void handleAdMetaData$vungle_ads_release(final AdPayload advertisement, SingleValueMetric singleValueMetric) { Lazy lazy; List loadAdUrls; Intrinsics.checkNotNullParameter(advertisement, "advertisement"); this.advertisement = advertisement; ConfigPayload config = advertisement.config(); if (config != null) { ConfigManager.INSTANCE.initWithConfig$vungle_ads_release(this.context, config, false, singleValueMetric); } ErrorInfo validateAdMetadata = validateAdMetadata(advertisement); if (validateAdMetadata != null) { AnalyticsClient.INSTANCE.logError$vungle_ads_release(validateAdMetadata.getReason(), validateAdMetadata.getDescription(), this.adRequest.getPlacement().getReferenceId(), advertisement.getCreativeId(), advertisement.eventId()); onAdLoadFailed(new InternalError(validateAdMetadata.getReason(), validateAdMetadata.getDescriptionExternal())); return; } File destinationDir = getDestinationDir(advertisement); if (destinationDir == null || !destinationDir.isDirectory() || !destinationDir.exists()) { onAdLoadFailed(new AssetDownloadError()); return; } ServiceLocator.Companion companion = ServiceLocator.Companion; final Context context = this.context; lazy = LazyKt__LazyJVMKt.lazy(LazyThreadSafetyMode.SYNCHRONIZED, new Function0() { // from class: com.vungle.ads.internal.load.BaseAdLoader$handleAdMetaData$$inlined$inject$1 /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(0); } /* JADX WARN: Type inference failed for: r0v2, types: [com.vungle.ads.internal.signals.SignalManager, java.lang.Object] */ @Override // kotlin.jvm.functions.Function0 public final SignalManager invoke() { return ServiceLocator.Companion.getInstance(context).getService(SignalManager.class); } }); AdPayload.AdUnit adUnit = advertisement.adUnit(); if (adUnit != null && (loadAdUrls = adUnit.getLoadAdUrls()) != null) { TpatSender tpatSender = new TpatSender(this.vungleApiClient, advertisement.placementId(), advertisement.getCreativeId(), advertisement.eventId(), this.sdkExecutors.getIoExecutor(), this.pathProvider, m3891handleAdMetaData$lambda5(lazy)); Iterator it = loadAdUrls.iterator(); while (it.hasNext()) { tpatSender.sendTpat((String) it.next(), this.sdkExecutors.getJobExecutor()); } } if (!this.adAssets.isEmpty()) { this.adAssets.clear(); } this.adAssets.addAll(advertisement.getDownloadableAssets(destinationDir)); if (this.adAssets.isEmpty()) { onAdLoadFailed(new AssetDownloadError()); } else { MraidJsLoader.INSTANCE.downloadJs(this.pathProvider, this.downloader, this.sdkExecutors.getBackgroundExecutor(), new MraidJsLoader.DownloadResultListener() { // from class: com.vungle.ads.internal.load.BaseAdLoader$handleAdMetaData$3 @Override // com.vungle.ads.internal.load.MraidJsLoader.DownloadResultListener public void onDownloadResult(int i) { AdLoaderCallback adLoaderCallback; if (i == 10 || i == 13) { if (i == 10) { AnalyticsClient.INSTANCE.logMetric$vungle_ads_release(Sdk.SDKMetric.SDKMetricType.MRAID_DOWNLOAD_JS_RETRY_SUCCESS, (r15 & 2) != 0 ? 0L : 0L, (r15 & 4) != 0 ? null : BaseAdLoader.this.getAdRequest().getPlacement().getReferenceId(), (r15 & 8) != 0 ? null : null, (r15 & 16) != 0 ? null : null, (r15 & 32) == 0 ? null : null); } BaseAdLoader.this.downloadAssets(advertisement); } else { adLoaderCallback = BaseAdLoader.this.adLoaderCallback; if (adLoaderCallback != null) { adLoaderCallback.onFailure(new MraidJsError(null, 1, null)); } } } }); } } /* renamed from: handleAdMetaData$lambda-5, reason: not valid java name */ private static final SignalManager m3891handleAdMetaData$lambda5(Lazy lazy) { return (SignalManager) lazy.getValue(); } private final DownloadRequest.Priority getAssetPriority(AdAsset adAsset) { if (adAsset.isRequired()) { return DownloadRequest.Priority.CRITICAL; } return DownloadRequest.Priority.HIGHEST; } private final ErrorInfo validateAdMetadata(AdPayload adPayload) { AdPayload.AdUnit adUnit = adPayload.adUnit(); if ((adUnit != null ? adUnit.getSleep() : null) != null) { return getErrorInfo(adPayload); } String referenceId = this.adRequest.getPlacement().getReferenceId(); AdPayload adPayload2 = this.advertisement; if (!Intrinsics.areEqual(referenceId, adPayload2 != null ? adPayload2.placementId() : null)) { return new ErrorInfo(215, "Requests and responses don't match the placement Id.", null, false, 12, null); } AdPayload.AdUnit adUnit2 = adPayload.adUnit(); AdPayload.TemplateSettings templateSettings = adUnit2 != null ? adUnit2.getTemplateSettings() : null; if (templateSettings == null) { return new ErrorInfo(113, "Missing template settings", null, false, 12, null); } Map cacheableReplacements = templateSettings.getCacheableReplacements(); if (!adPayload.isNativeTemplateType()) { AdPayload.AdUnit adUnit3 = adPayload.adUnit(); String templateURL = adUnit3 != null ? adUnit3.getTemplateURL() : null; AdPayload.AdUnit adUnit4 = adPayload.adUnit(); String vmURL = adUnit4 != null ? adUnit4.getVmURL() : null; if ((templateURL == null || templateURL.length() == 0) && (vmURL == null || vmURL.length() == 0)) { return new ErrorInfo(105, "Failed to prepare vmURL or templateURL for downloading.", null, false, 12, null); } if (templateURL != null && templateURL.length() != 0 && !isUrlValid(templateURL)) { return new ErrorInfo(112, "Failed to load template asset.", null, false, 12, null); } if (vmURL != null && vmURL.length() != 0 && !isUrlValid(vmURL)) { return new ErrorInfo(112, "Failed to load vm url asset.", null, false, 12, null); } } else if (cacheableReplacements != null) { AdPayload.CacheableReplacement cacheableReplacement = cacheableReplacements.get(NativeAdInternal.TOKEN_MAIN_IMAGE); if ((cacheableReplacement != null ? cacheableReplacement.getUrl() : null) == null) { return new ErrorInfo(600, "Unable to load main image.", null, false, 12, null); } AdPayload.CacheableReplacement cacheableReplacement2 = cacheableReplacements.get(NativeAdInternal.TOKEN_VUNGLE_PRIVACY_ICON_URL); if ((cacheableReplacement2 != null ? cacheableReplacement2.getUrl() : null) == null) { return new ErrorInfo(600, "Unable to load privacy image.", null, false, 12, null); } } if (adPayload.hasExpired()) { return new ErrorInfo(304, "The ad markup has expired for playback.", null, false, 12, null); } String eventId = adPayload.eventId(); if (eventId == null || eventId.length() == 0) { return new ErrorInfo(200, "Event id is invalid.", null, false, 12, null); } if (cacheableReplacements != null) { Iterator> it = cacheableReplacements.entrySet().iterator(); while (it.hasNext()) { String url = it.next().getValue().getUrl(); if (url == null || url.length() == 0) { return new ErrorInfo(111, "Invalid asset URL " + url, null, false, 12, null); } if (!isUrlValid(url)) { return new ErrorInfo(112, "Invalid asset URL " + url, null, false, 12, null); } } } return null; } private final ErrorInfo getErrorInfo(AdPayload adPayload) { Integer errorCode; AdPayload.AdUnit adUnit = adPayload.adUnit(); int intValue = (adUnit == null || (errorCode = adUnit.getErrorCode()) == null) ? 212 : errorCode.intValue(); AdPayload.AdUnit adUnit2 = adPayload.adUnit(); Integer sleep = adUnit2 != null ? adUnit2.getSleep() : null; AdPayload.AdUnit adUnit3 = adPayload.adUnit(); String info = adUnit3 != null ? adUnit3.getInfo() : null; if (intValue == 10001 || intValue == 10002 || intValue == 20001 || intValue == 30001 || intValue == 30002) { return new ErrorInfo(intValue, "Response error: " + sleep, "Request failed with error: " + intValue + ", " + info, false, 8, null); } return new ErrorInfo(212, "Response error: " + sleep, "Request failed with error: 212, " + info, false, 8, null); } public static final class ErrorInfo { private final String description; private final String descriptionExternal; private final boolean errorIsTerminal; private final int reason; public final String getDescription() { return this.description; } public final String getDescriptionExternal() { return this.descriptionExternal; } public final boolean getErrorIsTerminal() { return this.errorIsTerminal; } public final int getReason() { return this.reason; } public ErrorInfo(int i, String description, String descriptionExternal, boolean z) { Intrinsics.checkNotNullParameter(description, "description"); Intrinsics.checkNotNullParameter(descriptionExternal, "descriptionExternal"); this.reason = i; this.description = description; this.descriptionExternal = descriptionExternal; this.errorIsTerminal = z; } public /* synthetic */ ErrorInfo(int i, String str, String str2, boolean z, int i2, DefaultConstructorMarker defaultConstructorMarker) { this(i, str, (i2 & 4) != 0 ? str : str2, (i2 & 8) != 0 ? false : z); } } }