package csdk.gluads; import android.content.Context; import android.content.SharedPreferences; import androidx.annotation.NonNull; import com.ironsource.mediationsdk.utils.IronSourceConstants; import csdk.gluads.util.Common; import csdk.gluads.util.ConfigUtil; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /* loaded from: classes4.dex */ public class FrequencyController { private static final String AD_TYPE_FREQUENCY_CAP_KEY = "adTypeFrequencyCapKey"; private static final String AD_TYPE_FREQUENCY_COUNT_KEY = "adTypeFrequencyCountKey"; private static final String AD_TYPE_KEY = "type"; private static final String CROSS_PROMO_FREQUENCY_CAP_KEY = "crossPromoFrequencyCapKey"; private static final String CROSS_PROMO_FREQUENCY_COUNT_KEY = "crossPromoFrequencyCountKey"; private static final String CROSS_PROMO_FREQUENCY_SESSION_KEY = "crossPromoFrequencySessionKey"; private static final String FREQUENCY_CAP = "cap"; private static final String FREQUENCY_CAP_KEY = "frequencyCapKey"; private static final String FREQUENCY_COUNT_KEY = "frequencyCountKey"; private static final String FREQUENCY_RESET_TIMESTAMP = "resetTimeStamp"; private static final String FREQUENCY_SESSION = "session"; private static final String FREQUENCY_SESSION_KEY = "frequencySessionKey"; private static final String FREQUENCY_SPACING = "spacing"; private static final String FREQUENCY_TIMESTAMP = "timestamp"; private static final String SHARED_PREFS_NAME = "gluFrequency"; private Map mAdTypeFrequencyCapCache; private Map mAdTypeFrequencyCountCache; private final String mAdvertisementType; private Map mCrossPromoFrequencyCapCache; private Map mCrossPromoFrequencyCountCache; private Map mCrossPromoFrequencySessionCache; private Map mFrequencyCapCache; private Map mFrequencyCountCache; private Map mFrequencySessionCache; private long mLastRulesRefreshRequestTS; private final SharedPreferences mSharedPrefs; private boolean mShouldIgnoreGlobalFreqCapping; private Map mShowCount; private static void log(String str) { } public void enableGlobalFrequencyCapping() { this.mShouldIgnoreGlobalFreqCapping = false; } public FrequencyController(String str, Context context, Map map) { this.mAdvertisementType = str; SharedPreferences sharedPreferences = context.getSharedPreferences(getSharedPrefsNameByAdType(str, SHARED_PREFS_NAME), 0); this.mSharedPrefs = sharedPreferences; this.mFrequencyCountCache = Common.loadMap(sharedPreferences, FREQUENCY_COUNT_KEY); this.mFrequencyCapCache = Common.loadMap(sharedPreferences, FREQUENCY_CAP_KEY); this.mFrequencySessionCache = Common.loadMap(sharedPreferences, FREQUENCY_SESSION_KEY); if (map != null && this.mFrequencyCapCache.size() == 0) { this.mFrequencyCapCache = ConfigUtil.getMap(map, str, Collections.EMPTY_MAP); } this.mCrossPromoFrequencyCountCache = Common.loadMap(sharedPreferences, CROSS_PROMO_FREQUENCY_COUNT_KEY); this.mCrossPromoFrequencyCapCache = Common.loadMap(sharedPreferences, CROSS_PROMO_FREQUENCY_CAP_KEY); this.mCrossPromoFrequencySessionCache = Common.loadMap(sharedPreferences, CROSS_PROMO_FREQUENCY_SESSION_KEY); this.mShowCount = new ConcurrentHashMap(); this.mAdTypeFrequencyCountCache = Common.loadMap(sharedPreferences, AD_TYPE_FREQUENCY_COUNT_KEY); Map loadMap = Common.loadMap(sharedPreferences, AD_TYPE_FREQUENCY_CAP_KEY); this.mAdTypeFrequencyCapCache = loadMap; if (map != null && loadMap.size() == 0) { this.mAdTypeFrequencyCapCache = ConfigUtil.getMap(map, "type", Collections.EMPTY_MAP); } this.mShouldIgnoreGlobalFreqCapping = true; this.mLastRulesRefreshRequestTS = 0L; } private boolean internal_isLimitReached(@NonNull String str) { boolean z; synchronized (this.mSharedPrefs) { long longValue = getLongValue(this.mFrequencyCountCache, str); long longValue2 = getLongValue(this.mFrequencyCapCache.get(str), FREQUENCY_CAP); z = longValue2 > 0 && longValue >= longValue2; } return z; } private boolean internal_isLimitReachedForAdType() { boolean z; synchronized (this.mSharedPrefs) { long longValue = getLongValue(this.mAdTypeFrequencyCountCache, this.mAdvertisementType); long longValue2 = getLongValue(this.mAdTypeFrequencyCapCache.get(this.mAdvertisementType), FREQUENCY_CAP); z = longValue2 > 0 && longValue >= longValue2; } return z; } public boolean isLimitReached(@NonNull String str) { return (!this.mShouldIgnoreGlobalFreqCapping && internal_isLimitReachedForAdType()) || internal_isLimitReached(str); } public boolean shouldShowCrossPromo(@NonNull String str) { synchronized (this.mSharedPrefs) { try { long longValue = getLongValue(this.mShowCount, str); long longValue2 = getLongValue(this.mCrossPromoFrequencyCapCache.get(str), FREQUENCY_SPACING); if (longValue2 != 0 && longValue % (1 + longValue2) != longValue2) { return false; } long longValue3 = getLongValue(this.mCrossPromoFrequencyCountCache, str); long longValue4 = getLongValue(this.mCrossPromoFrequencyCapCache.get(str), FREQUENCY_CAP); return longValue4 == 0 || longValue3 < longValue4; } catch (Throwable th) { throw th; } } } public void onNewSession() { synchronized (this.mSharedPrefs) { try { boolean z = false; if (this.mFrequencyCapCache.size() > 0) { boolean z2 = false; for (Map.Entry entry : this.mFrequencyCapCache.entrySet()) { String key = entry.getKey(); long longValue = getLongValue(entry.getValue(), FREQUENCY_SESSION); if (longValue > 0) { long longValue2 = getLongValue(this.mFrequencySessionCache, key); if (longValue2 < longValue) { this.mFrequencySessionCache.put(key, Long.valueOf(longValue2 + 1)); } else { this.mFrequencySessionCache.remove(key); if (this.mFrequencyCountCache.containsKey(key)) { this.mFrequencyCountCache.remove(key); Common.saveMap(this.mSharedPrefs, FREQUENCY_COUNT_KEY, this.mFrequencyCountCache); } } z2 = true; } } if (z2) { Common.saveMap(this.mSharedPrefs, FREQUENCY_SESSION_KEY, this.mFrequencySessionCache); } } if (this.mCrossPromoFrequencyCapCache.size() > 0) { for (Map.Entry entry2 : this.mCrossPromoFrequencyCapCache.entrySet()) { String key2 = entry2.getKey(); long longValue3 = getLongValue(entry2.getValue(), FREQUENCY_SESSION); if (longValue3 > 0) { long longValue4 = getLongValue(this.mCrossPromoFrequencySessionCache, key2); if (longValue4 < longValue3) { this.mCrossPromoFrequencySessionCache.put(key2, Long.valueOf(longValue4 + 1)); } else { this.mCrossPromoFrequencySessionCache.remove(key2); if (this.mCrossPromoFrequencyCountCache.containsKey(key2)) { this.mCrossPromoFrequencyCountCache.remove(key2); Common.saveMap(this.mSharedPrefs, CROSS_PROMO_FREQUENCY_COUNT_KEY, this.mCrossPromoFrequencyCountCache); } } z = true; } } if (z) { Common.saveMap(this.mSharedPrefs, CROSS_PROMO_FREQUENCY_SESSION_KEY, this.mCrossPromoFrequencySessionCache); } } } catch (Throwable th) { throw th; } } } public void update(@NonNull Map map) { boolean z; synchronized (this.mSharedPrefs) { try { Map map2 = ConfigUtil.getMap(map, this.mAdvertisementType, Collections.EMPTY_MAP); if (map2.isEmpty()) { this.mFrequencyCountCache.clear(); this.mFrequencyCapCache.clear(); Common.saveMap(this.mSharedPrefs, FREQUENCY_COUNT_KEY, this.mFrequencyCountCache); Common.saveMap(this.mSharedPrefs, FREQUENCY_CAP_KEY, this.mFrequencyCapCache); } else { Map createMap = Common.createMap(); createMap.putAll(map2); for (Map.Entry entry : createMap.entrySet()) { String key = entry.getKey(); if (getLongValue(entry.getValue(), FREQUENCY_SESSION) > 0 || getLongValue(entry.getValue(), "timestamp") == getLongValue(this.mFrequencyCapCache.get(key), "timestamp")) { z = false; } else { if (this.mFrequencyCountCache.containsKey(key)) { this.mFrequencyCountCache.remove(key); Common.saveMap(this.mSharedPrefs, FREQUENCY_COUNT_KEY, this.mFrequencyCountCache); } z = true; } Map map3 = (Map) Common.get((Map) entry.getValue(), "crossPromo"); if (map3 != null) { if ((getLongValue(map3, FREQUENCY_SESSION) <= 0 || z) && ((getLongValue(map3, "timestamp") != getLongValue(this.mCrossPromoFrequencyCapCache.get(key), "timestamp") || z) && this.mCrossPromoFrequencyCountCache.containsKey(key))) { this.mCrossPromoFrequencyCountCache.remove(key); Common.saveMap(this.mSharedPrefs, CROSS_PROMO_FREQUENCY_COUNT_KEY, this.mCrossPromoFrequencyCountCache); } this.mCrossPromoFrequencyCapCache.put(key, map3); } } this.mFrequencyCapCache = createMap; Common.saveMap(this.mSharedPrefs, FREQUENCY_CAP_KEY, createMap); Common.saveMap(this.mSharedPrefs, CROSS_PROMO_FREQUENCY_CAP_KEY, this.mCrossPromoFrequencyCapCache); } Map map4 = ConfigUtil.getMap(map, "type", Collections.EMPTY_MAP); if (map4.isEmpty()) { this.mAdTypeFrequencyCountCache.clear(); this.mAdTypeFrequencyCapCache.clear(); Common.saveMap(this.mSharedPrefs, AD_TYPE_FREQUENCY_COUNT_KEY, this.mAdTypeFrequencyCountCache); Common.saveMap(this.mSharedPrefs, AD_TYPE_FREQUENCY_CAP_KEY, this.mAdTypeFrequencyCapCache); } else { Map createMap2 = Common.createMap(); createMap2.putAll(map4); for (Map.Entry entry2 : createMap2.entrySet()) { String str = (String) entry2.getKey(); if (str.equals(this.mAdvertisementType) && getLongValue(entry2.getValue(), FREQUENCY_SESSION) <= 0 && getLongValue(entry2.getValue(), FREQUENCY_RESET_TIMESTAMP) != getLongValue(this.mAdTypeFrequencyCapCache.get(str), FREQUENCY_RESET_TIMESTAMP) && this.mAdTypeFrequencyCountCache.containsKey(str)) { this.mAdTypeFrequencyCountCache.remove(str); Common.saveMap(this.mSharedPrefs, AD_TYPE_FREQUENCY_COUNT_KEY, this.mAdTypeFrequencyCountCache); } } this.mAdTypeFrequencyCapCache = map4; Common.saveMap(this.mSharedPrefs, AD_TYPE_FREQUENCY_CAP_KEY, map4); } } catch (Throwable th) { throw th; } } } public long getCurrentServerTime() { return System.currentTimeMillis() / 1000; } private boolean internal_isFreqCountRefreshRequiredForAdType() { long longValue = getLongValue(this.mAdTypeFrequencyCapCache.get(this.mAdvertisementType), FREQUENCY_RESET_TIMESTAMP); if (longValue <= 0) { return false; } long currentServerTime = getCurrentServerTime(); if (currentServerTime <= longValue || this.mLastRulesRefreshRequestTS >= currentServerTime) { return false; } this.mLastRulesRefreshRequestTS = currentServerTime + ConfigUtil.getLong(this.mAdTypeFrequencyCapCache.get(this.mAdvertisementType), "minTimeToNextRefreshInSeconds", 60L); return true; } public boolean isFreqCountRefreshRequired(@NonNull String str) { return !this.mShouldIgnoreGlobalFreqCapping && internal_isFreqCountRefreshRequiredForAdType(); } private void internal_increaseCount(boolean z, @NonNull String str) { synchronized (this.mSharedPrefs) { try { long longValue = getLongValue(this.mFrequencyCountCache, str); long longValue2 = getLongValue(this.mFrequencyCapCache.get(str), FREQUENCY_CAP); if (longValue2 != 0 && longValue < longValue2) { this.mFrequencyCountCache.put(str, Long.valueOf(longValue + 1)); Common.saveMap(this.mSharedPrefs, FREQUENCY_COUNT_KEY, this.mFrequencyCountCache); } if (z) { long longValue3 = getLongValue(this.mCrossPromoFrequencyCountCache, str); long longValue4 = getLongValue(this.mCrossPromoFrequencyCapCache.get(str), FREQUENCY_CAP); if (longValue4 != 0 && longValue3 < longValue4) { this.mCrossPromoFrequencyCountCache.put(str, Long.valueOf(longValue3 + 1)); Common.saveMap(this.mSharedPrefs, CROSS_PROMO_FREQUENCY_COUNT_KEY, this.mCrossPromoFrequencyCountCache); } } } catch (Throwable th) { throw th; } } Map map = this.mShowCount; map.put(str, Long.valueOf(getLongValue(map, str) + 1)); } private void internal_increaseCountForAdType() { synchronized (this.mSharedPrefs) { try { long longValue = getLongValue(this.mAdTypeFrequencyCountCache, this.mAdvertisementType); long longValue2 = getLongValue(this.mAdTypeFrequencyCapCache.get(this.mAdvertisementType), FREQUENCY_CAP); if (longValue2 != 0 && longValue < longValue2) { this.mAdTypeFrequencyCountCache.put(this.mAdvertisementType, Long.valueOf(longValue + 1)); Common.saveMap(this.mSharedPrefs, AD_TYPE_FREQUENCY_COUNT_KEY, this.mAdTypeFrequencyCountCache); } } catch (Throwable th) { throw th; } } } public void increaseCount(boolean z, @NonNull String str) { if (!this.mShouldIgnoreGlobalFreqCapping) { internal_increaseCountForAdType(); } internal_increaseCount(z, str); } private static long getLongValue(Object obj, String str) { Object obj2; if (str == null || obj == null || !(obj instanceof Map) || (obj2 = ((Map) obj).get(str)) == null || !(obj2 instanceof Number)) { return 0L; } return ((Number) obj2).longValue(); } private static String getSharedPrefsNameByAdType(@NonNull String str, @NonNull String str2) { if (str.equals("interstitial")) { return str2 + "INT"; } if (!str.equals(Consts.ADVERTISEMENT_TYPE_REWARDED_INTERSTITIAL)) { return str2; } return str2 + IronSourceConstants.REWARDED_VIDEO_EVENT_TYPE; } }