Snaptube v4.40.0.4401610版本的 MD5 值为:ca2b95dcff63007195db02a6c93091b9

以下内容为反编译后的 PubnativeConfigManager.java 源代码,内容仅作参考


package net.pubnative.mediation.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.mobvista.msdk.base.common.CommonConst;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import net.pubnative.mediation.config.model.PubnativeConfigAPIResponseModel;
import net.pubnative.mediation.config.model.PubnativeConfigModel;
import net.pubnative.mediation.config.model.PubnativeConfigRequestModel;
import net.pubnative.mediation.config.model.PubnativePlacementModel;
import net.pubnative.mediation.config.model.SnaptubePubnativeConfigAPIResponseModel;
import net.pubnative.mediation.dragger.PubnativeMediationDelegate;
import net.pubnative.mediation.task.PubnativeHttpTask;
import o.agb;
import o.cds;
import o.crc;
public class PubnativeConfigManager {
    protected static final String APP_TOKEN_KEY = "?app_token=";
    protected static final String APP_TOKEN_STRING_KEY = "appToken";
    protected static final String CONFIG_STRING_KEY = "config";
    private static final int DEFAULT_MAX_REQUEST_CONTINUOUS_FAIL_COUNT = 10;
    private static final int DEFAULT_REQUEST_COUNTER_TIMEOUT = 3600;
    private static final long INVALID_START_TIME = -1;
    public static final String PREF_NAME = "pref.fan";
    public static final String PROP_CONFIG_URL = "config_url.pubnative.prop";
    protected static final String REFRESH_LONG_KEY = "refresh";
    protected static final String SHARED_PREFERENCES_CONFIG = "net.pubnative.mediation";
    protected static final String TIMESTAMP_LONG_KEY = "config.timestamp";
    private static final String VALUE_REMOVED = "_removed_";
    protected static volatile PubnativeConfigManager pubnativeConfigManager;
    @crc
    PubnativeMediationDelegate pubnativeMediationDelegate;
    private static String TAG = PubnativeConfigManager.class.getSimpleName();
    protected static List<PubnativeConfigRequestModel> sQueue = null;
    protected static boolean sIdle = true;
    private static int maxRequestContinuousFailCount = 10;
    private static int requestCounterTimeoutMs = 3600000;
    private static long startTime = -1;
    private static int continuousFailCount = 0;
    private static AtomicBoolean downloadingConfig = new AtomicBoolean(false);

    public interface ConfigFetchListener {
        void onMediationConfigFetchError(String str);

        void onMediationConfigFetchSuccess(String str);
    }

    public interface Injector {
        void injector(PubnativeConfigManager pubnativeConfigManager);
    }

    public interface Listener {
        void onConfigLoaded(PubnativeConfigModel pubnativeConfigModel);
    }

    private static PubnativeMediationDelegate getPubnativeMediationDelegate(Context context) {
        if (pubnativeConfigManager == null) {
            synchronized (PubnativeConfigManager.class) {
                if (pubnativeConfigManager == null) {
                    pubnativeConfigManager = new PubnativeConfigManager(context.getApplicationContext());
                }
            }
        }
        return pubnativeConfigManager.getPubnativeMediationDelegate();
    }

    private PubnativeConfigManager() {
    }

    public PubnativeConfigManager(Context context) {
        ((Injector) cds.m20403(context)).injector(this);
        SharedPreferences sharedPreferences = context.getSharedPreferences("pref.fan", 0);
        if (sharedPreferences != null) {
            maxRequestContinuousFailCount = sharedPreferences.getInt("/adsconfig_request_retry/max_request_continuous_fail_count", 10);
            requestCounterTimeoutMs = sharedPreferences.getInt("/adsconfig_request_retry/request_counter_timeout", 3600) * 1000;
        }
    }

    PubnativeMediationDelegate getPubnativeMediationDelegate() {
        return this.pubnativeMediationDelegate;
    }

