cordova-plugin-admob-nextgen 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/LICENSE +21 -0
- package/README.md +458 -0
- package/capacitor-hook-admob-ids.js +109 -0
- package/package.json +42 -0
- package/plugin.xml +53 -0
- package/src/android/AdMobNextGen.java +306 -0
- package/src/android/AppOpenAdExecutor.java +246 -0
- package/src/android/BannerExecutor.java +447 -0
- package/src/android/BannerPreloadExecutor.java +364 -0
- package/src/android/ConsentExecutor.java +279 -0
- package/src/android/GlobalSettingsExecutor.java +139 -0
- package/src/android/InterstitialExecutor.java +223 -0
- package/src/android/NativeExecutor.java +422 -0
- package/src/android/RewardedExecutor.java +239 -0
- package/src/android/RewardedInterstitialExecutor.java +210 -0
- package/src/android/hooks/admob-nextgen.gradle +12 -0
- package/www/admob-nextgen.js +118 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
package com.emi.cordova.admob.nextgen;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.app.Application;
|
|
5
|
+
import android.content.Context;
|
|
6
|
+
import android.content.pm.ApplicationInfo;
|
|
7
|
+
import android.content.pm.PackageManager;
|
|
8
|
+
import android.os.Build;
|
|
9
|
+
import android.os.Bundle;
|
|
10
|
+
import android.util.Log;
|
|
11
|
+
import android.view.WindowInsets;
|
|
12
|
+
import android.view.WindowInsetsController;
|
|
13
|
+
|
|
14
|
+
import androidx.annotation.NonNull;
|
|
15
|
+
import androidx.annotation.Nullable;
|
|
16
|
+
import androidx.annotation.RequiresApi;
|
|
17
|
+
|
|
18
|
+
import com.google.android.libraries.ads.mobile.sdk.MobileAds;
|
|
19
|
+
import com.google.android.libraries.ads.mobile.sdk.common.RequestConfiguration;
|
|
20
|
+
import com.google.android.libraries.ads.mobile.sdk.initialization.InitializationConfig;
|
|
21
|
+
import org.apache.cordova.CallbackContext;
|
|
22
|
+
import org.apache.cordova.CordovaInterface;
|
|
23
|
+
import org.apache.cordova.CordovaPlugin;
|
|
24
|
+
import org.apache.cordova.CordovaWebView;
|
|
25
|
+
import org.json.JSONArray;
|
|
26
|
+
import org.json.JSONException;
|
|
27
|
+
import org.json.JSONObject;
|
|
28
|
+
|
|
29
|
+
public class AdMobNextGen extends CordovaPlugin {
|
|
30
|
+
|
|
31
|
+
private static final String TAG = "AdMobNextGen";
|
|
32
|
+
private BannerExecutor bannerExecutor;
|
|
33
|
+
private AppOpenAdExecutor appOpenAdExecutor;
|
|
34
|
+
private InterstitialExecutor interstitialExecutor;
|
|
35
|
+
private RewardedExecutor rewardedExecutor;
|
|
36
|
+
private RewardedInterstitialExecutor rewardedInterstitialExecutor;
|
|
37
|
+
private ConsentExecutor consentExecutor;
|
|
38
|
+
private GlobalSettingsExecutor globalSettingsExecutor;
|
|
39
|
+
private NativeExecutor nativeExecutor;
|
|
40
|
+
|
|
41
|
+
private BannerPreloadExecutor bannerPreloadExecutor;
|
|
42
|
+
|
|
43
|
+
@Override
|
|
44
|
+
public void pluginInitialize() {
|
|
45
|
+
super.pluginInitialize();
|
|
46
|
+
|
|
47
|
+
applyAdMobAPI35WorkaroundIfNeeded(cordova.getActivity().getApplication());
|
|
48
|
+
|
|
49
|
+
bannerExecutor = new BannerExecutor(cordova, webView);
|
|
50
|
+
interstitialExecutor = new InterstitialExecutor(cordova, webView);
|
|
51
|
+
rewardedExecutor = new RewardedExecutor(cordova, webView);
|
|
52
|
+
rewardedInterstitialExecutor = new RewardedInterstitialExecutor(cordova, webView);
|
|
53
|
+
consentExecutor = new ConsentExecutor(cordova, webView);
|
|
54
|
+
globalSettingsExecutor = new GlobalSettingsExecutor(cordova);
|
|
55
|
+
|
|
56
|
+
appOpenAdExecutor = AppOpenAdExecutor.getInstance();
|
|
57
|
+
appOpenAdExecutor.initialize(cordova, webView);
|
|
58
|
+
nativeExecutor = new NativeExecutor(cordova, webView);
|
|
59
|
+
|
|
60
|
+
bannerPreloadExecutor = new BannerPreloadExecutor(cordova, webView);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@Override
|
|
64
|
+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
65
|
+
|
|
66
|
+
if ("setAppVolume".equals(action)) {
|
|
67
|
+
globalSettingsExecutor.setAppVolume(args, callbackContext);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
if ("setAppMuted".equals(action)) {
|
|
71
|
+
globalSettingsExecutor.setAppMuted(args, callbackContext);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
if ("setRequestConfiguration".equals(action)) {
|
|
75
|
+
globalSettingsExecutor.setRequestConfiguration(args, callbackContext);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if ("requestConsentInfo".equals(action)) {
|
|
80
|
+
consentExecutor.requestConsentInfo(args, callbackContext);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if ("showPrivacyOptionsForm".equals(action)) {
|
|
84
|
+
consentExecutor.showPrivacyOptionsForm(callbackContext);
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if ("canRequestAds".equals(action)) {
|
|
88
|
+
consentExecutor.canRequestAds(callbackContext);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
if ("getTCData".equals(action)) {
|
|
92
|
+
consentExecutor.getTCData(callbackContext);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if ("initialize".equals(action)) {
|
|
97
|
+
this.initializeSDK(args, callbackContext);
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if ("createBanner".equals(action)) {
|
|
102
|
+
bannerExecutor.createBanner(args, callbackContext);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
if ("hideBanner".equals(action)) {
|
|
106
|
+
bannerExecutor.hideBanner(callbackContext);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
if ("showBanner".equals(action)) {
|
|
110
|
+
bannerExecutor.showBanner(callbackContext);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if ("startBannerPreload".equals(action)) {
|
|
115
|
+
bannerPreloadExecutor.startPreload(args, callbackContext);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
if ("showPreloadedBanner".equals(action)) {
|
|
119
|
+
bannerPreloadExecutor.showPolledAd(args, callbackContext);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
if ("stopBannerPreload".equals(action)) {
|
|
123
|
+
bannerPreloadExecutor.stopPreloadAndClear();
|
|
124
|
+
callbackContext.success();
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if ("removeBanner".equals(action)) {
|
|
129
|
+
bannerExecutor.destroy();
|
|
130
|
+
callbackContext.success();
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if ("loadAppOpenAd".equals(action)) {
|
|
135
|
+
if (appOpenAdExecutor != null) {
|
|
136
|
+
appOpenAdExecutor.loadAd(args, callbackContext);
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if ("showAppOpenAd".equals(action)) {
|
|
142
|
+
if (appOpenAdExecutor != null) {
|
|
143
|
+
appOpenAdExecutor.showAdIfAvailable(cordova.getActivity());
|
|
144
|
+
}
|
|
145
|
+
callbackContext.success();
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if ("setAppOpenAutoShow".equals(action)) {
|
|
150
|
+
boolean shouldShow = args.getBoolean(0);
|
|
151
|
+
if (appOpenAdExecutor != null) {
|
|
152
|
+
appOpenAdExecutor.setAutoShow(shouldShow);
|
|
153
|
+
}
|
|
154
|
+
callbackContext.success();
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if ("createNativeAd".equals(action)) {
|
|
159
|
+
nativeExecutor.createNativeAd(args, callbackContext);
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
if ("removeNativeAd".equals(action)) {
|
|
163
|
+
nativeExecutor.removeNativeAd();
|
|
164
|
+
callbackContext.success();
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if ("createInterstitial".equals(action)) {
|
|
169
|
+
interstitialExecutor.createInterstitial(args, callbackContext);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
if ("showInterstitial".equals(action)) {
|
|
173
|
+
interstitialExecutor.showInterstitial(callbackContext);
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if ("createRewarded".equals(action)) {
|
|
178
|
+
rewardedExecutor.createRewarded(args, callbackContext);
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
if ("showRewarded".equals(action)) {
|
|
182
|
+
rewardedExecutor.showRewarded(callbackContext);
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if ("createRewardedInterstitial".equals(action)) {
|
|
187
|
+
rewardedInterstitialExecutor.createRewardedInterstitial(args, callbackContext);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
if ("showRewardedInterstitial".equals(action)) {
|
|
191
|
+
rewardedInterstitialExecutor.showRewardedInterstitial(callbackContext);
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
@Override
|
|
199
|
+
public void onResume(boolean multitasking) {
|
|
200
|
+
super.onResume(multitasking);
|
|
201
|
+
if (appOpenAdExecutor != null && appOpenAdExecutor.shouldAutoShow()) {
|
|
202
|
+
appOpenAdExecutor.showAdIfAvailable(cordova.getActivity());
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private void initializeSDK(JSONArray args, CallbackContext callbackContext) {
|
|
207
|
+
String appId = getAppIdFromManifest();
|
|
208
|
+
if (appId == null) {
|
|
209
|
+
callbackContext.error("AdMob App ID missing");
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
cordova.getThreadPool().execute(() -> {
|
|
214
|
+
try {
|
|
215
|
+
InitializationConfig.Builder initConfigBuilder = new InitializationConfig.Builder(appId);
|
|
216
|
+
|
|
217
|
+
JSONObject configJson = args.optJSONObject(0);
|
|
218
|
+
|
|
219
|
+
if (configJson != null) {
|
|
220
|
+
|
|
221
|
+
RequestConfiguration requestConfig = GlobalSettingsExecutor.buildRequestConfiguration(configJson);
|
|
222
|
+
initConfigBuilder.setRequestConfiguration(requestConfig);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
InitializationConfig config = initConfigBuilder.build();
|
|
226
|
+
|
|
227
|
+
MobileAds.initialize(
|
|
228
|
+
cordova.getActivity(),
|
|
229
|
+
config,
|
|
230
|
+
initializationStatus -> {
|
|
231
|
+
cordova.getActivity().runOnUiThread(() -> callbackContext.success("Initialized"));
|
|
232
|
+
}
|
|
233
|
+
);
|
|
234
|
+
} catch (Exception e) {
|
|
235
|
+
cordova.getActivity().runOnUiThread(() -> callbackContext.error(e.getMessage()));
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private String getAppIdFromManifest() {
|
|
241
|
+
try {
|
|
242
|
+
Context context = cordova.getActivity().getApplicationContext();
|
|
243
|
+
ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
|
|
244
|
+
context.getPackageName(), PackageManager.GET_META_DATA);
|
|
245
|
+
Bundle bundle = ai.metaData;
|
|
246
|
+
return bundle.getString("com.google.android.gms.ads.APPLICATION_ID");
|
|
247
|
+
} catch (Exception e) { return null; }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
@Override
|
|
251
|
+
public void onDestroy() {
|
|
252
|
+
if (bannerExecutor != null) bannerExecutor.destroy();
|
|
253
|
+
super.onDestroy();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
public static void applyAdMobAPI35WorkaroundIfNeeded(Application application) {
|
|
257
|
+
|
|
258
|
+
if (Build.VERSION.SDK_INT < 35) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
|
|
263
|
+
@Override
|
|
264
|
+
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {}
|
|
265
|
+
|
|
266
|
+
@Override
|
|
267
|
+
public void onActivityStarted(@NonNull Activity activity) {
|
|
268
|
+
applyAPI35WorkaroundToActivity(activity);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
@Override
|
|
272
|
+
public void onActivityResumed(@NonNull Activity activity) {
|
|
273
|
+
applyAPI35WorkaroundToActivity(activity);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
@Override
|
|
277
|
+
public void onActivityPaused(@NonNull Activity activity) {}
|
|
278
|
+
|
|
279
|
+
@Override
|
|
280
|
+
public void onActivityStopped(@NonNull Activity activity) {}
|
|
281
|
+
|
|
282
|
+
@Override
|
|
283
|
+
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {}
|
|
284
|
+
|
|
285
|
+
@Override
|
|
286
|
+
public void onActivityDestroyed(@NonNull Activity activity) {}
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
@RequiresApi(api = Build.VERSION_CODES.S)
|
|
291
|
+
private static void applyAPI35WorkaroundToActivity(Activity activity) {
|
|
292
|
+
|
|
293
|
+
if (!"com.google.android.gms.ads.AdActivity".equals(activity.getClass().getName())) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (Build.VERSION.SDK_INT >= 30) {
|
|
298
|
+
WindowInsetsController c = activity.getWindow().getInsetsController();
|
|
299
|
+
if (c != null) {
|
|
300
|
+
|
|
301
|
+
c.hide(WindowInsets.Type.systemBars());
|
|
302
|
+
c.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_DEFAULT);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
package com.emi.cordova.admob.nextgen;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
|
|
6
|
+
import androidx.annotation.NonNull;
|
|
7
|
+
|
|
8
|
+
import org.apache.cordova.CallbackContext;
|
|
9
|
+
import org.apache.cordova.CordovaInterface;
|
|
10
|
+
import org.apache.cordova.CordovaWebView;
|
|
11
|
+
import org.apache.cordova.PluginResult;
|
|
12
|
+
import org.json.JSONArray;
|
|
13
|
+
import org.json.JSONException;
|
|
14
|
+
import org.json.JSONObject;
|
|
15
|
+
|
|
16
|
+
import java.util.Date;
|
|
17
|
+
|
|
18
|
+
import com.google.android.libraries.ads.mobile.sdk.appopen.AppOpenAd;
|
|
19
|
+
import com.google.android.libraries.ads.mobile.sdk.appopen.AppOpenAdEventCallback;
|
|
20
|
+
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback;
|
|
21
|
+
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest;
|
|
22
|
+
import com.google.android.libraries.ads.mobile.sdk.common.AdValue;
|
|
23
|
+
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError;
|
|
24
|
+
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError;
|
|
25
|
+
|
|
26
|
+
public class AppOpenAdExecutor {
|
|
27
|
+
|
|
28
|
+
private static final String TAG = "AdMobAppOpen";
|
|
29
|
+
private static AppOpenAdExecutor instance;
|
|
30
|
+
|
|
31
|
+
private CordovaInterface cordova;
|
|
32
|
+
private CordovaWebView webView;
|
|
33
|
+
|
|
34
|
+
private AppOpenAd appOpenAd;
|
|
35
|
+
|
|
36
|
+
private boolean isLoadingAd = false;
|
|
37
|
+
private boolean isShowingAd = false;
|
|
38
|
+
private long loadTime = 0;
|
|
39
|
+
|
|
40
|
+
private boolean isAutoShow = false;
|
|
41
|
+
private String currentAdUnitId = null;
|
|
42
|
+
|
|
43
|
+
private long lastLoadTime = 0;
|
|
44
|
+
private long minLoadInterval = 5000;
|
|
45
|
+
|
|
46
|
+
public static synchronized AppOpenAdExecutor getInstance() {
|
|
47
|
+
if (instance == null) {
|
|
48
|
+
instance = new AppOpenAdExecutor();
|
|
49
|
+
}
|
|
50
|
+
return instance;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
|
54
|
+
this.cordova = cordova;
|
|
55
|
+
this.webView = webView;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public void loadAd(JSONArray args, CallbackContext callbackContext) {
|
|
59
|
+
try {
|
|
60
|
+
JSONObject options = args.optJSONObject(0);
|
|
61
|
+
String adUnitId;
|
|
62
|
+
|
|
63
|
+
if (options != null) {
|
|
64
|
+
adUnitId = options.getString("adUnitId");
|
|
65
|
+
if (options.has("isAutoShow")) this.isAutoShow = options.getBoolean("isAutoShow");
|
|
66
|
+
if (options.has("retryInterval")) this.minLoadInterval = options.getLong("retryInterval");
|
|
67
|
+
} else {
|
|
68
|
+
adUnitId = args.getString(0);
|
|
69
|
+
this.isAutoShow = false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.currentAdUnitId = adUnitId;
|
|
73
|
+
loadAdInternal(adUnitId, callbackContext);
|
|
74
|
+
|
|
75
|
+
} catch (JSONException e) {
|
|
76
|
+
if (callbackContext != null) callbackContext.error("Invalid Args: " + e.getMessage());
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private void loadAdInternal(String adUnitId, CallbackContext callbackContext) {
|
|
81
|
+
long currentTime = new Date().getTime();
|
|
82
|
+
|
|
83
|
+
if (isLoadingAd || isAdAvailable()) {
|
|
84
|
+
if (isAdAvailable()) {
|
|
85
|
+
fireEvent("on.appopen.loaded", null);
|
|
86
|
+
if (callbackContext != null) callbackContext.success("Ad Ready");
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if ((currentTime - lastLoadTime) < minLoadInterval) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
isLoadingAd = true;
|
|
96
|
+
lastLoadTime = currentTime;
|
|
97
|
+
|
|
98
|
+
if (callbackContext != null) {
|
|
99
|
+
PluginResult r = new PluginResult(PluginResult.Status.OK, "Loading started...");
|
|
100
|
+
r.setKeepCallback(true);
|
|
101
|
+
callbackContext.sendPluginResult(r);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
cordova.getActivity().runOnUiThread(() -> {
|
|
105
|
+
|
|
106
|
+
AdRequest request = new AdRequest.Builder(adUnitId).build();
|
|
107
|
+
|
|
108
|
+
AppOpenAd.load(
|
|
109
|
+
request,
|
|
110
|
+
new AdLoadCallback<AppOpenAd>() {
|
|
111
|
+
@Override
|
|
112
|
+
public void onAdLoaded(@NonNull AppOpenAd ad) {
|
|
113
|
+
isLoadingAd = false;
|
|
114
|
+
appOpenAd = ad;
|
|
115
|
+
loadTime = new Date().getTime();
|
|
116
|
+
|
|
117
|
+
fireEvent("on.appopen.loaded", null);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@Override
|
|
121
|
+
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
|
|
122
|
+
isLoadingAd = false;
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
JSONObject errData = new JSONObject();
|
|
126
|
+
errData.put("code", loadAdError.getCode());
|
|
127
|
+
errData.put("message", loadAdError.getMessage());
|
|
128
|
+
fireEvent("on.appopen.failed.load", errData);
|
|
129
|
+
} catch (JSONException e) {}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public void showAdIfAvailable(@NonNull Activity activity) {
|
|
137
|
+
if (isShowingAd) return;
|
|
138
|
+
|
|
139
|
+
if (!isAdAvailable()) {
|
|
140
|
+
try {
|
|
141
|
+
JSONObject errData = new JSONObject();
|
|
142
|
+
errData.put("message", "Ad not ready or expired");
|
|
143
|
+
fireEvent("on.appopen.failed.show", errData);
|
|
144
|
+
} catch (JSONException e) {}
|
|
145
|
+
|
|
146
|
+
if (currentAdUnitId != null) loadAdInternal(currentAdUnitId, null);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
appOpenAd.setAdEventCallback(new AppOpenAdEventCallback() {
|
|
151
|
+
|
|
152
|
+
@Override
|
|
153
|
+
public void onAdPaid(@NonNull AdValue adValue) {
|
|
154
|
+
try {
|
|
155
|
+
JSONObject data = new JSONObject();
|
|
156
|
+
data.put("value", adValue.getValueMicros());
|
|
157
|
+
data.put("currency", adValue.getCurrencyCode());
|
|
158
|
+
data.put("precision", adValue.getPrecisionType());
|
|
159
|
+
fireEvent("on.appopen.revenue", data);
|
|
160
|
+
} catch (JSONException e) {}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
@Override
|
|
164
|
+
public void onAdShowedFullScreenContent() {
|
|
165
|
+
isShowingAd = true;
|
|
166
|
+
fireEvent("on.appopen.shown", null);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
@Override
|
|
170
|
+
public void onAdDismissedFullScreenContent() {
|
|
171
|
+
appOpenAd = null;
|
|
172
|
+
isShowingAd = false;
|
|
173
|
+
fireEvent("on.appopen.dismissed", null);
|
|
174
|
+
|
|
175
|
+
if (currentAdUnitId != null) {
|
|
176
|
+
loadAdInternal(currentAdUnitId, null);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@Override
|
|
181
|
+
public void onAdFailedToShowFullScreenContent(@NonNull FullScreenContentError error) {
|
|
182
|
+
appOpenAd = null;
|
|
183
|
+
isShowingAd = false;
|
|
184
|
+
try {
|
|
185
|
+
JSONObject errData = new JSONObject();
|
|
186
|
+
errData.put("code", error.getCode());
|
|
187
|
+
errData.put("message", error.getMessage());
|
|
188
|
+
fireEvent("on.appopen.failed.show", errData);
|
|
189
|
+
} catch (JSONException e) {}
|
|
190
|
+
|
|
191
|
+
if (currentAdUnitId != null) {
|
|
192
|
+
loadAdInternal(currentAdUnitId, null);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
@Override
|
|
197
|
+
public void onAdImpression() {
|
|
198
|
+
fireEvent("on.appopen.impression", null);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
@Override
|
|
202
|
+
public void onAdClicked() {
|
|
203
|
+
fireEvent("on.appopen.clicked", null);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
isShowingAd = true;
|
|
208
|
+
cordova.getActivity().runOnUiThread(() -> {
|
|
209
|
+
appOpenAd.show(activity);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
public boolean shouldAutoShow() {
|
|
214
|
+
return this.isAutoShow;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public void setAutoShow(boolean value) {
|
|
218
|
+
this.isAutoShow = value;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
|
|
222
|
+
long dateDifference = new Date().getTime() - loadTime;
|
|
223
|
+
long numMilliSecondsPerHour = 3600000L;
|
|
224
|
+
return dateDifference < (numMilliSecondsPerHour * numHours);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private boolean isAdAvailable() {
|
|
228
|
+
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private void fireEvent(String eventName, JSONObject data) {
|
|
232
|
+
if (cordova == null) return;
|
|
233
|
+
cordova.getActivity().runOnUiThread(() -> {
|
|
234
|
+
StringBuilder js = new StringBuilder();
|
|
235
|
+
js.append("javascript:cordova.fireDocumentEvent('");
|
|
236
|
+
js.append(eventName);
|
|
237
|
+
js.append("'");
|
|
238
|
+
if (data != null) {
|
|
239
|
+
js.append(", ");
|
|
240
|
+
js.append(data.toString());
|
|
241
|
+
}
|
|
242
|
+
js.append(");");
|
|
243
|
+
if (webView != null && webView.getView() != null) webView.loadUrl(js.toString());
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|