package com.vungle.ads.internal.signals; import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; import androidx.annotation.VisibleForTesting; import com.vungle.ads.ServiceLocator; import com.vungle.ads.internal.ConfigManager; import com.vungle.ads.internal.executor.Executors; import com.vungle.ads.internal.model.UnclosedAd; import com.vungle.ads.internal.persistence.FilePreferences; import com.vungle.ads.internal.session.UnclosedAdDetector; import com.vungle.ads.internal.util.ActivityManager; import com.vungle.ads.internal.util.Logger; import com.vungle.ads.internal.util.PathProvider; import java.util.concurrent.ConcurrentHashMap; import kotlin.Lazy; import kotlin.LazyKt__LazyJVMKt; import kotlin.LazyThreadSafetyMode; import kotlin.Unit; import kotlin.jvm.functions.Function0; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.Reflection; import kotlinx.serialization.SerializationStrategy; import kotlinx.serialization.SerializersKt; import kotlinx.serialization.StringFormat; import kotlinx.serialization.json.Json; import kotlinx.serialization.json.JsonBuilder; import kotlinx.serialization.json.JsonKt; /* loaded from: classes4.dex */ public final class SignalManager { public static final Companion Companion = new Companion(null); public static final String SESSION_COUNT_KEY = "vungle_signal_session_count"; private static final int SESSION_COUNT_NOT_SET = -1; public static final String SESSION_TIME_KEY = "vungle_signal_session_creation_time"; public static final int SIGNAL_VERSION = 1; private static final String TAG = "SignalManager"; public static final long TWENTY_FOUR_HOURS_MILLIS = 86400000; private final Context context; private SessionData currentSession; private long enterBackgroundTime; private long enterForegroundTime; private final Lazy filePreferences$delegate; private final Json json; private ConcurrentHashMap mapOfLastLoadTimes; private int sessionCount; private long sessionDuration; private long sessionSeriesCreatedTime; private UnclosedAdDetector unclosedAdDetector; @VisibleForTesting(otherwise = 2) public static /* synthetic */ void getCurrentSession$vungle_ads_release$annotations() { } public final Context getContext() { return this.context; } public final SessionData getCurrentSession$vungle_ads_release() { return this.currentSession; } public final long getEnterBackgroundTime() { return this.enterBackgroundTime; } public final long getEnterForegroundTime() { return this.enterForegroundTime; } public final ConcurrentHashMap getMapOfLastLoadTimes() { return this.mapOfLastLoadTimes; } public final int getSessionCount() { return this.sessionCount; } public final long getSessionDuration() { return this.sessionDuration; } public final long getSessionSeriesCreatedTime() { return this.sessionSeriesCreatedTime; } public final void setCurrentSession$vungle_ads_release(SessionData sessionData) { Intrinsics.checkNotNullParameter(sessionData, ""); this.currentSession = sessionData; } public final void setEnterBackgroundTime(long j) { this.enterBackgroundTime = j; } public final void setEnterForegroundTime(long j) { this.enterForegroundTime = j; } public final void setMapOfLastLoadTimes(ConcurrentHashMap concurrentHashMap) { Intrinsics.checkNotNullParameter(concurrentHashMap, ""); this.mapOfLastLoadTimes = concurrentHashMap; } public final void setSessionCount(int i) { this.sessionCount = i; } public final void setSessionDuration(long j) { this.sessionDuration = j; } public final void setSessionSeriesCreatedTime(long j) { this.sessionSeriesCreatedTime = j; } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } public SignalManager(final Context context) { Lazy lazy; Lazy lazy2; Lazy lazy3; Intrinsics.checkNotNullParameter(context, "context"); this.context = context; this.json = JsonKt.Json$default((Json) null, new Function1() { // from class: com.vungle.ads.internal.signals.SignalManager$json$1 @Override // kotlin.jvm.functions.Function1 public /* bridge */ /* synthetic */ Object invoke(Object obj) { invoke((JsonBuilder) obj); return Unit.INSTANCE; } public final void invoke(JsonBuilder Json) { Intrinsics.checkNotNullParameter(Json, "$this$Json"); Json.setIgnoreUnknownKeys(true); Json.setEncodeDefaults(true); Json.setExplicitNulls(false); } }, 1, (Object) null); this.enterForegroundTime = System.currentTimeMillis(); this.sessionCount = -1; this.mapOfLastLoadTimes = new ConcurrentHashMap<>(); ServiceLocator.Companion companion = ServiceLocator.Companion; LazyThreadSafetyMode lazyThreadSafetyMode = LazyThreadSafetyMode.SYNCHRONIZED; lazy = LazyKt__LazyJVMKt.lazy(lazyThreadSafetyMode, new Function0() { // from class: com.vungle.ads.internal.signals.SignalManager$special$$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.persistence.FilePreferences, java.lang.Object] */ @Override // kotlin.jvm.functions.Function0 public final FilePreferences invoke() { return ServiceLocator.Companion.getInstance(context).getService(FilePreferences.class); } }); this.filePreferences$delegate = lazy; registerNotifications(); this.sessionSeriesCreatedTime = getFilePreferences().getLong(SESSION_TIME_KEY, -1L); updateSessionCount(); this.currentSession = new SessionData(this.sessionCount); lazy2 = LazyKt__LazyJVMKt.lazy(lazyThreadSafetyMode, new Function0() { // from class: com.vungle.ads.internal.signals.SignalManager$special$$inlined$inject$2 /* 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.executor.Executors, java.lang.Object] */ @Override // kotlin.jvm.functions.Function0 public final Executors invoke() { return ServiceLocator.Companion.getInstance(context).getService(Executors.class); } }); lazy3 = LazyKt__LazyJVMKt.lazy(lazyThreadSafetyMode, new Function0() { // from class: com.vungle.ads.internal.signals.SignalManager$special$$inlined$inject$3 /* 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.util.PathProvider, java.lang.Object] */ @Override // kotlin.jvm.functions.Function0 public final PathProvider invoke() { return ServiceLocator.Companion.getInstance(context).getService(PathProvider.class); } }); UnclosedAdDetector unclosedAdDetector = new UnclosedAdDetector(context, this.currentSession.getSessionId(), m3970_init_$lambda0(lazy2), m3971_init_$lambda1(lazy3)); this.unclosedAdDetector = unclosedAdDetector; this.currentSession.setUnclosedAd(unclosedAdDetector.retrieveUnclosedAd()); } public final FilePreferences getFilePreferences() { return (FilePreferences) this.filePreferences$delegate.getValue(); } public final String getUuid() { return this.currentSession.getSessionId(); } /* renamed from: _init_$lambda-0, reason: not valid java name */ private static final Executors m3970_init_$lambda0(Lazy lazy) { return (Executors) lazy.getValue(); } /* renamed from: _init_$lambda-1, reason: not valid java name */ private static final PathProvider m3971_init_$lambda1(Lazy lazy) { return (PathProvider) lazy.getValue(); } private final void updateSessionCount() { if (this.sessionCount == -1) { this.sessionCount = getFilePreferences().getInt(SESSION_COUNT_KEY, 0); } long currentTimeMillis = System.currentTimeMillis(); long j = this.sessionSeriesCreatedTime; long j2 = currentTimeMillis - j; if (j < 0 || j2 >= TWENTY_FOUR_HOURS_MILLIS) { this.sessionCount = 1; getFilePreferences().put(SESSION_TIME_KEY, currentTimeMillis); this.sessionSeriesCreatedTime = currentTimeMillis; } else { this.sessionCount++; } getFilePreferences().put(SESSION_COUNT_KEY, this.sessionCount); getFilePreferences().apply(); } private final void registerNotifications() { ActivityManager.Companion.addLifecycleListener(new ActivityManager.LifeCycleCallback() { // from class: com.vungle.ads.internal.signals.SignalManager$registerNotifications$1 @Override // com.vungle.ads.internal.util.ActivityManager.LifeCycleCallback public void onResume() { super.onResume(); if (SignalManager.this.getEnterBackgroundTime() == 0) { Logger.Companion.d("SignalManager", "SignalManager#onResume skipped"); return; } long currentTimeMillis = System.currentTimeMillis(); if (currentTimeMillis - SignalManager.this.getEnterBackgroundTime() > ConfigManager.INSTANCE.getSignalsSessionTimeout()) { SignalManager.this.createNewSessionData(); } SignalManager.this.setEnterForegroundTime(currentTimeMillis); SignalManager.this.setEnterBackgroundTime(0L); } @Override // com.vungle.ads.internal.util.ActivityManager.LifeCycleCallback public void onPause() { super.onPause(); SignalManager.this.setEnterBackgroundTime(System.currentTimeMillis()); SignalManager signalManager = SignalManager.this; signalManager.setSessionDuration(signalManager.getSessionDuration() + (SignalManager.this.getEnterBackgroundTime() - SignalManager.this.getEnterForegroundTime())); } }); } public final void createNewSessionData() { updateSessionCount(); this.currentSession = new SessionData(this.sessionCount); } public final synchronized SignaledAd getSignaledAd(String placementId) { long currentTimeMillis; Long l; try { Intrinsics.checkNotNullParameter(placementId, "placementId"); currentTimeMillis = System.currentTimeMillis(); l = this.mapOfLastLoadTimes.containsKey(placementId) ? this.mapOfLastLoadTimes.get(placementId) : null; this.mapOfLastLoadTimes.put(placementId, Long.valueOf(currentTimeMillis)); } catch (Throwable th) { throw th; } return new SignaledAd(l, currentTimeMillis); } public final synchronized void increaseSessionDepthCounter() { SessionData sessionData = this.currentSession; sessionData.setSessionDepthCounter(sessionData.getSessionDepthCounter() + 1); } public final void recordUnclosedAd(UnclosedAd unclosedAd) { Intrinsics.checkNotNullParameter(unclosedAd, "unclosedAd"); if (ConfigManager.INSTANCE.signalsDisabled()) { return; } this.unclosedAdDetector.addUnclosedAd(unclosedAd); } public final void removeUnclosedAd(UnclosedAd unclosedAd) { Intrinsics.checkNotNullParameter(unclosedAd, "unclosedAd"); if (ConfigManager.INSTANCE.signalsDisabled()) { return; } this.unclosedAdDetector.removeUnclosedAd(unclosedAd); } public final void registerSignaledAd(Context context, SignaledAd signaledAd) { Intrinsics.checkNotNullParameter(signaledAd, "signaledAd"); this.currentSession.getSignaledAd().clear(); this.currentSession.getSignaledAd().add(signaledAd); this.currentSession.getSignaledAd().get(0).setScreenOrientation(screenOrientation(context)); } public final void updateTemplateSignals(String str) { if (str == null || str.length() == 0 || !(!this.currentSession.getSignaledAd().isEmpty())) { return; } this.currentSession.getSignaledAd().get(0).setTemplateSignals(str); } private final void updateSessionDuration() { this.currentSession.setSessionDuration((this.sessionDuration + System.currentTimeMillis()) - this.enterForegroundTime); } public final String generateSignals() { updateSessionDuration(); try { StringBuilder sb = new StringBuilder(); sb.append("1:"); StringFormat stringFormat = this.json; SessionData sessionData = this.currentSession; SerializationStrategy serializer = SerializersKt.serializer(stringFormat.getSerializersModule(), Reflection.typeOf(SessionData.class)); Intrinsics.checkNotNull(serializer, "null cannot be cast to non-null type kotlinx.serialization.KSerializer"); sb.append(stringFormat.encodeToString(serializer, sessionData)); return sb.toString(); } catch (Error | Exception unused) { return null; } } public final int screenOrientation(Context context) { Configuration configuration; if (context == null) { context = this.context; } Resources resources = context.getResources(); Integer valueOf = (resources == null || (configuration = resources.getConfiguration()) == null) ? null : Integer.valueOf(configuration.orientation); if (valueOf != null && valueOf.intValue() == 2) { return 2; } if (valueOf != null && valueOf.intValue() == 1) { return 1; } return (valueOf != null && valueOf.intValue() == 0) ? 0 : -1; } }