cordova-admob-tomitank 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -0
- package/README.md +17 -0
- package/esm/admob.d.ts +3 -0
- package/esm/admob.js +22 -0
- package/esm/admob.js.map +1 -0
- package/esm/api.d.ts +61 -0
- package/esm/api.js +232 -0
- package/esm/api.js.map +1 -0
- package/esm/app-open.d.ts +17 -0
- package/esm/app-open.js +83 -0
- package/esm/app-open.js.map +1 -0
- package/esm/banner.d.ts +33 -0
- package/esm/banner.js +143 -0
- package/esm/banner.js.map +1 -0
- package/esm/generated.d.ts +77 -0
- package/esm/generated.js +104 -0
- package/esm/generated.js.map +1 -0
- package/esm/index.d.ts +31 -0
- package/esm/index.js +95 -0
- package/esm/index.js.map +1 -0
- package/esm/interstitial.d.ts +7 -0
- package/esm/interstitial.js +35 -0
- package/esm/interstitial.js.map +1 -0
- package/esm/native.d.ts +19 -0
- package/esm/native.js +134 -0
- package/esm/native.js.map +1 -0
- package/esm/rewarded-interstitial.d.ts +10 -0
- package/esm/rewarded-interstitial.js +35 -0
- package/esm/rewarded-interstitial.js.map +1 -0
- package/esm/rewarded.d.ts +14 -0
- package/esm/rewarded.js +35 -0
- package/esm/rewarded.js.map +1 -0
- package/esm/shared.d.ts +2 -0
- package/esm/shared.js +3 -0
- package/esm/shared.js.map +1 -0
- package/lib/admob.d.ts +3 -0
- package/lib/admob.js +50 -0
- package/lib/admob.js.map +1 -0
- package/lib/api.d.ts +61 -0
- package/lib/api.js +240 -0
- package/lib/api.js.map +1 -0
- package/lib/app-open.d.ts +17 -0
- package/lib/app-open.js +85 -0
- package/lib/app-open.js.map +1 -0
- package/lib/banner.d.ts +33 -0
- package/lib/banner.js +145 -0
- package/lib/banner.js.map +1 -0
- package/lib/generated.d.ts +77 -0
- package/lib/generated.js +110 -0
- package/lib/generated.js.map +1 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +120 -0
- package/lib/index.js.map +1 -0
- package/lib/interstitial.d.ts +7 -0
- package/lib/interstitial.js +37 -0
- package/lib/interstitial.js.map +1 -0
- package/lib/native.d.ts +19 -0
- package/lib/native.js +136 -0
- package/lib/native.js.map +1 -0
- package/lib/rewarded-interstitial.d.ts +10 -0
- package/lib/rewarded-interstitial.js +37 -0
- package/lib/rewarded-interstitial.js.map +1 -0
- package/lib/rewarded.d.ts +14 -0
- package/lib/rewarded.js +37 -0
- package/lib/rewarded.js.map +1 -0
- package/lib/shared.d.ts +2 -0
- package/lib/shared.js +19 -0
- package/lib/shared.js.map +1 -0
- package/package.json +73 -0
- package/plugin.xml +333 -0
- package/scripts/iosNSAppTransportSecurity.js +29 -0
- package/scripts/iosNSAppTransportSecurity.ts +33 -0
- package/scripts/tsconfig.json +5 -0
- package/scripts/util.js +34 -0
- package/scripts/util.ts +44 -0
- package/src/android/cordova/AdMob.java +256 -0
- package/src/android/cordova/ExecuteContext.java +174 -0
- package/src/android/cordova/Generated.java +72 -0
- package/src/android/cordova/ads/AdBase.java +54 -0
- package/src/android/cordova/ads/AppOpen.java +98 -0
- package/src/android/cordova/ads/Banner.java +370 -0
- package/src/android/cordova/ads/Interstitial.java +101 -0
- package/src/android/cordova/ads/Native.java +162 -0
- package/src/android/cordova/ads/Rewarded.java +108 -0
- package/src/android/cordova/ads/RewardedInterstitial.java +108 -0
- package/src/android/core/Ad.java +73 -0
- package/src/android/core/Context.java +186 -0
- package/src/android/core/GenericAd.java +22 -0
- package/src/android/core/Helper.java +134 -0
- package/src/browser/AdMobProxy.js +33 -0
- package/src/ios/AMBAdBase.swift +72 -0
- package/src/ios/AMBAppOpenAd.swift +87 -0
- package/src/ios/AMBBanner.swift +294 -0
- package/src/ios/AMBContext.swift +166 -0
- package/src/ios/AMBCore.swift +232 -0
- package/src/ios/AMBGenerated.swift +45 -0
- package/src/ios/AMBInterstitial.swift +69 -0
- package/src/ios/AMBNativeAd.swift +128 -0
- package/src/ios/AMBPlugin.swift +198 -0
- package/src/ios/AMBRewarded.swift +72 -0
- package/src/ios/AMBRewardedInterstitial.swift +72 -0
- package/src/www/admob.ts +26 -0
- package/src/www/api.ts +156 -0
- package/src/www/app-open.ts +27 -0
- package/src/www/banner.ts +102 -0
- package/src/www/cordova.d.ts +9 -0
- package/src/www/generated.ts +125 -0
- package/src/www/index.ts +88 -0
- package/src/www/interstitial.ts +17 -0
- package/src/www/native.ts +54 -0
- package/src/www/rewarded-interstitial.ts +20 -0
- package/src/www/rewarded.ts +26 -0
- package/src/www/shared.ts +2 -0
- package/www/admob.js +675 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
package admob.plus.cordova.ads;
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint;
|
|
4
|
+
import android.content.res.Configuration;
|
|
5
|
+
import android.util.Log;
|
|
6
|
+
import android.view.Gravity;
|
|
7
|
+
import android.view.View;
|
|
8
|
+
import android.view.ViewGroup;
|
|
9
|
+
import android.view.ViewTreeObserver.OnPreDrawListener;
|
|
10
|
+
import android.widget.LinearLayout;
|
|
11
|
+
import android.widget.RelativeLayout;
|
|
12
|
+
|
|
13
|
+
import androidx.annotation.NonNull;
|
|
14
|
+
import androidx.annotation.Nullable;
|
|
15
|
+
|
|
16
|
+
import com.google.android.gms.ads.AdListener;
|
|
17
|
+
import com.google.android.gms.ads.AdSize;
|
|
18
|
+
import com.google.android.gms.ads.AdView;
|
|
19
|
+
import com.google.android.gms.ads.LoadAdError;
|
|
20
|
+
|
|
21
|
+
import java.util.HashMap;
|
|
22
|
+
|
|
23
|
+
import admob.plus.cordova.ExecuteContext;
|
|
24
|
+
import admob.plus.cordova.Generated.Events;
|
|
25
|
+
import admob.plus.core.Context;
|
|
26
|
+
|
|
27
|
+
import static admob.plus.core.Helper.getParentView;
|
|
28
|
+
import static admob.plus.core.Helper.pxToDp;
|
|
29
|
+
import static admob.plus.core.Helper.removeFromParentView;
|
|
30
|
+
|
|
31
|
+
public class Banner extends AdBase {
|
|
32
|
+
private static final String TAG = "AdMobPlus.Banner";
|
|
33
|
+
@SuppressLint("StaticFieldLeak")
|
|
34
|
+
private static ViewGroup rootLinearLayout;
|
|
35
|
+
private static int screenWidth = 0;
|
|
36
|
+
|
|
37
|
+
private final AdSize adSize;
|
|
38
|
+
private final int gravity;
|
|
39
|
+
private final Integer offset;
|
|
40
|
+
private AdView mAdView;
|
|
41
|
+
private RelativeLayout mRelativeLayout = null;
|
|
42
|
+
private AdView mAdViewOld = null;
|
|
43
|
+
|
|
44
|
+
public Banner(ExecuteContext ctx) {
|
|
45
|
+
super(ctx);
|
|
46
|
+
|
|
47
|
+
this.adSize = ctx.optAdSize();
|
|
48
|
+
this.gravity = "top".equals(ctx.optPosition()) ? Gravity.TOP : Gravity.BOTTOM;
|
|
49
|
+
this.offset = ctx.optOffset();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public static void destroyParentView() {
|
|
53
|
+
ViewGroup vg = getParentView(rootLinearLayout);
|
|
54
|
+
if (vg != null) vg.removeAllViews();
|
|
55
|
+
rootLinearLayout = null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable) {
|
|
59
|
+
final OnPreDrawListener preDrawListener = new OnPreDrawListener() {
|
|
60
|
+
@Override
|
|
61
|
+
public boolean onPreDraw() {
|
|
62
|
+
view.getViewTreeObserver().removeOnPreDrawListener(this);
|
|
63
|
+
runnable.run();
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
view.getViewTreeObserver().addOnPreDrawListener(preDrawListener);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@Override
|
|
71
|
+
public boolean isLoaded() {
|
|
72
|
+
return mAdView != null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@Override
|
|
76
|
+
public void load(Context ctx) {
|
|
77
|
+
if (mAdView == null) {
|
|
78
|
+
mAdView = createBannerView();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
mAdView.loadAd(adRequest);
|
|
82
|
+
ctx.resolve();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private AdView createBannerView() {
|
|
86
|
+
AdView adView = new AdView(getActivity());
|
|
87
|
+
adView.setAdUnitId(adUnitId);
|
|
88
|
+
adView.setAdSize(adSize);
|
|
89
|
+
adView.setAdListener(new AdListener() {
|
|
90
|
+
@Override
|
|
91
|
+
public void onAdClicked() {
|
|
92
|
+
emit(Events.AD_CLICK);
|
|
93
|
+
emit(Events.BANNER_CLICK);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Override
|
|
97
|
+
public void onAdClosed() {
|
|
98
|
+
emit(Events.AD_DISMISS);
|
|
99
|
+
emit(Events.BANNER_CLOSE);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@Override
|
|
103
|
+
public void onAdFailedToLoad(LoadAdError error) {
|
|
104
|
+
emit(Events.AD_LOAD_FAIL, error);
|
|
105
|
+
emit(Events.BANNER_LOAD_FAIL, error);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@Override
|
|
109
|
+
public void onAdImpression() {
|
|
110
|
+
emit(Events.AD_IMPRESSION);
|
|
111
|
+
emit(Events.BANNER_IMPRESSION);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@Override
|
|
115
|
+
public void onAdLoaded() {
|
|
116
|
+
if (mAdViewOld != null) {
|
|
117
|
+
removeBannerView(mAdViewOld);
|
|
118
|
+
mAdViewOld = null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
runJustBeforeBeingDrawn(adView, () -> emit(Events.BANNER_SIZE, computeAdSize()));
|
|
122
|
+
|
|
123
|
+
emit(Events.AD_LOAD, computeAdSize());
|
|
124
|
+
emit(Events.BANNER_LOAD);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
@Override
|
|
128
|
+
public void onAdOpened() {
|
|
129
|
+
emit(Events.AD_SHOW);
|
|
130
|
+
emit(Events.BANNER_OPEN);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
return adView;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
@NonNull
|
|
137
|
+
private HashMap<String, Object> computeAdSize() {
|
|
138
|
+
int width = mAdView.getWidth();
|
|
139
|
+
int height = mAdView.getHeight();
|
|
140
|
+
|
|
141
|
+
return new HashMap<String, Object>() {{
|
|
142
|
+
put("size", new HashMap<String, Object>() {{
|
|
143
|
+
put("width", pxToDp(width));
|
|
144
|
+
put("height", pxToDp(height));
|
|
145
|
+
put("widthInPixels", width);
|
|
146
|
+
put("heightInPixels", height);
|
|
147
|
+
}});
|
|
148
|
+
}};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
@Override
|
|
152
|
+
public void show(Context ctx) {
|
|
153
|
+
if (mAdView.getParent() == null) {
|
|
154
|
+
addBannerView();
|
|
155
|
+
} else if (mAdView.getVisibility() == View.GONE) {
|
|
156
|
+
mAdView.resume();
|
|
157
|
+
mAdView.setVisibility(View.VISIBLE);
|
|
158
|
+
} else {
|
|
159
|
+
ViewGroup wvParentView = getParentView(getWebView());
|
|
160
|
+
if (rootLinearLayout != wvParentView) {
|
|
161
|
+
removeFromParentView(rootLinearLayout);
|
|
162
|
+
addBannerView();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
ctx.resolve();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
@Override
|
|
170
|
+
public void hide(Context ctx) {
|
|
171
|
+
if (mAdView != null) {
|
|
172
|
+
mAdView.pause();
|
|
173
|
+
mAdView.setVisibility(View.GONE);
|
|
174
|
+
}
|
|
175
|
+
ctx.resolve();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
@Override
|
|
179
|
+
public void onConfigurationChanged(Configuration newConfig) {
|
|
180
|
+
super.onConfigurationChanged(newConfig);
|
|
181
|
+
|
|
182
|
+
int w = getActivity().getResources().getDisplayMetrics().widthPixels;
|
|
183
|
+
if (w != screenWidth) {
|
|
184
|
+
screenWidth = w;
|
|
185
|
+
getActivity().runOnUiThread(this::reloadBannerView);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private void reloadBannerView() {
|
|
190
|
+
if (mAdView == null || mAdView.getVisibility() == View.GONE) return;
|
|
191
|
+
|
|
192
|
+
pauseBannerViews();
|
|
193
|
+
if (mAdViewOld != null) removeBannerView(mAdViewOld);
|
|
194
|
+
mAdViewOld = mAdView;
|
|
195
|
+
|
|
196
|
+
mAdView = createBannerView();
|
|
197
|
+
mAdView.loadAd(adRequest);
|
|
198
|
+
addBannerView();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
@Override
|
|
202
|
+
public void onPause(boolean multitasking) {
|
|
203
|
+
pauseBannerViews();
|
|
204
|
+
super.onPause(multitasking);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private void pauseBannerViews() {
|
|
208
|
+
if (mAdView != null) mAdView.pause();
|
|
209
|
+
if (mAdViewOld != null && mAdViewOld != mAdView) {
|
|
210
|
+
mAdViewOld.pause();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@Override
|
|
215
|
+
public void onResume(boolean multitasking) {
|
|
216
|
+
super.onResume(multitasking);
|
|
217
|
+
resumeBannerViews();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private void resumeBannerViews() {
|
|
221
|
+
if (mAdView != null) mAdView.resume();
|
|
222
|
+
if (mAdViewOld != null) mAdViewOld.resume();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
@Override
|
|
226
|
+
public void onDestroy() {
|
|
227
|
+
if (mAdView != null) {
|
|
228
|
+
removeBannerView(mAdView);
|
|
229
|
+
mAdView = null;
|
|
230
|
+
}
|
|
231
|
+
if (mAdViewOld != null) {
|
|
232
|
+
removeBannerView(mAdViewOld);
|
|
233
|
+
mAdViewOld = null;
|
|
234
|
+
}
|
|
235
|
+
if (mRelativeLayout != null) {
|
|
236
|
+
removeFromParentView(mRelativeLayout);
|
|
237
|
+
mRelativeLayout = null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
super.onDestroy();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private void removeBannerView(@NonNull AdView adView) {
|
|
244
|
+
removeFromParentView(adView);
|
|
245
|
+
adView.removeAllViews();
|
|
246
|
+
adView.destroy();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private void addBannerView() {
|
|
250
|
+
if (mAdView == null) return;
|
|
251
|
+
if (this.offset == null) {
|
|
252
|
+
if (getParentView(mAdView) == rootLinearLayout && rootLinearLayout != null) return;
|
|
253
|
+
addBannerViewWithLinearLayout();
|
|
254
|
+
} else {
|
|
255
|
+
if (getParentView(mAdView) == mRelativeLayout && mRelativeLayout != null) return;
|
|
256
|
+
addBannerViewWithRelativeLayout();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
ViewGroup contentView = getContentView();
|
|
260
|
+
if (contentView != null) {
|
|
261
|
+
contentView.bringToFront();
|
|
262
|
+
contentView.requestLayout();
|
|
263
|
+
contentView.requestFocus();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private void addBannerViewWithLinearLayout() {
|
|
268
|
+
View webView = getWebView();
|
|
269
|
+
ViewGroup wvParentView = getParentView(webView);
|
|
270
|
+
if (rootLinearLayout == null) {
|
|
271
|
+
rootLinearLayout = new LinearLayout(getActivity());
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (wvParentView != null && wvParentView != rootLinearLayout) {
|
|
275
|
+
wvParentView.removeView(webView);
|
|
276
|
+
LinearLayout content = (LinearLayout) rootLinearLayout;
|
|
277
|
+
content.setOrientation(LinearLayout.VERTICAL);
|
|
278
|
+
rootLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(
|
|
279
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
280
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
281
|
+
0.0F));
|
|
282
|
+
webView.setLayoutParams(new LinearLayout.LayoutParams(
|
|
283
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
284
|
+
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
285
|
+
1.0F));
|
|
286
|
+
rootLinearLayout.addView(webView);
|
|
287
|
+
|
|
288
|
+
ViewGroup view = getParentView(rootLinearLayout);
|
|
289
|
+
if (view != wvParentView) {
|
|
290
|
+
removeFromParentView(rootLinearLayout);
|
|
291
|
+
wvParentView.addView(rootLinearLayout);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
removeFromParentView(mAdView);
|
|
296
|
+
if (isPositionTop()) {
|
|
297
|
+
rootLinearLayout.addView(mAdView, 0);
|
|
298
|
+
} else {
|
|
299
|
+
rootLinearLayout.addView(mAdView);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
ViewGroup contentView = getContentView();
|
|
303
|
+
if (contentView != null) {
|
|
304
|
+
for (int i = 0; i < contentView.getChildCount(); i++) {
|
|
305
|
+
View view = contentView.getChildAt(i);
|
|
306
|
+
if (view instanceof RelativeLayout) {
|
|
307
|
+
view.bringToFront();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private void addBannerViewWithRelativeLayout() {
|
|
314
|
+
RelativeLayout.LayoutParams paramsContent = new RelativeLayout.LayoutParams(
|
|
315
|
+
RelativeLayout.LayoutParams.MATCH_PARENT,
|
|
316
|
+
RelativeLayout.LayoutParams.WRAP_CONTENT);
|
|
317
|
+
paramsContent.addRule(isPositionTop() ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM);
|
|
318
|
+
|
|
319
|
+
if (mRelativeLayout == null) {
|
|
320
|
+
mRelativeLayout = new RelativeLayout(getActivity());
|
|
321
|
+
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
|
|
322
|
+
RelativeLayout.LayoutParams.MATCH_PARENT,
|
|
323
|
+
RelativeLayout.LayoutParams.MATCH_PARENT);
|
|
324
|
+
if (isPositionTop()) {
|
|
325
|
+
params.setMargins(0, this.offset, 0, 0);
|
|
326
|
+
} else {
|
|
327
|
+
params.setMargins(0, 0, 0, this.offset);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
ViewGroup contentView = getContentView();
|
|
331
|
+
if (contentView != null) {
|
|
332
|
+
contentView.addView(mRelativeLayout, params);
|
|
333
|
+
} else {
|
|
334
|
+
Log.e(TAG, "Unable to find content view");
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
removeFromParentView(mAdView);
|
|
339
|
+
mRelativeLayout.addView(mAdView, paramsContent);
|
|
340
|
+
mRelativeLayout.bringToFront();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
private boolean isPositionTop() {
|
|
344
|
+
return gravity == Gravity.TOP;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
public enum AdSizeType {
|
|
348
|
+
BANNER, LARGE_BANNER, MEDIUM_RECTANGLE, FULL_BANNER, LEADERBOARD, SMART_BANNER;
|
|
349
|
+
|
|
350
|
+
@Nullable
|
|
351
|
+
public static AdSize getAdSize(int adSize) {
|
|
352
|
+
switch (AdSizeType.values()[adSize]) {
|
|
353
|
+
case BANNER:
|
|
354
|
+
return AdSize.BANNER;
|
|
355
|
+
case LARGE_BANNER:
|
|
356
|
+
return AdSize.LARGE_BANNER;
|
|
357
|
+
case MEDIUM_RECTANGLE:
|
|
358
|
+
return AdSize.MEDIUM_RECTANGLE;
|
|
359
|
+
case FULL_BANNER:
|
|
360
|
+
return AdSize.FULL_BANNER;
|
|
361
|
+
case LEADERBOARD:
|
|
362
|
+
return AdSize.LEADERBOARD;
|
|
363
|
+
case SMART_BANNER:
|
|
364
|
+
return AdSize.SMART_BANNER;
|
|
365
|
+
default:
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
package admob.plus.cordova.ads;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
|
|
5
|
+
import com.google.android.gms.ads.AdError;
|
|
6
|
+
import com.google.android.gms.ads.AdRequest;
|
|
7
|
+
import com.google.android.gms.ads.FullScreenContentCallback;
|
|
8
|
+
import com.google.android.gms.ads.LoadAdError;
|
|
9
|
+
import com.google.android.gms.ads.interstitial.InterstitialAd;
|
|
10
|
+
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
|
|
11
|
+
|
|
12
|
+
import admob.plus.cordova.ExecuteContext;
|
|
13
|
+
import admob.plus.cordova.Generated.Events;
|
|
14
|
+
import admob.plus.core.Context;
|
|
15
|
+
|
|
16
|
+
public class Interstitial extends AdBase {
|
|
17
|
+
private InterstitialAd mAd = null;
|
|
18
|
+
|
|
19
|
+
public Interstitial(ExecuteContext ctx) {
|
|
20
|
+
super(ctx);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Override
|
|
24
|
+
public void onDestroy() {
|
|
25
|
+
clear();
|
|
26
|
+
|
|
27
|
+
super.onDestroy();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@Override
|
|
31
|
+
public void load(Context ctx) {
|
|
32
|
+
clear();
|
|
33
|
+
|
|
34
|
+
InterstitialAd.load(getActivity(), adUnitId, adRequest, new InterstitialAdLoadCallback() {
|
|
35
|
+
@Override
|
|
36
|
+
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
|
|
37
|
+
mAd = interstitialAd;
|
|
38
|
+
mAd.setFullScreenContentCallback(new FullScreenContentCallback() {
|
|
39
|
+
@Override
|
|
40
|
+
public void onAdDismissedFullScreenContent() {
|
|
41
|
+
emit(Events.AD_DISMISS);
|
|
42
|
+
emit(Events.INTERSTITIAL_DISMISS);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@Override
|
|
46
|
+
public void onAdFailedToShowFullScreenContent(AdError adError) {
|
|
47
|
+
emit(Events.AD_SHOW_FAIL, adError);
|
|
48
|
+
emit(Events.INTERSTITIAL_SHOW_FAIL, adError);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@Override
|
|
52
|
+
public void onAdShowedFullScreenContent() {
|
|
53
|
+
mAd = null;
|
|
54
|
+
emit(Events.AD_SHOW);
|
|
55
|
+
emit(Events.INTERSTITIAL_SHOW);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Override
|
|
59
|
+
public void onAdImpression() {
|
|
60
|
+
emit(Events.AD_IMPRESSION);
|
|
61
|
+
emit(Events.INTERSTITIAL_IMPRESSION);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
emit(Events.AD_LOAD);
|
|
66
|
+
emit(Events.INTERSTITIAL_LOAD);
|
|
67
|
+
ctx.resolve();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@Override
|
|
71
|
+
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
|
|
72
|
+
mAd = null;
|
|
73
|
+
emit(Events.AD_LOAD_FAIL, loadAdError);
|
|
74
|
+
emit(Events.INTERSTITIAL_LOAD_FAIL, loadAdError);
|
|
75
|
+
ctx.reject(loadAdError.toString());
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Override
|
|
81
|
+
public boolean isLoaded() {
|
|
82
|
+
return mAd != null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@Override
|
|
86
|
+
public void show(Context ctx) {
|
|
87
|
+
if (isLoaded()) {
|
|
88
|
+
mAd.show(getActivity());
|
|
89
|
+
ctx.resolve();
|
|
90
|
+
} else {
|
|
91
|
+
ctx.reject("Ad is not loaded");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private void clear() {
|
|
96
|
+
if (mAd != null) {
|
|
97
|
+
mAd.setFullScreenContentCallback(null);
|
|
98
|
+
mAd = null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
package admob.plus.cordova.ads;
|
|
2
|
+
|
|
3
|
+
import android.view.View;
|
|
4
|
+
import android.view.ViewGroup;
|
|
5
|
+
|
|
6
|
+
import androidx.annotation.NonNull;
|
|
7
|
+
|
|
8
|
+
import com.google.android.gms.ads.AdListener;
|
|
9
|
+
import com.google.android.gms.ads.AdLoader;
|
|
10
|
+
import com.google.android.gms.ads.AdRequest;
|
|
11
|
+
import com.google.android.gms.ads.LoadAdError;
|
|
12
|
+
import com.google.android.gms.ads.nativead.NativeAd;
|
|
13
|
+
import com.google.android.gms.ads.nativead.NativeAdView;
|
|
14
|
+
|
|
15
|
+
import java.util.HashMap;
|
|
16
|
+
import java.util.Map;
|
|
17
|
+
import java.util.Objects;
|
|
18
|
+
|
|
19
|
+
import admob.plus.cordova.ExecuteContext;
|
|
20
|
+
import admob.plus.cordova.Generated.Events;
|
|
21
|
+
import admob.plus.core.Context;
|
|
22
|
+
|
|
23
|
+
import static admob.plus.core.Helper.dpToPx;
|
|
24
|
+
|
|
25
|
+
public class Native extends AdBase {
|
|
26
|
+
public static final String VIEW_DEFAULT_KEY = "default";
|
|
27
|
+
public static final Map<String, ViewProvider> providers = new HashMap<String, ViewProvider>();
|
|
28
|
+
|
|
29
|
+
private final AdRequest mAdRequest;
|
|
30
|
+
private final ViewProvider viewProvider;
|
|
31
|
+
private AdLoader mLoader;
|
|
32
|
+
private NativeAd mAd;
|
|
33
|
+
private View view;
|
|
34
|
+
|
|
35
|
+
public Native(ExecuteContext ctx) {
|
|
36
|
+
super(ctx);
|
|
37
|
+
|
|
38
|
+
mAdRequest = ctx.optAdRequest();
|
|
39
|
+
String key = ctx.optString("view");
|
|
40
|
+
if (key == null || "".equals(key)) {
|
|
41
|
+
key = VIEW_DEFAULT_KEY;
|
|
42
|
+
}
|
|
43
|
+
viewProvider = providers.get(key);
|
|
44
|
+
if (viewProvider == null) {
|
|
45
|
+
throw new RuntimeException("cannot find viewProvider: " + key);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@Override
|
|
50
|
+
public void onDestroy() {
|
|
51
|
+
clear();
|
|
52
|
+
|
|
53
|
+
super.onDestroy();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@Override
|
|
57
|
+
public boolean isLoaded() {
|
|
58
|
+
return mLoader != null && !mLoader.isLoading();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Override
|
|
62
|
+
public void load(Context ctx) {
|
|
63
|
+
clear();
|
|
64
|
+
|
|
65
|
+
mLoader = new AdLoader.Builder(getActivity(), adUnitId)
|
|
66
|
+
.forNativeAd(nativeAd -> {
|
|
67
|
+
mAd = nativeAd;
|
|
68
|
+
})
|
|
69
|
+
.withAdListener(new AdListener() {
|
|
70
|
+
@Override
|
|
71
|
+
public void onAdFailedToLoad(LoadAdError adError) {
|
|
72
|
+
emit(Events.AD_LOAD_FAIL, adError);
|
|
73
|
+
if (isLoaded()) {
|
|
74
|
+
ctx.reject(adError.toString());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public void onAdClosed() {
|
|
79
|
+
emit(Events.AD_DISMISS);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public void onAdOpened() {
|
|
83
|
+
emit(Events.AD_SHOW);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public void onAdLoaded() {
|
|
87
|
+
emit(Events.AD_LOAD);
|
|
88
|
+
if (isLoaded()) {
|
|
89
|
+
ctx.resolve();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public void onAdClicked() {
|
|
94
|
+
emit(Events.AD_CLICK);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public void onAdImpression() {
|
|
98
|
+
emit(Events.AD_IMPRESSION);
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
.build();
|
|
102
|
+
mLoader.loadAd(mAdRequest);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@Override
|
|
106
|
+
public void show(Context ctx) {
|
|
107
|
+
if (view == null) {
|
|
108
|
+
view = viewProvider.createView(mAd);
|
|
109
|
+
Objects.requireNonNull(getContentView()).addView(view);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
view.setVisibility(View.VISIBLE);
|
|
113
|
+
view.setX((float) dpToPx(ctx.optDouble("x", 0.0)));
|
|
114
|
+
view.setY((float) dpToPx(ctx.optDouble("y", 0.0)));
|
|
115
|
+
ViewGroup.LayoutParams params = view.getLayoutParams();
|
|
116
|
+
params.width = (int) dpToPx(ctx.optDouble("width", 0.0));
|
|
117
|
+
params.height = (int) dpToPx(ctx.optDouble("height", 0.0));
|
|
118
|
+
view.setLayoutParams(params);
|
|
119
|
+
|
|
120
|
+
viewProvider.didShow(this);
|
|
121
|
+
|
|
122
|
+
view.requestLayout();
|
|
123
|
+
ctx.resolve(true);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@Override
|
|
127
|
+
public void hide(Context ctx) {
|
|
128
|
+
if (view != null) {
|
|
129
|
+
view.setVisibility(View.GONE);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
viewProvider.didHide(this);
|
|
133
|
+
ctx.resolve();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private void clear() {
|
|
137
|
+
if (mAd != null) {
|
|
138
|
+
mAd.destroy();
|
|
139
|
+
mAd = null;
|
|
140
|
+
}
|
|
141
|
+
if (view != null) {
|
|
142
|
+
if (view instanceof NativeAdView) {
|
|
143
|
+
NativeAdView v = (NativeAdView) view;
|
|
144
|
+
v.removeAllViews();
|
|
145
|
+
v.destroy();
|
|
146
|
+
}
|
|
147
|
+
view = null;
|
|
148
|
+
}
|
|
149
|
+
mLoader = null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public interface ViewProvider {
|
|
153
|
+
@NonNull
|
|
154
|
+
View createView(NativeAd nativeAd);
|
|
155
|
+
|
|
156
|
+
default void didShow(@NonNull Native ad) {
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
default void didHide(@NonNull Native ad) {
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|