Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

AdSizes for AdMob banner ads #402

Merged
merged 7 commits into from
Mar 19, 2018
Merged
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 @@ -6,6 +6,7 @@

import android.app.Activity;
import android.view.Gravity;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.MobileAds;
import com.google.firebase.FirebaseApp;
import io.flutter.plugin.common.MethodCall;
Expand Down Expand Up @@ -38,14 +39,61 @@ private FirebaseAdMobPlugin(Registrar registrar, MethodChannel channel) {
private void callInitialize(MethodCall call, Result result) {
String appId = call.argument("appId");
if (appId == null || appId.isEmpty()) {
result.error("no_app_id", "a non-empty AdMob appId was not provided", null);
result.error("no_app_id", "a null or empty AdMob appId was provided", null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE

return;
}
MobileAds.initialize(registrar.context(), appId);
result.success(Boolean.TRUE);
}

private void callLoadAd(MobileAd ad, MethodCall call, Result result) {
private void callLoadBannerAd(
int id, Activity activity, MethodChannel channel, MethodCall call, Result result) {
String adUnitId = call.argument("adUnitId");
if (adUnitId == null || adUnitId.isEmpty()) {
result.error("no_unit_id", "a null or empty adUnitId was provided for ad id=" + id, null);
return;
}

int width = call.argument("width");
int height = call.argument("height");
String adSizeType = call.argument("adSizeType");

if (!adSizeType.equals("AdSizeType.WidthAndHeight")
&& !adSizeType.equals("AdSizeType.SmartBanner")) {
String errMsg =
String.format("an invalid adSizeType (%s) was provided for banner id=%d", adSizeType, id);
result.error("invalid_adsizetype", errMsg, null);
}

if (adSizeType.equals("AdSizeType.WidthAndHeight") && (width <= 0 || height <= 0)) {
String errMsg =
String.format(
"an invalid AdSize (%d, %d) was provided for banner id=%d", width, height, id);
result.error("invalid_adsize", errMsg, null);
}

AdSize adSize;
if (adSizeType.equals("AdSizeType.SmartBanner")) {
adSize = AdSize.SMART_BANNER;
} else {
adSize = new AdSize(width, height);
}

MobileAd.Banner banner = MobileAd.createBanner(id, adSize, activity, channel);

if (banner.status != MobileAd.Status.CREATED) {
if (banner.status == MobileAd.Status.FAILED)
result.error("load_failed_ad", "cannot reload a failed ad, id=" + id, null);
else result.success(Boolean.TRUE); // The ad was already loaded.
return;
}

Map<String, Object> targetingInfo = call.argument("targetingInfo");
banner.load(adUnitId, targetingInfo);
result.success(Boolean.TRUE);
}

private void callLoadInterstitialAd(MobileAd ad, MethodCall call, Result result) {
if (ad.status != MobileAd.Status.CREATED) {
if (ad.status == MobileAd.Status.FAILED)
result.error("load_failed_ad", "cannot reload a failed ad, id=" + ad.id, null);
Expand All @@ -55,7 +103,8 @@ private void callLoadAd(MobileAd ad, MethodCall call, Result result) {

String adUnitId = call.argument("adUnitId");
if (adUnitId == null || adUnitId.isEmpty()) {
result.error("no_unit_id", "a non-empty adUnitId was not provided for ad id=" + ad.id, null);
result.error(
"no_adunit_id", "a null or empty adUnitId was provided for ad id=" + ad.id, null);
return;
}
Map<String, Object> targetingInfo = call.argument("targetingInfo");
Expand Down Expand Up @@ -142,10 +191,10 @@ public void onMethodCall(MethodCall call, Result result) {

switch (call.method) {
case "loadBannerAd":
callLoadAd(MobileAd.createBanner(id, activity, channel), call, result);
callLoadBannerAd(id, activity, channel, call, result);
break;
case "loadInterstitialAd":
callLoadAd(MobileAd.createInterstitial(id, activity, channel), call, result);
callLoadInterstitialAd(MobileAd.createInterstitial(id, activity, channel), call, result);
break;
case "loadRewardedVideoAd":
callLoadRewardedVideoAd(call, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ private MobileAd(int id, Activity activity, MethodChannel channel) {
allAds.put(id, this);
}

static Banner createBanner(Integer id, Activity activity, MethodChannel channel) {
static Banner createBanner(Integer id, AdSize adSize, Activity activity, MethodChannel channel) {
MobileAd ad = getAdForId(id);
return (ad != null) ? (Banner) ad : new Banner(id, activity, channel);
return (ad != null) ? (Banner) ad : new Banner(id, adSize, activity, channel);
}

static Interstitial createInterstitial(Integer id, Activity activity, MethodChannel channel) {
Expand Down Expand Up @@ -123,9 +123,11 @@ public void onAdClosed() {

static class Banner extends MobileAd {
private AdView adView;
private AdSize adSize;

private Banner(Integer id, Activity activity, MethodChannel channel) {
private Banner(Integer id, AdSize adSize, Activity activity, MethodChannel channel) {
super(id, activity, channel);
this.adSize = adSize;
}

@Override
Expand All @@ -134,7 +136,7 @@ void load(String adUnitId, Map<String, Object> targetingInfo) {
status = Status.LOADING;

adView = new AdView(activity);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdSize(adSize);
adView.setAdUnitId(adUnitId);
adView.setAdListener(this);

Expand Down Expand Up @@ -167,7 +169,7 @@ void show() {
activity.addContentView(
content,
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/firebase_admob/example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
.DS_Store
.atom/
.DS_Store
.flutter-plugins
.idea
.packages
.pub/
build/
ios/.generated/
ios/Flutter/app.flx
packages
pubspec.lock
.flutter-plugins
pubspec.lock
1 change: 1 addition & 0 deletions packages/firebase_admob/example/ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
6E27B1151F0DAFA70028FD65 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 6E27B1141F0DAFA70028FD65 /* GoogleService-Info.plist */; };
Expand Down Expand Up @@ -43,8 +43,8 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
6E27B1141F0DAFA70028FD65 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -248,13 +248,16 @@
files = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
Expand Down Expand Up @@ -291,9 +294,12 @@
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/.symlinks/flutter/ios/Flutter.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down
105 changes: 54 additions & 51 deletions packages/firebase_admob/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class _MyAppState extends State<MyApp> {
BannerAd createBannerAd() {
return new BannerAd(
adUnitId: BannerAd.testAdUnitId,
size: AdSize.banner,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
print("BannerAd event $event");
Expand Down Expand Up @@ -78,59 +79,61 @@ class _MyAppState extends State<MyApp> {
appBar: new AppBar(
title: const Text('AdMob Plugin example app'),
),
body: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new RaisedButton(
child: const Text('SHOW BANNER'),
body: new SingleChildScrollView(
child: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new RaisedButton(
child: const Text('SHOW BANNER'),
onPressed: () {
_bannerAd ??= createBannerAd();
_bannerAd
..load()
..show();
}),
new RaisedButton(
child: const Text('REMOVE BANNER'),
onPressed: () {
_bannerAd?.dispose();
_bannerAd = null;
}),
new RaisedButton(
child: const Text('LOAD INTERSTITIAL'),
onPressed: () {
_bannerAd ??= createBannerAd();
_bannerAd
..load()
..show();
}),
new RaisedButton(
child: const Text('REMOVE BANNER'),
_interstitialAd?.dispose();
_interstitialAd = createInterstitialAd()..load();
},
),
new RaisedButton(
child: const Text('SHOW INTERSTITIAL'),
onPressed: () {
_bannerAd?.dispose();
_bannerAd = null;
}),
new RaisedButton(
child: const Text('LOAD INTERSTITIAL'),
onPressed: () {
_interstitialAd?.dispose();
_interstitialAd = createInterstitialAd()..load();
},
),
new RaisedButton(
child: const Text('SHOW INTERSTITIAL'),
onPressed: () {
_interstitialAd?.show();
},
),
new RaisedButton(
child: const Text('LOAD REWARDED VIDEO'),
onPressed: () {
RewardedVideoAd.instance.load(
adUnitId: RewardedVideoAd.testAdUnitId,
targetingInfo: targetingInfo);
},
),
new RaisedButton(
child: const Text('SHOW REWARDED VIDEO'),
onPressed: () {
RewardedVideoAd.instance.show();
},
),
new Text("You have $_coins coins."),
].map((Widget button) {
return new Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: button,
);
}).toList(),
_interstitialAd?.show();
},
),
new RaisedButton(
child: const Text('LOAD REWARDED VIDEO'),
onPressed: () {
RewardedVideoAd.instance.load(
adUnitId: RewardedVideoAd.testAdUnitId,
targetingInfo: targetingInfo);
},
),
new RaisedButton(
child: const Text('SHOW REWARDED VIDEO'),
onPressed: () {
RewardedVideoAd.instance.show();
},
),
new Text("You have $_coins coins."),
].map((Widget button) {
return new Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: button,
);
}).toList(),
),
),
),
),
Expand Down
4 changes: 3 additions & 1 deletion packages/firebase_admob/ios/Classes/FLTMobileAd.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ typedef enum : NSUInteger {
@end

@interface FLTBannerAd : FLTMobileAd<GADBannerViewDelegate>
+ (instancetype)withId:(NSNumber *)mobileAdId channel:(FlutterMethodChannel *)channel;
+ (instancetype)withId:(NSNumber *)mobileAdId
adSize:(GADAdSize)adSize
channel:(FlutterMethodChannel *)channel;
@end

@interface FLTInterstitialAd : FLTMobileAd<GADInterstitialDelegate>
Expand Down
21 changes: 18 additions & 3 deletions packages/firebase_admob/ios/Classes/FLTMobileAd.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,32 @@ - (NSString *)description {

@implementation FLTBannerAd
GADBannerView *_banner;
GADAdSize _adSize;

+ (instancetype)withId:(NSNumber *)mobileAdId channel:(FlutterMethodChannel *)channel {
+ (instancetype)withId:(NSNumber *)mobileAdId
adSize:(GADAdSize)adSize
channel:(FlutterMethodChannel *)channel {
FLTMobileAd *ad = [FLTMobileAd getAdForId:mobileAdId];
return ad != nil ? (FLTBannerAd *)ad
: [[FLTBannerAd alloc] initWithId:mobileAdId channel:channel];
: [[FLTBannerAd alloc] initWithId:mobileAdId adSize:adSize channel:channel];
}

- (instancetype)initWithId:mobileAdId
adSize:(GADAdSize)adSize
channel:(FlutterMethodChannel *)channel {
self = [super initWithId:mobileAdId channel:channel];
if (self) {
_adSize = adSize;
return self;
}

return nil;
}

- (void)loadWithAdUnitId:(NSString *)adUnitId targetingInfo:(NSDictionary *)targetingInfo {
if (_status != CREATED) return;
_status = LOADING;
_banner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
_banner = [[GADBannerView alloc] initWithAdSize:_adSize];
_banner.delegate = self;
_banner.adUnitID = adUnitId;
_banner.rootViewController = [FLTMobileAd rootViewController];
Expand Down
Loading