-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGodotGooglePlayServices.java
460 lines (397 loc) · 16.4 KB
/
GodotGooglePlayServices.java
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package com.android.godot;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import com.google.android.gms.ads.*;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.view.ViewGroup.LayoutParams;
import android.provider.Settings;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class GodotGooglePlayServices extends Godot.SingletonBase {
private Activity m_activity;
private AdView m_adview;
private AdRequest m_banner_adrequest;
private AdRequest m_interstitial_adrequest;
private InterstitialAd m_interstitial;
private boolean isBannerOnTop = false;
private boolean isBannerDisabled = false;
private boolean isShowingBanner = false;
private boolean isShowingInterstitial = false;
private boolean isInterstitialDisabled = false;
private boolean wasShowingBanner = false;
private boolean isBannerReady = false;
private boolean isinterstitialReady = false;
private boolean isDebug = true;
private boolean isFirstRun = true;
private boolean isTest;
private String m_banner_id;
private String m_interstitial_id;
private int m_device_id;
static public Godot.SingletonBase initialize(Activity p_activity) { return new GodotGooglePlayServices(p_activity); }
public GodotGooglePlayServices(Activity p_activity) {
registerClass("bbAdmob", new String[]{"init_admob_test","init_admob_real",
"init_admob_banner_test","init_admob_banner_real",
"show_banner","hide_banner","show_interstitial",
"init_admob_interstitial_test", "init_admob_interstitial_real","get_instance_id"});
m_activity = p_activity;
}
private String get_device_id(){
String device_id_string = Settings.Secure.getString(m_activity.getContentResolver(), Settings.Secure.ANDROID_ID);
String admobDeviceId = md5(device_id_string).toUpperCase();
return admobDeviceId;
}
private String md5(final String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
for(int i=0; i<messageDigest.length; i++) {
String h = Integer.toHexString(0xFF & messageDigest[i]);
while (h.length() < 2) {
h = "0" + h;
}
hexString.append(h);
}
return hexString.toString();
}
catch(NoSuchAlgorithmException e) {
Log.w("--------- godot ----------", "failed to get android DEVICE ID from Java");
}
return "";
}
private void banner() {
if(isBannerReady){
m_activity.runOnUiThread(new Runnable(){
@Override
public void run(){
FrameLayout layout = ((Godot)m_activity).layout;
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(isBannerOnTop){
layoutParams.gravity = Gravity.TOP;
} else {
layoutParams.gravity = Gravity.BOTTOM;
}
if(m_adview != null) {
layout.removeView(m_adview);
}
layout.addView(m_adview, layoutParams);
if(isShowingBanner){
m_adview.setVisibility(View.GONE);
m_adview.pause();
isShowingBanner = false;
if(isDebug) {
Log.d("--------- godot ----------", "admob banner paused");
}
} else {
m_adview.setVisibility(View.VISIBLE);
m_adview.resume();
isShowingBanner = true;
if(isDebug){
Log.d("--------- godot ----------", "admob banner resumed");
}
}
}
});
} else {
Log.w("--------- godot ----------", "Trying to show banner ad before it's ready");
return;
}
}
private void prepare_banner_ads() {
if(isDebug) {
Log.d("--------- godot ----------", "banner preparing to show ads");
}
m_adview = new AdView(m_activity);
m_adview.setAdUnitId(m_banner_id);
m_adview.setAdSize(AdSize.SMART_BANNER);
m_adview.setAdListener(new AdListener() {
@Override public void onAdLoaded(){
isBannerReady = true;
if(isDebug){
Log.d("--------- godot ----------", "banner ad listener loaded an ad: ");
}
}
@Override public void onAdFailedToLoad(int errorCode) {
String log = "banner failed to load an ad: ";
String err;
switch(errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
err = "ERROR_CODE_INTERNAL_ERROR";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
err = "ERROR_CODE_INVALID_REQUEST";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
err = "ERROR_CODE_NETWORK_ERROR";
break;
case AdRequest.ERROR_CODE_NO_FILL:
err = "ERROR_CODE_NO_FILL";
break;
default:
err = "banner ad failed to load due to unknown error code";
break;
}
Log.w("--------- godot ----------", log + err);
}
});
m_banner_adrequest = get_ads();
m_adview.loadAd(m_banner_adrequest);
}
private void prepare_interstitial_ads() {
if(isDebug) {
Log.d("--------- godot ----------", "m_interstitial preparing to show ads");
}
m_interstitial = new InterstitialAd(m_activity);
m_interstitial.setAdUnitId(m_interstitial_id);
m_interstitial.setAdListener(new AdListener(){
@Override
public void onAdLoaded(){
isinterstitialReady = true;
Log.d("--------- godot ----------", "m_interstitial ad listener loaded an ad: ");
}
@Override
public void onAdFailedToLoad(int errorCode) {
String log = "m_interstitial ad listener failed to load an ad: ";
String err;
switch(errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
err = "ERROR_CODE_INTERNAL_ERROR";
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
err = "ERROR_CODE_INVALID_REQUEST";
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
err = "ERROR_CODE_NETWORK_ERROR";
break;
case AdRequest.ERROR_CODE_NO_FILL:
err = "ERROR_CODE_NO_FILL";
break;
default:
err = "m_interstitial failed to load an ad because of unknown error code";
break;
}
Log.w("--------- godot ----------", log + err);
}
@Override
public void onAdClosed(){
isShowingInterstitial = false;
m_interstitial_adrequest = get_ads();
m_interstitial.loadAd(m_interstitial_adrequest);
}
});
m_interstitial_adrequest = get_ads();
m_interstitial.loadAd(m_interstitial_adrequest);
}
private AdRequest get_ads() {
AdRequest ad;
if(isTest) {
if(isDebug) {
Log.d("--------- godot ----------", "requesting to get test ads");
}
ad = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(get_device_id())
.build();
} else {
if(isDebug) {
Log.d("--------- godot ----------", "requesting to get real ads");
}
ad = new AdRequest.Builder().build();
}
return ad;
}
private void init(String banner_id, String interstitial_id, boolean isTop, boolean isTesting, boolean isDebuging) {
isDebug = isDebuging;
isBannerOnTop = isTop;
isTest = isTesting;
if(banner_id == null) {
isBannerDisabled = true;
Log.d("--------- godot ----------", "Failed to provide a banner id, cannot request banner ads");
banner_id = "";
} else if (banner_id.length() <= 0) {
isBannerDisabled = true;
} else {
m_banner_id = banner_id;
}
if(interstitial_id == null ) {
isInterstitialDisabled = true;
interstitial_id = "";
Log.d("--------- godot ----------", "Failed to provide a interstitial id, cannot request interstitial ads");
return;
} else if (interstitial_id.length() <= 0) {
isInterstitialDisabled = true;
} else {
m_interstitial_id = interstitial_id;
}
isFirstRun = false;
}
private void init_both() {
prepare_banner_ads();
prepare_interstitial_ads();
}
public void init_admob_test(String banner_id, String interstitial_id, boolean isTop) {
if(isFirstRun) {
init(banner_id, interstitial_id, isTop, true, true);
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
init_both();
Log.w("--------- godot ----------", "initing both in test mode");
}
});
} else {
Log.w("--------- godot ----------", "trying to init admob twice, should only be called once");
return;
}
}
public void init_admob_real(String banner_id, String interstitial_id, boolean isTop) {
if(isFirstRun) {
init(banner_id, interstitial_id, isTop, false, false);
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
init_both();
Log.w("--------- godot ----------", "initing both in real mode");
}
});
} else {
Log.w("--------- godot ----------", "trying to init admob twice, should only be called once");
return;
}
}
public void init_admob_banner_test(String banner_id, boolean isTop) {
if(isFirstRun) {
init(banner_id, null , isTop, true, true);
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
isInterstitialDisabled = true;
prepare_banner_ads();
Log.w("--------- godot ----------", "initing banner in test mode");
}
});
} else {
Log.w("--------- godot ----------", "trying to init admob twice, should only be called once");
return;
}
}
public void init_admob_banner_real(String banner_id, boolean isTop) {
if(isFirstRun) {
init(banner_id, null , isTop, false, false);
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
isInterstitialDisabled = true;
prepare_banner_ads();
Log.w("--------- godot ----------", "initing banner in real mode");
}
});
} else {
Log.w("--------- godot ----------", "trying to init admob twice, should only be called once");
return;
}
}
public void init_admob_interstitial_test(String interstitial_id, boolean isTop) {
if(isFirstRun) {
init(null, interstitial_id , isTop, true, true);
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
isBannerDisabled = true;
prepare_interstitial_ads();
Log.w("--------- godot ----------", "initing interstitial in test mode");
}
});
} else {
Log.w("--------- godot ----------", "trying to init admob twice, should only be called once");
return;
}
}
public void init_admob_interstitial_real(String interstitial_id, boolean isTop) {
if(isFirstRun) {
init(null, interstitial_id , isTop, false, false);
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
isBannerDisabled = true;
prepare_interstitial_ads();
Log.w("--------- godot ----------", "initing interstitial in real mode");
}
});
} else {
Log.w("--------- godot ----------", "trying to init admob twice, should only be called once");
return;
}
}
public void show_banner() {
if(!isBannerDisabled) {
if(isShowingBanner) {
return;
} else {
banner();
}
} else {
Log.w("--------- godot ----------", "cannot show or hide banner ads after init_admob_banner_");
return;
}
}
public void hide_banner() {
if(!isBannerDisabled) {
if(!isShowingBanner) {
return;
} else {
banner();
}
} else {
Log.w("--------- godot ----------", "cannot show or hide ads after init_admob_banner_");
return;
}
}
public void show_interstitial() {
if(!isInterstitialDisabled) {
m_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(m_interstitial.isLoaded()) {
m_interstitial.show();
isShowingInterstitial = true;
if(isDebug) {
Log.d("--------- godot ----------", "m_interstitial_adrequest loaded and your request an ad to be shown");
}
} else {
if(isDebug) {
Log.d("--------- godot ----------", "you tried to show m_interstitial_adrequest but an ad isn't loaded yet");
}
}
}
});
} else {
Log.w("--------- godot ----------", "cannot use interstitial ads after init_admob_interstitial_");
return;
}
}
protected void onMainPause() {
if(isShowingBanner && isShowingInterstitial) {
wasShowingBanner = true;
hide_banner();
} else if (isShowingBanner && !isShowingInterstitial) {
wasShowingBanner = true;
hide_banner();
}
}
protected void onMainResume() {
if(wasShowingBanner) {
show_banner();
wasShowingBanner = false;
}
}
public void get_instance_id(int id) {
m_device_id = id;
}
// http://stackoverflow.com/questions/3934331/android-how-to-encrypt-a-string
}