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.
 
 
 

163 lines
5.3 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. using UnityEngine;
  21. namespace GoogleMobileAds.iOS
  22. {
  23. internal class CustomNativeTemplateClient : ICustomNativeTemplateClient, IDisposable
  24. {
  25. private IntPtr customNativeAdPtr;
  26. private IntPtr customNativeTemplateAdClientPtr;
  27. private Action<CustomNativeTemplateAd, string> clickHandler;
  28. // This property should be used when setting the customNativeAdPtr.
  29. private IntPtr CustomNativeAdPtr
  30. {
  31. get
  32. {
  33. return this.customNativeAdPtr;
  34. }
  35. set
  36. {
  37. Externs.GADURelease(this.customNativeAdPtr);
  38. this.customNativeAdPtr = value;
  39. }
  40. }
  41. public CustomNativeTemplateClient(
  42. IntPtr customNativeAd, Action<CustomNativeTemplateAd, string> clickHandler)
  43. {
  44. this.customNativeAdPtr = customNativeAd;
  45. this.clickHandler = clickHandler;
  46. this.customNativeTemplateAdClientPtr = (IntPtr)GCHandle.Alloc(this);
  47. Externs.GADUSetNativeCustomTemplateAdUnityClient(
  48. customNativeAd,
  49. this.customNativeTemplateAdClientPtr);
  50. Externs.GADUSetNativeCustomTemplateAdCallbacks(
  51. customNativeAd,
  52. NativeCustomTemplateDidReceiveClickCallback);
  53. }
  54. internal delegate void GADUNativeCustomTemplateDidReceiveClick(
  55. IntPtr nativeCustomTemplateAd, string error);
  56. public List<string> GetAvailableAssetNames()
  57. {
  58. IntPtr unmanagedAssetArray =
  59. Externs.GADUNativeCustomTemplateAdAvailableAssetKeys(this.CustomNativeAdPtr);
  60. int numOfAssets =
  61. Externs.GADUNativeCustomTemplateAdNumberOfAvailableAssetKeys(
  62. this.CustomNativeAdPtr);
  63. IntPtr[] intPtrArray = new IntPtr[numOfAssets];
  64. string[] managedAssetArray = new string[numOfAssets];
  65. Marshal.Copy(unmanagedAssetArray, intPtrArray, 0, numOfAssets);
  66. for (int i = 0; i < numOfAssets; i++)
  67. {
  68. managedAssetArray[i] = Marshal.PtrToStringAuto(intPtrArray[i]);
  69. Marshal.FreeHGlobal(intPtrArray[i]);
  70. }
  71. Marshal.FreeHGlobal(unmanagedAssetArray);
  72. return new List<string>(managedAssetArray);
  73. }
  74. public string GetTemplateId()
  75. {
  76. return Externs.GADUNativeCustomTemplateAdTemplateID(this.CustomNativeAdPtr);
  77. }
  78. public byte[] GetImageByteArray(string key)
  79. {
  80. string bytesString = Externs.GADUNativeCustomTemplateAdImageAsBytesForKey(
  81. this.CustomNativeAdPtr, key);
  82. if (bytesString == null)
  83. {
  84. return null;
  85. }
  86. return System.Convert.FromBase64String(bytesString);
  87. }
  88. public string GetText(string key)
  89. {
  90. return Externs.GADUNativeCustomTemplateAdStringForKey(this.CustomNativeAdPtr, key);
  91. }
  92. public void PerformClick(string assetName)
  93. {
  94. bool customClickAction = this.clickHandler != null;
  95. Externs.GADUNativeCustomTemplateAdPerformClickOnAssetWithKey(
  96. this.CustomNativeAdPtr, assetName, customClickAction);
  97. }
  98. public void RecordImpression()
  99. {
  100. Externs.GADUNativeCustomTemplateAdRecordImpression(this.CustomNativeAdPtr);
  101. }
  102. public void DestroyCustomNativeTemplateAd()
  103. {
  104. this.CustomNativeAdPtr = IntPtr.Zero;
  105. }
  106. public void Dispose()
  107. {
  108. this.DestroyCustomNativeTemplateAd();
  109. ((GCHandle)this.customNativeTemplateAdClientPtr).Free();
  110. }
  111. ~CustomNativeTemplateClient()
  112. {
  113. this.Dispose();
  114. }
  115. [MonoPInvokeCallback(typeof(GADUNativeCustomTemplateDidReceiveClick))]
  116. private static void NativeCustomTemplateDidReceiveClickCallback(
  117. IntPtr nativeCustomAd, string assetName)
  118. {
  119. CustomNativeTemplateClient client = IntPtrToAdLoaderClient(nativeCustomAd);
  120. if (client.clickHandler != null)
  121. {
  122. CustomNativeTemplateAd nativeAd = new CustomNativeTemplateAd(client);
  123. client.clickHandler(nativeAd, assetName);
  124. }
  125. }
  126. private static CustomNativeTemplateClient IntPtrToAdLoaderClient(
  127. IntPtr customNativeTemplateAd)
  128. {
  129. GCHandle handle = (GCHandle)customNativeTemplateAd;
  130. return handle.Target as CustomNativeTemplateClient;
  131. }
  132. }
  133. }
  134. #endif