-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathAdMobPlugin.java
executable file
·366 lines (313 loc) · 11.1 KB
/
AdMobPlugin.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
//Copyright (c) 2014 Sang Ki Kwon (Cranberrygame)
//Email: [email protected]
//Homepage: http://cranberrygame.github.io
//License: MIT (http://opensource.org/licenses/MIT)
package com.cranberrygame.cordova.plugin.ad.admob;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import android.app.Activity;
import android.util.Log;
//
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.os.Build;
import android.provider.Settings;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.os.Handler;
//
import java.util.*;//Random
interface Plugin {
public CordovaWebView getWebView();
public CordovaInterface getCordova();
public CallbackContext getCallbackContextKeepCallback();
}
interface PluginDelegate {
public void _setLicenseKey(String email, String licenseKey);
public void _setUp(String bannerAdUnit, String interstitialAdUnit, boolean isOverlap, boolean isTest);
public void _preloadBannerAd();
public void _showBannerAd(String position, String size);
public void _reloadBannerAd();
public void _hideBannerAd();
public void _preloadInterstitialAd();
public void _showInterstitialAd();
public void onPause(boolean multitasking);
public void onResume(boolean multitasking);
public void onDestroy();
}
public class AdMobPlugin extends CordovaPlugin implements PluginDelegate, Plugin {
protected static final String LOG_TAG = "AdMobPlugin";
protected CallbackContext callbackContextKeepCallback;
//
protected PluginDelegate pluginDelegate;
//
public String email;
public String licenseKey;
public boolean validLicenseKey;
protected String TEST_BANNER_AD_UNIT = "ca-app-pub-4906074177432504/6997786077";
protected String TEST_FULL_SCREEN_AD_UNIT = "ca-app-pub-4906074177432504/8474519270";
@Override
public void pluginInitialize() {
super.pluginInitialize();
//
}
//@Override
//public void onCreate(Bundle savedInstanceState) {//build error
// super.onCreate(savedInstanceState);
// //
//}
//@Override
//public void onStart() {//build error
// super.onStart();
// //
//}
@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
pluginDelegate.onPause(multitasking);
}
@Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
pluginDelegate.onResume(multitasking);
}
//@Override
//public void onStop() {//build error
// super.onStop();
// //
//}
@Override
public void onDestroy() {
super.onDestroy();
pluginDelegate.onDestroy();
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("setLicenseKey")) {
setLicenseKey(action, args, callbackContext);
return true;
}
else if (action.equals("setUp")) {
setUp(action, args, callbackContext);
return true;
}
else if (action.equals("preloadBannerAd")) {
preloadBannerAd(action, args, callbackContext);
return true;
}
else if (action.equals("showBannerAd")) {
showBannerAd(action, args, callbackContext);
return true;
}
else if (action.equals("reloadBannerAd")) {
reloadBannerAd(action, args, callbackContext);
return true;
}
else if (action.equals("hideBannerAd")) {
hideBannerAd(action, args, callbackContext);
return true;
}
else if (action.equals("preloadInterstitialAd")) {
preloadInterstitialAd(action, args, callbackContext);
return true;
}
else if (action.equals("showInterstitialAd")) {
showInterstitialAd(action, args, callbackContext);
return true;
}
return false; // Returning false results in a "MethodNotFound" error.
}
private void setLicenseKey(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
final String email = args.getString(0);
final String licenseKey = args.getString(1);
Log.d(LOG_TAG, String.format("%s", email));
Log.d(LOG_TAG, String.format("%s", licenseKey));
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
_setLicenseKey(email, licenseKey);
}
});
}
private void setUp(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
//Activity activity=cordova.getActivity();
//webView
//args.length()
//args.getString(0)
//args.getString(1)
//args.getInt(0)
//args.getInt(1)
//args.getBoolean(0)
//args.getBoolean(1)
//JSONObject json = args.optJSONObject(0);
//json.optString("bannerAdUnit")
//json.optString("interstitialAdUnit")
//JSONObject inJson = json.optJSONObject("inJson");
//final String bannerAdUnit = args.getString(0);
//final String interstitialAdUnit = args.getString(1);
//final boolean isOverlap = args.getBoolean(2);
//final boolean isTest = args.getBoolean(3);
//final String[] zoneIds = new String[args.getJSONArray(4).length()];
//for (int i = 0; i < args.getJSONArray(4).length(); i++) {
// zoneIds[i] = args.getJSONArray(4).getString(i);
//}
//Log.d(LOG_TAG, String.format("%s", bannerAdUnit));
//Log.d(LOG_TAG, String.format("%s", interstitialAdUnit));
//Log.d(LOG_TAG, String.format("%b", isOverlap));
//Log.d(LOG_TAG, String.format("%b", isTest));
final String bannerAdUnit = args.getString(0);
final String interstitialAdUnit = args.getString(1);
final boolean isOverlap = args.getBoolean(2);
final boolean isTest = args.getBoolean(3);
Log.d(LOG_TAG, String.format("%s", bannerAdUnit));
Log.d(LOG_TAG, String.format("%s", interstitialAdUnit));
Log.d(LOG_TAG, String.format("%b", isOverlap));
Log.d(LOG_TAG, String.format("%b", isTest));
callbackContextKeepCallback = callbackContext;
if(isOverlap)
pluginDelegate = new AdMobOverlap(this);
else
pluginDelegate = new AdMobSplit(this);
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
_setUp(bannerAdUnit, interstitialAdUnit, isOverlap, isTest);
}
});
}
private void preloadBannerAd(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
cordova.getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
_preloadBannerAd();
}
});
}
private void showBannerAd(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
final String position = args.getString(0);
final String size = args.getString(1);
Log.d(LOG_TAG, String.format("%s", position));
Log.d(LOG_TAG, String.format("%s", size));
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
_showBannerAd(position, size);
}
});
}
private void reloadBannerAd(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
cordova.getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
_reloadBannerAd();
}
});
}
private void hideBannerAd(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
cordova.getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
_hideBannerAd();
}
});
}
private void preloadInterstitialAd(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
cordova.getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
_preloadInterstitialAd();
}
});
}
private void showInterstitialAd(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
cordova.getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
_showInterstitialAd();
}
});
}
//cranberrygame start: Plugin
public CordovaWebView getWebView() {
return webView;
}
public CordovaInterface getCordova() {
return cordova;
}
public CallbackContext getCallbackContextKeepCallback() {
return callbackContextKeepCallback;
}
//cranberrygame end: Plugin
//cranberrygame start: AdMobPluginDelegate
public void _setLicenseKey(String email, String licenseKey) {
//pluginDelegate._setLicenseKey(email, licenseKey);
this.email = email;
this.licenseKey = licenseKey;
//
String str1 = Util.md5("cordova-plugin-: " + email);
String str2 = Util.md5("cordova-plugin-ad-admob: " + email);
String str3 = Util.md5("com.cranberrygame.cordova.plugin.: " + email);
String str4 = Util.md5("com.cranberrygame.cordova.plugin.ad.admob: " + email);
if(licenseKey != null && (licenseKey.equalsIgnoreCase(str1) || licenseKey.equalsIgnoreCase(str2) || licenseKey.equalsIgnoreCase(str3) || licenseKey.equalsIgnoreCase(str4))) {
this.validLicenseKey = true;
//
String[] excludedLicenseKeys = {"xxx"};
for (int i = 0 ; i < excludedLicenseKeys.length ; i++) {
if (excludedLicenseKeys[i].equals(licenseKey)) {
this.validLicenseKey = false;
break;
}
}
if (this.validLicenseKey)
Log.d(LOG_TAG, String.format("%s", "valid licenseKey"));
else
Log.d(LOG_TAG, String.format("%s", "invalid licenseKey"));
}
else {
Log.d(LOG_TAG, String.format("%s", "invalid licenseKey"));
this.validLicenseKey = false;
}
//if (!this.validLicenseKey)
// Util.alert(plugin.getCordova().getActivity(),"Cordova Admob: invalid email / license key. You can get free license key from https://play.google.com/store/apps/details?id=com.cranberrygame.pluginsforcordova");
}
public void _setUp(String bannerAdUnit, String interstitialAdUnit, boolean isOverlap, boolean isTest) {
if (!validLicenseKey) {
if (new Random().nextInt(100) <= 1) {//0~99
bannerAdUnit = TEST_BANNER_AD_UNIT;
interstitialAdUnit = TEST_FULL_SCREEN_AD_UNIT;
}
}
pluginDelegate._setUp(bannerAdUnit, interstitialAdUnit, isOverlap, isTest);
}
public void _preloadBannerAd() {
pluginDelegate._preloadBannerAd();
}
public void _showBannerAd(String position, String size) {
pluginDelegate._showBannerAd(position, size);
}
public void _reloadBannerAd() {
pluginDelegate._reloadBannerAd();
}
public void _hideBannerAd() {
pluginDelegate._hideBannerAd();
}
public void _preloadInterstitialAd() {
pluginDelegate._preloadInterstitialAd();
}
public void _showInterstitialAd() {
pluginDelegate._showInterstitialAd();
}
//cranberrygame end: AdMobPluginDelegate
}