Skip to content

Commit

Permalink
feat(cordova-android): add banner size option
Browse files Browse the repository at this point in the history
  • Loading branch information
ratson committed Dec 1, 2018
1 parent 8865ddb commit 14f3367
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/cordova/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<source-file src="src/android/Action.java" target-dir="src/admob/plugin" />
<source-file src="src/android/Actions.java" target-dir="src/admob/plugin" />
<source-file src="src/android/AdMob.java" target-dir="src/admob/plugin" />
<source-file src="src/android/AdSizeType.java" target-dir="src/admob/plugin" />
<source-file src="src/android/Events.java" target-dir="src/admob/plugin" />
<source-file src="src/android/ads/AdBase.java" target-dir="src/admob/plugin/ads" />
<source-file src="src/android/ads/BannerAd.java" target-dir="src/admob/plugin/ads" />
Expand Down
37 changes: 37 additions & 0 deletions packages/cordova/scripts/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ const Events: { [index: string]: string } = {
}
/* tslint:enable:object-literal-sort-keys */

const AdSizeTypes = [
'BANNER',
'LARGE_BANNER',
'MEDIUM_RECTANGLE',
'FULL_BANNER',
'LEADERBOARD',
'SMART_BANNER',
]

function buildActionsJava(): string {
const linesActions = Object.keys(Actions)
.map(k => ` static final String ${k.toUpperCase()} = "${Actions[k]}";`)
Expand Down Expand Up @@ -83,6 +92,27 @@ ${linesEvents}
`
}

function buildAdSizeTypeJava(): string {
return `// ${warnMessage}
package admob.plugin;
import com.google.android.gms.ads.AdSize;
public enum AdSizeType {
${AdSizeTypes.map(s => `${s}`).join(', ')};
public static AdSize getAdSize(Object adSize) {
${AdSizeTypes.map(
s => ` if (AdSizeType.${s}.equals(adSize)) {
return AdSize.${s};
}`,
).join('\n')}
return null;
}
}
`
}

function buildConstantsSwift(): string {
const linesEvents = Object.keys(Events)
.map(k => ` static let ${_.camelCase(k)} = "${Events[k]}"`)
Expand All @@ -107,6 +137,8 @@ function buildConstantsTs(): string {
.sort()
.join('\n')

const adSizeType = AdSizeTypes.map(s => ` ${s},`).join('\n')

return `// ${warnMessage}
export const enum NativeActions {
Service = 'AdMob',
Expand All @@ -116,6 +148,10 @@ ${linesActions}
export const enum Events {
${linesEvents}
}
export enum AdSizeType {
${adSizeType}
}
`
}

Expand Down Expand Up @@ -150,6 +186,7 @@ async function updateConfigXML() {

async function main() {
const l = [
{ filepath: 'src/android/AdSizeType.java', f: buildAdSizeTypeJava },
{ filepath: 'src/android/Actions.java', f: buildActionsJava },
{ filepath: 'src/android/Events.java', f: buildEventsJava },
{ filepath: 'src/ios/AMSConstants.swift', f: buildConstantsSwift },
Expand Down
15 changes: 15 additions & 0 deletions packages/cordova/src/android/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;

import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -21,10 +22,24 @@ public int optId() {
return opts.optInt("id");
}


public AdBase getAd() {
return AdBase.getAd(optId());
}

public AdSize getAdSize() {
final String name = "size";
if (!this.opts.has(name)) {
return AdSize.SMART_BANNER;
}
AdSize adSize = AdSizeType.getAdSize(this.opts.opt(name));
if (adSize != null) {
return adSize;
}
JSONObject adSizeObj = this.opts.optJSONObject(name);
return new AdSize(adSizeObj.optInt("width"), adSizeObj.optInt("height"));
}

public String getAdUnitID() {
return this.opts.optString("adUnitID");
}
Expand Down
30 changes: 30 additions & 0 deletions packages/cordova/src/android/AdSizeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
package admob.plugin;

import com.google.android.gms.ads.AdSize;

public enum AdSizeType {
BANNER, LARGE_BANNER, MEDIUM_RECTANGLE, FULL_BANNER, LEADERBOARD, SMART_BANNER;

public static AdSize getAdSize(Object adSize) {
if (AdSizeType.BANNER.equals(adSize)) {
return AdSize.BANNER;
}
if (AdSizeType.LARGE_BANNER.equals(adSize)) {
return AdSize.LARGE_BANNER;
}
if (AdSizeType.MEDIUM_RECTANGLE.equals(adSize)) {
return AdSize.MEDIUM_RECTANGLE;
}
if (AdSizeType.FULL_BANNER.equals(adSize)) {
return AdSize.FULL_BANNER;
}
if (AdSizeType.LEADERBOARD.equals(adSize)) {
return AdSize.LEADERBOARD;
}
if (AdSizeType.SMART_BANNER.equals(adSize)) {
return AdSize.SMART_BANNER;
}
return null;
}
}
8 changes: 5 additions & 3 deletions packages/cordova/src/android/ads/BannerAd.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
public class BannerAd extends AdBase {
private AdView adView;
private ViewGroup parentView;
private AdSize adSize;

BannerAd(int id, String adUnitID) {
BannerAd(int id, String adUnitID, AdSize adSize) {
super(id, adUnitID);
this.adSize = adSize;
}

public static boolean executeShowAction(Action action, CallbackContext callbackContext) {
Expand All @@ -31,7 +33,7 @@ public static boolean executeShowAction(Action action, CallbackContext callbackC
public void run() {
BannerAd bannerAd = (BannerAd) action.getAd();
if (bannerAd == null) {
bannerAd = new BannerAd(action.optId(), action.getAdUnitID());
bannerAd = new BannerAd(action.optId(), action.getAdUnitID(), action.getAdSize());
}
bannerAd.show(action.buildAdRequest());

Expand Down Expand Up @@ -64,7 +66,7 @@ public void show(AdRequest adRequest) {
if (adView == null) {
adView = new AdView(plugin.cordova.getActivity());
adView.setAdUnitId(adUnitID);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdSize(adSize);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Expand Down
5 changes: 3 additions & 2 deletions packages/cordova/ts/banner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IAdRequest } from '@admob-plus/core'
import { AdSizeType, IBannerRequest } from '@admob-plus/core'

import { AdBase, execAsync, NativeActions, TestIds } from './base'

Expand All @@ -8,9 +8,10 @@ export default class Banner extends AdBase {
protected testIdForAndroid = TestIds.banner_android
protected testIdForIOS = TestIds.banner_ios

public show(opts: IAdRequest = {}) {
public show(opts: IBannerRequest) {
return execAsync(NativeActions.banner_show, [
{
size: AdSizeType.SMART_BANNER,
...opts,
adUnitID: this.resolveAdUnitID(opts.id),
id: AD_ID,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ export const enum Events {
reward_video_reward = 'admob.reward_video.reward',
reward_video_start = 'admob.reward_video.start',
}

export enum AdSizeType {
BANNER,
LARGE_BANNER,
MEDIUM_RECTANGLE,
FULL_BANNER,
LEADERBOARD,
SMART_BANNER,
}
20 changes: 20 additions & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ export interface IAdRequest {
childDirectedTreatment?: boolean
underAgeOfConsent?: boolean
}

export enum AdSizeType {
BANNER,
LARGE_BANNER,
MEDIUM_RECTANGLE,
FULL_BANNER,
LEADERBOARD,
SMART_BANNER,
}

type AdSize =
| AdSizeType
| {
width: number;
height: number;
}

export interface IBannerRequest extends IAdRequest {
size?: AdSize
}
4 changes: 2 additions & 2 deletions packages/ionic-ngx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'
import { cordova, IonicNativePlugin } from '@ionic-native/core'
import { fromEvent, Observable } from 'rxjs'

import { IAdRequest } from '@admob-plus/core'
import { IAdRequest, IBannerRequest } from '@admob-plus/core'

export class Banner extends IonicNativePlugin {
public static plugin = 'cordova-admob-plus'
Expand All @@ -13,7 +13,7 @@ export class Banner extends IonicNativePlugin {
return cordova(this, 'hide', { otherPromise: true }, arguments)
}

public show(opts: IAdRequest): Promise<any> {
public show(opts: IBannerRequest): Promise<any> {
return cordova(this, 'show', { otherPromise: true }, arguments)
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ionic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core'
import { Observable } from 'rxjs/Observable'
import { fromEvent } from 'rxjs/observable/fromEvent'

import { IAdRequest } from '@admob-plus/core'
import { IAdRequest, IBannerRequest } from '@admob-plus/core'

@Plugin({
plugin: 'cordova-admob-plus',
Expand All @@ -17,7 +17,7 @@ export class Banner {
}

@Cordova({ otherPromise: true })
public show(opts: IAdRequest): Promise<any> {
public show(opts: IBannerRequest): Promise<any> {
return Promise.resolve()
}
}
Expand Down

0 comments on commit 14f3367

Please sign in to comment.