Skip to content

Commit

Permalink
Merge pull request #29 from defold/set-rating-feature
Browse files Browse the repository at this point in the history
Add new function `admob.set_max_ad_content_rating()`
  • Loading branch information
AGulev authored Mar 18, 2023
2 parents a323725 + 827c100 commit 07b64c6
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 1 deletion.
38 changes: 38 additions & 0 deletions extension-admob/api/admob.script_api
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,30 @@
end
```

#*****************************************************************************************************

- name: set_max_ad_content_rating
type: function
desc: Sets a maximum ad content rating. AdMob ads returned for your app will have a content rating at or below that level.
Original docs
[Android](https://developers.google.com/admob/android/targeting#ad_content_filtering),
[iOS](https://developers.google.com/admob/ios/targeting#ad_content_filtering)

parameters:
- name: max_ad_rating
type: number
desc: "Max Ad Rating, possible values:
`admob.MAX_AD_CONTENT_RATING_G`,
`admob.MAX_AD_CONTENT_RATING_PG`,
`admob.MAX_AD_CONTENT_RATING_T`,
`admob.MAX_AD_CONTENT_RATING_MA`"

examples:
- desc: |-
```lua
admob.set_max_ad_content_rating(admob.MAX_AD_CONTENT_RATING_PG)
```

#*****************************************************************************************************

- name: hide_banner
Expand Down Expand Up @@ -516,3 +540,17 @@
type: number

#*****************************************************************************************************

- name: MAX_AD_CONTENT_RATING_G
type: number

- name: MAX_AD_CONTENT_RATING_PG
type: number

- name: MAX_AD_CONTENT_RATING_T
type: number

- name: MAX_AD_CONTENT_RATING_MA
type: number

#*****************************************************************************************************
14 changes: 14 additions & 0 deletions extension-admob/src/admob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ static int Lua_ShowAdInspector(lua_State* L)
return 0;
}

static int Lua_SetMaxAdContentRating(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);
MaxAdRating max_ad_rating = (MaxAdRating)luaL_checknumber(L, 1);
SetMaxAdContentRating(max_ad_rating);
return 0;
}

static const luaL_reg Module_methods[] =
{
{"initialize", Lua_Initialize},
Expand All @@ -167,6 +175,7 @@ static const luaL_reg Module_methods[] =
{"set_privacy_settings", Lua_SetPrivacySettings},
{"request_idfa", Lua_RequestIDFA},
{"show_ad_inspector", Lua_ShowAdInspector},
{"set_max_ad_content_rating", Lua_SetMaxAdContentRating},
{0, 0}
};

Expand Down Expand Up @@ -223,6 +232,11 @@ static void LuaInit(lua_State* L)
SETCONSTANT(POS_BOTTOM_RIGHT)
SETCONSTANT(POS_CENTER)

SETCONSTANT(MAX_AD_CONTENT_RATING_G)
SETCONSTANT(MAX_AD_CONTENT_RATING_PG)
SETCONSTANT(MAX_AD_CONTENT_RATING_T)
SETCONSTANT(MAX_AD_CONTENT_RATING_MA)

#undef SETCONSTANT

// for backwards compatibility (Issue #18)
Expand Down
7 changes: 7 additions & 0 deletions extension-admob/src/admob_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct Admob
jmethodID m_RequestIDFA;
jmethodID m_ShowAdInspector;
jmethodID m_UpdateBannerLayout;
jmethodID m_SetMaxAdContentRating;

};

Expand Down Expand Up @@ -111,6 +112,7 @@ static void InitJNIMethods(JNIEnv* env, jclass cls)
g_admob.m_IsRewardedLoaded = env->GetMethodID(cls, "isRewardedLoaded", "()Z");
g_admob.m_IsInterstitialLoaded = env->GetMethodID(cls, "isInterstitialLoaded", "()Z");
g_admob.m_IsBannerLoaded = env->GetMethodID(cls, "isBannerLoaded", "()Z");
g_admob.m_SetMaxAdContentRating = env->GetMethodID(cls, "setMaxAdContentRating", "(I)V");
}

void Initialize_Ext()
Expand Down Expand Up @@ -206,6 +208,11 @@ void ActivateApp()
CallVoidMethod(g_admob.m_AdmobJNI, g_admob.m_UpdateBannerLayout);
}

void SetMaxAdContentRating(MaxAdRating max_ad_rating)
{
CallVoidMethodInt(g_admob.m_AdmobJNI, g_admob.m_SetMaxAdContentRating, (int)max_ad_rating);
}

}//namespace dmAdmob

#endif
2 changes: 1 addition & 1 deletion extension-admob/src/admob_callback_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace dmAdmob {

// The same events and messages are in AdmobJNI.java
// If you change enums here, pls nake sure you update the constants there as well
// If you change enums here, pls make sure you update the constants there as well

enum MessageId
{
Expand Down
17 changes: 17 additions & 0 deletions extension-admob/src/admob_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,23 @@ void ShowAdInspector() {
void ActivateApp() {
}

void SetMaxAdContentRating(MaxAdRating max_ad_rating) {
switch (max_ad_rating) {
case MAX_AD_CONTENT_RATING_G:
GADMobileAds.sharedInstance.requestConfiguration.maxAdContentRating = GADMaxAdContentRatingGeneral;
break;
case MAX_AD_CONTENT_RATING_PG:
GADMobileAds.sharedInstance.requestConfiguration.maxAdContentRating = GADMaxAdContentRatingParentalGuidance;
break;
case MAX_AD_CONTENT_RATING_T:
GADMobileAds.sharedInstance.requestConfiguration.maxAdContentRating = GADMaxAdContentRatingTeen;
break;
case MAX_AD_CONTENT_RATING_MA:
GADMobileAds.sharedInstance.requestConfiguration.maxAdContentRating = GADMaxAdContentRatingMatureAudience;
break;
}
}

} //namespace

@implementation AdmobExtInterstitialAdDelegate
Expand Down
13 changes: 13 additions & 0 deletions extension-admob/src/admob_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace dmAdmob {


// The same constants/enums are in AdmobJNI.java
// If you change enums here, pls make sure you update the constants there as well

enum BannerSize
{
SIZE_ADAPTIVE_BANNER = 0,
Expand All @@ -30,6 +34,14 @@ enum BannerPosition
POS_CENTER = 7
};

enum MaxAdRating
{
MAX_AD_CONTENT_RATING_G = 0,
MAX_AD_CONTENT_RATING_PG = 1,
MAX_AD_CONTENT_RATING_T = 2,
MAX_AD_CONTENT_RATING_MA = 3
};

void Initialize_Ext();

void Initialize();
Expand All @@ -45,6 +57,7 @@ void SetPrivacySettings(bool enable_rdp);
void RequestIDFA();
void ShowAdInspector();
void ActivateApp();
void SetMaxAdContentRating(MaxAdRating max_ad_rating);

bool IsInterstitialLoaded();
bool IsRewardedLoaded();
Expand Down
32 changes: 32 additions & 0 deletions extension-admob/src/java/com/defold/admob/AdmobJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdInspectorError;
import com.google.android.gms.ads.OnAdInspectorClosedListener;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

Expand Down Expand Up @@ -88,6 +89,11 @@ public class AdmobJNI {
private static final int POS_BOTTOM_RIGHT = 6;
private static final int POS_CENTER = 7;

private static final int MAX_AD_CONTENT_RATING_G = 0;
private static final int MAX_AD_CONTENT_RATING_PG = 1;
private static final int MAX_AD_CONTENT_RATING_T = 2;
private static final int MAX_AD_CONTENT_RATING_MA = 3;

// END CONSTANTS


Expand All @@ -113,6 +119,32 @@ public void setPrivacySettings(boolean enable_rdp) {
editor.commit();
}

public void setMaxAdContentRating(final int max_ad_rating) {
String rating = null;
switch (max_ad_rating) {
case MAX_AD_CONTENT_RATING_G:
rating = RequestConfiguration.MAX_AD_CONTENT_RATING_G;
break;
case MAX_AD_CONTENT_RATING_PG:
rating = RequestConfiguration.MAX_AD_CONTENT_RATING_PG;
break;
case MAX_AD_CONTENT_RATING_T:
rating = RequestConfiguration.MAX_AD_CONTENT_RATING_T;
break;
case MAX_AD_CONTENT_RATING_MA:
rating = RequestConfiguration.MAX_AD_CONTENT_RATING_MA;
break;
}
if (rating != null) {
RequestConfiguration requestConfiguration = MobileAds.getRequestConfiguration()
.toBuilder()
.setMaxAdContentRating(rating)
.build();
MobileAds.setRequestConfiguration(requestConfiguration);
Log.d(TAG, "setMaxAdContentRating " + rating);
}
}

public void requestIDFA() {
sendSimpleMessage(MSG_IDFA, EVENT_NOT_SUPPORTED);
}
Expand Down
Loading

0 comments on commit 07b64c6

Please sign in to comment.