package anywheresoftware.b4a.admobwrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.DontInheritEvents;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.objects.ViewWrapper;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
/**
* The AdMob library allows you to add ads served by AdMob to your application.
*See the <link>AdMob Tutorial|http://www.b4x.com/forum/basic4android-getting-started-tutorials/7300-admob-tutorial-add-ads-your-application.html</link>.
*This library requires some additional configuration as described in the tutorial.
*By default the view must be set to a size of 320dip x 50dip. Otherwise it will not display.
*Tablets support three additional sizes: IAB_BANNER - 468dip x 60dip, IAB_MRECT - 300dip x 250dip and IAB_LEADERBOARD - 728dip x 90dip.
*The size of AdView must match the selected size.
*/
@Version(1.35f)
@ShortName("AdView")
@Events(values={"ReceiveAd", "FailedToReceiveAd (ErrorCode As String)",
"AdScreenDismissed", "PresentScreen"})
@ActivityObject
@DontInheritEvents
@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
@DependsOn(values={"GoogleAdMobAdsSdk"})
public class AdViewWrapper extends ViewWrapper<AdView> {
/**
* 320dip x 50dip (default size)
*/
public static Object SIZE_BANNER = AdSize.BANNER;
/**
* 468dip x 60dip - tablet only
*/
public static Object SIZE_IAB_BANNER = AdSize.IAB_BANNER;
/**
* 728dip x 90dip - tablet only
*/
public static Object SIZE_IAB_LEADERBOARD = AdSize.IAB_LEADERBOARD;
/**
* 300dip x 250dip - tablet only
*/
public static Object SIZE_IAB_MRECT = AdSize.IAB_MRECT;
/**
* Initializes the AdView using the default 320dip x 50dip size.
*EventName - Name of Subs that will handle the events.
*PublisherId - The publisher id you received from AdMob.
*/
public void Initialize(final BA ba, String EventName, String PublisherId) {
Initialize2(ba, EventName, PublisherId, AdSize.BANNER);
}
/**
* Initializes the AdView.
*EventName - Name of Subs that will handle the events.
*PublisherId - The publisher id you received from AdMob.
*Size - One of the SIZE constants.
*/
public void Initialize2(final BA ba, String EventName, String PublisherId, Object Size) {
setObject(new AdView(ba.activity, (AdSize)Size, PublisherId));
super.Initialize(ba, EventName);
final String eventName = EventName.toLowerCase(BA.cul);
getObject().setAdListener(new AdListener() {
@Override
public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode e){
ba.raiseEvent(getObject(), eventName + "_failedtoreceivead", e.toString());
}
@Override
public void onReceiveAd(Ad ad) {
ba.raiseEvent(getObject(), eventName + "_receivead");
}
@Override
public void onDismissScreen(Ad arg0) {
ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_adscreendismissed", false, null);
}
@Override
public void onLeaveApplication(Ad arg0) {
//
}
@Override
public void onPresentScreen(Ad arg0) {
ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_presentscreen", false, null);
}
});
}
/**
* Sends a request to AdMob, requesting an ad.
*/
public void LoadAd() {
AdRequest req = new AdRequest();
req.setTesting(true);
getObject().loadAd(req);
}
}