Skip to content

Commit

Permalink
Added Rewarded Interstitial Ads
Browse files Browse the repository at this point in the history
  • Loading branch information
Guhan-SenSam committed Sep 19, 2021
1 parent e68bf8a commit 2eaf3a7
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 17 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ These are the things that are still left to be implemented in KivAds.
- [x] Banner Ads
- [x] Reward Video Ads
- [ ] Native Ads(May or May not be added. Need to figure out how to attach android layouts to a kivy widget)
- [ ] Reward Interstitial Ads
- [x] Reward Interstitial Ads
- [ ] Host Docs on readthedocs
- [ ] Complete all Demos

Expand Down Expand Up @@ -73,12 +73,10 @@ android.meta_data = com.google.android.gms.ads.APPLICATION_ID=<Your app ID>


## Test Ids
You can use these ID's(Provided By Google) in order to test ads on your app without the need of an admob account. Just remember to change these to your actual AdMob id's or else you wont be able to earn any revenue.
You can use this ID(Provided By Google) in order to test ads on your app without the need of an admob account. Just remember to change these to your actual AdMob id's or else you wont be able to earn any revenue. KivAds also provides Test Ids for all types of ads within your code.

Test App Id: `ca-app-pub-3940256099942544~3347511713`

Test InterstitialAd Id: `ca-app-pub-3940256099942544/1033173712`


## Important Links
* https://github.com/MichaelStott/KivMob
Expand Down
21 changes: 21 additions & 0 deletions copy/RICallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.org.kivads;

import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;


public class RICallback extends RewardedInterstitialAdLoadCallback {

private RewardedInterstitialAd mRewardedInterstitialAd;

private boolean loaded;

@Override
public void onAdLoaded(RewardedInterstitialAd rewardedAd) {
// The mRewardedAd reference will be null until
// an ad is loaded.
mRewardedInterstitialAd = rewardedAd;
loaded = true;
}

}
Binary file removed demo/assets/native.png
Binary file not shown.
Binary file added demo/assets/reload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/bin/kivads-0.1-arm64-v8a-debug.apk
Binary file not shown.
87 changes: 85 additions & 2 deletions demo/kivads.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
activity = autoclass("org.kivy.android.PythonActivity")
_RewardedAd = autoclass("com.google.android.gms.ads.rewarded.RewardedAd")
RewardCallback = autoclass("org.org.kivads.RCallback")
_RewardedInterstitialAd = autoclass(
"com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd"
)
RewardInterstitialCallback = autoclass("org.org.kivads.RICallback")


class RewardEarnedListener(PythonJavaClass):
Expand All @@ -51,6 +55,81 @@ def onUserEarnedReward(self, reward):
self.rewarded = True


class RewardedInterstitial:

callback = RewardInterstitialCallback()

reward_listener = RewardEarnedListener()

full_screen_callback = FullScreenContentCallback()

def __init__(self, UnitID, on_reward=None):
if platform == "android":
Logger.info("KivAds: Loading Interstitial Rewarded Ad")
self.on_reward = on_reward
self.callback.loaded = False
self.callback.mRewardedIinterstitialAd = None
self.load(UnitID)

@run_on_ui_thread
def load(self, UnitID):
"""This function is used to load the interstitial reward ad. You don't need to call this
as when you instance the class it is auto ran. If ad is already loaded nothing
will happen.
"""

if not self.callback.loaded:
builder = AdRequestBuilder().build()
_RewardedInterstitialAd.load(context, UnitID, builder, self.callback)
else:
Logger.info("KivAds: Interstitial Ad already Loaded and Ready to Show")

@run_on_ui_thread
def show(self, immersive=False):
"""Show your rewarded interstitial ad. Takes one argument `immersive`. When set to
True your ad will be shown without the navigation drawer and the notification shade.
Function will only run if an ad is already loaded or else nothing will happen.
"""

