﻿B4A=true
Group=Default Group\modulos
ModulesStructureVersion=1
Type=Class
Version=10.7
@EndOfDesignText@
'@arf programas - version: 04/2021

Sub Class_Globals
	Private mCallback As Object
	Private mEventName As String
	Private ctxt As JavaObject	
	Private mAdUnit As String
	Private AppRAdCallback, AppRAdFullScreenCallback As JavaObject
	Private Initialized, mIntersticial, mAutoLoad, mNonPersonalizedAds As Boolean
End Sub

#Event: Rewarded (Item As Object)
#Event: AdClosed

'Intersticial = True -> Rewarded Intersticial Ad -> https://developers.google.com/admob/android/rewarded-interstitial
'Intersticial = False -> Rewarded Ad -> https://developers.google.com/admob/android/rewarded-fullscreen
'AutoLoad -> load new ad every time
'NonPersonalizedAds -> most family program
Public Sub Initialize(Callback As Object, EventName As String, Intersticial As Boolean, AdUnit As String, AutoLoad As Boolean, NonPersonalizedAds As Boolean)
	Initialized = True
	mCallback = Callback
	mEventName = EventName
	mIntersticial = Intersticial
	mAdUnit = AdUnit
	mAutoLoad = AutoLoad
	mNonPersonalizedAds = NonPersonalizedAds
	ctxt.InitializeContext
End Sub

Public Sub setCallback(Callback As Object) 
	If Not(IsInitialized) Then
		Log("call initialize first")
		Return 
	End If	
	mCallback = Callback
	ctxt.InitializeContext
	'Log("New callback for RewardedAd")
End Sub

Public Sub IsInitialized As Boolean
	Return Initialized
End Sub

Private Sub GetAdRequest As Object
	Dim builder As AdRequestBuilder
	builder.Initialize
	If mNonPersonalizedAds Then builder.NonPersonalizedAds
	Dim jo As JavaObject = builder
	Return jo.RunMethod("build", Null)
End Sub

Public Sub LoadAd
	If mAdUnit = "" Or Ready Then Return
	
	If mIntersticial Then
		AppRAdCallback.InitializeNewInstance(Application.PackageName & ".arfrewardedad$MyRewardedInterstitialAdLoadCallback", Null)
	Else
		AppRAdCallback.InitializeNewInstance(Application.PackageName & ".arfrewardedad$MyRewardedAdLoadCallback", Null)
	End If
	AppRAdCallback.RunMethod("configure", Array(Me))
	AppRAdFullScreenCallback.InitializeNewInstance(Application.PackageName & ".arfrewardedad$MyFullScreenContentCallback", Null)
	AppRAdFullScreenCallback.RunMethod("configure", Array(Me))

	FetchAd
End Sub

Private Sub FetchAd
	Dim RAd As JavaObject
	If mIntersticial Then
		RAd.InitializeStatic("com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd")
	Else
		RAd.InitializeStatic("com.google.android.gms.ads.rewarded.RewardedAd")
	End If
	
	Do While True
		If AppRAdCallback.GetField("ad") = Null Then
			RAd.RunMethod("load", Array(ctxt, mAdUnit, GetAdRequest, AppRAdCallback))
		Else
			Exit
		End If
		Sleep(10000)
	Loop
End Sub

Public Sub Show
	Try
		If AppRAdCallback.IsInitialized = False Then Return
		If AppRAdFullScreenCallback.GetField("isShowingAd") = True Then Return
	
		Dim Ad As JavaObject = AppRAdCallback.GetField("ad")
		If Ad.IsInitialized Then
			Ad.RunMethod("setFullScreenContentCallback", Array(AppRAdFullScreenCallback))
			AppRAdCallback.RunMethod("show", Null)
			AppRAdCallback.SetField("ad", Null)
		Else
			Log("nothing to show")
		End If
	Catch
		Log(LastException)
	End Try	
End Sub


Public Sub Ready As Boolean
	Try
		If AppRAdCallback.GetField("ad") = Null Then Return False Else Return True
	Catch
		Return False
	End Try
End Sub

Private Sub AdReward_Event (rewardItem As Object)
	If Not(SubExists(mCallback, mEventName & "_Rewarded")) Then Return
	
	Dim reward As JavaObject = rewardItem
	Dim a As Int = reward.RunMethod("getAmount", Null)
	Dim t As String = reward.RunMethod("getType", Null)
	'Log("AdReward: " & t & " -> " & a)
	
	'Log($"calling sub ${mEventName}_Rewarded"$)
	Do While AppRAdFullScreenCallback.GetField("isShowingAd") = True
		Sleep (100)
	Loop
	Sleep(100)
	CallSub2(mCallback, mEventName & "_Rewarded", Array(t, a))
End Sub

Private Sub AdClosed_Event
	If mAutoLoad Then 
		Sleep(1000)
		FetchAd
	End If
	
	'Log($"calling sub ${mEventName}_AdClosed"$)
	If Not(SubExists(mCallback, mEventName & "_AdClosed")) Then Return
	Sleep(100)
	CallSub(mCallback, mEventName & "_AdClosed")
End Sub

#if java
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.rewarded.*;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewardedinterstitial.*;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.gms.ads.*;

public static class MyFullScreenContentCallback extends FullScreenContentCallback {
	public boolean isShowingAd;	
	private BA ba;
	
	public void configure(B4AClass parent) {
		ba = parent.getBA();
	}

	@Override
    public void onAdDismissedFullScreenContent() {
	    isShowingAd = false;
		ba.Log("full screen content dismissed");
  		ba.raiseEventFromDifferentThread (this, null, 0, "adclosed_event", false, null);
    }
	
    @Override
    public void onAdFailedToShowFullScreenContent(AdError adError) {}

    @Override
    public void onAdShowedFullScreenContent() {
		isShowingAd = true;
    }
}

public static class MyRewardedInterstitialAdLoadCallback extends RewardedInterstitialAdLoadCallback {
	public RewardedInterstitialAd ad;
	private BA ba;
	
	public void configure(B4AClass parent) {
		ba = parent.getBA();
	}
		
	public void show() {
		ad.show(
		    ba.activity,
		    new OnUserEarnedRewardListener() {
				@Override
				public void onUserEarnedReward(RewardItem rewardItem) {
					ba.Log("User earned reward");
					ba.raiseEventFromDifferentThread (this, null, 0, "adreward_event", false, new Object[] {rewardItem});
				}
			});
	}
	
	@Override
	public void onAdFailedToLoad(LoadAdError adError) {
		BA.Log("Failed to load RewardedInterstitialAd: " + adError);
	}
	
	@Override
    public void onAdLoaded(RewardedInterstitialAd ad) {
		BA.Log("RewardedInterstitialAd received");
		this.ad = ad;
	}	
}

public static class MyRewardedAdLoadCallback extends RewardedAdLoadCallback {
	public RewardedAd ad;
	private BA ba;
	
	public void configure(B4AClass parent) {
		ba = parent.getBA();
	}
	
	@Override
	public void onAdFailedToLoad(LoadAdError adError) {
		BA.Log("Failed to load RewardedAd: " + adError);
	}
	
	@Override
    public void onAdLoaded(RewardedAd ad) {
		BA.Log("RewardedAd received");
		this.ad = ad;
	}	
		
	public void show() {
		ad.show(
		    ba.activity,
		    new OnUserEarnedRewardListener() {
				@Override
				public void onUserEarnedReward(RewardItem rewardItem) {
					ba.Log("User earned reward");
					ba.raiseEventFromDifferentThread (this, null, 0, "adreward_event", false, new Object[] {rewardItem});
				}
			});
	}	
}
#End If