    public static synchronized void getConfig(Context context, String str, Map<String, String> map, Listener listener) {
        synchronized (PubnativeConfigManager.class) {
            Log.v(TAG, "getConfig: " + str);
            if (context != null && !TextUtils.isEmpty(str)) {
                if (listener != null) {
                    PubnativeConfigRequestModel pubnativeConfigRequestModel = new PubnativeConfigRequestModel();
                    pubnativeConfigRequestModel.context = context;
                    pubnativeConfigRequestModel.listener = listener;
                    pubnativeConfigRequestModel.parameters = map;
                    pubnativeConfigRequestModel.setAppToken(str);
                    enqueueRequest(pubnativeConfigRequestModel);
                    doNextConfigRequest();
                }
            } else {
                invokeLoaded(listener, null);
            }
        }
    }

    public static void clean(Context context) {
        Log.v(TAG, "clean");
        setStoredAppToken(context, null);
        setStoredTimestamp(context, null);
        setStoredRefresh(context, null);
        setStoredConfig(context, null);
    }

    protected static void doNextConfigRequest() {
        PubnativeConfigRequestModel dequeueRequest;
        Log.v(TAG, "doNextConfigRequest");
        if (sIdle && (dequeueRequest = dequeueRequest()) != null) {
            sIdle = false;
            getNextConfig(dequeueRequest);
        }
    }

    private static void getNextConfig(PubnativeConfigRequestModel pubnativeConfigRequestModel) {
        Log.v(TAG, "getNextConfig: " + pubnativeConfigRequestModel.getAppToken());
        if (pubnativeConfigRequestModel.context != null && !TextUtils.isEmpty(pubnativeConfigRequestModel.getAppToken())) {
            if (!TextUtils.equals(getStoredAppToken(pubnativeConfigRequestModel.context), pubnativeConfigRequestModel.getAppToken())) {
                downloadConfig(pubnativeConfigRequestModel);
                return;
            }
            serveStoredConfig(pubnativeConfigRequestModel.context, pubnativeConfigRequestModel.listener);
            if (configNeedsUpdate(pubnativeConfigRequestModel.context, pubnativeConfigRequestModel.getAppToken())) {
                PubnativeConfigRequestModel pubnativeConfigRequestModel2 = new PubnativeConfigRequestModel();
                pubnativeConfigRequestModel2.context = pubnativeConfigRequestModel.context;
                pubnativeConfigRequestModel2.listener = null;
                pubnativeConfigRequestModel2.parameters = new HashMap(pubnativeConfigRequestModel.parameters);
                pubnativeConfigRequestModel2.setAppToken(pubnativeConfigRequestModel.getAppToken());
                downloadConfig(pubnativeConfigRequestModel2);
                return;
            }
            return;
        }
        invokeLoaded(pubnativeConfigRequestModel.listener, null);
    }

    protected static void serveStoredConfig(Context context, Listener listener) {
        Log.v(TAG, "serveStoredConfig");
        invokeLoaded(listener, getStoredConfig(context));
    }

    public static PubnativeConfigModel getStoredConfig(Context context) {
        PubnativeConfigModel pubnativeConfigModel;
        Log.v(TAG, "getStoredConfig");
        String storedConfigString = getStoredConfigString(context);
        if (!TextUtils.isEmpty(storedConfigString)) {
            try {
                pubnativeConfigModel = (PubnativeConfigModel) new agb().m13315(storedConfigString, (Class<Object>) PubnativeConfigModel.class);
            } catch (Throwable th) {
                Log.e(TAG, "getStoredConfig - Error: " + th);
            }
            if (pubnativeConfigModel == null && pubnativeConfigModel.isNullOrEmpty()) {
                return null;
            }
        }
        pubnativeConfigModel = null;
        return pubnativeConfigModel == null ? pubnativeConfigModel : pubnativeConfigModel;
    }

