This repository was archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathadmob.android.ts
More file actions
383 lines (338 loc) · 14.3 KB
/
admob.android.ts
File metadata and controls
383 lines (338 loc) · 14.3 KB
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
import { firebase } from "../firebase-common";
import { BannerOptions, InterstitialOptions, PreloadRewardedVideoAdOptions, ShowRewardedVideoAdOptions } from "./admob";
import { AD_SIZE, BANNER_DEFAULTS, rewardedVideoCallbacks } from "./admob-common";
import { Application, Frame, Utils } from "@nativescript/core";
declare const com: any;
export { AD_SIZE };
export function showBanner(arg: BannerOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
const settings = firebase.merge(arg, BANNER_DEFAULTS);
// always close a previously opened banner
if (firebase.admob.adView !== null && firebase.admob.adView !== undefined) {
const parent = firebase.admob.adView.getParent();
if (parent !== null) {
parent.removeView(firebase.admob.adView);
}
}
firebase.admob.adView = new com.google.android.gms.ads.AdView(Application.android.foregroundActivity);
firebase.admob.adView.setAdUnitId(settings.androidBannerId);
const bannerType = _getBannerType(settings.size);
firebase.admob.adView.setAdSize(bannerType);
// need these to support showing a banner more than once
this.resolve = resolve;
this.reject = reject;
const BannerAdListener = com.google.android.gms.ads.AdListener.extend({
resolve: null,
reject: null,
onAdLoaded: () => this.resolve(),
onAdFailedToLoad: errorCode => this.reject(errorCode),
onAdClicked: () => arg.onClicked && arg.onClicked(),
onAdOpened: () => arg.onOpened && arg.onOpened(),
onAdLeftApplication: () => arg.onLeftApplication && arg.onLeftApplication(),
onAdClosed: () => {
if (firebase.admob.adView) {
firebase.admob.adView.setAdListener(null);
firebase.admob.adView = null;
}
arg.onClosed && arg.onClosed();
}
});
firebase.admob.adView.setAdListener(new BannerAdListener());
const ad = _buildAdRequest(settings);
firebase.admob.adView.loadAd(ad);
const density = Utils.layout.getDisplayDensity(),
top = settings.margins.top * density,
bottom = settings.margins.bottom * density;
const relativeLayoutParams = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
if (bottom > -1) {
relativeLayoutParams.bottomMargin = bottom;
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM);
} else {
if (top > -1) {
relativeLayoutParams.topMargin = top;
}
relativeLayoutParams.addRule(android.widget.RelativeLayout.ALIGN_PARENT_TOP);
}
const adViewLayout = new android.widget.RelativeLayout(Application.android.foregroundActivity);
adViewLayout.addView(firebase.admob.adView, relativeLayoutParams);
const relativeLayoutParamsOuter = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
// Wrapping it in a timeout makes sure that when this function is loaded from a Page.loaded event 'frame.Frame.topmost()' doesn't resolve to 'undefined'.
// Also, in NativeScript 4+ it may be undefined anyway.. so using the appModule in that case.
setTimeout(() => {
const top = Frame.topmost();
if (top !== undefined && top.currentPage && top.currentPage.android && top.currentPage.android.getParent()) {
top.currentPage.android.getParent().addView(adViewLayout, relativeLayoutParamsOuter);
} else if (Application.android && Application.android.foregroundActivity) {
Application.android.foregroundActivity.getWindow().getDecorView().addView(adViewLayout, relativeLayoutParamsOuter);
} else {
console.log("Could not find a view to add the banner to");
}
}, 100);
} catch (ex) {
console.log("Error in firebase.admob.showBanner: " + ex);
reject(ex);
}
});
}
export function preloadInterstitial(arg: InterstitialOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
const settings = firebase.merge(arg, BANNER_DEFAULTS);
const activity = Application.android.foregroundActivity || Application.android.startActivity;
firebase.admob.interstitialView = new com.google.android.gms.ads.InterstitialAd(activity);
firebase.admob.interstitialView.setAdUnitId(settings.androidInterstitialId);
// need these to support preloadInterstitial more than once
this.resolve = resolve;
this.reject = reject;
// Interstitial ads must be loaded before they can be shown, so adding a listener
const InterstitialAdListener = com.google.android.gms.ads.AdListener.extend({
onAdLoaded: () => this.resolve(),
onAdFailedToLoad: errorCode => this.reject(errorCode),
onAdClicked: () => arg.onClicked && arg.onClicked(),
onAdOpened: () => arg.onOpened && arg.onOpened(),
onAdLeftApplication: () => arg.onLeftApplication && arg.onLeftApplication(),
onAdClosed: () => {
if (firebase.admob.interstitialView) {
firebase.admob.interstitialView.setAdListener(null);
firebase.admob.interstitialView = null;
}
arg.onAdClosed && arg.onAdClosed(); // TODO remove one day
arg.onClosed && arg.onClosed();
}
});
firebase.admob.interstitialView.setAdListener(new InterstitialAdListener());
const ad = _buildAdRequest(settings);
firebase.admob.interstitialView.loadAd(ad);
} catch (ex) {
console.log("Error in firebase.admob.showInterstitial: " + ex);
reject(ex);
}
});
}
export function showInterstitial(arg?: InterstitialOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
// if no arg is passed, the interstitial has probably been preloaded
if (!arg) {
if (firebase.admob.interstitialView) {
firebase.admob.interstitialView.show();
resolve();
} else {
reject("Please call 'preloadInterstitial' first");
}
return;
}
const settings = firebase.merge(arg, BANNER_DEFAULTS);
const activity = Application.android.foregroundActivity || Application.android.startActivity;
firebase.admob.interstitialView = new com.google.android.gms.ads.InterstitialAd(activity);
firebase.admob.interstitialView.setAdUnitId(settings.androidInterstitialId);
// Interstitial ads must be loaded before they can be shown, so adding a listener
const InterstitialAdListener = com.google.android.gms.ads.AdListener.extend({
onAdLoaded: () => {
if (firebase.admob.interstitialView) {
firebase.admob.interstitialView.show();
}
resolve();
},
onAdFailedToLoad: errorCode => reject(errorCode),
onAdClicked: () => arg.onClicked && arg.onClicked(),
onAdOpened: () => arg.onOpened && arg.onOpened(),
onAdLeftApplication: () => arg.onLeftApplication && arg.onLeftApplication(),
onAdClosed: () => {
if (firebase.admob.interstitialView) {
firebase.admob.interstitialView.setAdListener(null);
firebase.admob.interstitialView = null;
}
arg.onAdClosed && arg.onAdClosed(); // TODO remove one day
arg.onClosed && arg.onClosed();
}
});
firebase.admob.interstitialView.setAdListener(new InterstitialAdListener());
const ad = _buildAdRequest(settings);
firebase.admob.interstitialView.loadAd(ad);
} catch (ex) {
console.log("Error in firebase.admob.showInterstitial: " + ex);
reject(ex);
}
});
}
export function preloadRewardedVideoAd(arg: PreloadRewardedVideoAdOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
const settings = firebase.merge(arg, BANNER_DEFAULTS);
const activity = Application.android.foregroundActivity || Application.android.startActivity;
firebase.admob.rewardedAdVideoView = com.google.android.gms.ads.MobileAds.getRewardedVideoAdInstance(activity);
if (firebase.admob.rewardedAdVideoView) {
//https://developers.google.com/admob/android/ssv#ssv_callback_parameters
if (settings.userId) {
firebase.admob.rewardedAdVideoView.setUserId(settings.userId);
}
if (settings.customData) {
firebase.admob.rewardedAdVideoView.setCustomData(settings.customData);
}
}
rewardedVideoCallbacks.onLoaded = resolve;
rewardedVideoCallbacks.onFailedToLoad = reject;
// rewarded Ads must be loaded before they can be shown, so adding a listener
const RewardedVideoAdListener = com.google.android.gms.ads.reward.RewardedVideoAdListener.extend({
onRewarded(reward) {
rewardedVideoCallbacks.onRewarded({
amount: reward.getAmount(),
type: reward.getType()
});
},
onRewardedVideoAdLeftApplication() {
rewardedVideoCallbacks.onLeftApplication();
},
onRewardedVideoAdClosed() {
if (firebase.admob.rewardedAdVideoView) {
firebase.admob.rewardedAdVideoView.setRewardedVideoAdListener(null);
firebase.admob.rewardedAdVideoView = null;
}
rewardedVideoCallbacks.onClosed();
},
onRewardedVideoAdFailedToLoad(errorCode) {
rewardedVideoCallbacks.onFailedToLoad(errorCode);
},
onRewardedVideoAdLoaded() {
rewardedVideoCallbacks.onLoaded();
},
onRewardedVideoAdOpened() {
rewardedVideoCallbacks.onOpened();
},
onRewardedVideoStarted() {
rewardedVideoCallbacks.onStarted();
},
onRewardedVideoCompleted() {
rewardedVideoCallbacks.onCompleted();
}
});
firebase.admob.rewardedAdVideoView.setRewardedVideoAdListener(new RewardedVideoAdListener());
const ad = _buildAdRequest(settings);
firebase.admob.rewardedAdVideoView.loadAd(settings.androidAdPlacementId, ad);
} catch (ex) {
console.log("Error in firebase.admob.preloadRewardedVideoAd: " + ex);
reject(ex);
}
});
}
export function showRewardedVideoAd(arg?: ShowRewardedVideoAdOptions): Promise<any> {
return new Promise((resolve, reject) => {
try {
if (!firebase.admob.rewardedAdVideoView) {
reject("Please call 'preloadRewardedVideoAd' first");
return;
}
if (arg.onRewarded) {
rewardedVideoCallbacks.onRewarded = arg.onRewarded;
}
if (arg.onLeftApplication) {
rewardedVideoCallbacks.onLeftApplication = arg.onLeftApplication;
}
if (arg.onClosed) {
rewardedVideoCallbacks.onClosed = arg.onClosed;
}
if (arg.onOpened) {
rewardedVideoCallbacks.onOpened = arg.onOpened;
}
if (arg.onStarted) {
rewardedVideoCallbacks.onStarted = arg.onStarted;
}
if (arg.onCompleted) {
rewardedVideoCallbacks.onCompleted = arg.onCompleted;
}
firebase.admob.rewardedAdVideoView.show();
resolve();
} catch (ex) {
console.log("Error in firebase.admob.showRewardedVideoAd: " + ex);
reject(ex);
}
});
}
export function hideBanner(): Promise<any> {
return new Promise((resolve, reject) => {
try {
if (firebase.admob.adView !== null) {
const parent = firebase.admob.adView.getParent();
if (parent !== null) {
parent.removeView(firebase.admob.adView);
}
firebase.admob.adView = null;
}
resolve();
} catch (ex) {
console.log("Error in firebase.admob.hideBanner: " + ex);
reject(ex);
}
});
}
function _getBannerType(size): any {
if (size === AD_SIZE.BANNER) {
return com.google.android.gms.ads.AdSize.BANNER;
} else if (size === AD_SIZE.LARGE_BANNER) {
return com.google.android.gms.ads.AdSize.LARGE_BANNER;
} else if (size === AD_SIZE.MEDIUM_RECTANGLE) {
return com.google.android.gms.ads.AdSize.MEDIUM_RECTANGLE;
} else if (size === AD_SIZE.FULL_BANNER) {
return com.google.android.gms.ads.AdSize.FULL_BANNER;
} else if (size === AD_SIZE.LEADERBOARD) {
return com.google.android.gms.ads.AdSize.LEADERBOARD;
} else if (size === AD_SIZE.SMART_BANNER) {
return com.google.android.gms.ads.AdSize.SMART_BANNER;
} else {
return null;
}
}
function _buildAdRequest(settings): any {
const builder = new com.google.android.gms.ads.AdRequest.Builder();
if (settings.testing) {
builder.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR);
// This will request test ads on the emulator and device by passing this hashed device ID.
const activity = Application.android.foregroundActivity || Application.android.startActivity;
const ANDROID_ID = android.provider.Settings.Secure.getString(activity.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
let deviceId = _md5(ANDROID_ID);
if (deviceId !== null) {
deviceId = deviceId.toUpperCase();
console.log("Treating this deviceId as testdevice: " + deviceId);
builder.addTestDevice(deviceId);
}
}
if (settings.keywords !== undefined && settings.keywords.length > 0) {
for (let i = 0; i < settings.keywords.length; i++) {
builder.addKeyword(settings.keywords[i]);
}
}
const bundle = new android.os.Bundle();
bundle.putInt("nativescript", 1);
const adextras = new com.google.android.gms.ads.mediation.admob.AdMobExtras(bundle);
// builder = builder.addNetworkExtras(adextras);
return builder.build();
}
function _md5(input): string {
try {
const digest = java.security.MessageDigest.getInstance("MD5");
const bytes = [];
for (let j = 0; j < input.length; ++j) {
bytes.push(input.charCodeAt(j));
}
const s = new java.lang.String(input);
digest.update(s.getBytes());
const messageDigest = digest.digest();
let hexString = "";
for (let i = 0; i < messageDigest.length; i++) {
let h = java.lang.Integer.toHexString(0xFF & messageDigest[i]);
while (h.length < 2)
h = "0" + h;
hexString += h;
}
return hexString;
} catch (noSuchAlgorithmException) {
console.log("error generating md5: " + noSuchAlgorithmException);
return null;
}
}