if self.callback.loaded:
if immersive:
self.callback.mRewardedInterstitialAd.setImmersiveMode(True)
# If user has given a callback we set it here or else we leave it as None
if self.on_reward:
self.reward_listener.callback = self.on_reward
# Set the full screen content callback
self.full_screen_callback.dismissed = False
self.callback.mRewardedInterstitialAd.setFullScreenContentCallback = (
self.full_screen_callback
)
self.callback.mRewardedInterstitialAd.show(mActivity, self.reward_listener)
self.callback.loaded = False
else:
Logger.info("KivAds:The ad hasn't loaded yet. Not showing")

def is_loaded(self):
"""Call this function to check if the rewarded interstitial ad has been loaded"""
return self.callback.loaded

def is_dismissed(self):
"""
Returns if the ad was dismissed by pressing the close button
"""

if self.full_screen_callback.dismissed:
return True
else:
False

def get_reward_amount(self):
"""Returns the reward amount"""
return self.reward_listener.reward.getAmount()

def get_reward_type(self):
"""Returns the reward type"""
return self.reward_listener.reward.getType()


class RewardedAd:
"""This class represent a RewardedVideoAd Object. Instance this class to create
a RewardedVideo Ad. This class takes two arguments, `UnitID` and on_reward.
Expand Down Expand Up @@ -414,9 +493,9 @@ def is_intialized(self):
"""


class TestId:
class TestID:

