-
Notifications
You must be signed in to change notification settings - Fork 2
/
kivads.py
382 lines (312 loc) · 12.8 KB
/
kivads.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from kivy.logger import Logger
from kivy.metrics import dp
from kivy.properties import BooleanProperty
from kivy.utils import platform
if platform == "android":
from android import PythonJavaClass, autoclass, java_method, mActivity
from android.runnable import run_on_ui_thread
context = mActivity.getApplicationContext()
MobileAds = autoclass("com.google.android.gms.ads.MobileAds")
AdRequest = autoclass("com.google.android.gms.ads.AdRequest")
AdRequestBuilder = autoclass("com.google.android.gms.ads.AdRequest$Builder")
RequestConfigurationBuilder = autoclass(
"com.google.android.gms.ads.RequestConfiguration$Builder"
)
RequestConfiguration = autoclass("com.google.android.gms.ads.RequestConfiguration")
_InterstitialAd = autoclass(
"com.google.android.gms.ads.interstitial.InterstitialAd"
)
AdView = autoclass("com.google.android.gms.ads.AdView")
BListener = autoclass("org.org.kivads.BListener")
InterstitialCallback = autoclass("org.org.kivads.ICallback")
FullScreenContentCallback = autoclass("org.org.kivads.FullScreen")
Gravity = autoclass("android.view.Gravity")
LayoutParams = autoclass("android.view.ViewGroup$LayoutParams")
LinearLayout = autoclass("android.widget.LinearLayout")
AdSize = autoclass("com.google.android.gms.ads.AdSize")
View = autoclass("android.view.View")
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):
__javainterfaces__ = ["com/google/android/gms/ads/OnUserEarnedRewardListener"]
__javacontext__ = "app"
callback = None
rewarded = False
@java_method("(Lcom/google/android/gms/ads/rewarded/RewardItem;)V")
def onUserEarnedReward(self, reward):
self.reward = reward
# Run the `on_reward` callback that the user has provided
if self.callback:
self.callback()
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):
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):
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):
return self.callback.loaded
def is_dismissed(self):
if self.full_screen_callback.dismissed:
return True
else:
False
def get_reward_amount(self):
return self.reward_listener.reward.getAmount()
def get_reward_type(self):
return self.reward_listener.reward.getType()
class RewardedAd:
callback = RewardCallback()
reward_listener = RewardEarnedListener()
full_screen_callback = FullScreenContentCallback()
def __init__(self, UnitID, on_reward=None):
if platform == "android":
Logger.info("KivAds: Loading Rewarded Ad")
self.on_reward = on_reward
self.callback.loaded = False
self.callback.mRewardedAd = None
self.load(UnitID)
@run_on_ui_thread
def load(self, UnitID):
if not self.callback.loaded:
builder = AdRequestBuilder().build()
_RewardedAd.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):
if self.callback.loaded:
if immersive:
self.callback.mRewardedAd.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.mRewardedAd.setFullScreenContentCallback = (
self.full_screen_callback
)
self.callback.mRewardedAd.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):
return self.callback.loaded
def is_dismissed(self):
if self.full_screen_callback.dismissed:
return True
else:
False
def get_reward_amount(self):
return self.reward_listener.reward.getAmount()
def get_reward_type(self):
return self.reward_listener.reward.getType()
class BannerAd:
adview = AdView(context)
adlistener = BListener()
showing = False
def __init__(self, UnitID, size=None, bottom=False):
if platform == "android":
Logger.info("KivAds: Loading Banner Ad")
self.adlistener.loaded = False
self.load(UnitID, size, bottom)
@run_on_ui_thread
def load(self, UnitID, size, bottom):
if not self.adlistener.loaded:
if isinstance(size, tuple):
banner_size = AdSize(size[0], size[1])
elif isinstance(size, int):
banner_size = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
context, size / dp(1)
)
elif size == "BANNER":
banner_size = AdSize.BANNER
elif size == "LARGE_BANNER":
banner_size = AdSize.LARGE_BANNER
elif size == "MEDIUM_RECTANGLE":
banner_size = AdSize.MEDIUM_RECTANGLE
elif size == "FULL_BANNER":
banner_size = AdSize.FULL_BANNER
elif size == "LEADERBOARD":
banner_size = AdSize.LEADERBOARD
else:
banner_size = AdSize.SMART_BANNER
self.adview.setAdSize(banner_size)
self.adview.setAdUnitId(UnitID)
self.adview.setVisibility(View.GONE)
self.adview.setAdListener(self.adlistener)
layout = LinearLayout(context)
layout.addView(self.adview)
layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT
)
layout.setLayoutParams(layoutParams)
if bottom:
layout.setGravity(Gravity.BOTTOM)
activity.addContentView(layout, layoutParams)
builder = AdRequestBuilder().build()
self.adview.loadAd(builder)
else:
Logger.info(
"KivAds: Banner Ad already loaded. Call show() method to display"
)
@run_on_ui_thread
def show(self):
if self.adlistener.loaded:
if not self.showing:
self.adview.setVisibility(View.VISIBLE)
self.showing = True
else:
Logger.info(
"KivAds: Banner Ad already showing, Ignoring this function call"
)
else:
Logger.info("KivAds: Banner Ad not loaded, not showing")
@run_on_ui_thread
def hide(self):
self.adview.setVisibility(View.GONE)
self.showing = False
def is_loaded(self):
return self.adlistener.loaded
def is_clicked(self):
return self.adlistener.clicked
class InterstitialAd:
callback = InterstitialCallback()
full_screen_callback = FullScreenContentCallback()
def __init__(self, UnitID):
if platform == "android":
Logger.info("KivAds: Loading Interstitial Ad")
self.callback.loaded = False
self.callback.mInterstitialAd = None
self.load(UnitID)
@run_on_ui_thread
def load(self, UnitID):
if not self.callback.loaded:
builder = AdRequestBuilder().build()
_InterstitialAd.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):
if self.callback.loaded:
self.full_screen_callback.dismissed = False
self.callback.mInterstitialAd.setFullScreenContentCallback(
self.full_screen_callback
)
if immersive:
self.callback.mInterstitialAd.setImmersiveMode(True)
self.callback.mInterstitialAd.show(mActivity)
self.callback.loaded = False
else:
Logger.warning("KivAds:The ad hasn't loaded yet. Not showing")
def is_loaded(self):
return self.callback.loaded
def is_dismissed(self):
if self.full_screen_callback.dismissed:
return True
else:
False
class KivAds:
initialized = False
def __init__(self, show_child=False, rating=None, test_id=None, *args):
if platform == "android":
Logger.info("KivAds: Running on Android")
self.initialize_connection(show_child, rating, test_id)
else:
Logger.warning("KivAds: Not on android, Ads will not be shown")
def initialize_connection(self, show_child, rating, test_id):
Logger.info("KivAds: Initializing Google SDK connection")
self.initialized = True
# Set if to show child ads or not
if show_child:
requestConfiguration = (
RequestConfigurationBuilder().setTagForChildDirectedTreatment(
RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE
)
)
else:
requestConfiguration = (
RequestConfigurationBuilder().setTagForChildDirectedTreatment(
RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE
)
)
# Set Content Rating levels
if rating == "G":
requestConfiguration.setMaxAdContentRating(
RequestConfiguration.MAX_AD_CONTENT_RATING_G
)
elif rating == "PG":
requestConfiguration.setMaxAdContentRating(
RequestConfiguration.MAX_AD_CONTENT_RATING_PG
)
elif rating == "T":
requestConfiguration.setMaxAdContentRating(
RequestConfiguration.MAX_AD_CONTENT_RATING_T
)
elif rating == "MA":
requestConfiguration.setMaxAdContentRating(
RequestConfiguration.MAX_AD_CONTENT_RATING_MA
)
# Add the test devices if there are any
if test_id:
requestConfiguration.setTestDeviceIds(test_id)
MobileAds.setRequestConfiguration(requestConfiguration.build())
MobileAds.initialize(context)
def is_intialized(self):
return self.initialized
class TestID:
"""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.
"""
INTERSTITIAL = "ca-app-pub-3940256099942544/1033173712"
""" Test id for Interstitial ads
"""
BANNER = "ca-app-pub-3940256099942544/6300978111"
""" Test id for Banner Ads
"""
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
"""