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 all 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,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 {
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,15 +43,18 @@ 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() {

}

// 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);
Expand All @@ -67,7 +71,6 @@ public static FetchAdIdInfoTask initAdId(final Context context, final AdIdFetchL
listener.adIdFetchFailure();
}
}, AD_ID_TIMEOUT_MS);
return getAdIdInfoTask;
}
else {
listener.adIdFetchCompletion();
Expand All @@ -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<Void, Void, Void>() {
@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() {
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");
}
});
}
}
}

/**
Expand Down Expand Up @@ -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) {
Expand Down