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.
 
 
 

164 lines
5.6 KiB

  1. // Copyright (C) 2016 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 GoogleMobileAds.Api;
  19. using GoogleMobileAds.Common;
  20. namespace GoogleMobileAds.iOS
  21. {
  22. [StructLayout(LayoutKind.Sequential),Serializable]
  23. // The System.Boolean (bool in C#) type is special. A bool within a structure is marshaled in a different format
  24. // than when passed as an argument to a function (4-byte integer vs 2-byte integer, non zero = true vs -1 = true).
  25. // Using ints instead for simplicity.
  26. public struct NativeAdTypes
  27. {
  28. public int CustomTemplateAd;
  29. }
  30. public class AdLoaderClient : IAdLoaderClient, IDisposable
  31. {
  32. private IntPtr adLoaderPtr;
  33. private IntPtr adLoaderClientPtr;
  34. private NativeAdTypes adTypes;
  35. private Dictionary<string, Action<CustomNativeTemplateAd, string>>
  36. customNativeTemplateCallbacks;
  37. public AdLoaderClient(AdLoader unityAdLoader)
  38. {
  39. this.adLoaderClientPtr = (IntPtr)GCHandle.Alloc(this);
  40. this.customNativeTemplateCallbacks = unityAdLoader.CustomNativeTemplateClickHandlers;
  41. string[] templateIdsArray = new string[unityAdLoader.TemplateIds.Count];
  42. unityAdLoader.TemplateIds.CopyTo(templateIdsArray);
  43. this.adTypes = new NativeAdTypes();
  44. if (unityAdLoader.AdTypes.Contains(NativeAdType.CustomTemplate))
  45. {
  46. adTypes.CustomTemplateAd = 1;
  47. }
  48. this.AdLoaderPtr = Externs.GADUCreateAdLoader(
  49. this.adLoaderClientPtr,
  50. unityAdLoader.AdUnitId,
  51. templateIdsArray,
  52. templateIdsArray.Length,
  53. ref adTypes);
  54. Externs.GADUSetAdLoaderCallbacks(
  55. this.AdLoaderPtr,
  56. AdLoaderDidReceiveNativeCustomTemplateAdCallback,
  57. AdLoaderDidFailToReceiveAdWithErrorCallback);
  58. }
  59. internal delegate void GADUAdLoaderDidReceiveNativeCustomTemplateAdCallback(
  60. IntPtr adLoader, IntPtr nativeCustomTemplateAd, string templateID);
  61. internal delegate void GADUAdLoaderDidFailToReceiveAdWithErrorCallback(
  62. IntPtr AdLoader, string error);
  63. public event EventHandler<CustomNativeEventArgs> OnCustomNativeTemplateAdLoaded;
  64. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
  65. // This property should be used when setting the adLoaderPtr.
  66. private IntPtr AdLoaderPtr
  67. {
  68. get
  69. {
  70. return this.adLoaderPtr;
  71. }
  72. set
  73. {
  74. Externs.GADURelease(this.adLoaderPtr);
  75. this.adLoaderPtr = value;
  76. }
  77. }
  78. public void LoadAd(AdRequest request)
  79. {
  80. IntPtr requestPtr = Utils.BuildAdRequest(request);
  81. Externs.GADURequestNativeAd(this.AdLoaderPtr, requestPtr);
  82. Externs.GADURelease(requestPtr);
  83. }
  84. // Destroys the AdLoader.
  85. public void DestroyAdLoader()
  86. {
  87. this.AdLoaderPtr = IntPtr.Zero;
  88. }
  89. public void Dispose()
  90. {
  91. this.DestroyAdLoader();
  92. ((GCHandle)this.adLoaderClientPtr).Free();
  93. }
  94. ~AdLoaderClient()
  95. {
  96. this.Dispose();
  97. }
  98. [MonoPInvokeCallback(typeof(GADUAdLoaderDidReceiveNativeCustomTemplateAdCallback))]
  99. private static void AdLoaderDidReceiveNativeCustomTemplateAdCallback(
  100. IntPtr adLoader, IntPtr nativeCustomTemplateAd, string templateID)
  101. {
  102. AdLoaderClient client = IntPtrToAdLoaderClient(adLoader);
  103. Action<CustomNativeTemplateAd, string> clickHandler =
  104. client.customNativeTemplateCallbacks.ContainsKey(templateID) ?
  105. client.customNativeTemplateCallbacks[templateID] : null;
  106. if (client.OnCustomNativeTemplateAdLoaded != null)
  107. {
  108. CustomNativeEventArgs args = new CustomNativeEventArgs()
  109. {
  110. nativeAd = new CustomNativeTemplateAd(new CustomNativeTemplateClient(
  111. nativeCustomTemplateAd, clickHandler))
  112. };
  113. client.OnCustomNativeTemplateAdLoaded(client, args);
  114. }
  115. }
  116. [MonoPInvokeCallback(typeof(GADUAdLoaderDidFailToReceiveAdWithErrorCallback))]
  117. private static void AdLoaderDidFailToReceiveAdWithErrorCallback(
  118. IntPtr adLoader, string error)
  119. {
  120. AdLoaderClient client = IntPtrToAdLoaderClient(adLoader);
  121. if (client.OnAdFailedToLoad != null)
  122. {
  123. AdFailedToLoadEventArgs args = new AdFailedToLoadEventArgs()
  124. {
  125. Message = error
  126. };
  127. client.OnAdFailedToLoad(client, args);
  128. }
  129. }
  130. private static AdLoaderClient IntPtrToAdLoaderClient(IntPtr adLoader)
  131. {
  132. GCHandle handle = (GCHandle)adLoader;
  133. return handle.Target as AdLoaderClient;
  134. }
  135. }
  136. }
  137. #endif