diff --git a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/networking/modelcontrollers/Requester.java b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/networking/modelcontrollers/Requester.java index c34f14a88..7a7b267b0 100644 --- a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/networking/modelcontrollers/Requester.java +++ b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/networking/modelcontrollers/Requester.java @@ -61,7 +61,6 @@ public abstract class Requester { protected URLBuilder urlBuilder; protected ResponseHandler adResponseCallBack; protected BaseNetworkTask networkTask; - protected AdIdManager.FetchAdIdInfoTask fetchAdIdInfoTask; Requester( AdUnitConfiguration config, @@ -88,11 +87,7 @@ public void destroy() { networkTask.cancel(true); } networkTask = null; - if (fetchAdIdInfoTask != null) { - fetchAdIdInfoTask.cancel(true); - } adResponseCallBack = null; - fetchAdIdInfoTask = null; } protected List getParameterBuilders() { @@ -131,17 +126,27 @@ protected void getAdId() { UserConsentManager userConsentManager = ManagersResolver.getInstance().getUserConsentManager(); if (userConsentManager.canAccessDeviceData()) { - fetchAdIdInfoTask = AdIdManager.initAdId(context, new AdIdFetchListener() { + AdIdManager.fetchAdvertisingId(context, new AdIdFetchListener() { @Override public void adIdFetchCompletion() { - LogUtil.info(TAG, "Advertising id was received"); - makeAdRequest(); + LogUtil.info(TAG, "Advertising id was loaded from cache"); + try { + makeAdRequest(); + } catch (Exception e) { + LogUtil.info(TAG, + "makeAdRequest() failed because Requester object doesn't exist"); + } } @Override public void adIdFetchFailure() { - LogUtil.warning(TAG, "Can't get advertising id"); - makeAdRequest(); + LogUtil.warning(TAG, "Can't get advertising id from cache"); + try { + makeAdRequest(); + } catch (Exception e) { + LogUtil.info(TAG, + "makeAdRequest() failed because Requester object doesn't exist"); + } } }); } else { diff --git a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/sdk/SdkInitializer.java b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/sdk/SdkInitializer.java index fe0af917e..3bc767aba 100644 --- a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/sdk/SdkInitializer.java +++ b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/sdk/SdkInitializer.java @@ -11,8 +11,10 @@ import org.prebid.mobile.LogUtil.PrebidLogger; import org.prebid.mobile.PrebidMobile; import org.prebid.mobile.api.rendering.PrebidRenderer; +import org.prebid.mobile.rendering.listeners.AdIdFetchListener; import org.prebid.mobile.rendering.listeners.SdkInitializationListener; import org.prebid.mobile.rendering.session.manager.OmAdSessionManager; +import org.prebid.mobile.rendering.utils.helpers.AdIdManager; import org.prebid.mobile.rendering.utils.helpers.AppInfoManager; import java.util.concurrent.ExecutorService; @@ -62,6 +64,18 @@ public static void init( ManagersResolver.getInstance().prepare(applicationContext); JSLibraryManager.getInstance(applicationContext).checkIfScriptsDownloadedAndStartDownloadingIfNot(); + + AdIdManager.initAdId(context, new AdIdFetchListener() { + @Override + public void adIdFetchCompletion() { + LogUtil.info(TAG, "Advertising id was received"); + } + + @Override + public void adIdFetchFailure() { + LogUtil.warning(TAG, "Can't get advertising id"); + } + }); } catch (Throwable throwable) { initializationNotifier.initializationFailed("Exception during initialization: " + throwable.getMessage() + "\n" + Log.getStackTraceString(throwable)); return; diff --git a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/utils/helpers/AdIdManager.java b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/utils/helpers/AdIdManager.java index ed5b57272..6f7c0db18 100644 --- a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/utils/helpers/AdIdManager.java +++ b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/rendering/utils/helpers/AdIdManager.java @@ -32,6 +32,7 @@ import org.prebid.mobile.rendering.listeners.AdIdFetchListener; import java.lang.ref.WeakReference; +import java.util.Date; public class AdIdManager { private static final String TAG = AdIdManager.class.getSimpleName(); @@ -42,7 +43,10 @@ public class AdIdManager { */ private static final long AD_ID_TIMEOUT_MS = 3000; + private static final long AD_ID_MINIMUM_UPDATE_MS = 60000; + private static volatile String sAdId = null; + private static Date adIdLastUpdateTime = null; private static boolean sLimitAdTrackingEnabled; private AdIdManager() { @@ -50,7 +54,7 @@ private AdIdManager() { } // Wrap method execution in try / catch to avoid crashes in runtime if publisher didn't include identifier dependencies - public static FetchAdIdInfoTask initAdId(final Context context, final AdIdFetchListener listener) { + public static void initAdId(final Context context, final AdIdFetchListener listener) { try { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(context); @@ -67,7 +71,6 @@ public static FetchAdIdInfoTask initAdId(final Context context, final AdIdFetchL listener.adIdFetchFailure(); } }, AD_ID_TIMEOUT_MS); - return getAdIdInfoTask; } else { listener.adIdFetchCompletion(); @@ -76,7 +79,39 @@ public static FetchAdIdInfoTask initAdId(final Context context, final AdIdFetchL catch (Throwable throwable) { LogUtil.error(TAG, "Failed to initAdId: " + Log.getStackTraceString(throwable) + "\nDid you add necessary dependencies?"); } - return null; + } + + /** + * Updates Advertising Id only if a minute has passed instead of fetching with every bid request + */ + public static void fetchAdvertisingId(Context context, AdIdFetchListener listener) { + Date now = new Date(); + if (adIdLastUpdateTime == null) { + initAdId(context, listener); + } else { + new AsyncTask() { + @Override + protected Void doInBackground(Void... voids) { + if (listener != null) { + listener.adIdFetchCompletion(); + } + return null; + } + }.execute(); + if (now.getTime() - adIdLastUpdateTime.getTime() >= AD_ID_MINIMUM_UPDATE_MS) { + initAdId(context, new AdIdFetchListener() { + @Override + public void adIdFetchCompletion() { + LogUtil.info(TAG, "Advertising id was updated"); + } + + @Override + public void adIdFetchFailure() { + LogUtil.warning(TAG, "Can't update advertising id"); + } + }); + } + } } /** @@ -130,6 +165,7 @@ protected Void doInBackground(Void... voids) { try { AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context); sAdId = adInfo.getId(); + adIdLastUpdateTime = new Date(); sLimitAdTrackingEnabled = adInfo.isLimitAdTrackingEnabled(); } catch (Throwable e) {