package anywheresoftware.b4a.admobwrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
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.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.
*This view must be set to a size of 320dip x 50dip. Otherwise it will not display.
*/
@Version(1.1f)
@ShortName("AdView")
@Events(values={"ReceiveAd", "FailedToReceiveAd (ErrorCode As String)"})
@ActivityObject
@DontInheritEvents
@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE"})
public class AdViewWrapper extends ViewWrapper<AdView> {
/**
* Initializes the AdView.
*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) {
setObject(new AdView(ba.activity, AdSize.BANNER, 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) {
}
@Override
public void onLeaveApplication(Ad arg0) {
}
@Override
public void onPresentScreen(Ad arg0) {
}
});
}
/**
* Sends a request to AdMob, requesting an ad.
*/
public void LoadAd() {
AdRequest req = new AdRequest();
req.setTesting(true);
getObject().loadAd(req);
}
}