// Copyright (C) 2015 Google, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using System; using System.Collections.Generic; using System.Reflection; using GoogleMobileAds.Common; namespace GoogleMobileAds.Api { public enum NativeAdType { CustomTemplate = 0 } public class AdLoader { private IAdLoaderClient adLoaderClient; private AdLoader(Builder builder) { this.AdUnitId = string.Copy(builder.AdUnitId); this.CustomNativeTemplateClickHandlers = new Dictionary>( builder.CustomNativeTemplateClickHandlers); this.TemplateIds = new HashSet(builder.TemplateIds); this.AdTypes = new HashSet(builder.AdTypes); Type googleMobileAdsClientFactory = Type.GetType( "GoogleMobileAds.GoogleMobileAdsClientFactory,Assembly-CSharp"); MethodInfo method = googleMobileAdsClientFactory.GetMethod( "BuildAdLoaderClient", BindingFlags.Static | BindingFlags.Public); this.adLoaderClient = (IAdLoaderClient)method.Invoke(null, new object[] { this }); this.adLoaderClient.OnCustomNativeTemplateAdLoaded += delegate (object sender, CustomNativeEventArgs args) { this.OnCustomNativeTemplateAdLoaded(this, args); }; this.adLoaderClient.OnAdFailedToLoad += delegate ( object sender, AdFailedToLoadEventArgs args) { if (this.OnAdFailedToLoad != null) { this.OnAdFailedToLoad(this, args); } }; } public event EventHandler OnAdFailedToLoad; public event EventHandler OnCustomNativeTemplateAdLoaded; public Dictionary> CustomNativeTemplateClickHandlers { get; private set; } public string AdUnitId { get; private set; } public HashSet AdTypes { get; private set; } public HashSet TemplateIds { get; private set; } public void LoadAd(AdRequest request) { this.adLoaderClient.LoadAd(request); } public class Builder { public Builder(string adUnitId) { this.AdUnitId = adUnitId; this.AdTypes = new HashSet(); this.TemplateIds = new HashSet(); this.CustomNativeTemplateClickHandlers = new Dictionary>(); } internal string AdUnitId { get; private set; } internal HashSet AdTypes { get; private set; } internal HashSet TemplateIds { get; private set; } internal Dictionary> CustomNativeTemplateClickHandlers { get; private set; } public Builder ForCustomNativeAd(string templateId) { this.TemplateIds.Add(templateId); this.AdTypes.Add(NativeAdType.CustomTemplate); return this; } public Builder ForCustomNativeAd( string templateId, Action callback) { this.TemplateIds.Add(templateId); this.CustomNativeTemplateClickHandlers[templateId] = callback; this.AdTypes.Add(NativeAdType.CustomTemplate); return this; } public AdLoader Build() { return new AdLoader(this); } } } }