YoYoGames / GMEXT-AdMob

Repository for GameMaker's AdMob Extension
Other
9 stars 3 forks source link

AppOpen - add ability to manually show it #55

Closed romhaviv closed 2 months ago

romhaviv commented 3 months ago

right now seems like the extensions supports app open only on Resume.

I modified modified the code a little to bring back AdMob_AppOpenAd_IsAdAvailable and AdMob_AppOpenAd_Show: and also make showOnResume optional with AdMob_AppOpenAd_Enable(double orientation, double showOnResume)

java:

// #region App Open

    private Boolean isShowingAd = false;
    private Boolean isAppOpenAdEnabled = false;
    private Boolean appOpenAdShowOnResume = false;
    private AppOpenAd appOpenAdInstance = null;

    private long appOpenAdLoadTime = 0;
    private int appOpenAdOrientation = AppOpenAd.APP_OPEN_AD_ORIENTATION_LANDSCAPE;

    private String appOpenAdUnitId = "";

    public void AdMob_AppOpenAd_Set_AdUnit(String adUnitId) {
        appOpenAdUnitId = adUnitId;
    }

    public double AdMob_AppOpenAd_Enable(double orientation, double showOnResume) {

        final String callingMethod = "AdMob_AppOpenAd_Enable";

        if (!validateInitialized(callingMethod))
            return ADMOB_ERROR_NOT_INITIALIZED;

        if (!validateAdId(appOpenAdUnitId, callingMethod))
            return ADMOB_ERROR_INVALID_AD_ID;

        isAppOpenAdEnabled = true;
        appOpenAdShowOnResume = showOnResume > 0.5 ? true : false;
        appOpenAdOrientation = (orientation == 0) ? AppOpenAd.APP_OPEN_AD_ORIENTATION_LANDSCAPE
                : AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT;
        if(appOpenAdInstance == null) {
            loadAppOpenAd();
        }

        return 0;
    }

    public void AdMob_AppOpenAd_Disable() {
        appOpenAdInstance = null;
        isAppOpenAdEnabled = false;
    }

    public double AdMob_AppOpenAd_IsEnabled() {
        return isAppOpenAdEnabled ? 1.0 : 0.0;
    }

    public double AdMob_AppOpenAd_IsAdAvailable() {
        return appOpenAdInstance == null ? 0.0 : 1.0;
    }

    public void AdMob_AppOpenAd_Show() {
        showAppOpenAd();
    }

    public void onResume() {
        if(appOpenAdShowOnResume) {
            showAppOpenAd();
        }
        isShowingAd = false;
    }

    private void loadAppOpenAd() {

        final String callingMethod = "__AdMob_AppOpenAd_Load";

        if (!isAppOpenAdEnabled)
            return;

        if (!validateInitialized(callingMethod))
            return;

        if (!validateAdId(appOpenAdUnitId, callingMethod))
            return;

        RunnerActivity.ViewHandler.post(() -> {

            final String adUnitId = appOpenAdUnitId;

            AppOpenAd.load(activity, appOpenAdUnitId, buildAdRequest(), appOpenAdOrientation,
                    new AppOpenAdLoadCallback() {
                        @Override
                        public void onAdLoaded(@NonNull AppOpenAd appOpenAd) {

                            appOpenAdLoadTime = (new Date()).getTime();
                            appOpenAdInstance = appOpenAd;

                            if (triggerOnPaidEvent) {

                                final AppOpenAd appOpenAdRef = appOpenAd;
                                appOpenAd.setOnPaidEventListener(adValue -> {
                                    AdapterResponseInfo loadedAdapterResponseInfo = appOpenAdRef
                                            .getResponseInfo()
                                            .getLoadedAdapterResponseInfo();
                                    if(loadedAdapterResponseInfo == null) return;
                                    onPaidEventHandler(adValue, appOpenAdRef.getAdUnitId(), "AppOpen",
                                            loadedAdapterResponseInfo,
                                            appOpenAdRef.getResponseInfo().getMediationAdapterClassName());
                                });
                            }

                            int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
                            RunnerJNILib.DsMapAddString(dsMapIndex, "type", "AdMob_AppOpenAd_OnLoaded");
                            RunnerJNILib.DsMapAddString(dsMapIndex, "unit_id", adUnitId);
                            RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
                        }

                        @Override
                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                            appOpenAdInstance = null;

                            int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
                            RunnerJNILib.DsMapAddString(dsMapIndex, "type", "AdMob_AppOpenAd_OnLoadFailed");
                            RunnerJNILib.DsMapAddString(dsMapIndex, "unit_id", adUnitId);
                            RunnerJNILib.DsMapAddString(dsMapIndex, "errorMessage", loadAdError.getMessage());
                            RunnerJNILib.DsMapAddDouble(dsMapIndex, "errorCode", loadAdError.getCode());
                            RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
                        }
                    });
        });
    }

    private void showAppOpenAd() {
        if(isShowingAd) {
            return;
        }
        if (!isAppOpenAdEnabled)
            return;

        final String callingMethod = "__AdMob_AppOpenAd_Show";

        if (!validateInitialized(callingMethod))
            return;

        if (!appOpenAdIsValid(4, callingMethod)) {
            appOpenAdInstance = null;
            loadAppOpenAd();
            return;
        }

        RunnerActivity.ViewHandler.post(() -> {
            if (appOpenAdInstance == null)
                return;

            appOpenAdInstance.setFullScreenContentCallback(new FullScreenContentCallback() {
                @Override
                public void onAdDismissedFullScreenContent() {

                    appOpenAdInstance = null;
                    int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
                    RunnerJNILib.DsMapAddString(dsMapIndex, "type", "AdMob_AppOpenAd_OnDismissed");
                    RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
                    loadAppOpenAd();
                }

                @Override
                public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {

                    appOpenAdInstance = null;
                    int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
                    RunnerJNILib.DsMapAddString(dsMapIndex, "type", "AdMob_AppOpenAd_OnShowFailed");
                    RunnerJNILib.DsMapAddString(dsMapIndex, "errorMessage", adError.getMessage());
                    RunnerJNILib.DsMapAddDouble(dsMapIndex, "errorCode", adError.getCode());
                    RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
                    loadAppOpenAd();
                }

                @Override
                public void onAdShowedFullScreenContent() {
                    appOpenAdInstance = null;
                    int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
                    RunnerJNILib.DsMapAddString(dsMapIndex, "type", "AdMob_AppOpenAd_OnFullyShown");
                    RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
                    loadAppOpenAd();
                }
            });

            isShowingAd = true;
            appOpenAdInstance.show(activity);
            appOpenAdInstance = null;
        });
    }

