org.prebid.mobile.admob.PrebidRewardedAdapter Maven / Gradle / Ivy
The newest version!
package org.prebid.mobile.admob;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.rewarded.RewardItem;
import org.jetbrains.annotations.NotNull;
import org.prebid.mobile.api.exceptions.AdException;
import org.prebid.mobile.rendering.bidding.display.InterstitialController;
import org.prebid.mobile.rendering.bidding.interfaces.InterstitialControllerListener;
import org.prebid.mobile.rendering.interstitial.rewarded.Reward;
/**
* Prebid rewarded adapter for AdMob integration.
*/
@Keep
public class PrebidRewardedAdapter extends PrebidBaseAdapter {
public static final String EXTRA_RESPONSE_ID = "PrebidRewardedAdapterExtraId";
@Nullable
private InterstitialController interstitialController;
@Nullable
private MediationRewardedAdCallback rewardedAdCallback;
@Override
public void loadRewardedAd(
@NonNull MediationRewardedAdConfiguration configuration,
@NonNull MediationAdLoadCallback adMobLoadListener
) {
String responseId = getResponseIdAndCheckParameters(
configuration,
EXTRA_RESPONSE_ID,
adMobLoadListener::onFailure
);
if (responseId == null) {
return;
}
try {
InterstitialControllerListener prebidListener = getListener(adMobLoadListener);
interstitialController = new InterstitialController(configuration.getContext(), prebidListener);
interstitialController.setRewardListener(prebidListener::onUserEarnedReward);
interstitialController.loadAd(responseId, true);
} catch (AdException e) {
adMobLoadListener.onFailure(AdErrors.interstitialControllerError(e.getMessage()));
}
}
private InterstitialControllerListener getListener(
MediationAdLoadCallback adMobCallback
) {
return new InterstitialControllerListener() {
@Override
public void onInterstitialReadyForDisplay() {
rewardedAdCallback = adMobCallback.onSuccess(context -> {
if (interstitialController != null) {
interstitialController.show();
}
});
}
@Override
public void onInterstitialDisplayed() {
if (rewardedAdCallback != null) {
rewardedAdCallback.reportAdImpression();
rewardedAdCallback.onAdOpened();
rewardedAdCallback.onVideoStart();
}
}
@Override
public void onInterstitialClicked() {
if (rewardedAdCallback != null) {
rewardedAdCallback.reportAdClicked();
}
}
@Override
public void onInterstitialClosed() {
if (rewardedAdCallback != null) {
rewardedAdCallback.onVideoComplete();
rewardedAdCallback.onAdClosed();
}
}
@Override
public void onInterstitialFailedToLoad(AdException exception) {
adMobCallback.onFailure(AdErrors.failedToLoadAd(
exception.getMessage() != null ? exception.getMessage() : "Failed to load ad"
));
}
@Override
public void onUserEarnedReward() {
if (rewardedAdCallback != null && interstitialController != null) {
Reward reward = interstitialController.getReward();
rewardedAdCallback.onUserEarnedReward(new RewardItem() {
@Override
public int getAmount() {
if (reward != null) {
return reward.getCount();
}
return 0;
}
@NonNull
@Override
public @NotNull String getType() {
if (reward != null) {
return reward.getType();
}
return "";
}
});
}
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy