ATRewardedVideoAdClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 ATRewardedVideoAdClient : AndroidJavaProxy,IATRewardedVideoAdClient
  11. {
  12. public event EventHandler<ATAdEventArgs> onAdLoadEvent;
  13. public event EventHandler<ATAdErrorEventArgs> onAdLoadFailureEvent;
  14. public event EventHandler<ATAdEventArgs> onAdVideoStartEvent;
  15. public event EventHandler<ATAdEventArgs> onAdVideoEndEvent;
  16. public event EventHandler<ATAdErrorEventArgs> onAdVideoFailureEvent;
  17. public event EventHandler<ATAdRewardEventArgs> onAdVideoCloseEvent;
  18. public event EventHandler<ATAdEventArgs> onAdClickEvent;
  19. public event EventHandler<ATAdEventArgs> onRewardEvent;
  20. public event EventHandler<ATAdEventArgs> onAdSourceAttemptEvent;
  21. public event EventHandler<ATAdEventArgs> onAdSourceFilledEvent;
  22. public event EventHandler<ATAdErrorEventArgs> onAdSourceLoadFailureEvent;
  23. public event EventHandler<ATAdEventArgs> onAdSourceBiddingAttemptEvent;
  24. public event EventHandler<ATAdEventArgs> onAdSourceBiddingFilledEvent;
  25. public event EventHandler<ATAdErrorEventArgs> onAdSourceBiddingFailureEvent;
  26. public event EventHandler<ATAdEventArgs> onPlayAgainStart;
  27. public event EventHandler<ATAdEventArgs> onPlayAgainEnd;
  28. public event EventHandler<ATAdErrorEventArgs> onPlayAgainFailure;
  29. public event EventHandler<ATAdEventArgs> onPlayAgainClick;
  30. public event EventHandler<ATAdEventArgs> onPlayAgainReward;
  31. private Dictionary<string, AndroidJavaObject> videoHelperMap = new Dictionary<string, AndroidJavaObject>();
  32. private AndroidJavaObject videoAutoAdHelper;
  33. //private AndroidJavaObject videoHelper;
  34. private ATRewardedVideoListener anyThinkListener;
  35. public ATRewardedVideoAdClient() : base("com.anythink.unitybridge.videoad.VideoListener")
  36. {
  37. videoAutoAdHelper = new AndroidJavaObject("com.anythink.unitybridge.videoad.VideoAutoAdHelper", this);
  38. }
  39. public void loadVideoAd(string placementId, string mapJson)
  40. {
  41. if(!videoHelperMap.ContainsKey(placementId))
  42. {
  43. AndroidJavaObject videoHelper = new AndroidJavaObject(
  44. "com.anythink.unitybridge.videoad.VideoHelper", this);
  45. videoHelper.Call("initVideo", placementId);
  46. videoHelperMap.Add(placementId, videoHelper);
  47. Debug.Log("ATRewardedVideoAdClient : no exit helper ,create helper ");
  48. }
  49. try
  50. {
  51. Debug.Log("ATRewardedVideoAdClient : loadVideoAd ");
  52. videoHelperMap[placementId].Call("fillVideo", mapJson);
  53. }
  54. catch (System.Exception e)
  55. {
  56. System.Console.WriteLine("Exception caught: {0}", e);
  57. Debug.Log ("ATRewardedVideoAdClient : error."+e.Message);
  58. }
  59. }
  60. public void setListener(ATRewardedVideoListener listener)
  61. {
  62. anyThinkListener = listener;
  63. }
  64. public bool hasAdReady(string placementId)
  65. {
  66. bool isready = false;
  67. Debug.Log ("ATRewardedVideoAdClient : hasAdReady....");
  68. try{
  69. if (videoHelperMap.ContainsKey(placementId)) {
  70. isready = videoHelperMap[placementId].Call<bool> ("isAdReady");
  71. }
  72. }catch(System.Exception e){
  73. System.Console.WriteLine("Exception caught: {0}", e);
  74. Debug.Log ("ATRewardedVideoAdClient : error."+e.Message);
  75. }
  76. return isready;
  77. }
  78. public string checkAdStatus(string placementId)
  79. {
  80. string adStatusJsonString = "";
  81. Debug.Log("ATRewardedVideoAdClient : checkAdStatus....");
  82. try
  83. {
  84. if (videoHelperMap.ContainsKey(placementId))
  85. {
  86. adStatusJsonString = videoHelperMap[placementId].Call<string>("checkAdStatus");
  87. }
  88. }
  89. catch (System.Exception e)
  90. {
  91. System.Console.WriteLine("Exception caught: {0}", e);
  92. Debug.Log("ATRewardedVideoAdClient : error." + e.Message);
  93. }
  94. return adStatusJsonString;
  95. }
  96. public void entryScenarioWithPlacementID(string placementId, string scenarioID){
  97. Debug.Log("ATRewardedVideoAdClient : entryScenarioWithPlacementID....");
  98. try
  99. {
  100. if (videoHelperMap.ContainsKey(placementId))
  101. {
  102. videoHelperMap[placementId].Call("entryAdScenario", scenarioID);
  103. }
  104. }
  105. catch (System.Exception e)
  106. {
  107. System.Console.WriteLine("Exception caught: {0}", e);
  108. Debug.Log("ATRewardedVideoAdClient entryScenarioWithPlacementID: error." + e.Message);
  109. }
  110. }
  111. public string getValidAdCaches(string placementId)
  112. {
  113. string validAdCachesString = "";
  114. Debug.Log("ATNativeAdClient : getValidAdCaches....");
  115. try
  116. {
  117. if (videoHelperMap.ContainsKey(placementId))
  118. {
  119. validAdCachesString = videoHelperMap[placementId].Call<string>("getValidAdCaches");
  120. }
  121. }
  122. catch (System.Exception e)
  123. {
  124. System.Console.WriteLine("Exception caught: {0}", e);
  125. Debug.Log("ATNativeAdClient : error." + e.Message);
  126. }
  127. return validAdCachesString;
  128. }
  129. public void showAd(string placementId, string scenario)
  130. {
  131. Debug.Log("ATRewardedVideoAdClient : showAd " );
  132. try{
  133. if (videoHelperMap.ContainsKey(placementId)) {
  134. this.videoHelperMap[placementId].Call ("showVideo", scenario);
  135. }
  136. }catch(System.Exception e){
  137. System.Console.WriteLine("Exception caught: {0}", e);
  138. Debug.Log ("ATRewardedVideoAdClient : error."+e.Message);
  139. }
  140. }
  141. public void onRewardedVideoAdLoaded(string placementId)
  142. {
  143. Debug.Log("onRewardedVideoAdLoaded...unity3d.");
  144. onAdLoadEvent?.Invoke(this, new ATAdEventArgs(placementId));
  145. }
  146. public void onRewardedVideoAdFailed(string placementId,string code, string error)
  147. {
  148. Debug.Log("onRewardedVideoAdFailed...unity3d.");
  149. onAdLoadFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId, code, error));
  150. }
  151. public void onRewardedVideoAdPlayStart(string placementId, string callbackJson)
  152. {
  153. Debug.Log("onRewardedVideoAdPlayStart...unity3d.");
  154. onAdVideoStartEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  155. }
  156. public void onRewardedVideoAdPlayEnd(string placementId, string callbackJson)
  157. {
  158. Debug.Log("onRewardedVideoAdPlayEnd...unity3d.");
  159. onAdVideoEndEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  160. }
  161. public void onRewardedVideoAdPlayFailed(string placementId,string code, string error)
  162. {
  163. Debug.Log("onRewardedVideoAdPlayFailed...unity3d.");
  164. onAdVideoFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId, code, error));
  165. }
  166. public void onRewardedVideoAdClosed(string placementId,bool isRewarded, string callbackJson)
  167. {
  168. Debug.Log("onRewardedVideoAdClosed...unity3d.");
  169. onAdVideoCloseEvent?.Invoke(this, new ATAdRewardEventArgs(placementId, callbackJson, isRewarded));
  170. }
  171. public void onRewardedVideoAdPlayClicked(string placementId, string callbackJson)
  172. {
  173. Debug.Log("onRewardedVideoAdPlayClicked...unity3d.");
  174. onAdClickEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  175. }
  176. public void onReward(string placementId, string callbackJson)
  177. {
  178. Debug.Log("onReward...unity3d.");
  179. onRewardEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  180. }
  181. public void onRewardedVideoAdAgainPlayStart(string placementId, string callbackJson)
  182. {
  183. Debug.Log("onRewardedVideoAdAgainPlayStart...unity3d.");
  184. onPlayAgainStart?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  185. }
  186. public void onRewardedVideoAdAgainPlayEnd(string placementId, string callbackJson)
  187. {
  188. Debug.Log("onRewardedVideoAdAgainPlayEnd...unity3d.");
  189. onPlayAgainEnd?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  190. }
  191. public void onRewardedVideoAdAgainPlayFailed(string placementId, string code, string error)
  192. {
  193. Debug.Log("onRewardedVideoAdAgainPlayFailed...unity3d.");
  194. onPlayAgainFailure?.Invoke(this, new ATAdErrorEventArgs(placementId, code, error));
  195. }
  196. public void onRewardedVideoAdAgainPlayClicked(string placementId, string callbackJson)
  197. {
  198. Debug.Log("onRewardedVideoAdAgainPlayClicked...unity3d.");
  199. onPlayAgainClick?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  200. }
  201. public void onAgainReward(string placementId, string callbackJson)
  202. {
  203. Debug.Log("onAgainReward...unity3d.");
  204. onPlayAgainReward?.Invoke(this, new ATAdRewardEventArgs(placementId, callbackJson, true));
  205. }
  206. // Adsource Listener
  207. public void onAdSourceBiddingAttempt(string placementId, string callbackJson)
  208. {
  209. Debug.Log("onAdSourceBiddingAttempt...unity3d."+ placementId + "," + callbackJson);
  210. onAdSourceBiddingAttemptEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  211. }
  212. public void onAdSourceBiddingFilled(string placementId, string callbackJson)
  213. {
  214. Debug.Log("onAdSourceBiddingFilled...unity3d." + placementId + "," + callbackJson);
  215. onAdSourceBiddingFilledEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  216. }
  217. public void onAdSourceBiddingFail(string placementId, string callbackJson, string code, string error)
  218. {
  219. Debug.Log("onAdSourceBiddingFail...unity3d." + placementId + "," + code + "," + error + "," + callbackJson);
  220. onAdSourceBiddingFailureEvent?.Invoke(this, new ATAdErrorEventArgs(placementId, code, error));
  221. }
  222. public void onAdSourceAttempt(string placementId, string callbackJson)
  223. {
  224. Debug.Log("onAdSourceAttempt...unity3d." + placementId + "," + callbackJson);
  225. onAdSourceAttemptEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  226. }
  227. public void onAdSourceLoadFilled(string placementId, string callbackJson)
  228. {
  229. Debug.Log("onAdSourceLoadFilled...unity3d." + placementId + "," + callbackJson);
  230. onAdSourceFilledEvent?.Invoke(this, new ATAdEventArgs(placementId, callbackJson));
  231. }
  232. public void onAdSourceLoadFail(string placementId, string callbackJson, string code, string error)
  233. {
  234. Debug.Log("onAdSourceLoadFail...unity3d." + placementId + "," + code + "," + error + "," + callbackJson);
  235. onAdSourceLoadFailureEvent?.Invoke(this,new ATAdErrorEventArgs(placementId, code, error));
  236. }
  237. // Auto
  238. public void addAutoLoadAdPlacementID(string[] placementIDList){
  239. Debug.Log("Unity: ATRewardedVideoAdClient:addAutoLoadAdPlacementID()" + JsonMapper.ToJson(placementIDList));
  240. try
  241. {
  242. videoAutoAdHelper.Call("addPlacementIds", JsonMapper.ToJson(placementIDList));
  243. }
  244. catch (System.Exception e)
  245. {
  246. System.Console.WriteLine("Exception caught: {0}", e);
  247. Debug.Log("Unity: ATRewardedVideoAdClient addAutoLoadAdPlacementID: error." + e.Message);
  248. }
  249. }
  250. public void removeAutoLoadAdPlacementID(string placementId) {
  251. Debug.Log("Unity: ATRewardedVideoAdClient:removeAutoLoadAdPlacementID()");
  252. try
  253. {
  254. videoAutoAdHelper.Call("removePlacementIds", placementId);
  255. }
  256. catch (System.Exception e)
  257. {
  258. System.Console.WriteLine("Exception caught: {0}", e);
  259. Debug.Log("Unity: ATRewardedVideoAdClient removeAutoLoadAdPlacementID: error." + e.Message);
  260. }
  261. }
  262. public bool autoLoadRewardedVideoReadyForPlacementID(string placementId)
  263. {
  264. Debug.Log("Unity: ATRewardedVideoAdClient:autoLoadRewardedVideoReadyForPlacementID()");
  265. bool isready = false;
  266. try
  267. {
  268. isready = videoAutoAdHelper.Call<bool>("isAdReady", placementId);
  269. }
  270. catch (System.Exception e)
  271. {
  272. System.Console.WriteLine("Exception caught: {0}", e);
  273. Debug.Log("ATRewardedVideoAdClient:autoLoadRewardedVideoReadyForPlacementID( : error." + e.Message);
  274. }
  275. return isready;
  276. }
  277. public string getAutoValidAdCaches(string placementId)
  278. {
  279. Debug.Log("Unity: ATRewardedVideoAdClient:getAutoValidAdCaches()");
  280. string adStatusJsonString = "";
  281. try
  282. {
  283. adStatusJsonString = videoAutoAdHelper.Call<string>("getValidAdCaches", placementId);
  284. }
  285. catch (System.Exception e)
  286. {
  287. System.Console.WriteLine("Exception caught: {0}", e);
  288. Debug.Log("ATRewardedVideoAdClient:getAutoValidAdCaches() : error." + e.Message);
  289. }
  290. return adStatusJsonString;
  291. }
  292. public void setAutoLocalExtra(string placementId, string mapJson)
  293. {
  294. Debug.Log("Unity: ATRewardedVideoAdClient:setAutoLocalExtra()");
  295. try
  296. {
  297. videoAutoAdHelper.Call("setAdExtraData", placementId, mapJson);
  298. }
  299. catch (System.Exception e)
  300. {
  301. System.Console.WriteLine("Exception caught: {0}", e);
  302. Debug.Log("ATRewardedVideoAdClient:setAutoLocalExtra() : error." + e.Message);
  303. }
  304. }
  305. public void entryAutoAdScenarioWithPlacementID(string placementId, string scenarioID)
  306. {
  307. Debug.Log("Unity: ATRewardedVideoAdClient:entryAutoAdScenarioWithPlacementID()");
  308. try
  309. {
  310. videoAutoAdHelper.Call("entryAdScenario", placementId, scenarioID);
  311. }
  312. catch (System.Exception e)
  313. {
  314. System.Console.WriteLine("Exception caught: {0}", e);
  315. Debug.Log("ATRewardedVideoAdClient:entryAutoAdScenarioWithPlacementID() : error." + e.Message);
  316. }
  317. }
  318. public void showAutoAd(string placementId, string mapJson) {
  319. Debug.Log("Unity: ATRewardedVideoAdClient:showAutoAd()");
  320. try
  321. {
  322. videoAutoAdHelper.Call("show", placementId, mapJson);
  323. }
  324. catch (System.Exception e)
  325. {
  326. System.Console.WriteLine("Exception caught: {0}", e);
  327. Debug.Log("Unity: ATRewardedVideoAdClient:showAutoAd() : error." + e.Message);
  328. }
  329. }
  330. public string checkAutoAdStatus(string placementId)
  331. {
  332. Debug.Log("Unity: ATRewardedVideoAdClient:checkAutoAdStatus() : checkAutoAdStatus....");
  333. string adStatusJsonString = "";
  334. try
  335. {
  336. adStatusJsonString = videoAutoAdHelper.Call<string>("checkAdStatus", placementId);
  337. }
  338. catch (System.Exception e)
  339. {
  340. System.Console.WriteLine("Exception caught: {0}", e);
  341. Debug.Log("Unity: ATRewardedVideoAdClient:checkAutoAdStatus() : error." + e.Message);
  342. }
  343. return adStatusJsonString;
  344. }
  345. }
  346. }