    protected static void invokeLoaded(Listener listener, PubnativeConfigModel pubnativeConfigModel) {
        Log.v(TAG, "invokeLoaded");
        if (listener != null) {
            listener.onConfigLoaded(pubnativeConfigModel);
        }
        sIdle = true;
        doNextConfigRequest();
    }

    protected static void updateConfig(Context context, String str, PubnativeConfigModel pubnativeConfigModel) {
        Log.v(TAG, "updateConfig");
        if (context != null && !TextUtils.isEmpty(str)) {
            if (pubnativeConfigModel != null && !pubnativeConfigModel.isNullOrEmpty()) {
                setStoredConfig(context, pubnativeConfigModel);
                setStoredAppToken(context, str);
                setStoredTimestamp(context, Long.valueOf(System.currentTimeMillis()));
                if (pubnativeConfigModel.globals.containsKey("refresh")) {
                    setStoredRefresh(context, Long.valueOf(((Double) pubnativeConfigModel.globals.get("refresh")).longValue()));
                    return;
                }
                return;
            }
            clean(context);
            return;
        }
        clean(context);
    }

    protected static synchronized void downloadConfig(final PubnativeConfigRequestModel pubnativeConfigRequestModel) {
        synchronized (PubnativeConfigManager.class) {
            Log.v(TAG, "downloadConfig");
            if (pubnativeConfigRequestModel.context != null && !TextUtils.isEmpty(pubnativeConfigRequestModel.getAppToken())) {
                if (downloadingConfig.compareAndSet(false, true)) {
                    Uri.Builder buildUpon = Uri.parse(getConfigDownloadBaseUrl(pubnativeConfigRequestModel.context)).buildUpon();
                    for (String str : pubnativeConfigRequestModel.parameters.keySet()) {
                        buildUpon.appendQueryParameter(str, pubnativeConfigRequestModel.parameters.get(str));
                    }
                    String uri = buildUpon.build().toString();
                    final long currentTimeMillis = System.currentTimeMillis();
                    PubnativeMediationDelegate pubnativeMediationDelegate = getPubnativeMediationDelegate(pubnativeConfigRequestModel.context);
                    if (shouldDownloadConfig()) {
                        if (pubnativeMediationDelegate != null) {
                            pubnativeMediationDelegate.fetchMediationConfig(uri, new ConfigFetchListener() {
                                @Override
                                public void onMediationConfigFetchSuccess(String str2) {
                                    Log.v(PubnativeConfigManager.TAG, "fetchConfigSuccess duration = " + (System.currentTimeMillis() - currentTimeMillis));
                                    PubnativeConfigManager.processConfigDownloadResponse(pubnativeConfigRequestModel.context, pubnativeConfigRequestModel.getAppToken(), str2);
                                    PubnativeConfigManager.serveStoredConfig(pubnativeConfigRequestModel.context, pubnativeConfigRequestModel.listener);
                                    PubnativeConfigManager.downloadingConfig.set(false);
                                    PubnativeConfigManager.updateRetryInfo(true);
                                }

                                @Override
                                public void onMediationConfigFetchError(String str2) {
                                    Log.v(PubnativeConfigManager.TAG, "fetchConfigError: " + str2);
                                    PubnativeConfigManager.serveStoredConfig(pubnativeConfigRequestModel.context, pubnativeConfigRequestModel.listener);
                                    PubnativeConfigManager.downloadingConfig.set(false);
                                    PubnativeConfigManager.updateRetryInfo(false);
                                }
                            });
                        } else {
                            PubnativeHttpTask pubnativeHttpTask = new PubnativeHttpTask(pubnativeConfigRequestModel.context);
                            pubnativeHttpTask.setListener(new PubnativeHttpTask.Listener() {
                                @Override
                                public void onHttpTaskSuccess(PubnativeHttpTask pubnativeHttpTask2, String str2) {
                                    Log.v(PubnativeConfigManager.TAG, "onHttpTaskSuccess");
                                    PubnativeConfigManager.processConfigDownloadResponse(PubnativeConfigRequestModel.this.context, PubnativeConfigRequestModel.this.getAppToken(), str2);
                                    PubnativeConfigManager.serveStoredConfig(PubnativeConfigRequestModel.this.context, PubnativeConfigRequestModel.this.listener);
                                    PubnativeConfigManager.downloadingConfig.set(false);
                                    PubnativeConfigManager.updateRetryInfo(true);
                                }

                                @Override
                                public void onHttpTaskFailed(PubnativeHttpTask pubnativeHttpTask2, String str2) {
                                    Log.v(PubnativeConfigManager.TAG, "onHttpTaskFailed: " + str2);
                                    PubnativeConfigManager.serveStoredConfig(PubnativeConfigRequestModel.this.context, PubnativeConfigRequestModel.this.listener);
                                    PubnativeConfigManager.downloadingConfig.set(false);
                                    PubnativeConfigManager.updateRetryInfo(false);
                                }
                            });
                            pubnativeHttpTask.execute(uri);
                        }
                    }
                }
            } else {
                serveStoredConfig(pubnativeConfigRequestModel.context, pubnativeConfigRequestModel.listener);
            }
        }
    }

