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.
@@ -0,0 +1,139 @@
1
+ package com.emi.cordova.admob.nextgen;
2
+
3
+ import android.util.Log;
4
+
5
+ import org.apache.cordova.CallbackContext;
6
+ import org.apache.cordova.CordovaInterface;
7
+ import org.json.JSONArray;
8
+ import org.json.JSONException;
9
+ import org.json.JSONObject;
10
+
11
+ import com.google.android.libraries.ads.mobile.sdk.MobileAds;
12
+ import com.google.android.libraries.ads.mobile.sdk.common.RequestConfiguration;
13
+
14
+ import java.util.ArrayList;
15
+ import java.util.List;
16
+
17
+ public class GlobalSettingsExecutor {
18
+
19
+ private static final String TAG = "AdMobSettings";
20
+ private CordovaInterface cordova;
21
+
22
+ public GlobalSettingsExecutor(CordovaInterface cordova) {
23
+ this.cordova = cordova;
24
+ }
25
+
26
+ public void setAppVolume(JSONArray args, CallbackContext callbackContext) {
27
+ try {
28
+ double volume = args.getDouble(0);
29
+ MobileAds.setUserControlledAppVolume((float) volume);
30
+ callbackContext.success();
31
+ } catch (JSONException e) {
32
+ callbackContext.error("Invalid volume value");
33
+ }
34
+ }
35
+
36
+ public void setAppMuted(JSONArray args, CallbackContext callbackContext) {
37
+ try {
38
+ boolean muted = args.getBoolean(0);
39
+ MobileAds.setUserMutedApp(muted);
40
+ callbackContext.success();
41
+ } catch (JSONException e) {
42
+ callbackContext.error("Invalid mute value");
43
+ }
44
+ }
45
+
46
+ public void setRequestConfiguration(JSONArray args, CallbackContext callbackContext) {
47
+ try {
48
+ JSONObject config = args.getJSONObject(0);
49
+ RequestConfiguration requestConfiguration = buildRequestConfiguration(config);
50
+
51
+ MobileAds.setRequestConfiguration(requestConfiguration);
52
+ callbackContext.success("Configuration Updated");
53
+ } catch (JSONException e) {
54
+ callbackContext.error("Config Error: " + e.getMessage());
55
+ }
56
+ }
57
+
58
+ public static RequestConfiguration buildRequestConfiguration(JSONObject config) {
59
+ RequestConfiguration.Builder builder = new RequestConfiguration.Builder();
60
+
61
+ try {
62
+
63
+ RequestConfiguration.TagForChildDirectedTreatment coppaTag = parseChildDirectedTag(config, "tagForChildDirectedTreatment");
64
+ builder.setTagForChildDirectedTreatment(coppaTag);
65
+
66
+ RequestConfiguration.TagForUnderAgeOfConsent tfuaTag = parseUnderAgeTag(config, "tagForUnderAgeOfConsent");
67
+ builder.setTagForUnderAgeOfConsent(tfuaTag);
68
+
69
+ if (config.has("maxAdContentRating")) {
70
+ String rating = config.getString("maxAdContentRating");
71
+ if ("G".equals(rating)) builder.setMaxAdContentRating(RequestConfiguration.MaxAdContentRating.MAX_AD_CONTENT_RATING_G);
72
+ else if ("PG".equals(rating)) builder.setMaxAdContentRating(RequestConfiguration.MaxAdContentRating.MAX_AD_CONTENT_RATING_PG);
73
+ else if ("T".equals(rating)) builder.setMaxAdContentRating(RequestConfiguration.MaxAdContentRating.MAX_AD_CONTENT_RATING_T);
74
+ else if ("MA".equals(rating)) builder.setMaxAdContentRating(RequestConfiguration.MaxAdContentRating.MAX_AD_CONTENT_RATING_MA);
75
+ }
76
+
77
+ if (config.has("testDeviceIds")) {
78
+ JSONArray ids = config.getJSONArray("testDeviceIds");
79
+ List<String> testDevices = new ArrayList<>();
80
+ for (int i = 0; i < ids.length(); i++) {
81
+ testDevices.add(ids.getString(i));
82
+ }
83
+ builder.setTestDeviceIds(testDevices);
84
+ }
85
+ } catch (JSONException e) {
86
+
87
+ }
88
+
89
+ return builder.build();
90
+ }
91
+
92
+ private static RequestConfiguration.TagForChildDirectedTreatment parseChildDirectedTag(JSONObject config, String key) {
93
+ if (!config.has(key) || config.isNull(key)) {
94
+ return RequestConfiguration.TagForChildDirectedTreatment.TAG_FOR_CHILD_DIRECTED_TREATMENT_UNSPECIFIED;
95
+ }
96
+
97
+ if (config.optBoolean(key, false) || !config.optBoolean(key, true)) {
98
+ Object val = config.opt(key);
99
+ if (val instanceof Boolean) {
100
+ return (Boolean) val ?
101
+ RequestConfiguration.TagForChildDirectedTreatment.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE :
102
+ RequestConfiguration.TagForChildDirectedTreatment.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE;
103
+ }
104
+ }
105
+
106
+ String valStr = config.optString(key);
107
+ if ("true".equalsIgnoreCase(valStr)) {
108
+ return RequestConfiguration.TagForChildDirectedTreatment.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE;
109
+ }
110
+ if ("false".equalsIgnoreCase(valStr)) {
111
+ return RequestConfiguration.TagForChildDirectedTreatment.TAG_FOR_CHILD_DIRECTED_TREATMENT_FALSE;
112
+ }
113
+
114
+ return RequestConfiguration.TagForChildDirectedTreatment.TAG_FOR_CHILD_DIRECTED_TREATMENT_UNSPECIFIED;
115
+ }
116
+
117
+ private static RequestConfiguration.TagForUnderAgeOfConsent parseUnderAgeTag(JSONObject config, String key) {
118
+ if (!config.has(key) || config.isNull(key)) {
119
+ return RequestConfiguration.TagForUnderAgeOfConsent.TAG_FOR_UNDER_AGE_OF_CONSENT_UNSPECIFIED;
120
+ }
121
+
122
+ Object val = config.opt(key);
123
+ if (val instanceof Boolean) {
124
+ return (Boolean) val ?
125
+ RequestConfiguration.TagForUnderAgeOfConsent.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE :
126
+ RequestConfiguration.TagForUnderAgeOfConsent.TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE;
127
+ }
128
+
129
+ String valStr = config.optString(key);
130
+ if ("true".equalsIgnoreCase(valStr)) {
131
+ return RequestConfiguration.TagForUnderAgeOfConsent.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE;
132
+ }
133
+ if ("false".equalsIgnoreCase(valStr)) {
134
+ return RequestConfiguration.TagForUnderAgeOfConsent.TAG_FOR_UNDER_AGE_OF_CONSENT_FALSE;
135
+ }
136
+
137
+ return RequestConfiguration.TagForUnderAgeOfConsent.TAG_FOR_UNDER_AGE_OF_CONSENT_UNSPECIFIED;
138
+ }
139
+ }
@@ -0,0 +1,223 @@
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 com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback;
17
+ import com.google.android.libraries.ads.mobile.sdk.common.AdRequest;
18
+ import com.google.android.libraries.ads.mobile.sdk.common.AdValue;
19
+ import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError;
20
+ import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError;
21
+ import com.google.android.libraries.ads.mobile.sdk.interstitial.InterstitialAd;
22
+ import com.google.android.libraries.ads.mobile.sdk.interstitial.InterstitialAdEventCallback;
23
+
24
+ import java.util.Date;
25
+
26
+ public class InterstitialExecutor {
27
+
28
+ private static final String TAG = "AdMobInterstitial";
29
+ private CordovaInterface cordova;
30
+ private CordovaWebView webView;
31
+
32
+ private InterstitialAd interstitialAd;
33
+
34
+ private boolean isLoading = false;
35
+ private boolean isAutoShow = true;
36
+
37
+ private long lastLoadTime = 0;
38
+ private long minLoadInterval = 5000;
39
+
40
+ public InterstitialExecutor(CordovaInterface cordova, CordovaWebView webView) {
41
+ this.cordova = cordova;
42
+ this.webView = webView;
43
+ }
44
+
45
+ public void createInterstitial(JSONArray args, CallbackContext callbackContext) {
46
+ try {
47
+ JSONObject options = args.getJSONObject(0);
48
+ String adUnitId = options.getString("adUnitId");
49
+
50
+ if (options.has("isAutoShow")) {
51
+ this.isAutoShow = options.getBoolean("isAutoShow");
52
+ } else {
53
+ this.isAutoShow = true;
54
+ }
55
+
56
+ if (options.has("retryInterval")) {
57
+ this.minLoadInterval = options.getLong("retryInterval");
58
+ }
59
+
60
+ loadInterstitial(adUnitId, callbackContext);
61
+
62
+ } catch (JSONException e) {
63
+ callbackContext.error("Invalid JSON Args: " + e.getMessage());
64
+ }
65
+ }
66
+
67
+ private void loadInterstitial(String adUnitId, CallbackContext requestCallback) {
68
+ long currentTime = new Date().getTime();
69
+
70
+ if (isLoading) {
71
+
72
+ return;
73
+ }
74
+
75
+ if (interstitialAd != null) {
76
+
77
+ fireEvent("on.interstitial.loaded", null);
78
+ return;
79
+ }
80
+
81
+ if ((currentTime - lastLoadTime) < minLoadInterval) {
82
+
83
+ return;
84
+ }
85
+
86
+ isLoading = true;
87
+ lastLoadTime = currentTime;
88
+
89
+ PluginResult r = new PluginResult(PluginResult.Status.OK, "Loading started...");
90
+ requestCallback.sendPluginResult(r);
91
+
92
+ cordova.getThreadPool().execute(() -> {
93
+ AdRequest request = new AdRequest.Builder(adUnitId).build();
94
+
95
+ InterstitialAd.load(
96
+ request,
97
+ new AdLoadCallback<InterstitialAd>() {
98
+ @Override
99
+ public void onAdLoaded(@NonNull InterstitialAd ad) {
100
+ isLoading = false;
101
+ interstitialAd = ad;
102
+
103
+ fireEvent("on.interstitial.loaded", null);
104
+
105
+ if (isAutoShow) {
106
+ showInterstitialAd();
107
+ }
108
+ }
109
+
110
+ @Override
111
+ public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
112
+ isLoading = false;
113
+ interstitialAd = null;
114
+
115
+ try {
116
+ JSONObject errData = new JSONObject();
117
+ errData.put("code", loadAdError.getCode());
118
+ errData.put("message", loadAdError.getMessage());
119
+ fireEvent("on.interstitial.failed.load", errData);
120
+ } catch (JSONException e) {
121
+ e.printStackTrace();
122
+ }
123
+ }
124
+ }
125
+ );
126
+ });
127
+ }
128
+
129
+ public void showInterstitial(CallbackContext callbackContext) {
130
+ cordova.getActivity().runOnUiThread(() -> {
131
+ if (interstitialAd != null) {
132
+ showInterstitialAd();
133
+ callbackContext.success();
134
+ } else {
135
+ callbackContext.error("Ad not ready");
136
+ }
137
+ });
138
+ }
139
+
140
+ private void showInterstitialAd() {
141
+ if (interstitialAd == null) return;
142
+
143
+ Activity activity = cordova.getActivity();
144
+
145
+ activity.runOnUiThread(() -> {
146
+ interstitialAd.setAdEventCallback(new InterstitialAdEventCallback() {
147
+
148
+ @Override
149
+ public void onAdPaid(@NonNull AdValue adValue) {
150
+ try {
151
+ JSONObject data = new JSONObject();
152
+ data.put("value", adValue.getValueMicros());
153
+ data.put("currency", adValue.getCurrencyCode());
154
+ data.put("precision", adValue.getPrecisionType());
155
+
156
+ fireEvent("on.interstitial.revenue", data);
157
+ } catch (JSONException e) {
158
+
159
+ }
160
+ }
161
+
162
+ @Override
163
+ public void onAdShowedFullScreenContent() {
164
+
165
+ fireEvent("on.interstitial.shown", null);
166
+ }
167
+
168
+ @Override
169
+ public void onAdDismissedFullScreenContent() {
170
+
171
+ interstitialAd = null;
172
+ isLoading = false;
173
+ fireEvent("on.interstitial.dismissed", null);
174
+ }
175
+
176
+ @Override
177
+ public void onAdFailedToShowFullScreenContent(@NonNull FullScreenContentError error) {
178
+
179
+ interstitialAd = null;
180
+ try {
181
+ JSONObject errData = new JSONObject();
182
+ errData.put("message", error.getMessage());
183
+ fireEvent("on.interstitial.failed.show", errData);
184
+ } catch (JSONException e) {}
185
+ }
186
+
187
+ @Override
188
+ public void onAdImpression() {
189
+ fireEvent("on.interstitial.impression", null);
190
+ }
191
+
192
+ @Override
193
+ public void onAdClicked() {
194
+ fireEvent("on.interstitial.clicked", null);
195
+ }
196
+ });
197
+
198
+ interstitialAd.show(activity);
199
+ });
200
+ }
201
+
202
+ private void fireEvent(String eventName, JSONObject data) {
203
+ cordova.getActivity().runOnUiThread(() -> {
204
+ StringBuilder js = new StringBuilder();
205
+ js.append("javascript:cordova.fireDocumentEvent('");
206
+ js.append(eventName);
207
+ js.append("'");
208
+
209
+ if (data != null) {
210
+ js.append(", ");
211
+ js.append(data.toString());
212
+ }
213
+
214
+ js.append(");");
215
+
216
+ String jsCommand = js.toString();
217
+
218
+ if (webView != null) {
219
+ webView.loadUrl(jsCommand);
220
+ }
221
+ });
222
+ }
223
+ }