Monkey2AdView.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 onRewardedVideoAdLeftApplication(){
  78. Log.i( TAG,"onRewardedVideoAdLeftApplication" );
  79. }
  80. public void onRewarded( RewardItem reward ){
  81. Log.i( TAG,"onRewarded type='"+reward.getType()+"', amount="+reward.getAmount() );
  82. _rewardType=reward.getType();
  83. _rewardAmount=reward.getAmount();
  84. }
  85. public void onRewardedVideoAdClosed(){
  86. Log.i( TAG, "onRewardedVideoAdClosed" );
  87. setState( 0 );
  88. }
  89. } );
  90. }
  91. void createInterstitialAd(){
  92. if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/1033173712";
  93. _interstitialAd=new InterstitialAd( Monkey2Activity.instance() );
  94. _interstitialAd.setAdUnitId( _adUnitId );
  95. _interstitialAd.setAdListener( new AdListener() {
  96. public void onAdLoaded() {
  97. Log.i( TAG,"onAdLoaded" );
  98. setState( 2 );
  99. }
  100. public void onAdFailedToLoad( int _errorCode ){
  101. Log.i( TAG,"onAdFailedToLoad, _errorCode="+_errorCode );
  102. _error=_errorCode;
  103. setState( 3 );
  104. }
  105. public void onAdClosed() {
  106. Log.i( TAG,"onAdClosed" );
  107. setState( 0 );
  108. }
  109. });
  110. }
  111. void createBannerAd(){
  112. if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/6300978111";
  113. AdSize adSize=AdSize.SMART_BANNER;
  114. if( _size.equals( "banner" ) ){
  115. adSize=AdSize.BANNER;
  116. }else if( _size.equals( "large-banner" ) ) {
  117. adSize=AdSize.LARGE_BANNER;
  118. }else if( _size.equals( "medium-rectangle" ) ) {
  119. adSize=AdSize.MEDIUM_RECTANGLE;
  120. }else if( _size.equals( "full-banner" ) ){
  121. adSize=AdSize.FULL_BANNER;
  122. }else if( _size.equals( "leaderboard" ) ){
  123. adSize=AdSize.LEADERBOARD;
  124. }
  125. RelativeLayout.LayoutParams params=
  126. new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
  127. if( _layout.equals( "top" ) ){
  128. params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
  129. params.addRule( RelativeLayout.CENTER_HORIZONTAL );
  130. }else if( _layout.equals( "top-left" ) ){
  131. params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
  132. params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
  133. }else if( _layout.equals( "top-right" ) ){
  134. params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
  135. params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
  136. }else if( _layout.equals( "left" ) ){
  137. params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
  138. params.addRule( RelativeLayout.CENTER_VERTICAL );
  139. }else if( _layout.equals( "right") ){
  140. params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
  141. params.addRule( RelativeLayout.CENTER_VERTICAL );
  142. }else if( _layout.equals( "bottom" ) ){
  143. params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
  144. params.addRule( RelativeLayout.CENTER_HORIZONTAL );
  145. }else if( _layout.equals( "bottom-left") ){
  146. params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
  147. params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
  148. }else if( _layout.equals( "bottom-right") ){
  149. params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
  150. params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
  151. }else{
  152. params.addRule( RelativeLayout.CENTER_VERTICAL );
  153. params.addRule( RelativeLayout.CENTER_HORIZONTAL );
  154. }
  155. _adView=new AdView( Monkey2Activity.instance() );
  156. _adView.setAdSize( adSize );
  157. _adView.setAdUnitId( _adUnitId );
  158. if( !_visible ) _adView.setVisibility( View.GONE );
  159. _adView.setAdListener( new AdListener(){
  160. public void onAdLoaded() {
  161. Log.i( TAG,"onAdLoaded" );
  162. _adView.bringToFront();
  163. setState( 2 );
  164. }
  165. public void onAdFailedToLoad( int _errorCode ){
  166. Log.i( TAG,"onAdFailedToLoad, _errorCode="+_errorCode );
  167. _error=_errorCode;
  168. setState( 3 );
  169. }
  170. });
  171. Monkey2Activity.layout().addView( _adView,params );
  172. }
  173. //must be called on main thread...!
  174. //
  175. void requestAd(){
  176. AdRequest.Builder builder=new AdRequest.Builder();
  177. AdRequest adRequest=builder.build();
  178. setState( 1 );
  179. if( _rewardedVideoAd!=null ){
  180. _rewardedVideoAd.loadAd( _adUnitId,adRequest );
  181. }else if( _interstitialAd!=null ){
  182. if( _interstitialAd.isLoaded() ){ //interstitials cant be 'reloaded'?
  183. setState( 2 );
  184. }else{
  185. _interstitialAd.loadAd( adRequest );
  186. }
  187. }else if( _adView!=null ) {
  188. _adView.loadAd( adRequest );
  189. }
  190. }
  191. public void setVisible( boolean visible ){
  192. if( visible ){
  193. Monkey2Activity.instance().runOnUiThread( new Runnable(){
  194. public void run() {
  195. if( _adState!=2 ) return;
  196. if( _rewardedVideoAd!=null ){
  197. _rewardedVideoAd.show();
  198. }else if( _interstitialAd!=null ){
  199. _interstitialAd.show();
  200. }else if( _adView!=null ){
  201. _adView.setVisibility( View.VISIBLE );
  202. _adView.bringToFront();
  203. }
  204. }
  205. } );
  206. }else{
  207. Monkey2Activity.instance().runOnUiThread( new Runnable(){
  208. public void run() {
  209. if( _adState!=2 ) return;
  210. if( _adView!=null ){
  211. _adView.setVisibility( View.GONE );
  212. }
  213. }
  214. } );
  215. }
  216. }
  217. public boolean getVisible(){
  218. return (_adView!=null) ? _adView.getVisibility()==View.VISIBLE : false;
  219. }
  220. public int getState(){
  221. return _adState;
  222. }
  223. public String getRewardType(){
  224. return _rewardType;
  225. }
  226. public int getRewardAmount(){
  227. return _rewardAmount;
  228. }
  229. public void consumeReward(){
  230. _rewardType="";
  231. _rewardAmount=0;
  232. }
  233. public int getError(){
  234. return _error;
  235. }
  236. public void reload(){
  237. _adState=0;
  238. Monkey2Activity.instance().runOnUiThread( new Runnable(){
  239. public void run() {
  240. requestAd();
  241. }
  242. } );
  243. }
  244. }