    private static boolean shouldDownloadConfig() {
        return continuousFailCount < maxRequestContinuousFailCount || System.currentTimeMillis() - startTime >= ((long) requestCounterTimeoutMs);
    }

    public static void updateRetryInfo(boolean z) {
        if (z || (startTime > 0 && System.currentTimeMillis() - startTime >= requestCounterTimeoutMs)) {
            startTime = -1L;
            continuousFailCount = 0;
            return;
        }
        if (continuousFailCount == 0) {
            startTime = System.currentTimeMillis();
        }
        continuousFailCount++;
    }

    protected static void processConfigDownloadResponse(Context context, String str, String str2) {
        Log.v(TAG, "processConfigDownloadResponse");
        if (!TextUtils.isEmpty(str2)) {
            try {
                SnaptubePubnativeConfigAPIResponseModel snaptubePubnativeConfigAPIResponseModel = (SnaptubePubnativeConfigAPIResponseModel) new agb().m13315(str2, (Class<Object>) SnaptubePubnativeConfigAPIResponseModel.class);
                if (snaptubePubnativeConfigAPIResponseModel != null && snaptubePubnativeConfigAPIResponseModel.config != null && snaptubePubnativeConfigAPIResponseModel.config.size() > 0) {
                    PubnativeConfigAPIResponseModel pubnativeConfigAPIResponseModel = snaptubePubnativeConfigAPIResponseModel.config.get(0).value;
                    if (pubnativeConfigAPIResponseModel.config != null && !pubnativeConfigAPIResponseModel.config.isNullOrEmpty()) {
                        updateDeliveryManagerCache(context, pubnativeConfigAPIResponseModel.config);
                        updateConfig(context, str, pubnativeConfigAPIResponseModel.config);
                        return;
                    }
                    return;
                }
                throw new Exception("get empty config");
            } catch (Throwable th) {
                Log.e(TAG, "downloadConfig - Error: " + th);
            }
        }
    }

    protected static boolean configNeedsUpdate(Context context, String str) {
        Log.v(TAG, "configNeedsUpdate");
        if (context != null) {
            if (TextUtils.isEmpty(getStoredConfigString(context))) {
                return true;
            }
            String storedAppToken = getStoredAppToken(context);
            if (TextUtils.isEmpty(storedAppToken) || TextUtils.isEmpty(str) || !storedAppToken.equals(str)) {
                return true;
            }
            Long storedRefresh = getStoredRefresh(context);
            Long storedTimestamp = getStoredTimestamp(context);
            if (storedRefresh == null || storedTimestamp == null) {
                return true;
            }
            if (Long.valueOf(TimeUnit.MILLISECONDS.toMinutes(Long.valueOf(System.currentTimeMillis()).longValue() - storedTimestamp.longValue())).longValue() >= storedRefresh.longValue()) {
                return true;
            }
        }
        return false;
    }

