Monkey2AdView.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package com.monkey2.lib;
  2. import android.provider.Settings;
  3. import android.util.Log;
  4. import android.view.View;
  5. import android.widget.RelativeLayout;
  6. import com.google.android.gms.ads.AdListener;
  7. import com.google.android.gms.ads.AdRequest;
  8. import com.google.android.gms.ads.AdSize;
  9. import com.google.android.gms.ads.AdView;
  10. import com.google.android.gms.ads.InterstitialAd;
  11. import com.google.android.gms.ads.MobileAds;
  12. import com.google.android.gms.ads.reward.RewardItem;
  13. import com.google.android.gms.ads.reward.RewardedVideoAd;
  14. import com.google.android.gms.ads.reward.RewardedVideoAdListener;
  15. import org.libsdl.app.SDLActivity;
  16. import java.security.MessageDigest;
  17. import java.security.NoSuchAlgorithmException;
  18. public class Monkey2AdView{
  19. private static final String TAG = "Monkey2AdView";
  20. String _size;
  21. String _layout;
  22. String _adUnitId;
  23. boolean _visible;
  24. int _callback;
  25. RewardedVideoAd _rewardedVideoAd;
  26. InterstitialAd _interstitialAd;
  27. AdView _adView;
  28. int _adState;
  29. int _error;
  30. String _rewardType="";
  31. int _rewardAmount;
  32. public Monkey2AdView( final String size,final String layout,final String adUnitId,final boolean visible ){
  33. _size=size;
  34. _layout=layout;
  35. _adUnitId=adUnitId;
  36. _visible=visible;
  37. }
  38. void start( int callback ){
  39. this._callback=callback;
  40. Monkey2Activity.instance().runOnUiThread( new Runnable() {
  41. public void run() {
  42. if( _size.equals( "rewardedvideo" ) ){
  43. createRewardedVideoAd();
  44. }else if( _size.equals( "interstitial" ) ){
  45. createInterstitialAd();
  46. }else{
  47. createBannerAd();
  48. }
  49. requestAd();
  50. }
  51. } );
  52. }
  53. void setState( int state ){
  54. if( state==_adState ) return;
  55. _adState=state;
  56. Monkey2Async.invokeAsyncCallback( _callback );
  57. }
  58. void createRewardedVideoAd(){
  59. if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/5224354917";
  60. _rewardedVideoAd=MobileAds.getRewardedVideoAdInstance( Monkey2Activity.instance() );
  61. _rewardedVideoAd.setRewardedVideoAdListener( new RewardedVideoAdListener(){
  62. public void onRewardedVideoAdLoaded(){
  63. Log.i( TAG,"onRewardedVideoAdLoaded" );
  64. setState( 2 );
  65. }
  66. public void onRewardedVideoAdFailedToLoad( int _errorCode ){
  67. Log.i( TAG,"onRewardedVideoAdFailedToLoad, _errorCode="+_errorCode );
  68. _error=_errorCode;
  69. setState( 3 );
  70. }
  71. public void onRewardedVideoAdOpened(){
  72. Log.i( TAG,"onRewardedVideoAdOpened" );
  73. }
  74. public void onRewardedVideoStarted(){
  75. Log.i( TAG,"onRewardedVideoStarted" );
  76. }
  77. public void onRewardedVideoCompleted(){
  78. Log.i( TAG,"onRewardedVideoCompleted" );
  79. }
  80. public void onRewardedVideoAdLeftApplication(){
  81. Log.i( TAG,"onRewardedVideoAdLeftApplication" );
  82. }
  83. public void onRewarded( RewardItem reward ){
  84. Log.i( TAG,"onRewarded type='"+reward.getType()+"', amount="+reward.getAmount() );
  85. _rewardType=reward.getType();
  86. _rewardAmount=reward.getAmount();
  87. }
  88. public void onRewardedVideoAdClosed(){
  89. Log.i( TAG, "onRewardedVideoAdClosed" );
  90. setState( 0 );
  91. }
  92. } );
  93. }
  94. void createInterstitialAd(){
  95. if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/1033173712";
  96. _interstitialAd=new InterstitialAd( Monkey2Activity.instance() );
  97. _interstitialAd.setAdUnitId( _adUnitId );
  98. _interstitialAd.setAdListener( new AdListener() {
  99. public void onAdLoaded() {
  100. Log.i( TAG,"onAdLoaded" );
  101. setState( 2 );
  102. }
  103. public void onAdFailedToLoad( int _errorCode ){
  104. Log.i( TAG,"onAdFailedToLoad, _errorCode="+_errorCode );
  105. _error=_errorCode;
  106. setState( 3 );
  107. }
  108. public void onAdClosed() {
  109. Log.i( TAG,"onAdClosed" );
  110. setState( 0 );
  111. }
  112. });
  113. }
  114. void createBannerAd(){
  115. if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/6300978111";
  116. AdSize adSize=AdSize.SMART_BANNER;
  117. if( _size.equals( "banner" ) ){
  118. adSize=AdSize.BANNER;
  119. }else if( _size.equals( "large-banner" ) ) {
  120. adSize=AdSize.LARGE_BANNER;
  121. }else if( _size.equals( "medium-rectangle" ) ) {
  122. adSize=AdSize.MEDIUM_RECTANGLE;
  123. }else if( _size.equals( "full-banner" ) ){
  124. adSize=AdSize.FULL_BANNER;
  125. }else if( _size.equals( "leaderboard" ) ){
  126. adSize=AdSize.LEADERBOARD;
  127. }
  128. RelativeLayout.LayoutParams params=
  129. new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
  130. if( _layout.equals( "top" ) ){
  131. params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
  132. params.addRule( RelativeLayout.CENTER_HORIZONTAL );
  133. }else if( _layout.equals( "top-left" ) ){
  134. params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
  135. params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
  136. }else if( _layout.equals( "top-right" ) ){
  137. params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
  138. params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
  139. }else if( _layout.equals( "left" ) ){
  140. params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
  141. params.addRule( RelativeLayout.CENTER_VERTICAL );
  142. }else if( _layout.equals( "right") ){
  143. params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
  144. params.addRule( RelativeLayout.CENTER_VERTICAL );
  145. }else if( _layout.equals( "bottom" ) ){
  146. params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
  147. params.addRule( RelativeLayout.CENTER_HORIZONTAL );
  148. }else if( _layout.equals( "bottom-left") ){
  149. params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
  150. params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
  151. }else if( _layout.equals( "bottom-right") ){
  152. params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
  153. params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
  154. }else{
  155. params.addRule( RelativeLayout.CENTER_VERTICAL );
  156. params.addRule( RelativeLayout.CENTER_HORIZONTAL );
  157. }
  158. _adView=new AdView( Monkey2Activity.instance() );
  159. _adView.setAdSize( adSize );
  160. _adView.setAdUnitId( _adUnitId );
  161. if( !_visible ) _adView.setVisibility( View.GONE );
  162. _adView.setAdListener( new AdListener(){
  163. public void onAdLoaded() {
  164. Log.i( TAG,"onAdLoaded" );
  165. _adView.bringToFront();
  166. setState( 2 );
  167. }
  168. public void onAdFailedToLoad( int _errorCode ){
  169. Log.i( TAG,"onAdFailedToLoad, _errorCode="+_errorCode );
  170. _error=_errorCode;
  171. setState( 3 );
  172. }
  173. });
  174. Monkey2Activity.layout().addView( _adView,params );
  175. }
  176. //must be called on main thread...!
  177. //
  178. void requestAd(){
  179. AdRequest.Builder builder=new AdRequest.Builder();
  180. AdRequest adRequest=builder.build();
  181. setState( 1 );
  182. if( _rewardedVideoAd!=null ){
  183. _rewardedVideoAd.loadAd( _adUnitId,adRequest );
  184. }else if( _interstitialAd!=null ){
  185. if( _interstitialAd.isLoaded() ){ //interstitials cant be 'reloaded'?
  186. setState( 2 );
  187. }else{
  188. _interstitialAd.loadAd( adRequest );
  189. }
  190. }else if( _adView!=null ) {
  191. _adView.loadAd( adRequest );
  192. }
  193. }
  194. public void setVisible( boolean visible ){
  195. if( visible ){
  196. Monkey2Activity.instance().runOnUiThread( new Runnable(){
  197. public void run() {
  198. if( _adState!=2 ) return;
  199. if( _rewardedVideoAd!=null ){
  200. _rewardedVideoAd.show();
  201. }else if( _interstitialAd!=null ){
  202. _interstitialAd.show();
  203. }else if( _adView!=null ){
  204. _adView.setVisibility( View.VISIBLE );
  205. _adView.bringToFront();
  206. }
  207. }
  208. } );
  209. }else{
  210. Monkey2Activity.instance().runOnUiThread( new Runnable(){
  211. public void run() {
  212. if( _adState!=2 ) return;
  213. if( _adView!=null ){
  214. _adView.setVisibility( View.GONE );
  215. }
  216. }
  217. } );
  218. }
  219. }
  220. public boolean getVisible(){
  221. return (_adView!=null) ? _adView.getVisibility()==View.VISIBLE : false;
  222. }
  223. public int getState(){
  224. return _adState;
  225. }
  226. public String getRewardType(){
  227. return _rewardType;
  228. }
  229. public int getRewardAmount(){
  230. return _rewardAmount;
  231. }
  232. public void consumeReward(){
  233. _rewardType="";
  234. _rewardAmount=0;
  235. }
  236. public int getError(){
  237. return _error;
  238. }
  239. public void reload(){
  240. _adState=0;
  241. Monkey2Activity.instance().runOnUiThread( new Runnable(){
  242. public void run() {
  243. requestAd();
  244. }
  245. } );
  246. }
  247. }