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

Serve multiple ad types with a single AdLoaderAd widget #723

Open
wants to merge 7 commits into
base: main
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 @@ -68,6 +68,10 @@ class AdMessageCodec extends StandardMessageCodec {
private static final byte VALUE_NATIVE_TEMPLATE_TYPE = (byte) 152;
private static final byte VALUE_COLOR = (byte) 153;
private static final byte VALUE_MEDIATION_EXTRAS = (byte) 154;
private static final byte VALUE_AD_MANAGER_AD_VIEW_OPTIONS = (byte) 155;
private static final byte VALUE_BANNER_PARAMETERS = (byte) 156;
private static final byte VALUE_CUSTOM_PARAMETERS = (byte) 157;
private static final byte VALUE_NATIVE_PARAMETERS = (byte) 158;

@NonNull Context context;
@NonNull final FlutterAdSize.AdSizeFactory adSizeFactory;
Expand Down Expand Up @@ -253,6 +257,26 @@ protected void writeValue(ByteArrayOutputStream stream, Object value) {
writeValue(stream, Color.red(colorValue));
writeValue(stream, Color.green(colorValue));
writeValue(stream, Color.blue(colorValue));
} else if (value instanceof FlutterAdManagerAdViewOptions) {
stream.write(VALUE_AD_MANAGER_AD_VIEW_OPTIONS);
FlutterAdManagerAdViewOptions options = (FlutterAdManagerAdViewOptions) value;
writeValue(stream, options.manualImpressionsEnabled);
} else if (value instanceof FlutterBannerParameters) {
stream.write(VALUE_BANNER_PARAMETERS);
FlutterBannerParameters bannerParameters = (FlutterBannerParameters) value;
writeValue(stream, bannerParameters.sizes);
writeValue(stream, bannerParameters.adManagerAdViewOptions);
} else if (value instanceof FlutterCustomParameters) {
stream.write(VALUE_CUSTOM_PARAMETERS);
FlutterCustomParameters customParameters = (FlutterCustomParameters) value;
writeValue(stream, customParameters.formatIds);
writeValue(stream, customParameters.viewOptions);
} else if (value instanceof FlutterNativeParameters) {
stream.write(VALUE_NATIVE_PARAMETERS);
FlutterNativeParameters nativeParameters = (FlutterNativeParameters) value;
writeValue(stream, nativeParameters.factoryId);
writeValue(stream, nativeParameters.nativeAdOptions);
writeValue(stream, nativeParameters.viewOptions);
} else {
super.writeValue(stream, value);
}
Expand Down Expand Up @@ -434,6 +458,21 @@ protected Object readValueOfType(byte type, ByteBuffer buffer) {
final Integer green = (Integer) readValueOfType(buffer.get(), buffer);
final Integer blue = (Integer) readValueOfType(buffer.get(), buffer);
return new ColorDrawable(Color.argb(alpha, red, green, blue));
case VALUE_AD_MANAGER_AD_VIEW_OPTIONS:
return new FlutterAdManagerAdViewOptions((Boolean) readValueOfType(buffer.get(), buffer));
case VALUE_BANNER_PARAMETERS:
return new FlutterBannerParameters(
(List<FlutterAdSize>) readValueOfType(buffer.get(), buffer),
(FlutterAdManagerAdViewOptions) readValueOfType(buffer.get(), buffer));
case VALUE_CUSTOM_PARAMETERS:
return new FlutterCustomParameters(
(List<String>) readValueOfType(buffer.get(), buffer),
(Map<String, Object>) readValueOfType(buffer.get(), buffer));
case VALUE_NATIVE_PARAMETERS:
return new FlutterNativeParameters(
(String) readValueOfType(buffer.get(), buffer),
(FlutterNativeAdOptions) readValueOfType(buffer.get(), buffer),
(Map<String, Object>) readValueOfType(buffer.get(), buffer));
default:
return super.readValueOfType(type, buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
import androidx.annotation.NonNull;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.admanager.AdManagerAdView;
import com.google.android.gms.ads.formats.OnAdManagerAdViewLoadedListener;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAd.OnNativeAdLoadedListener;
import com.google.android.gms.ads.nativead.NativeCustomFormatAd;
import com.google.android.gms.ads.nativead.NativeCustomFormatAd.OnCustomFormatAdLoadedListener;
import java.lang.ref.WeakReference;

/** Callback type to notify when an ad successfully loads. */
Expand Down Expand Up @@ -94,19 +98,61 @@ public void onAdLoaded() {
}
}

/** Listener for adloader ads. */
class FlutterAdLoaderAdListener extends FlutterAdListener {

FlutterAdLoaderAdListener(int adId, AdInstanceManager manager) {
super(adId, manager);
}
}

/** {@link OnNativeAdLoadedListener} for native ads. */
class FlutterNativeAdLoadedListener implements OnNativeAdLoadedListener {

private final WeakReference<FlutterNativeAd> nativeAdWeakReference;
private final WeakReference<OnNativeAdLoadedListener> listenerWeakReference;

FlutterNativeAdLoadedListener(FlutterNativeAd flutterNativeAd) {
nativeAdWeakReference = new WeakReference<>(flutterNativeAd);
FlutterNativeAdLoadedListener(OnNativeAdLoadedListener listener) {
listenerWeakReference = new WeakReference<>(listener);
}

@Override
public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
if (nativeAdWeakReference.get() != null) {
nativeAdWeakReference.get().onNativeAdLoaded(nativeAd);
if (listenerWeakReference.get() != null) {
listenerWeakReference.get().onNativeAdLoaded(nativeAd);
}
}
}

/** {@link OnAdManagerAdViewLoadedListener} for banner ads. */
class FlutterAdManagerAdViewLoadedListener implements OnAdManagerAdViewLoadedListener {

private final WeakReference<OnAdManagerAdViewLoadedListener> reference;

FlutterAdManagerAdViewLoadedListener(OnAdManagerAdViewLoadedListener listener) {
reference = new WeakReference<>(listener);
}

@Override
public void onAdManagerAdViewLoaded(AdManagerAdView adView) {
if (reference.get() != null) {
reference.get().onAdManagerAdViewLoaded(adView);
}
}
}

/** {@link OnCustomFormatAdLoadedListener} for custom ads. */
class FlutterCustomFormatAdLoadedListener implements OnCustomFormatAdLoadedListener {

private final WeakReference<OnCustomFormatAdLoadedListener> reference;

FlutterCustomFormatAdLoadedListener(OnCustomFormatAdLoadedListener listener) {
reference = new WeakReference<>(listener);
}

@Override
public void onCustomFormatAdLoaded(NativeCustomFormatAd ad) {
if (reference.get() != null) {
reference.get().onCustomFormatAdLoaded(ad);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
Expand Down Expand Up @@ -137,4 +138,62 @@ public void loadAdManagerNativeAd(
.build()
.loadAd(adManagerAdRequest);
}

/** Load an ad loader ad. */
public void loadAdLoaderAd(
@NonNull String adUnitId,
@NonNull AdListener adListener,
@NonNull AdRequest request,
@Nullable FlutterAdLoaderAd.BannerParameters bannerParameters,
@Nullable FlutterAdLoaderAd.CustomParameters customParameters,
@Nullable FlutterAdLoaderAd.NativeParameters nativeParameters) {
AdLoader.Builder builder = new AdLoader.Builder(context, adUnitId);
if (bannerParameters != null) {
builder = builder.forAdManagerAdView(bannerParameters.listener, bannerParameters.adSizes);
if (bannerParameters.adManagerAdViewOptions != null) {
builder.withAdManagerAdViewOptions(bannerParameters.adManagerAdViewOptions);
}
}
if (customParameters != null) {
for (String formatId : customParameters.factories.keySet()) {
builder = builder.forCustomFormatAd(formatId, customParameters.listener, null);
}
}
if (nativeParameters != null) {
builder = builder.forNativeAd(nativeParameters.listener);
if (nativeParameters.nativeAdOptions != null) {
builder = builder.withNativeAdOptions(nativeParameters.nativeAdOptions);
}
}
builder.withAdListener(adListener).build().loadAd(request);
}

/** Load an ad manager ad loader ad. */
public void loadAdManagerAdLoaderAd(
@NonNull String adUnitId,
@NonNull AdListener adListener,
@NonNull AdManagerAdRequest adManagerAdRequest,
@Nullable FlutterAdLoaderAd.BannerParameters bannerParameters,
@Nullable FlutterAdLoaderAd.CustomParameters customParameters,
@Nullable FlutterAdLoaderAd.NativeParameters nativeParameters) {
AdLoader.Builder builder = new AdLoader.Builder(context, adUnitId);
if (bannerParameters != null) {
builder = builder.forAdManagerAdView(bannerParameters.listener, bannerParameters.adSizes);
if (bannerParameters.adManagerAdViewOptions != null) {
builder.withAdManagerAdViewOptions(bannerParameters.adManagerAdViewOptions);
}
}
if (customParameters != null) {
for (String formatId : customParameters.factories.keySet()) {
builder = builder.forCustomFormatAd(formatId, customParameters.listener, null);
}
}
if (nativeParameters != null) {
builder = builder.forNativeAd(nativeParameters.listener);
if (nativeParameters.nativeAdOptions != null) {
builder = builder.withNativeAdOptions(nativeParameters.nativeAdOptions);
}
}
builder.withAdListener(adListener).build().loadAd(adManagerAdRequest);
}
}
Loading