    private static void updateDeliveryManagerCache(Context context, PubnativeConfigModel pubnativeConfigModel) {
        PubnativeConfigModel storedConfig;
        Log.v(TAG, "updateDeliveryManagerCache");
        if (pubnativeConfigModel != null && (storedConfig = getStoredConfig(context)) != null) {
            for (String str : storedConfig.placements.keySet()) {
                PubnativePlacementModel pubnativePlacementModel = pubnativeConfigModel.placements.get(str);
                PubnativePlacementModel pubnativePlacementModel2 = storedConfig.placements.get(str);
                if (pubnativePlacementModel == null) {
                    PubnativeDeliveryManager.resetHourlyImpressionCount(context, str);
                    PubnativeDeliveryManager.resetDailyImpressionCount(context, str);
                    PubnativeDeliveryManager.resetPacingCalendar(str);
                } else {
                    if (pubnativePlacementModel2.delivery_rule.imp_cap_hour != pubnativePlacementModel.delivery_rule.imp_cap_hour) {
                        PubnativeDeliveryManager.resetHourlyImpressionCount(context, str);
                    }
                    if (pubnativePlacementModel2.delivery_rule.imp_cap_day != pubnativePlacementModel.delivery_rule.imp_cap_day) {
                        PubnativeDeliveryManager.resetDailyImpressionCount(context, str);
                    }
                    if (pubnativePlacementModel2.delivery_rule.pacing_cap_minute != pubnativePlacementModel.delivery_rule.pacing_cap_minute || pubnativePlacementModel2.delivery_rule.pacing_cap_hour != pubnativePlacementModel.delivery_rule.pacing_cap_hour) {
                        PubnativeDeliveryManager.resetPacingCalendar(str);
                    }
                }
            }
        }
    }

    protected static void enqueueRequest(PubnativeConfigRequestModel pubnativeConfigRequestModel) {
        Log.v(TAG, "enqueueRequest");
        if (pubnativeConfigRequestModel != null) {
            if (sQueue == null) {
                sQueue = new ArrayList();
            }
            sQueue.add(pubnativeConfigRequestModel);
        }
    }

    protected static PubnativeConfigRequestModel dequeueRequest() {
        Log.v(TAG, "dequeueRequest");
        if (sQueue == null || sQueue.size() <= 0) {
            return null;
        }
        return sQueue.remove(0);
    }

    protected static String getConfigDownloadBaseUrl(Context context) {
        String str = "https://config.ad.snappea.com/v2/config?configName=android_ads_for_mediation_merge.json";
        String systemProperty = getSystemProperty("snaptube.mediation.config.test", null);
        String[][] strArr = {new String[]{"baidu", "http://api.snappea.com/onlineConfig/android_ads_for_pubnative_mediation_v2_baidu.json"}, new String[]{CommonConst.SHARED_PERFERENCE_KEY, "http://api.snappea.com/onlineConfig/android_ads_for_pubnative_mediation_v2_mobvista.json"}, new String[]{"pubnative", "http://api.snappea.com/onlineConfig/android_ads_for_pubnative_mediation_v2_pubnative.json"}, new String[]{"selfbuild", "http://api.snappea.com/onlineConfig/android_ads_for_pubnative_mediation_v2_selfbuild.json"}};
        if (!TextUtils.isEmpty(systemProperty)) {
            int length = strArr.length;
            int i = 0;
            while (true) {
                if (i >= length) {
                    break;
                }
                String[] strArr2 = strArr[i];
                if (!TextUtils.equals(strArr2[0], systemProperty)) {
                    i++;
                } else {
                    str = strArr2[1];
                    break;
                }
            }
        }
        Log.i("ads", "mediation config url: " + str);
        return str;
    }

