您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

132 行
4.4 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.Collections.Generic;
  16. using System.Reflection;
  17. using GoogleMobileAds.Common;
  18. namespace GoogleMobileAds.Api
  19. {
  20. public enum NativeAdType
  21. {
  22. CustomTemplate = 0
  23. }
  24. public class AdLoader
  25. {
  26. private IAdLoaderClient adLoaderClient;
  27. private AdLoader(Builder builder)
  28. {
  29. this.AdUnitId = string.Copy(builder.AdUnitId);
  30. this.CustomNativeTemplateClickHandlers =
  31. new Dictionary<string, Action<CustomNativeTemplateAd, string>>(
  32. builder.CustomNativeTemplateClickHandlers);
  33. this.TemplateIds = new HashSet<string>(builder.TemplateIds);
  34. this.AdTypes = new HashSet<NativeAdType>(builder.AdTypes);
  35. Type googleMobileAdsClientFactory = Type.GetType(
  36. "GoogleMobileAds.GoogleMobileAdsClientFactory,Assembly-CSharp");
  37. MethodInfo method = googleMobileAdsClientFactory.GetMethod(
  38. "BuildAdLoaderClient",
  39. BindingFlags.Static | BindingFlags.Public);
  40. this.adLoaderClient = (IAdLoaderClient)method.Invoke(null, new object[] { this });
  41. this.adLoaderClient.OnCustomNativeTemplateAdLoaded +=
  42. delegate (object sender, CustomNativeEventArgs args)
  43. {
  44. this.OnCustomNativeTemplateAdLoaded(this, args);
  45. };
  46. this.adLoaderClient.OnAdFailedToLoad += delegate (
  47. object sender, AdFailedToLoadEventArgs args)
  48. {
  49. if (this.OnAdFailedToLoad != null)
  50. {
  51. this.OnAdFailedToLoad(this, args);
  52. }
  53. };
  54. }
  55. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
  56. public event EventHandler<CustomNativeEventArgs> OnCustomNativeTemplateAdLoaded;
  57. public Dictionary<string, Action<CustomNativeTemplateAd, string>>
  58. CustomNativeTemplateClickHandlers
  59. {
  60. get; private set;
  61. }
  62. public string AdUnitId { get; private set; }
  63. public HashSet<NativeAdType> AdTypes { get; private set; }
  64. public HashSet<string> TemplateIds { get; private set; }
  65. public void LoadAd(AdRequest request)
  66. {
  67. this.adLoaderClient.LoadAd(request);
  68. }
  69. public class Builder
  70. {
  71. public Builder(string adUnitId)
  72. {
  73. this.AdUnitId = adUnitId;
  74. this.AdTypes = new HashSet<NativeAdType>();
  75. this.TemplateIds = new HashSet<string>();
  76. this.CustomNativeTemplateClickHandlers =
  77. new Dictionary<string, Action<CustomNativeTemplateAd, string>>();
  78. }
  79. internal string AdUnitId { get; private set; }
  80. internal HashSet<NativeAdType> AdTypes { get; private set; }
  81. internal HashSet<string> TemplateIds { get; private set; }
  82. internal Dictionary<string, Action<CustomNativeTemplateAd, string>>
  83. CustomNativeTemplateClickHandlers
  84. {
  85. get; private set;
  86. }
  87. public Builder ForCustomNativeAd(string templateId)
  88. {
  89. this.TemplateIds.Add(templateId);
  90. this.AdTypes.Add(NativeAdType.CustomTemplate);
  91. return this;
  92. }
  93. public Builder ForCustomNativeAd(
  94. string templateId,
  95. Action<CustomNativeTemplateAd, string> callback)
  96. {
  97. this.TemplateIds.Add(templateId);
  98. this.CustomNativeTemplateClickHandlers[templateId] = callback;
  99. this.AdTypes.Add(NativeAdType.CustomTemplate);
  100. return this;
  101. }
  102. public AdLoader Build()
  103. {
  104. return new AdLoader(this);
  105. }
  106. }
  107. }
  108. }