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.
 
 
 

202 lines
6.7 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. #if UNITY_IOS
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Runtime.InteropServices;
  18. using UnityEngine;
  19. using GoogleMobileAds.Api;
  20. using GoogleMobileAds.Common;
  21. namespace GoogleMobileAds.iOS
  22. {
  23. public class InterstitialClient : IInterstitialClient, IDisposable
  24. {
  25. private IntPtr interstitialPtr;
  26. private IntPtr interstitialClientPtr;
  27. #region Interstitial callback types
  28. internal delegate void GADUInterstitialDidReceiveAdCallback(IntPtr interstitialClient);
  29. internal delegate void GADUInterstitialDidFailToReceiveAdWithErrorCallback(
  30. IntPtr interstitialClient, string error);
  31. internal delegate void GADUInterstitialWillPresentScreenCallback(IntPtr interstitialClient);
  32. internal delegate void GADUInterstitialDidDismissScreenCallback(IntPtr interstitialClient);
  33. internal delegate void GADUInterstitialWillLeaveApplicationCallback(
  34. IntPtr interstitialClient);
  35. #endregion
  36. public event EventHandler<EventArgs> OnAdLoaded;
  37. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
  38. public event EventHandler<EventArgs> OnAdOpening;
  39. public event EventHandler<EventArgs> OnAdClosed;
  40. public event EventHandler<EventArgs> OnAdLeavingApplication;
  41. // This property should be used when setting the interstitialPtr.
  42. private IntPtr InterstitialPtr
  43. {
  44. get
  45. {
  46. return this.interstitialPtr;
  47. }
  48. set
  49. {
  50. Externs.GADURelease(this.interstitialPtr);
  51. this.interstitialPtr = value;
  52. }
  53. }
  54. #region IInterstitialClient implementation
  55. // Creates an interstitial ad.
  56. public void CreateInterstitialAd(string adUnitId)
  57. {
  58. this.interstitialClientPtr = (IntPtr)GCHandle.Alloc(this);
  59. this.InterstitialPtr = Externs.GADUCreateInterstitial(this.interstitialClientPtr, adUnitId);
  60. Externs.GADUSetInterstitialCallbacks(
  61. this.InterstitialPtr,
  62. InterstitialDidReceiveAdCallback,
  63. InterstitialDidFailToReceiveAdWithErrorCallback,
  64. InterstitialWillPresentScreenCallback,
  65. InterstitialDidDismissScreenCallback,
  66. InterstitialWillLeaveApplicationCallback);
  67. }
  68. // Loads an ad.
  69. public void LoadAd(AdRequest request)
  70. {
  71. IntPtr requestPtr = Utils.BuildAdRequest(request);
  72. Externs.GADURequestInterstitial(this.InterstitialPtr, requestPtr);
  73. Externs.GADURelease(requestPtr);
  74. }
  75. // Checks if interstitial has loaded.
  76. public bool IsLoaded()
  77. {
  78. return Externs.GADUInterstitialReady(this.InterstitialPtr);
  79. }
  80. // Presents the interstitial ad on the screen
  81. public void ShowInterstitial()
  82. {
  83. Externs.GADUShowInterstitial(this.InterstitialPtr);
  84. }
  85. // Destroys the interstitial ad.
  86. public void DestroyInterstitial()
  87. {
  88. this.InterstitialPtr = IntPtr.Zero;
  89. }
  90. // Returns the mediation adapter class name.
  91. public string MediationAdapterClassName()
  92. {
  93. return Externs.GADUMediationAdapterClassNameForInterstitial(this.InterstitialPtr);
  94. }
  95. public void Dispose()
  96. {
  97. this.DestroyInterstitial();
  98. ((GCHandle)this.interstitialClientPtr).Free();
  99. }
  100. ~InterstitialClient()
  101. {
  102. this.Dispose();
  103. }
  104. #endregion
  105. #region Interstitial callback methods
  106. [MonoPInvokeCallback(typeof(GADUInterstitialDidReceiveAdCallback))]
  107. private static void InterstitialDidReceiveAdCallback(IntPtr interstitialClient)
  108. {
  109. InterstitialClient client = IntPtrToInterstitialClient(interstitialClient);
  110. if (client.OnAdLoaded != null)
  111. {
  112. client.OnAdLoaded(client, EventArgs.Empty);
  113. }
  114. }
  115. [MonoPInvokeCallback(typeof(GADUInterstitialDidFailToReceiveAdWithErrorCallback))]
  116. private static void InterstitialDidFailToReceiveAdWithErrorCallback(
  117. IntPtr interstitialClient, string error)
  118. {
  119. InterstitialClient client = IntPtrToInterstitialClient(interstitialClient);
  120. if (client.OnAdFailedToLoad != null)
  121. {
  122. AdFailedToLoadEventArgs args = new AdFailedToLoadEventArgs()
  123. {
  124. Message = error
  125. };
  126. client.OnAdFailedToLoad(client, args);
  127. }
  128. }
  129. [MonoPInvokeCallback(typeof(GADUInterstitialWillPresentScreenCallback))]
  130. private static void InterstitialWillPresentScreenCallback(IntPtr interstitialClient)
  131. {
  132. InterstitialClient client = IntPtrToInterstitialClient(interstitialClient);
  133. if (client.OnAdOpening != null)
  134. {
  135. client.OnAdOpening(client, EventArgs.Empty);
  136. }
  137. }
  138. [MonoPInvokeCallback(typeof(GADUInterstitialDidDismissScreenCallback))]
  139. private static void InterstitialDidDismissScreenCallback(IntPtr interstitialClient)
  140. {
  141. InterstitialClient client = IntPtrToInterstitialClient(interstitialClient);
  142. if (client.OnAdClosed != null)
  143. {
  144. client.OnAdClosed(client, EventArgs.Empty);
  145. }
  146. }
  147. [MonoPInvokeCallback(typeof(GADUInterstitialWillLeaveApplicationCallback))]
  148. private static void InterstitialWillLeaveApplicationCallback(IntPtr interstitialClient)
  149. {
  150. InterstitialClient client = IntPtrToInterstitialClient(interstitialClient);
  151. if (client.OnAdLeavingApplication != null)
  152. {
  153. client.OnAdLeavingApplication(client, EventArgs.Empty);
  154. }
  155. }
  156. private static InterstitialClient IntPtrToInterstitialClient(IntPtr interstitialClient)
  157. {
  158. GCHandle handle = (GCHandle)interstitialClient;
  159. return handle.Target as InterstitialClient;
  160. }
  161. #endregion
  162. }
  163. }
  164. #endif