mm:

self.appOpenAdShowOnResume = false;
-(double) AdMob_AppOpenAd_Enable:(double) orientation showOnResume:(double)showOnResume
{
    if (![self validateInitializedWithCallingMethod:__FUNCTION__]) return ADMOB_ERROR_NOT_INITIALIZED;

    if (![self validateAdId:self.appOpenAdUnitId callingMethod:__FUNCTION__]) return ADMOB_ERROR_INVALID_AD_ID;

    self.isAppOpenAdEnabled = true;
    self.appOpenAdShowOnResume = showOnResume > 0.5 ? true : false;
    self.appOpenAdInstance = nil;
    self.appOpenAdOrientation = (orientation == 0) ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait;
    if(self.appOpenAdInstance == nil)
        [self loadAppOpenAd];

    return 0;
}

-(void) AdMob_AppOpenAd_Disable
{
    self.isAppOpenAdEnabled = false;
    self.appOpenAdInstance = nil;
}

-(double) AdMob_AppOpenAd_IsEnabled
{
    return self.isAppOpenAdEnabled ? 1.0 : 0.0;
}

-(double) AdMob_AppOpenAd_IsAdAvailable
{
    return self.appOpenAdInstance == nil ? 0.0 : 1.0;
}

-(void) AdMob_AppOpenAd_Show
{
    [self showAppOpenAd];
}

-(void) onResume
{
    if(self.appOpenAdShowOnResume)
        [self showAppOpenAd];
}