ATInterstitialAdClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using AnyThinkAds.Common;
  6. using AnyThinkAds.Api;
  7. using AnyThinkAds.ThirdParty.LitJson;
  8. namespace AnyThinkAds.Android
  9. {
  10. public class ATInterstitialAdClient : AndroidJavaProxy,IATInterstitialAdClient
  11. {
  12. public event EventHandler<ATAdEventArgs> onAdLoadEvent;
  13. public event EventHandler<ATAdErrorEventArgs> onAdLoadFailureEvent;
  14. public event EventHandler<ATAdEventArgs> onAdShowEvent;
  15. public event EventHandler<ATAdErrorEventArgs> onAdShowFailureEvent;
  16. public event EventHandler<ATAdEventArgs> onAdCloseEvent;
  17. public event EventHandler<ATAdEventArgs> onAdClickEvent;
  18. public event EventHandler<ATAdEventArgs> onAdVideoStartEvent;
  19. public event EventHandler<ATAdErrorEventArgs> onAdVideoFailureEvent;
  20. public event EventHandler<ATAdEventArgs> onAdVideoEndEvent;
  21. public event EventHandler<ATAdEventArgs> onAdSourceAttemptEvent;
  22. public event EventHandler<ATAdEventArgs> onAdSourceFilledEvent;
  23. public event EventHandler<ATAdErrorEventArgs> onAdSourceLoadFailureEvent;
  24. public event EventHandler<ATAdEventArgs> onAdSourceBiddingAttemptEvent;
  25. public event EventHandler<ATAdEventArgs> onAdSourceBiddingFilledEvent;
  26. public event EventHandler<ATAdErrorEventArgs> onAdSourceBiddingFailureEvent;
  27. private Dictionary<string, AndroidJavaObject> interstitialHelperMap = new Dictionary<string, AndroidJavaObject>();
  28. //private AndroidJavaObject videoHelper;
  29. private ATInterstitialAdListener anyThinkListener;
  30. private AndroidJavaObject interstitialAutoAdHelper;
  31. public ATInterstitialAdClient() : base("com.anythink.unitybridge.interstitial.InterstitialListener")
  32. {
  33. interstitialAutoAdHelper = new AndroidJavaObject("com.anythink.unitybridge.interstitial.InterstitialAutoAdHelper", this);
  34. }
  35. public void loadInterstitialAd(string placementId, string mapJson)
  36. {
  37. //如果不存在则直接创建对应广告位的helper
  38. if(!interstitialHelperMap.ContainsKey(placementId))
  39. {
  40. AndroidJavaObject videoHelper = new AndroidJavaObject(
  41. "com.anythink.unitybridge.interstitial.InterstitialHelper", this);
  42. videoHelper.Call("initInterstitial", placementId);
  43. interstitialHelperMap.Add(placementId, videoHelper);
  44. Debug.Log("ATInterstitialAdClient : no exit helper ,create helper ");
  45. }
  46. try
  47. {
  48. Debug.Log("ATInterstitialAdClient : loadInterstitialAd ");
  49. interstitialHelperMap[placementId].Call("loadInterstitialAd", mapJson);
  50. }
  51. catch (System.Exception e)
  52. {
  53. System.Console.WriteLine("Exception caught: {0}", e);
  54. Debug.Log ("ATInterstitialAdClient : error."+e.Message);
  55. }
  56. }
  57. public void setListener(ATInterstitialAdListener listener)
  58. {
  59. anyThinkListener = listener;
  60. }
  61. public bool hasInterstitialAdReady(string placementId)
  62. {
  63. bool isready = false;
  64. Debug.Log ("ATInterstitialAdClient : hasAdReady....");
  65. try{
  66. if (interstitialHelperMap.ContainsKey(placementId)) {
  67. isready = interstitialHelperMap[placementId].Call<bool> ("isAdReady");
  68. }
  69. }catch(System.Exception e){
  70. System.Console.WriteLine("Exception caught: {0}", e);
  71. Debug.Log ("ATInterstitialAdClient : error."+e.Message);
  72. }
  73. return isready;
  74. }
  75. public string checkAdStatus(string placementId)
  76. {
  77. string adStatusJsonString = "";
  78. Debug.Log("ATInterstitialAdClient : checkAdStatus....");
  79. try
  80. {
  81. if (interstitialHelperMap.ContainsKey(placementId))
  82. {
  83. adStatusJsonString = interstitialHelperMap[placementId].Call<string>("checkAdStatus");
  84. }
  85. }
  86. catch (System.Exception e)
  87. {
  88. System.Console.WriteLine("Exception caught: {0}", e);
  89. Debug.Log("ATInterstitialAdClient : error." + e.Message);
  90. }
  91. return adStatusJsonString;
  92. }
  93. public void entryScenarioWithPlacementID(string placementId, string scenarioID){
  94. Debug.Log("ATInterstitialAdClient : entryScenarioWithPlacementID....");
  95. try
  96. {
  97. if (interstitialHelperMap.ContainsKey(placementId))
  98. {
  99. interstitialHelperMap[placementId].Call("entryAdScenario", scenarioID);
  100. }
  101. }
  102. catch (System.Exception e)
  103. {
  104. System.Console.WriteLine("Exception caught: {0}", e);
  105. Debug.Log("ATInterstitialAdClient entryScenarioWithPlacementID: error." + e.Message);
  106. }
  107. }
  108. public string getValidAdCaches(string placementId)
  109. {
  110. string validAdCachesString = "";
  111. Debug.Log("ATNativeAdClient : getValidAdCaches....");
  112. try
  113. {
  114. if (interstitialHelperMap.ContainsKey(placementId))
  115. {
  116. validAdCachesString = interstitialHelperMap[placementId].Call<string>("getValidAdCaches");
  117. }
  118. }
  119. catch (System.Exception e)
  120. {
  121. System.Console.WriteLine("Exception caught: {0}", e);
  122. Debug.Log("ATNativeAdClient : error." + e.Message);
  123. }
  124. return validAdCachesString;
  125. }
  126. public void showInterstitialAd(string placementId, string jsonmap)
  127. {
  128. Debug.Log("ATInterstitialAdClient : showAd " );
  129. try{
  130. if (interstitialHelperMap.ContainsKey(placementId)) {
  131. this.interstitialHelperMap[placementId].Call ("showInterstitialAd", jsonmap);
  132. }
  133. }catch(System.Exception e){
  134. System.Console.WriteLine("Exception caught: {0}", e);
  135. Debug.Log ("ATInterstitialAdClient : error."+e.Message);
  136. }
  137. }
  138. public void cleanCache(string placementId)
  139. {
  140. Debug.Log("ATInterstitialAdClient : clean" );
  141. try{
  142. if (interstitialHelperMap.ContainsKey(placementId)) {
  143. this.interstitialHelperMap[placementId].Call ("clean");
  144. }
  145. }catch(System.Exception e){
  146. System.Console.WriteLine("Exception caught: {0}", e);
  147. Debug.Log ("ATInterstitialAdClient : error."+e.Message);
  148. }
  149. }
  150. public void onApplicationForces(string placementId)
  151. {
  152. Debug.Log ("onApplicationForces.... ");
  153. try{
  154. if (interstitialHelperMap.ContainsKey(placementId)) {
  155. this.interstitialHelperMap[placementId].Call ("onResume");
  156. }
  157. }catch(System.Exception e){
  158. System.Console.WriteLine("Exception caught: {0}", e);
  159. Debug.Log ("ATInterstitialAdClient : error."+e.Message);
  160. }
  161. }
  162. public void onApplicationPasue(string placementId)
  163. {
  164. Debug.Log ("onApplicationPasue.... ");
  165. try{
  166. if (interstitialHelperMap.ContainsKey(placementId)) {
  167. this.interstitialHelperMap[placementId].Call ("onPause");
  168. }
  169. }catch(System.Exception e){
  170. System.Console.WriteLine("Exception caught: {0}", e);
  171. Debug.Log ("ATInterstitialAdClient : error."+e.Message);
  172. }
  173. }
  174. //广告加载成功
  175. public void onInterstitialAdLoaded(string placementId)
  176. {
  177. Debug.Log("onInterstitialAdLoaded...unity3d.");
  178. onAdLoadEvent?.Invoke(this, new ATAdEventArgs(placementId));
  179. }
  180. //广告加载失败
  181. public void onInterstitialAdLoadFail(string placementId,string code, string error)
  182. {
  183. Debug.Log("onInterstitialAdFailed...unity3d.");
  184. onAdLoadFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId, code, error));
  185. }
  186. //开始播放
  187. public void onInterstitialAdVideoStart(string placementId, string callbackJson)
  188. {
  189. Debug.Log("onInterstitialAdPlayStart...unity3d.");
  190. onAdVideoStartEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  191. }
  192. //结束播放
  193. public void onInterstitialAdVideoEnd(string placementId, string callbackJson)
  194. {
  195. Debug.Log("onInterstitialAdPlayEnd...unity3d.");
  196. onAdVideoEndEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  197. }
  198. //播放失败
  199. public void onInterstitialAdVideoError(string placementId,string code, string error)
  200. {
  201. Debug.Log("onInterstitialAdPlayFailed...unity3d.");
  202. onAdVideoFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId, code, error));
  203. }
  204. //展示失败
  205. public void OnInterstitialAdFailedToShow(string placementID) {
  206. Debug.Log("Unity: ATInterstitialAdClient::OnInterstitialAdFailedToShow()");
  207. onAdShowFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementID, "-1", "Failed to show video ad"));
  208. }
  209. //广告关闭
  210. public void onInterstitialAdClose(string placementId, string callbackJson)
  211. {
  212. Debug.Log("onInterstitialAdClosed...unity3d.");
  213. onAdCloseEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  214. }
  215. //广告点击
  216. public void onInterstitialAdClicked(string placementId, string callbackJson)
  217. {
  218. Debug.Log("onInterstitialAdClicked...unity3d.");
  219. onAdClickEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  220. }
  221. public void onInterstitialAdShow(string placementId, string callbackJson){
  222. Debug.Log("onInterstitialAdShow...unity3d.");
  223. onAdShowEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  224. }
  225. // Adsource Listener
  226. public void onAdSourceBiddingAttempt(string placementId, string callbackJson)
  227. {
  228. Debug.Log("onAdSourceBiddingAttempt...unity3d." + placementId + "," + callbackJson);
  229. onAdSourceBiddingAttemptEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  230. }
  231. public void onAdSourceBiddingFilled(string placementId, string callbackJson)
  232. {
  233. Debug.Log("onAdSourceBiddingFilled...unity3d." + placementId + "," + callbackJson);
  234. onAdSourceBiddingFilledEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  235. }
  236. public void onAdSourceBiddingFail(string placementId, string callbackJson, string code, string error)
  237. {
  238. Debug.Log("onAdSourceBiddingFail...unity3d." + placementId + "," + code + "," + error + "," + callbackJson);
  239. onAdSourceBiddingFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId, callbackJson, code, error));
  240. }
  241. public void onAdSourceAttempt(string placementId, string callbackJson)
  242. {
  243. Debug.Log("onAdSourceAttempt...unity3d." + placementId + "," + callbackJson);
  244. onAdSourceAttemptEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  245. }
  246. public void onAdSourceLoadFilled(string placementId, string callbackJson)
  247. {
  248. Debug.Log("onAdSourceLoadFilled...unity3d." + placementId + "," + callbackJson);
  249. onAdSourceFilledEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  250. }
  251. public void onAdSourceLoadFail(string placementId, string callbackJson, string code, string error)
  252. {
  253. Debug.Log("onAdSourceLoadFail...unity3d." + placementId + "," + code + "," + error + "," + callbackJson);
  254. onAdSourceLoadFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId, callbackJson, code, error));
  255. }
  256. // Auto
  257. public void addAutoLoadAdPlacementID(string[] placementIDList){
  258. Debug.Log("Unity: ATInterstitialAdClient:addAutoLoadAdPlacementID()" + JsonMapper.ToJson(placementIDList));
  259. try
  260. {
  261. interstitialAutoAdHelper.Call("addPlacementIds", JsonMapper.ToJson(placementIDList));
  262. }
  263. catch (System.Exception e)
  264. {
  265. System.Console.WriteLine("Exception caught: {0}", e);
  266. Debug.Log("Unity: ATInterstitialAdClient addAutoLoadAdPlacementID: error." + e.Message);
  267. }
  268. }
  269. public void removeAutoLoadAdPlacementID(string placementId)
  270. {
  271. Debug.Log("Unity: ATInterstitialAdClient:removeAutoLoadAdPlacementID()");
  272. try
  273. {
  274. interstitialAutoAdHelper.Call("removePlacementIds", placementId);
  275. }
  276. catch (System.Exception e)
  277. {
  278. System.Console.WriteLine("Exception caught: {0}", e);
  279. Debug.Log("Unity: ATInterstitialAdClient removeAutoLoadAdPlacementID: error." + e.Message);
  280. }
  281. }
  282. public bool autoLoadInterstitialAdReadyForPlacementID(string placementId)
  283. {
  284. Debug.Log("Unity: ATInterstitialAdClient:autoLoadInterstitialAdReadyForPlacementID()");
  285. bool isready = false;
  286. try
  287. {
  288. isready = interstitialAutoAdHelper.Call<bool>("isAdReady", placementId);
  289. }
  290. catch (System.Exception e)
  291. {
  292. System.Console.WriteLine("Exception caught: {0}", e);
  293. Debug.Log("ATInterstitialAdClient:autoLoadInterstitialAdReadyForPlacementID( : error." + e.Message);
  294. }
  295. return isready;
  296. }
  297. public string getAutoValidAdCaches(string placementId)
  298. {
  299. Debug.Log("Unity: ATInterstitialAdClient:getAutoValidAdCaches()");
  300. string adStatusJsonString = "";
  301. try
  302. {
  303. adStatusJsonString = interstitialAutoAdHelper.Call<string>("getValidAdCaches", placementId);
  304. }
  305. catch (System.Exception e)
  306. {
  307. System.Console.WriteLine("Exception caught: {0}", e);
  308. Debug.Log("ATInterstitialAdClient:getAutoValidAdCaches() : error." + e.Message);
  309. }
  310. return adStatusJsonString;
  311. }
  312. public void setAutoLocalExtra(string placementId, string mapJson)
  313. {
  314. Debug.Log("Unity: ATInterstitialAdClient:setAutoLocalExtra()");
  315. try
  316. {
  317. interstitialAutoAdHelper.Call("setAdExtraData", placementId, mapJson);
  318. }
  319. catch (System.Exception e)
  320. {
  321. System.Console.WriteLine("Exception caught: {0}", e);
  322. Debug.Log("ATInterstitialAdClient:setAutoLocalExtra() : error." + e.Message);
  323. }
  324. }
  325. public void entryAutoAdScenarioWithPlacementID(string placementId, string scenarioID)
  326. {
  327. Debug.Log("Unity: ATInterstitialAdClient:entryAutoAdScenarioWithPlacementID()");
  328. try
  329. {
  330. interstitialAutoAdHelper.Call("entryAdScenario", placementId, scenarioID);
  331. }
  332. catch (System.Exception e)
  333. {
  334. System.Console.WriteLine("Exception caught: {0}", e);
  335. Debug.Log("ATInterstitialAdClient:entryAutoAdScenarioWithPlacementID() : error." + e.Message);
  336. }
  337. }
  338. public void showAutoAd(string placementId, string mapJson)
  339. {
  340. Debug.Log("Unity: ATInterstitialAdClient::showAutoAd()");
  341. try
  342. {
  343. interstitialAutoAdHelper.Call("show", placementId, mapJson);
  344. }
  345. catch (System.Exception e)
  346. {
  347. System.Console.WriteLine("Exception caught: {0}", e);
  348. Debug.Log("Unity: ATInterstitialAdClient:showAutoAd() : error." + e.Message);
  349. }
  350. }
  351. public string checkAutoAdStatus(string placementId)
  352. {
  353. Debug.Log("Unity: ATInterstitialAdClient:checkAutoAdStatus() : checkAutoAdStatus....");
  354. string adStatusJsonString = "";
  355. try
  356. {
  357. adStatusJsonString = interstitialAutoAdHelper.Call<string>("checkAdStatus", placementId);
  358. }
  359. catch (System.Exception e)
  360. {
  361. System.Console.WriteLine("Exception caught: {0}", e);
  362. Debug.Log("Unity: ATInterstitialAdClient:checkAutoAdStatus() : error." + e.Message);
  363. }
  364. return adStatusJsonString;
  365. }
  366. }
  367. }