Skip to content
Closed
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
@@ -0,0 +1,12 @@
package io.flutter.plugins.googlemobileads;

import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.libraries.ads.mobile.sdk.nativead.CustomNativeAd;
import java.util.Map;

public interface CustomAdFactory {
View createCustomAd(
@NonNull CustomNativeAd customNativeAd, @Nullable Map<String, Object> customOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,10 @@ static class FlutterLoadAdError {

if (error.getResponseInfo() != null) {
responseInfo = new FlutterResponseInfo(error.getResponseInfo());
domain = responseInfo.getMediationAdapterClassName();
String adapterClassName = responseInfo.getMediationAdapterClassName();
domain = adapterClassName != null
? adapterClassName
: "com.google.android.libraries.ads.mobile.sdk";
} else {
domain = "com.google.android.libraries.ads.mobile.sdk";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.android.libraries.ads.mobile.sdk.common.AdValue;
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError;
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError;
import com.google.android.libraries.ads.mobile.sdk.nativead.CustomNativeAd;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAd;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdEventCallback;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdLoaderCallback;
Expand Down Expand Up @@ -165,4 +166,25 @@ public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
callbackWeakReference.get().onAdFailedToLoad(loadAdError);
}
}

@Override
public void onBannerAdLoaded(@NonNull BannerAd bannerAd) {
if (callbackWeakReference.get() != null) {
callbackWeakReference.get().onBannerAdLoaded(bannerAd);
}
}

@Override
public void onCustomNativeAdLoaded(@NonNull CustomNativeAd customNativeAd) {
if (callbackWeakReference.get() != null) {
callbackWeakReference.get().onCustomNativeAdLoaded(customNativeAd);
}
}

@Override
public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
if (callbackWeakReference.get() != null) {
callbackWeakReference.get().onNativeAdLoaded(nativeAd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.nativead.NativeAdOptions;
import com.google.android.libraries.ads.mobile.sdk.appopen.AppOpenAd;
import com.google.android.libraries.ads.mobile.sdk.common.AdChoicesPlacement;
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback;
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest;
import com.google.android.libraries.ads.mobile.sdk.common.VideoOptions;
import com.google.android.libraries.ads.mobile.sdk.interstitial.InterstitialAd;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAd.NativeAdType;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAd.NativeMediaAspectRatio;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdLoader;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdLoaderCallback;
import com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdRequest;
Expand Down Expand Up @@ -124,19 +127,102 @@ public void loadAdManagerNativeAd(
/** Load an ad loader ad. */
public void loadAdLoaderAd(
@NonNull String adUnitId,
@NonNull NativeAdLoaderCallback adLoaderAdLoaderCallback) {
@NonNull FlutterAdRequest request,
@NonNull NativeAdLoaderCallback adLoaderAdLoaderCallback,
@Nullable FlutterAdLoaderAd.BannerParameters bannerParameters,
@Nullable FlutterAdLoaderAd.CustomParameters customParameters,
@Nullable FlutterAdLoaderAd.NativeParameters nativeParameters) {
List<NativeAdType> nativeAdTypes = new ArrayList<>();

if (bannerParameters != null) {
nativeAdTypes.add(NativeAdType.BANNER);
}

if (customParameters != null) {
nativeAdTypes.add(NativeAdType.CUSTOM_NATIVE);
}

if (nativeParameters != null) {
nativeAdTypes.add(NativeAdType.NATIVE);
}

NativeAdRequest.Builder builder = new NativeAdRequest.Builder(adUnitId, nativeAdTypes);

if (bannerParameters != null) {
builder.setAdSizes(bannerParameters.adSizes);
if (bannerParameters.adManagerAdViewOptions != null) {
builder.setManualImpressionEnabled(bannerParameters.adManagerAdViewOptions.manualImpressionsEnabled);
}
}

if (customParameters != null) {
builder.setCustomFormatIds(new ArrayList<>(customParameters.factories.keySet()));
}

if (nativeParameters != null && nativeParameters.nativeAdOptions != null) {
builder.setAdChoicesPlacement(convertAdChoicesPlacement(nativeParameters.nativeAdOptions.getAdChoicesPlacement()));
builder.setMediaAspectRatio(convertMediaAspectRatio(nativeParameters.nativeAdOptions.getMediaAspectRatio()));
com.google.android.gms.ads.VideoOptions videoOptions = nativeParameters.nativeAdOptions.getVideoOptions();
if (videoOptions != null) {
builder.setVideoOptions(convertVideoOptions(videoOptions));
}
}

request.configureNativeAdRequestBuilder(builder, adUnitId);

NativeAdLoader.load(
new NativeAdRequest.Builder(adUnitId, nativeAdTypes)
.build(),
builder.build(),
adLoaderAdLoaderCallback);
}

/** Load an ad manager ad loader ad. */
public void loadAdManagerAdLoaderAd(
@NonNull String adUnitId,
@NonNull NativeAdLoaderCallback adLoaderAdLoaderCallback) {
loadAdLoaderAd(adUnitId, adLoaderAdLoaderCallback);
@NonNull FlutterAdManagerAdRequest adManagerRequest,
@NonNull NativeAdLoaderCallback adLoaderAdLoaderCallback,
@Nullable FlutterAdLoaderAd.BannerParameters bannerParameters,
@Nullable FlutterAdLoaderAd.CustomParameters customParameters,
@Nullable FlutterAdLoaderAd.NativeParameters nativeParameters) {
loadAdLoaderAd(adUnitId, adManagerRequest, adLoaderAdLoaderCallback, bannerParameters, customParameters, nativeParameters);
}

static AdChoicesPlacement convertAdChoicesPlacement(int adChoicesPlacement) {
switch(adChoicesPlacement) {
case NativeAdOptions.ADCHOICES_BOTTOM_LEFT:
return AdChoicesPlacement.BOTTOM_LEFT;
case NativeAdOptions.ADCHOICES_BOTTOM_RIGHT:
return AdChoicesPlacement.BOTTOM_RIGHT;
case NativeAdOptions.ADCHOICES_TOP_LEFT:
return AdChoicesPlacement.TOP_LEFT;
case NativeAdOptions.ADCHOICES_TOP_RIGHT:
return AdChoicesPlacement.TOP_RIGHT;
default:
return AdChoicesPlacement.TOP_RIGHT;
}
}

static NativeMediaAspectRatio convertMediaAspectRatio(int mediaAspectRatio) {
switch(mediaAspectRatio) {
case NativeAdOptions.NATIVE_MEDIA_ASPECT_RATIO_ANY:
return NativeMediaAspectRatio.ANY;
case NativeAdOptions.NATIVE_MEDIA_ASPECT_RATIO_LANDSCAPE:
return NativeMediaAspectRatio.LANDSCAPE;
case NativeAdOptions.NATIVE_MEDIA_ASPECT_RATIO_PORTRAIT:
return NativeMediaAspectRatio.PORTRAIT;
case NativeAdOptions.NATIVE_MEDIA_ASPECT_RATIO_SQUARE:
return NativeMediaAspectRatio.SQUARE;
case NativeAdOptions.NATIVE_MEDIA_ASPECT_RATIO_UNKNOWN:
return NativeMediaAspectRatio.UNKNOWN;
default:
return NativeMediaAspectRatio.UNKNOWN;
}
}

static VideoOptions convertVideoOptions(com.google.android.gms.ads.VideoOptions videoOptions) {
return new VideoOptions.Builder()
.setClickToExpandRequested(videoOptions.getClickToExpandRequested())
.setCustomControlsRequested(videoOptions.getCustomControlsRequested())
.setStartMuted(videoOptions.getStartMuted())
.build();
}
}
Loading
Loading