123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- package com.monkey2.lib;
- import android.provider.Settings;
- import android.util.Log;
- import android.view.View;
- import android.widget.RelativeLayout;
- import com.google.android.gms.ads.AdListener;
- import com.google.android.gms.ads.AdRequest;
- import com.google.android.gms.ads.AdSize;
- import com.google.android.gms.ads.AdView;
- import com.google.android.gms.ads.InterstitialAd;
- import com.google.android.gms.ads.MobileAds;
- import com.google.android.gms.ads.reward.RewardItem;
- import com.google.android.gms.ads.reward.RewardedVideoAd;
- import com.google.android.gms.ads.reward.RewardedVideoAdListener;
- import org.libsdl.app.SDLActivity;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class Monkey2AdView{
- private static final String TAG = "Monkey2AdView";
- String _size;
- String _layout;
- String _adUnitId;
- boolean _visible;
- int _callback;
- RewardedVideoAd _rewardedVideoAd;
- InterstitialAd _interstitialAd;
- AdView _adView;
- int _adState;
- int _error;
- String _rewardType="";
- int _rewardAmount;
- public Monkey2AdView( final String size,final String layout,final String adUnitId,final boolean visible ){
- _size=size;
- _layout=layout;
- _adUnitId=adUnitId;
- _visible=visible;
- }
- void start( int callback ){
- this._callback=callback;
- Monkey2Activity.instance().runOnUiThread( new Runnable() {
- public void run() {
- if( _size.equals( "rewardedvideo" ) ){
- createRewardedVideoAd();
- }else if( _size.equals( "interstitial" ) ){
- createInterstitialAd();
- }else{
- createBannerAd();
- }
- requestAd();
- }
- } );
- }
- void setState( int state ){
- if( state==_adState ) return;
- _adState=state;
- Monkey2Async.invokeAsyncCallback( _callback );
- }
- void createRewardedVideoAd(){
- if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/5224354917";
- _rewardedVideoAd=MobileAds.getRewardedVideoAdInstance( Monkey2Activity.instance() );
- _rewardedVideoAd.setRewardedVideoAdListener( new RewardedVideoAdListener(){
- public void onRewardedVideoAdLoaded(){
- Log.i( TAG,"onRewardedVideoAdLoaded" );
- setState( 2 );
- }
- public void onRewardedVideoAdFailedToLoad( int _errorCode ){
- Log.i( TAG,"onRewardedVideoAdFailedToLoad, _errorCode="+_errorCode );
- _error=_errorCode;
- setState( 3 );
- }
- public void onRewardedVideoAdOpened(){
- Log.i( TAG,"onRewardedVideoAdOpened" );
- }
- public void onRewardedVideoStarted(){
- Log.i( TAG,"onRewardedVideoStarted" );
- }
- public void onRewardedVideoCompleted(){
- Log.i( TAG,"onRewardedVideoCompleted" );
- }
- public void onRewardedVideoAdLeftApplication(){
- Log.i( TAG,"onRewardedVideoAdLeftApplication" );
- }
- public void onRewarded( RewardItem reward ){
- Log.i( TAG,"onRewarded type='"+reward.getType()+"', amount="+reward.getAmount() );
- _rewardType=reward.getType();
- _rewardAmount=reward.getAmount();
- }
- public void onRewardedVideoAdClosed(){
- Log.i( TAG, "onRewardedVideoAdClosed" );
- setState( 0 );
- }
- } );
- }
- void createInterstitialAd(){
- if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/1033173712";
- _interstitialAd=new InterstitialAd( Monkey2Activity.instance() );
- _interstitialAd.setAdUnitId( _adUnitId );
- _interstitialAd.setAdListener( new AdListener() {
- public void onAdLoaded() {
- Log.i( TAG,"onAdLoaded" );
- setState( 2 );
- }
- public void onAdFailedToLoad( int _errorCode ){
- Log.i( TAG,"onAdFailedToLoad, _errorCode="+_errorCode );
- _error=_errorCode;
- setState( 3 );
- }
- public void onAdClosed() {
- Log.i( TAG,"onAdClosed" );
- setState( 0 );
- }
- });
- }
- void createBannerAd(){
- if( _adUnitId.equals( "" ) ) _adUnitId="ca-app-pub-3940256099942544/6300978111";
- AdSize adSize=AdSize.SMART_BANNER;
- if( _size.equals( "banner" ) ){
- adSize=AdSize.BANNER;
- }else if( _size.equals( "large-banner" ) ) {
- adSize=AdSize.LARGE_BANNER;
- }else if( _size.equals( "medium-rectangle" ) ) {
- adSize=AdSize.MEDIUM_RECTANGLE;
- }else if( _size.equals( "full-banner" ) ){
- adSize=AdSize.FULL_BANNER;
- }else if( _size.equals( "leaderboard" ) ){
- adSize=AdSize.LEADERBOARD;
- }
- RelativeLayout.LayoutParams params=
- new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
- if( _layout.equals( "top" ) ){
- params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
- params.addRule( RelativeLayout.CENTER_HORIZONTAL );
- }else if( _layout.equals( "top-left" ) ){
- params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
- params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
- }else if( _layout.equals( "top-right" ) ){
- params.addRule( RelativeLayout.ALIGN_PARENT_TOP );
- params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
- }else if( _layout.equals( "left" ) ){
- params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
- params.addRule( RelativeLayout.CENTER_VERTICAL );
- }else if( _layout.equals( "right") ){
- params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
- params.addRule( RelativeLayout.CENTER_VERTICAL );
- }else if( _layout.equals( "bottom" ) ){
- params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
- params.addRule( RelativeLayout.CENTER_HORIZONTAL );
- }else if( _layout.equals( "bottom-left") ){
- params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
- params.addRule( RelativeLayout.ALIGN_PARENT_LEFT );
- }else if( _layout.equals( "bottom-right") ){
- params.addRule( RelativeLayout.ALIGN_PARENT_BOTTOM );
- params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT );
- }else{
- params.addRule( RelativeLayout.CENTER_VERTICAL );
- params.addRule( RelativeLayout.CENTER_HORIZONTAL );
- }
- _adView=new AdView( Monkey2Activity.instance() );
- _adView.setAdSize( adSize );
- _adView.setAdUnitId( _adUnitId );
- if( !_visible ) _adView.setVisibility( View.GONE );
- _adView.setAdListener( new AdListener(){
- public void onAdLoaded() {
- Log.i( TAG,"onAdLoaded" );
- _adView.bringToFront();
- setState( 2 );
- }
- public void onAdFailedToLoad( int _errorCode ){
- Log.i( TAG,"onAdFailedToLoad, _errorCode="+_errorCode );
- _error=_errorCode;
- setState( 3 );
- }
- });
- Monkey2Activity.layout().addView( _adView,params );
- }
- //must be called on main thread...!
- //
- void requestAd(){
- AdRequest.Builder builder=new AdRequest.Builder();
- AdRequest adRequest=builder.build();
- setState( 1 );
- if( _rewardedVideoAd!=null ){
- _rewardedVideoAd.loadAd( _adUnitId,adRequest );
- }else if( _interstitialAd!=null ){
- if( _interstitialAd.isLoaded() ){ //interstitials cant be 'reloaded'?
- setState( 2 );
- }else{
- _interstitialAd.loadAd( adRequest );
- }
- }else if( _adView!=null ) {
- _adView.loadAd( adRequest );
- }
- }
- public void setVisible( boolean visible ){
- if( visible ){
- Monkey2Activity.instance().runOnUiThread( new Runnable(){
- public void run() {
- if( _adState!=2 ) return;
- if( _rewardedVideoAd!=null ){
- _rewardedVideoAd.show();
- }else if( _interstitialAd!=null ){
- _interstitialAd.show();
- }else if( _adView!=null ){
- _adView.setVisibility( View.VISIBLE );
- _adView.bringToFront();
- }
- }
- } );
- }else{
- Monkey2Activity.instance().runOnUiThread( new Runnable(){
- public void run() {
- if( _adState!=2 ) return;
- if( _adView!=null ){
- _adView.setVisibility( View.GONE );
- }
- }
- } );
- }
- }
- public boolean getVisible(){
- return (_adView!=null) ? _adView.getVisibility()==View.VISIBLE : false;
- }
- public int getState(){
- return _adState;
- }
- public String getRewardType(){
- return _rewardType;
- }
- public int getRewardAmount(){
- return _rewardAmount;
- }
- public void consumeReward(){
- _rewardType="";
- _rewardAmount=0;
- }
- public int getError(){
- return _error;
- }
- public void reload(){
- _adState=0;
- Monkey2Activity.instance().runOnUiThread( new Runnable(){
- public void run() {
- requestAd();
- }
- } );
- }
- }
|