"""This class contains various testids that can be used while you are tesing your app.
"""This class contains various TestIDs that can be used while you are tesing your app.
Remeber to change these when you do the final build for your app or you won't earn any money.
"""
Expand All @@ -432,3 +511,7 @@ class TestId:
REWARD = "ca-app-pub-3940256099942544/5224354917"
""" Test id for Reward Video Ads
"""

REWARD_INTERSTITIAL = "ca-app-pub-3940256099942544/5354046379"
""" Test id for Reward Interstitial Ads
"""
33 changes: 24 additions & 9 deletions demo/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from kivads import BannerAd, InterstitialAd, KivAds, RewardedAd, TestId
from kivads import (
BannerAd,
InterstitialAd,
KivAds,
RewardedAd,
RewardedInterstitial,
TestID,
)
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
Expand Down Expand Up @@ -37,13 +44,15 @@
image:"assets/rewarded.png"
on_release:app.reward.show()
CardElement:
main_text:"Native Ads"
secondary_text:"Customizable ads that match the look and feel of your app. You decide how and where they're placed."
image:"assets/native.png"
main_text:"Rewarded Interstitial Ads"
secondary_text:"A type of incentivized ad format that allows you offer rewards for ads that appear automatically during natural app transitions. Unlike rewarded ads, users aren't required to opt-in to view a rewarded interstitial."
image:"assets/rewarded.png"
on_release:app.reward_interstitial.show()
CardElement:
main_text:"Reload Ads"
on_release:app.reload_ads(app)
image:"assets/reload.png"
Expand Down Expand Up @@ -101,16 +110,22 @@ class MainScreen(Screen):
class MainApp(MDApp):
def build(self):
self.ads = KivAds()
self.interstitial = InterstitialAd(TestId.INTERSTITIAL)
self.banner = BannerAd(TestId.BANNER, int(Window.width))
self.reward = RewardedAd(TestId.REWARD, self.reward_callback)
self.interstitial = InterstitialAd(TestID.INTERSTITIAL)
self.banner = BannerAd(TestID.BANNER, int(Window.width))
self.reward = RewardedAd(TestID.REWARD, self.reward_callback)
self.reward_interstitial = RewardedInterstitial(
TestID.REWARD_INTERSTITIAL, self.reward_callback
)
return MainScreen()

def reload_ads(self, *args):
toast("Reloading Ads")
self.interstitial = InterstitialAd(TestId.INTERSTITIAL)
self.interstitial = InterstitialAd(TestID.INTERSTITIAL)
self.banner.hide()
self.reward = RewardedAd(TestId.REWARD, self.reward_callback)
self.reward = RewardedAd(TestID.REWARD, self.reward_callback)
self.reward_interstitial = RewardedInterstitial(
TestID.REWARD_INTERSTITIAL, self.reward_callback
)

def reward_callback(self, *args):
toast("You have Recieved a Reward!!")
Expand Down
21 changes: 21 additions & 0 deletions demo/src/RICallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.org.kivads;

import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;


public class RICallback extends RewardedInterstitialAdLoadCallback {

private RewardedInterstitialAd mRewardedInterstitialAd;

private boolean loaded;

@Override
public void onAdLoaded(RewardedInterstitialAd rewardedAd) {
// The mRewardedAd reference will be null until
// an ad is loaded.
mRewardedInterstitialAd = rewardedAd;
loaded = true;
}

}
87 changes: 85 additions & 2 deletions kivads.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
activity = autoclass("org.kivy.android.PythonActivity")
_RewardedAd = autoclass("com.google.android.gms.ads.rewarded.RewardedAd")
RewardCallback = autoclass("org.org.kivads.RCallback")
_RewardedInterstitialAd = autoclass(
"com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd"
)
RewardInterstitialCallback = autoclass("org.org.kivads.RICallback")


class RewardEarnedListener(PythonJavaClass):
Expand All @@ -51,6 +55,81 @@ def onUserEarnedReward(self, reward):
self.rewarded = True


class RewardedInterstitial:

callback = RewardInterstitialCallback()

reward_listener = RewardEarnedListener()

full_screen_callback = FullScreenContentCallback()

def __init__(self, UnitID, on_reward=None):
if platform == "android":
Logger.info("KivAds: Loading Interstitial Rewarded Ad")
self.on_reward = on_reward
self.callback.loaded = False
self.callback.mRewardedIinterstitialAd = None
self.load(UnitID)

@run_on_ui_thread
def load(self, UnitID):
"""This function is used to load the interstitial reward ad. You don't need to call this
as when you instance the class it is auto ran. If ad is already loaded nothing
will happen.
"""

if not self.callback.loaded:
builder = AdRequestBuilder().build()
_RewardedInterstitialAd.load(context, UnitID, builder, self.callback)
else:
Logger.info("KivAds: Interstitial Ad already Loaded and Ready to Show")

@run_on_ui_thread
def show(self, immersive=False):
"""Show your rewarded interstitial ad. Takes one argument `immersive`. When set to
True your ad will be shown without the navigation drawer and the notification shade.
Function will only run if an ad is already loaded or else nothing will happen.
"""

if self.callback.loaded:
if immersive:
self.callback.mRewardedInterstitialAd.setImmersiveMode(True)
# If user has given a callback we set it here or else we leave it as None
if self.on_reward:
self.reward_listener.callback = self.on_reward
# Set the full screen content callback
self.full_screen_callback.dismissed = False
self.callback.mRewardedInterstitialAd.setFullScreenContentCallback = (
self.full_screen_callback
)
self.callback.mRewardedInterstitialAd.show(mActivity, self.reward_listener)
self.callback.loaded = False
else:
Logger.info("KivAds:The ad hasn't loaded yet. Not showing")

def is_loaded(self):
"""Call this function to check if the rewarded interstitial ad has been loaded"""
return self.callback.loaded

def is_dismissed(self):
"""
Returns if the ad was dismissed by pressing the close button
"""

if self.full_screen_callback.dismissed:
return True
else:
False

def get_reward_amount(self):
"""Returns the reward amount"""
return self.reward_listener.reward.getAmount()

def get_reward_type(self):
"""Returns the reward type"""
return self.reward_listener.reward.getType()


class RewardedAd:
"""This class represent a RewardedVideoAd Object. Instance this class to create
a RewardedVideo Ad. This class takes two arguments, `UnitID` and on_reward.
Expand Down Expand Up @@ -414,9 +493,9 @@ def is_intialized(self):
"""


class TestId:
class TestID:

"""This class contains various testids that can be used while you are tesing your app.
"""This class contains various TestIDs that can be used while you are tesing your app.
Remeber to change these when you do the final build for your app or you won't earn any money.
"""
Expand All @@ -432,3 +511,7 @@ class TestId:
REWARD = "ca-app-pub-3940256099942544/5224354917"
""" Test id for Reward Video Ads
"""

REWARD_INTERSTITIAL = "ca-app-pub-3940256099942544/5354046379"
""" Test id for Reward Interstitial Ads
"""

0 comments on commit 2eaf3a7

Please sign in to comment.