    protected static synchronized String getStoredConfigString(Context context) {
        String stringSharedPreference;
        synchronized (PubnativeConfigManager.class) {
            Log.v(TAG, "getStoredConfigString");
            stringSharedPreference = getStringSharedPreference(context, CONFIG_STRING_KEY);
        }
        return stringSharedPreference;
    }

    protected static synchronized void setStoredConfig(Context context, PubnativeConfigModel pubnativeConfigModel) {
        synchronized (PubnativeConfigManager.class) {
            Log.v(TAG, "setStoredConfig");
            setStringSharedPreference(context, CONFIG_STRING_KEY, pubnativeConfigModel != null ? new agb().m13332(pubnativeConfigModel) : null);
        }
    }

    protected static String getStoredAppToken(Context context) {
        Log.v(TAG, "getStoredAppToken");
        return getStringSharedPreference(context, APP_TOKEN_STRING_KEY);
    }

    protected static void setStoredAppToken(Context context, String str) {
        Log.v(TAG, "getStoredAppToken");
        setStringSharedPreference(context, APP_TOKEN_STRING_KEY, str);
    }

    public static Long getStoredTimestamp(Context context) {
        Log.v(TAG, "getStoredTimestamp");
        return getLongSharedPreference(context, TIMESTAMP_LONG_KEY);
    }

    protected static void setStoredTimestamp(Context context, Long l) {
        Log.v(TAG, "getStoredTimestamp");
        setLongSharedPreference(context, TIMESTAMP_LONG_KEY, l);
    }

    protected static Long getStoredRefresh(Context context) {
        Log.v(TAG, "getStoredRefresh");
        return getLongSharedPreference(context, "refresh");
    }

    protected static void setStoredRefresh(Context context, Long l) {
        Log.v(TAG, "setStoredRefresh");
        setLongSharedPreference(context, "refresh", l);
    }

    protected static String getStringSharedPreference(Context context, String str) {
        SharedPreferences sharedPreferences;
        Log.v(TAG, "getStringSharedPreference");
        if (context == null || TextUtils.isEmpty(str) || (sharedPreferences = getSharedPreferences(context)) == null || !sharedPreferences.contains(str)) {
            return null;
        }
        return sharedPreferences.getString(str, null);
    }

    protected static void setStringSharedPreference(Context context, String str, String str2) {
        SharedPreferences.Editor edit;
        Log.v(TAG, "setStringSharedPreference");
        if (context != null && !TextUtils.isEmpty(str) && (edit = getSharedPreferences(context).edit()) != null) {
            if (TextUtils.isEmpty(str2)) {
                edit.remove(str);
            } else {
                edit.putString(str, str2);
            }
            edit.apply();
        }
    }

    protected static Long getLongSharedPreference(Context context, String str) {
        Log.v(TAG, "getLongSharedPreference");
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        if (context != null && sharedPreferences.contains(str)) {
            Long valueOf = Long.valueOf(sharedPreferences.getLong(str, 0L));
            if (valueOf.longValue() > 0) {
                return valueOf;
            }
        }
        return null;
    }

    protected static void setLongSharedPreference(Context context, String str, Long l) {
        SharedPreferences.Editor edit;
        Log.v(TAG, "setLongSharedPreference");
        if (context != null && !TextUtils.isEmpty(str) && (edit = getSharedPreferences(context).edit()) != null) {
            if (l == null) {
                edit.remove(str);
            } else {
                edit.putLong(str, l.longValue());
            }
            edit.apply();
        }
    }

    protected static SharedPreferences getSharedPreferences(Context context) {
        Log.v(TAG, "getSharedPreferences");
        if (context == null) {
            return null;
        }
        return context.getSharedPreferences("net.pubnative.mediation", 0);
    }

    private static String getSystemProperty(String str, String str2) {
        try {
            String str3 = (String) Class.forName("android.os.SystemProperties").getDeclaredMethod("get", String.class).invoke(null, str);
            return VALUE_REMOVED.equals(str3) ? str2 : str3;
        } catch (Exception e) {
            e.printStackTrace();
            return str2;
        }
    }
}