Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

696 Prefetch the Advertising Info #753

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public abstract class Requester {
protected URLBuilder urlBuilder;
protected ResponseHandler adResponseCallBack;
protected BaseNetworkTask networkTask;
protected AdIdManager.FetchAdIdInfoTask fetchAdIdInfoTask;

Requester(
AdUnitConfiguration config,
Expand All @@ -88,11 +87,7 @@ public void destroy() {
networkTask.cancel(true);
}
networkTask = null;
if (fetchAdIdInfoTask != null) {
fetchAdIdInfoTask.cancel(true);
}
adResponseCallBack = null;
fetchAdIdInfoTask = null;
}

protected List<ParameterBuilder> getParameterBuilders() {
Expand Down Expand Up @@ -131,16 +126,16 @@ protected void getAdId() {

UserConsentManager userConsentManager = ManagersResolver.getInstance().getUserConsentManager();
if (userConsentManager.canAccessDeviceData()) {
fetchAdIdInfoTask = AdIdManager.initAdId(context, new AdIdFetchListener() {
AdIdManager.updateAdvertisingId(context, new AdIdFetchListener() {
@Override
public void adIdFetchCompletion() {
LogUtil.info(TAG, "Advertising id was received");
LogUtil.info(TAG, "Advertising id was loaded from cache");
makeAdRequest();
jsligh marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void adIdFetchFailure() {
LogUtil.warning(TAG, "Can't get advertising id");
LogUtil.warning(TAG, "Can't get advertising id from cache");
makeAdRequest();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +64,18 @@ public static void init(
ManagersResolver.getInstance().prepare(applicationContext);

JSLibraryManager.getInstance(applicationContext).checkIfScriptsDownloadedAndStartDownloadingIfNot();

AdIdManager.initAdId(context, new AdIdFetchListener() {
jsligh marked this conversation as resolved.
Show resolved Hide resolved
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand Down Expand Up @@ -79,6 +83,31 @@ public static FetchAdIdInfoTask initAdId(final Context context, final AdIdFetchL
return null;
}

/**
* Updates Advertising Id only if a minute has passed instead of fetching with every bid request
*/
public static void updateAdvertisingId(Context context, AdIdFetchListener listener) {
jsligh marked this conversation as resolved.
Show resolved Hide resolved
Date now = new Date();
if (adIdLastUpdateTime == null) {
initAdId(context, listener);
} else {
if (now.getTime() - adIdLastUpdateTime.getTime() >= AD_ID_MINIMUM_UPDATE_MS) {
initAdId(context, new AdIdFetchListener() {
jsligh marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void adIdFetchCompletion() {
LogUtil.info(TAG, "Advertising id was updated");
}

@Override
public void adIdFetchFailure() {
LogUtil.warning(TAG, "Can't update advertising id");
}
});
}
listener.adIdFetchCompletion();
}
}

/**
* @return Advertiser id, from gms-getAdvertisingIdInfo
*/
Expand Down Expand Up @@ -130,6 +159,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) {
Expand Down