1
1
package org .apache .cordova ;
2
2
3
- import org .apache .cordova .CordovaPlugin ;
4
- import org .apache .cordova .CallbackContext ;
5
3
import android .util .Log ;
6
4
import android .view .View ;
7
5
import android .view .ViewGroup ;
8
6
import android .widget .RelativeLayout ;
9
7
10
- import androidx .appcompat .app .AppCompatActivity ;
11
-
12
8
import org .json .JSONArray ;
13
9
import org .json .JSONException ;
14
10
import org .json .JSONObject ;
15
11
12
+ import com .google .android .gms .ads .AdListener ;
16
13
import com .google .android .gms .ads .AdRequest ;
17
14
import com .google .android .gms .ads .AdSize ;
18
15
import com .google .android .gms .ads .AdView ;
19
- import com .google .android .gms .ads .MobileAds ;
20
- import com .google .android .gms .ads .initialization .InitializationStatus ;
21
- import com .google .android .gms .ads .initialization .OnInitializationCompleteListener ;
22
-
23
- import io .cordova .hellocordova .MainActivity ;
16
+ import com .google .android .gms .ads .LoadAdError ;
24
17
25
18
class AdmobBanner extends CordovaPlugin {
26
19
private AdView mAdView ;
27
- private AdView adView ;
28
- private String bannerAdUnitId ;
29
- private static final String TAG = "AdmobInit " ;
20
+ private AdView adView = null ;
21
+ private String bannerAdUnitId = "ca-app-pub-3940256099942544/6300978111" ;
22
+ private static final String TAG = "AdmobBanner " ;
30
23
private RelativeLayout adViewLayout = null ;
24
+
25
+
31
26
@ Override
32
27
public boolean execute (String action , JSONArray args , CallbackContext callbackContext ) throws JSONException {
33
- if (args .length () < 1 ) {
34
- Log .d (TAG ,"No options" );
35
- callbackContext .error ("createAd failed" );
36
- return false ;
37
- }
38
- if (action .equals ("createAd" )) {
28
+
29
+ if (action .equals ("createBannerView" )) {
30
+ if (args .length () < 1 ) {
31
+ Log .d (TAG ,"No options" );
32
+ callbackContext .error ("createAd failed" );
33
+ return false ;
34
+ }
39
35
JSONObject option = args .getJSONObject (0 );
40
- this .createAd (option , callbackContext );
36
+ this .createBannerView (option , callbackContext );
37
+ return true ;
38
+ }
39
+ if (action .equals ("showAd" )) {
40
+ this .showAd (callbackContext );
41
41
return true ;
42
42
}
43
43
return false ;
44
44
}
45
- public void createAd (JSONObject option ,final CallbackContext callbackContext ) {
45
+ public void createBannerView (JSONObject option ,final CallbackContext callbackContext ) {
46
46
cordova .getActivity ().runOnUiThread (new Runnable () {
47
47
@ Override
48
48
public void run () {
49
+ float x = 0 ;
50
+ float y = 0 ;
51
+ int type = 0 ;
49
52
if (option .has ("adsCode" )) {
50
53
bannerAdUnitId = option .optString ("adsCode" );
51
54
}
52
- AdView adView = new AdView (cordova .getContext ());
53
- adView .setAdSize (AdSize .BANNER );
55
+ if (option .has ("x" )) {
56
+ x = option .optInt ("x" );
57
+ }
58
+ if (option .has ("y" )) {
59
+ y = option .optInt ("y" );
60
+ }
61
+ if (option .has ("size" )) {
62
+ type = option .optInt ("size" );
63
+ }
64
+ adView = new AdView (cordova .getContext ());
54
65
55
66
adView .setAdUnitId (bannerAdUnitId );
56
- adView .setY (0 );
67
+ adView .setY (y );
68
+ adView .setX (x );
69
+ adView .setAdSize (setSize (type ));
57
70
AdRequest adRequest = new AdRequest .Builder ().build ();
58
71
adView .loadAd (adRequest );
59
- adViewLayout = new RelativeLayout (cordova .getActivity ());
60
- RelativeLayout .LayoutParams params = new RelativeLayout .LayoutParams (RelativeLayout .LayoutParams .MATCH_PARENT ,
61
- RelativeLayout .LayoutParams .MATCH_PARENT );
62
- ((ViewGroup ) webView ).addView (adViewLayout , params );
63
- adViewLayout .addView (adView ,params );
64
- adViewLayout .bringToFront ();
72
+
73
+ adView .setAdListener (new AdListener () {
74
+ @ Override
75
+ public void onAdClicked () {
76
+ // Code to be executed when the user clicks on an ad.
77
+ fireAdEvent ("admob.banner.events.CLICK" );
78
+ }
79
+
80
+ @ Override
81
+ public void onAdClosed () {
82
+ // Code to be executed when the user is about to return
83
+ // to the app after tapping on an ad.
84
+ fireAdEvent ("admob.banner.events.CLOSE" );
85
+ }
86
+
87
+ @ Override
88
+ public void onAdFailedToLoad (LoadAdError adError ) {
89
+ // Code to be executed when an ad request fails.
90
+ fireAdEvent ("admob.banner.events.LOAD_FAIL" );
91
+ }
92
+
93
+ @ Override
94
+ public void onAdImpression () {
95
+ // Code to be executed when an impression is recorded
96
+ // for an ad.
97
+ fireAdEvent ("admob.banner.events.IMPRESSION" );
98
+ }
99
+
100
+ @ Override
101
+ public void onAdLoaded () {
102
+ // Code to be executed when an ad finishes loading.
103
+ fireAdEvent ("admob.banner.events.LOAD" );
104
+ }
105
+
106
+ @ Override
107
+ public void onAdOpened () {
108
+ // Code to be executed when an ad opens an overlay that
109
+ // covers the screen.
110
+ fireAdEvent ("admob.banner.events.OPEN" );
111
+ }
112
+ });
65
113
callbackContext .success ();
66
114
}
67
115
});
68
116
}
117
+ public PluginResult showAd (final CallbackContext callbackContext ){
118
+ if (adView == null ) {
119
+ callbackContext .error ("No init" );
120
+ return null ;
121
+ }
122
+ cordova .getActivity ().runOnUiThread (new Runnable () {
123
+ @ Override
124
+ public void run () {
125
+ adViewLayout = new RelativeLayout (cordova .getActivity ());
126
+
127
+ RelativeLayout .LayoutParams params = new RelativeLayout .LayoutParams (RelativeLayout .LayoutParams .MATCH_PARENT ,
128
+ RelativeLayout .LayoutParams .MATCH_PARENT );
129
+ try {
130
+ ((ViewGroup ) (((View ) webView .getClass ().getMethod ("getView" ).invoke (webView )).getParent ())).addView (adViewLayout , params );
131
+ } catch (Exception e ) {
132
+ ((ViewGroup ) webView ).addView (adViewLayout , params );
133
+ }
134
+
135
+ adViewLayout .addView (adView ,params );
136
+ adViewLayout .bringToFront ();
137
+ callbackContext .success ();
138
+ }
139
+ }
140
+ );
141
+
142
+ return null ;
143
+ }
144
+ public AdSize setSize (int type ){
145
+ AdSize size = AdSize .BANNER ;
146
+ switch (type ) {
147
+ default :
148
+ size = AdSize .BANNER ;
149
+ break ;
150
+ case 0 :
151
+ size = AdSize .BANNER ;
152
+ break ;
153
+ case 1 :
154
+ size = AdSize .LARGE_BANNER ;
155
+ break ;
156
+ case 2 :
157
+ size = AdSize .MEDIUM_RECTANGLE ;
158
+ break ;
159
+ case 3 :
160
+ size = AdSize .FULL_BANNER ;
161
+ break ;
162
+ case 4 :
163
+ size = AdSize .LEADERBOARD ;
164
+ break ;
165
+ }
166
+ return size ;
167
+ }
168
+ public void fireAdEvent (String eventName ) {
169
+ String js = new CordovaEventBuilder (eventName ).build ();
170
+ loadJS (js );
171
+ }
172
+
173
+ public void fireAdEvent (String eventName , JSONObject data ) {
174
+ String js = new CordovaEventBuilder (eventName ).withData (data ).build ();
175
+ loadJS (js );
176
+ }
177
+
178
+ private void loadJS (String js ) {
179
+ this .webView .loadUrl (js );
180
+ }
69
181
}
0 commit comments