You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
3.5 KiB

  1. // Copyright (C) 2015 Google, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.Reflection;
  16. using GoogleMobileAds.Common;
  17. namespace GoogleMobileAds.Api
  18. {
  19. public class InterstitialAd
  20. {
  21. private IInterstitialClient client;
  22. // Creates an InterstitialAd.
  23. public InterstitialAd(string adUnitId)
  24. {
  25. Type googleMobileAdsClientFactory = Type.GetType(
  26. "GoogleMobileAds.GoogleMobileAdsClientFactory,Assembly-CSharp");
  27. MethodInfo method = googleMobileAdsClientFactory.GetMethod(
  28. "BuildInterstitialClient",
  29. BindingFlags.Static | BindingFlags.Public);
  30. this.client = (IInterstitialClient)method.Invoke(null, null);
  31. client.CreateInterstitialAd(adUnitId);
  32. this.client.OnAdLoaded += (sender, args) =>
  33. {
  34. if (this.OnAdLoaded != null)
  35. {
  36. this.OnAdLoaded(this, args);
  37. }
  38. };
  39. this.client.OnAdFailedToLoad += (sender, args) =>
  40. {
  41. if (this.OnAdFailedToLoad != null)
  42. {
  43. this.OnAdFailedToLoad(this, args);
  44. }
  45. };
  46. this.client.OnAdOpening += (sender, args) =>
  47. {
  48. if (this.OnAdOpening != null)
  49. {
  50. this.OnAdOpening(this, args);
  51. }
  52. };
  53. this.client.OnAdClosed += (sender, args) =>
  54. {
  55. if (this.OnAdClosed != null)
  56. {
  57. this.OnAdClosed(this, args);
  58. }
  59. };
  60. this.client.OnAdLeavingApplication += (sender, args) =>
  61. {
  62. if (this.OnAdLeavingApplication != null)
  63. {
  64. this.OnAdLeavingApplication(this, args);
  65. }
  66. };
  67. }
  68. // These are the ad callback events that can be hooked into.
  69. public event EventHandler<EventArgs> OnAdLoaded;
  70. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
  71. public event EventHandler<EventArgs> OnAdOpening;
  72. public event EventHandler<EventArgs> OnAdClosed;
  73. public event EventHandler<EventArgs> OnAdLeavingApplication;
  74. // Loads an InterstitialAd.
  75. public void LoadAd(AdRequest request)
  76. {
  77. client.LoadAd(request);
  78. }
  79. // Determines whether the InterstitialAd has loaded.
  80. public bool IsLoaded()
  81. {
  82. return client.IsLoaded();
  83. }
  84. // Displays the InterstitialAd.
  85. public void Show()
  86. {
  87. client.ShowInterstitial();
  88. }
  89. // Destroys the InterstitialAd.
  90. public void Destroy()
  91. {
  92. client.DestroyInterstitial();
  93. }
  94. // Returns the mediation adapter class name.
  95. public string MediationAdapterClassName()
  96. {
  97. return this.client.MediationAdapterClassName();
  98. }
  99. }
  100. }