From 9d9cb28cf48231a652f9fd84299dc65b3ebd7eb9 Mon Sep 17 00:00:00 2001 From: Eric Leichtenschlag Date: Thu, 20 Feb 2014 09:37:31 -0800 Subject: [PATCH] Adding v1 of a Google Play services-compatible unity plugin --- .../Plugins/Android/AndroidManifest.xml | 33 ++ .../GoogleMobileAdsDemoScript.cs | 63 ++++ .../GoogleMobileAds/GoogleMobileAdsPlugin.cs | 114 +++++++ .../GoogleMobileAdsPlugin.prefab | Bin 0 -> 5736 bytes .../GoogleMobileAdsPlugin.unitypackage | Bin 0 -> 13453 bytes unity/android/README.md | 147 +++++++++ .../plugin-library/AndroidManifest.xml | 20 ++ .../android/plugin-library/project.properties | 16 + .../google/unity/GoogleMobileAdsPlugin.java | 298 ++++++++++++++++++ 9 files changed, 691 insertions(+) create mode 100755 unity/android/Assets/Plugins/Android/AndroidManifest.xml create mode 100755 unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsDemoScript.cs create mode 100755 unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.cs create mode 100755 unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.prefab create mode 100755 unity/android/GoogleMobileAdsPlugin.unitypackage create mode 100755 unity/android/README.md create mode 100755 unity/android/plugin-library/AndroidManifest.xml create mode 100755 unity/android/plugin-library/project.properties create mode 100755 unity/android/plugin-library/src/com/google/unity/GoogleMobileAdsPlugin.java diff --git a/unity/android/Assets/Plugins/Android/AndroidManifest.xml b/unity/android/Assets/Plugins/Android/AndroidManifest.xml new file mode 100755 index 000000000..bef6c92bd --- /dev/null +++ b/unity/android/Assets/Plugins/Android/AndroidManifest.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsDemoScript.cs b/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsDemoScript.cs new file mode 100755 index 000000000..64a96868e --- /dev/null +++ b/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsDemoScript.cs @@ -0,0 +1,63 @@ +using UnityEngine; + +// Example script showing how you can easily call into the GoogleMobileAdsPlugin. +public class GoogleMobileAdsDemoScript : MonoBehaviour { + + void Start() + { + print("Started"); + GoogleMobileAdsPlugin.CreateBannerView("ca-app-pub-9380760713574559/3453176828", + GoogleMobileAdsPlugin.AdSize.SmartBanner, + true); + print("Created Banner View"); + GoogleMobileAdsPlugin.RequestBannerAd(true); + print("Requested Banner Ad"); + } + + void OnEnable() + { + print("Registering for AdMob Events"); + GoogleMobileAdsPlugin.ReceivedAd += HandleReceivedAd; + GoogleMobileAdsPlugin.FailedToReceiveAd += HandleFailedToReceiveAd; + GoogleMobileAdsPlugin.ShowingOverlay += HandleShowingOverlay; + GoogleMobileAdsPlugin.DismissedOverlay += HandleDismissedOverlay; + GoogleMobileAdsPlugin.LeavingApplication += HandleLeavingApplication; + } + + void OnDisable() + { + print("Unregistering for AdMob Events"); + GoogleMobileAdsPlugin.ReceivedAd -= HandleReceivedAd; + GoogleMobileAdsPlugin.FailedToReceiveAd -= HandleFailedToReceiveAd; + GoogleMobileAdsPlugin.ShowingOverlay -= HandleShowingOverlay; + GoogleMobileAdsPlugin.DismissedOverlay -= HandleDismissedOverlay; + GoogleMobileAdsPlugin.LeavingApplication -= HandleLeavingApplication; + } + + public void HandleReceivedAd() + { + print("HandleReceivedAd event received"); + } + + public void HandleFailedToReceiveAd(string message) + { + print("HandleFailedToReceiveAd event received with message:"); + print(message); + } + + public void HandleShowingOverlay() + { + print("HandleShowingOverlay event received"); + } + + public void HandleDismissedOverlay() + { + print("HandleDismissedOverlay event received"); + } + + public void HandleLeavingApplication() + { + print("HandleLeavingApplication event received"); + } +} + diff --git a/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.cs b/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.cs new file mode 100755 index 000000000..4de870247 --- /dev/null +++ b/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.cs @@ -0,0 +1,114 @@ +using System; +using UnityEngine; + +// The Google Mobile Ads script used to call into the native Google Mobile Ads Plugin library. +public class GoogleMobileAdsPlugin : MonoBehaviour { + + // The plugin's class name. + private const string PluginClassName = "com.google.unity.GoogleMobileAdsPlugin"; + + // Defines string values for supported ad sizes. + public class AdSize + { + private string adSize; + private AdSize(string value) + { + this.adSize = value; + } + + public override string ToString() + { + return adSize; + } + + public static AdSize Banner = new AdSize("BANNER"); + public static AdSize MediumRectangle = new AdSize("IAB_MRECT"); + public static AdSize IABBanner = new AdSize("IAB_BANNER"); + public static AdSize Leaderboard = new AdSize("IAB_LEADERBOARD"); + public static AdSize SmartBanner = new AdSize("SMART_BANNER"); + } + + // These are the ad callback events that can be hooked into. + public static event Action ReceivedAd = delegate {}; + public static event Action FailedToReceiveAd = delegate {}; + public static event Action ShowingOverlay = delegate {}; + public static event Action DismissedOverlay = delegate {}; + public static event Action LeavingApplication = delegate {}; + + void Awake() + { + gameObject.name = this.GetType().ToString(); + SetCallbackHandlerName(gameObject.name); + DontDestroyOnLoad(this); + } + + // Create a banner view and add it into the view hierarchy. + public static void CreateBannerView(string publisherId, AdSize adSize, bool positionAtTop) + { + AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); + AndroidJavaObject activity = playerClass.GetStatic("currentActivity"); + AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName); + pluginClass.CallStatic("createBannerView", + new object[4] { activity, publisherId, adSize.ToString(), positionAtTop }); + } + + // Request a new ad for the banner view without any extras. + public static void RequestBannerAd(bool isTesting) + { + AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName); + pluginClass.CallStatic("requestBannerAd", new object[1] {isTesting}); + } + + // Request a new ad for the banner view with extras. + public static void RequestBannerAd(bool isTesting, string extras) + { + AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName); + pluginClass.CallStatic("requestBannerAd", new object[2] {isTesting, extras}); + } + + // Set the name of the callback handler so the right component gets ad callbacks. + public static void SetCallbackHandlerName(string callbackHandlerName) + { + AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName); + pluginClass.CallStatic("setCallbackHandlerName", new object[1] {callbackHandlerName}); + } + + // Hide the banner view from the screen. + public static void HideBannerView() + { + AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName); + pluginClass.CallStatic("hideBannerView"); + } + + // Show the banner view on the screen. + public static void ShowBannerView() { + AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName); + pluginClass.CallStatic("showBannerView"); + } + + public void OnReceiveAd(string unusedMessage) + { + ReceivedAd(); + } + + public void OnFailedToReceiveAd(string message) + { + FailedToReceiveAd(message); + } + + public void OnPresentScreen(string unusedMessage) + { + ShowingOverlay(); + } + + public void OnDismissScreen(string unusedMessage) + { + DismissedOverlay(); + } + + public void OnLeaveApplication(string unusedMessage) + { + LeavingApplication(); + } +} + diff --git a/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.prefab b/unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.prefab new file mode 100755 index 0000000000000000000000000000000000000000..c26d1727e0bb5555a243232b335f68367393e646 GIT binary patch literal 5736 zcmeHLy>A>v6d#8qBz!|cAmPgdkf(&%d3wzu zGvhJLW|}AC)D7p8v@p|~@2q{`2F2US&|RL`ac(doieZ?La6<^imj3%YgjoL-J9S~kz)+W1KWu;m)C=_P)K5#w zNrX^n>3<-|R3o8ygXa1MDP2tM+QbdH?Lj3sw-5@AwGPV{uk}N^TbbwS!KGFQS+Y@J zdLhSCdsy{AwsUHaC?QM_sWr7nLp{S6p}feFbW9qrk$KsPAJbB^(xh2tcgzC6;s1}5 z3`XFmS7XYPTaI1r32H;?^HZwz8Sgt*WDoR?S0uwBEaboj)X-J-)j1reIy5pf& z=r#-81Fjk!;#P*8>vfZ%M12#9m)=aV$a&hr4QbC}7}`I-D6Iy{b5?f*t{l#Iu-% zZ_}uVX)$VNSapST99B{ELLa3H%ym0n4QvF837hZ!h!Ds7BI*j`7}3*=p&kZBUc~Ee zyV%U!>z6>sOSFo-h$odevYljnN(lwG=qQjZm}ZTkwW_pw@1gB zsQnhVYX*$_f5`3n5F_pH@lJ8J0^Ij`!hk{15do4yoPssri25Qx0kA&Gh{Di60M@K0 z?M*p-KAE}~7N(TpeU^+vj$OonuG;C)mxA9fM=Xp{0mcc|w! zdJElE`>3Wm*@^;v3S*oPM?UP@n(`nlqeVNQ<~SL#6*!vDM5w8jMx|Q~?SfDp6WboD zUm!uGFk`$sczd3!W1-(@6~pzY&5xL*nHhu2&%fnIzVj{nAsvGUArhxIqarp;Rzyk| zJt>4|wQW){dALHdaUh8l=Do+J#;02)gbuBBiLZeWF{bi9jW}l)j4p(;0HEoD;79>B zM>66xl?&?gU|2IOhTm{mv7=Q6KZK=@(vV1EDNKzmCD+TsW@A^dZS+AA@}fTSSFCo68`h`|PqS4XoYCeXP$nlfdUt<}-IDMFR!uDGe+a>MFeAG}u+ zD7xTjE|MltvW}LD$_L|9PlKb+1x8HXuB(h&l7=PlIFMUwVJ`*N%Fvqgm}E-{dnS*{Tlu8xG>E7lg)9G zQa!xrMq1eMn2gm(umzHQLy2rg`jHpgNj)KXtIi{bvOIOme&b0SzCZ7LS$CqGv`MAL SA#Jer-TFy?`C=Y(G{8U3wz+}; literal 0 HcmV?d00001 diff --git a/unity/android/GoogleMobileAdsPlugin.unitypackage b/unity/android/GoogleMobileAdsPlugin.unitypackage new file mode 100755 index 0000000000000000000000000000000000000000..0f7e617e77f93d20450ad42569c5687b626a49bf GIT binary patch literal 13453 zcmb8VQ*17P5-nWYwzX@!{c7!Y*S2ljHg;`WyS8oHw%vaBoRk0If4DDqGP7nSlT0R= zWHJwnI2s0IS`<RJpyHg0tcf2%9<<&C3VEin;{HU z0RcuHbN3DRaD6RLo)BsA354s@TqK!ilTu+NL=NCIflHJKZwj0#W`$KDONO2Ell)Y+ z#Ay2Vhuy2jHId*Zk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VkmKlv2WQ4QwsdT{zZ0 zEof{i*=}2D0`Ks>^btvIY3k8vDg3S(OG9kbLy(}buMrZwj z#GzZ+Kw-*+{V!zNz=#w7FT6RIZ68pQy5=FVVXM0^%_l%g(B+h%79hv8%f+0X^K*Hvt zn2!~f{h "Import Package" -> "Custom Package". +3. Select the GoogleMobileAdsPlugin.unitypackage file. +4. Import all of the files for the plugins by selecting "Import". Make sure + to check for any conflicts with files. +5. Drag the GoogleMobileAds prefab from the Plugins/GoogleMobileAds/ folder into + your Unity scene. + +Note: If you already have an AndroidManifest.xml in Plugins/Android/, you can +just add the necessary activities and permissions to your existing manifest as +explained in https://developers.google.com/mobile-ads-sdk/docs#play instead of +importing the manifest from the .unitypackage. + +Additional dependencies: + +1. Add the google-play-services_lib folder, + located at /extras/google/google_play_services/libproject, + into the Plugins/Android folder of your project. + +This plugin also comes with an example plugin usage script, which is already +attached to the GoogleMobileAdsPlugin component for convenience. Simply edit +Plugins/GoogleMobileAdsPlugin/GoogleMobileAdsDemoScript.cs and include your ad +unit ID and run your project to see plugin working. + + +Google Mobile Ads Unity Plugin API +================================== +The plugin provides the following methods: + +1. CreateBannerView + + Takes in a publisherId string as well as a constant for the ad size. The + last boolean parameter denotes whether the ad should be shown at the top + or bottom of the screen. + + An example call placing the ad at the top of the screen is provided below: + + GoogleMobileAdsPlugin.CreateBannerView("INSERT_YOUR_AD_UNIT_ID_HERE", + GoogleMobileAdsPlugin.AdSize.Banner, + true); +2. RequestBannerAd + + Takes in a testing flag as well as an optional string representing a list + of extras. If you don't have any extras, you can request a live ad with: + + GoogleMobileAdsPlugin.RequestBannerAd(false); + + An example call requesting a test ad with some extras is shown below: + + string extras = "{\"color_bg\":\"AAAAFF\", \"color_text\":\"FFFFFF\"}"; + GoogleMobileAdsPlugin.RequestBannerAd(true, extras); + + NOTE: Make sure to use correctly formed JSON when passing an extras string. + If malformed JSON is passed, the extras will be ignored. + +3. HideBannerView + + Called after a BannerView has been created, this method can hide the ad from + showing on screen. An example call of this is shown below: + + GoogleMobileAdsPlugin.HideBannerView(); + +4. ShowBannerView + + Called after a BannerView has been created, this method can show any ad that + has been hidden. An example call of this is shown below: + + GoogleMobileAdsPlugin.ShowBannerView(); + +This plugin also allows you the option to listen for ad events. The following +events are supported: + + public static event Action ReceivedAd; + public static event Action FailedToReceiveAd; + public static event Action ShowingOverlay; + public static event Action DismissedOverlay; + public static event Action LeavingApplication; + +Registering for an event can be done using the += operater as is shown below: + + // Assume HandleReceivedAd is your function. + GoogleMobileAdsPlugin.ReceivedAd += HandleDidReceiveAd; + +Remember to un-register for events when you're cleaning up your GameObjects. +You can unregister using the -= operator as is shown below: + + // Assume HandleReceivedAd is your function. + GoogleMobileAdsPlugin.ReceivedAd -= HandleDidReceiveAd; + + +Updating the plugin +------------------- + +The plugin's .unitypackage only includes the compiled jar from the library +project. If you want to make changes to the library or see the source code, you +can find the project at +https://github.com/googleads/googleads-mobile-plugins/tree/master/unity. + +The library project depends on Unity's classes.jar, which can be found at +/Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidDevelopmentPlayer/bin +on Mac and usually at +C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidDevelopmentPlayer\bin +on Windows. + +Additional Resources +-------------------- +https://developers.google.com/mobile-ads-sdk/docs +https://groups.google.com/group/google-admob-ads-sdk +https://plus.google.com/+GoogleAdsDevelopers + diff --git a/unity/android/plugin-library/AndroidManifest.xml b/unity/android/plugin-library/AndroidManifest.xml new file mode 100755 index 000000000..3aa9bb25c --- /dev/null +++ b/unity/android/plugin-library/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/unity/android/plugin-library/project.properties b/unity/android/plugin-library/project.properties new file mode 100755 index 000000000..939080d15 --- /dev/null +++ b/unity/android/plugin-library/project.properties @@ -0,0 +1,16 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system edit +# "ant.properties", and override values to adapt the script to your +# project structure. +# +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt + +android.library=true +# Project target. +target=android-19 +android.library.reference.1=../../libproject/google-play-services_lib diff --git a/unity/android/plugin-library/src/com/google/unity/GoogleMobileAdsPlugin.java b/unity/android/plugin-library/src/com/google/unity/GoogleMobileAdsPlugin.java new file mode 100755 index 000000000..0e130605e --- /dev/null +++ b/unity/android/plugin-library/src/com/google/unity/GoogleMobileAdsPlugin.java @@ -0,0 +1,298 @@ +// Copyright 2014 Google Inc. All Rights Reserved. + +package com.google.unity; + +import com.google.android.gms.ads.AdListener; +import com.google.android.gms.ads.AdRequest; +import com.google.android.gms.ads.AdSize; +import com.google.android.gms.ads.AdView; +import com.google.android.gms.ads.mediation.admob.AdMobExtras; + +import android.app.Activity; +import android.graphics.Color; +import android.os.Bundle; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.widget.FrameLayout; + +import com.unity3d.player.UnityPlayer; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Iterator; + +/** + * This class represents the native implementation for the Google Mobile Ads Unity plugin. This + * class is used to request Google Mobile ads natively via the Google Mobile Ads library in Google + * Play services. The Google Play services library is a dependency for this plugin. + */ +public class GoogleMobileAdsPlugin { + /** The plugin version. */ + public static final String VERSION = "1.0"; + + /** Tag used for logging statements. */ + private static final String LOGTAG = "GoogleMobileAdsPlugin"; + + /** The {@code GoogleMobileAdsPlugin} singleton. */ + private static final GoogleMobileAdsPlugin instance = new GoogleMobileAdsPlugin(); + + /** The {@link AdView} to display to the user. */ + private AdView adView; + + /** The {@code Activity} that the banner will be displayed in. */ + private Activity activity; + + /** The Unity {@code GameObject} that should receive messages about ad events. */ + private String callbackHandlerName; + + /** Creates an instance of {@code GoogleMobileAdsPlugin}. */ + private GoogleMobileAdsPlugin() { + // Prevent instantiation. + } + + /** + * Gets a singleton instance of {@code GoogleMobileAdsPlugin}. + * + * @return the {@code GoogleMobileAdsPlugin} singleton. + */ + public static GoogleMobileAdsPlugin instance() { + return instance; + } + + /** + * Creates an {@link AdView} to old ads. + * + * @param activity The activity to place the {@code AdView}. + * @param publisherId Your ad unit ID. + * @param adSizeString A string ad size constant representing the desired ad size. + * @param positionAtTop True to position the ad at the top of the screen. False to position + * the ad at the bottom of the screen. + */ + public static void createBannerView(final Activity activity, final String publisherId, + final String adSizeString, final boolean positionAtTop) { + final GoogleMobileAdsPlugin plugin = GoogleMobileAdsPlugin.instance(); + plugin.activity = activity; + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + AdSize adSize = GoogleMobileAdsPlugin.adSizeFromSize(adSizeString); + if (adSize == null) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, "AdSize is null. Did you use an AdSize constant?"); + return; + } + plugin.adView = new AdView(activity); + // Setting the background color works around an issue where the first ad isn't visible. + plugin.adView.setBackgroundColor(Color.TRANSPARENT); + plugin.adView.setAdUnitId(publisherId); + plugin.adView.setAdSize(adSize); + plugin.adView.setAdListener(new AdListener() { + @Override + public void onAdLoaded() { + if (plugin.callbackHandlerName != null && !plugin.callbackHandlerName.isEmpty()) { + UnityPlayer.UnitySendMessage(plugin.callbackHandlerName, "OnReceiveAd", ""); + } + } + + @Override + public void onAdFailedToLoad(int errorCode) { + if (plugin.callbackHandlerName != null && !plugin.callbackHandlerName.isEmpty()) { + UnityPlayer.UnitySendMessage(plugin.callbackHandlerName, "OnFailedToReceiveAd", + getErrorReason(errorCode)); + } + } + + @Override + public void onAdOpened() { + if (plugin.callbackHandlerName != null && !plugin.callbackHandlerName.isEmpty()) { + UnityPlayer.UnitySendMessage(plugin.callbackHandlerName, "OnPresentScreen", ""); + } + } + + @Override + public void onAdClosed() { + if (plugin.callbackHandlerName != null && !plugin.callbackHandlerName.isEmpty()) { + UnityPlayer.UnitySendMessage(plugin.callbackHandlerName, "OnDismissScreen", ""); + } + } + + @Override + public void onAdLeftApplication() { + if (plugin.callbackHandlerName != null && !plugin.callbackHandlerName.isEmpty()) { + UnityPlayer.UnitySendMessage(plugin.callbackHandlerName, "OnLeaveApplication", ""); + } + } + }); + FrameLayout.LayoutParams adParams = new FrameLayout.LayoutParams( + FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); + adParams.gravity = positionAtTop ? Gravity.TOP : Gravity.BOTTOM; + activity.addContentView(plugin.adView, adParams); + } + }); + } + + /** + * Sets the name of the {@code GameComponent} that should listen for ad events. + * + * @param callbackHandlerName the {@code GameComponent} that should listen for ad events. + */ + public static void setCallbackHandlerName(String callbackHandlerName) { + final GoogleMobileAdsPlugin plugin = GoogleMobileAdsPlugin.instance(); + plugin.callbackHandlerName = callbackHandlerName; + } + + /** + * Requests a banner ad without any extras. This method should be called only after a banner + * view has been created. + * + * @param isTesting True to enable test ads. False to get network ads. + */ + public static void requestBannerAd(final boolean isTesting) { + requestBannerAd(isTesting, null); + } + + /** + * Requests a banner ad. This method should be called only after a banner view has been created. + * + * @param isTesting True to enable test ads. False to get network ads. + * @param extrasJson A json object with key/value pairs representing what extras to pass in the + * request. + */ + public static void requestBannerAd(final boolean isTesting, final String extrasJson) { + final GoogleMobileAdsPlugin plugin = GoogleMobileAdsPlugin.instance(); + if (plugin.activity == null) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, + "Activity is null. Call createBannerView before requestBannerAd."); + return; + } + + plugin.activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (plugin.adView == null) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, "AdView is null. Aborting requestBannerAd."); + } else { + AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); + if (isTesting) { + // This will request test ads on the emulator only. You can get your hashed device ID + // from LogCat when making a live request. Pass this hashed device ID to addTestDevice + // to get test ads on your device. + adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); + } + Bundle bundle = new Bundle(); + if (extrasJson != null) { + try { + JSONObject extrasObject = new JSONObject(extrasJson); + Iterator extrasIterator = extrasObject.keys(); + while (extrasIterator.hasNext()) { + String key = extrasIterator.next(); + bundle.putString(key, extrasObject.getString(key)); + } + } catch (JSONException exception) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, + "Extras are malformed. Aborting requestBannerAd."); + return; + } + } + bundle.putInt("unity", 1); + AdMobExtras extras = new AdMobExtras(bundle); + adRequestBuilder.addNetworkExtras(extras); + plugin.adView.loadAd(adRequestBuilder.build()); + } + } + }); + } + + /** + * Sets the banner view to be visible. + */ + public static void showBannerView() { + final GoogleMobileAdsPlugin plugin = GoogleMobileAdsPlugin.instance(); + if (plugin.activity == null) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, + "Activity is null. Call createBannerView before showBannerView."); + return; + } + + plugin.activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (plugin.adView == null) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, "AdView is null. Aborting showBannerView."); + return; + } + plugin.adView.setVisibility(View.VISIBLE); + } + }); + } + + /** + * Hides the banner view from the user. + */ + public static void hideBannerView() { + final GoogleMobileAdsPlugin plugin = GoogleMobileAdsPlugin.instance(); + if (plugin.activity == null) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, + "Activity is null. Call createBannerView before hideBannerView."); + return; + } + + plugin.activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (plugin.adView == null) { + Log.e(GoogleMobileAdsPlugin.LOGTAG, "AdView is null. Aborting hideBannerView."); + return; + } + plugin.adView.setVisibility(View.GONE); + } + }); + } + + /** + * Gets an AdSize object from the string size passed in from JavaScript. Returns null if an + * improper string is provided. + * + * @param size The string size representing an ad format constant. + * @return An AdSize object used to create a banner. + */ + private static AdSize adSizeFromSize(String size) { + if ("BANNER".equals(size)) { + return AdSize.BANNER; + } else if ("IAB_MRECT".equals(size)) { + return AdSize.MEDIUM_RECTANGLE; + } else if ("IAB_BANNER".equals(size)) { + return AdSize.FULL_BANNER; + } else if ("IAB_LEADERBOARD".equals(size)) { + return AdSize.LEADERBOARD; + } else if ("SMART_BANNER".equals(size)) { + return AdSize.SMART_BANNER; + } else { + Log.w(LOGTAG, String.format("Unexpected ad size string: %s", size)); + return null; + } + } + + /** + * Gets a string error reason from an error code. + * + * @param errorCode The error code. + * @return The reason for the error. + */ + private static String getErrorReason(int errorCode) { + switch(errorCode) { + case AdRequest.ERROR_CODE_INTERNAL_ERROR: + return "Internal error"; + case AdRequest.ERROR_CODE_INVALID_REQUEST: + return "Invalid request"; + case AdRequest.ERROR_CODE_NETWORK_ERROR: + return "Network Error"; + case AdRequest.ERROR_CODE_NO_FILL: + return "No fill"; + default: + Log.w(LOGTAG, String.format("Unexpected error code: %s", errorCode)); + return ""; + } + } +}