1919
2020import android .app .Activity ;
2121import android .view .ViewGroup ;
22+
2223import androidx .annotation .NonNull ;
24+
2325import com .facebook .react .bridge .Arguments ;
2426import com .facebook .react .bridge .ReactContext ;
2527import com .facebook .react .bridge .ReadableArray ;
4648import com .google .android .gms .ads .OnPaidEventListener ;
4749import com .google .android .gms .ads .admanager .AdManagerAdView ;
4850import com .google .android .gms .ads .admanager .AppEventListener ;
51+
4952import io .invertase .googlemobileads .common .ReactNativeAdView ;
5053import io .invertase .googlemobileads .common .SharedUtils ;
54+
5155import java .util .ArrayList ;
5256import java .util .List ;
5357import java .util .Map ;
58+
5459import javax .annotation .Nonnull ;
5560import javax .annotation .Nullable ;
61+
5662import org .json .JSONException ;
5763import org .json .JSONObject ;
5864
5965public class ReactNativeGoogleMobileAdsBannerAdViewManager
60- extends SimpleViewManager <ReactNativeAdView > implements RNGoogleMobileAdsBannerViewManagerInterface <ReactNativeAdView > {
66+ extends SimpleViewManager <ReactNativeAdView > implements RNGoogleMobileAdsBannerViewManagerInterface <ReactNativeAdView > {
6167 private static final String REACT_CLASS = "RNGoogleMobileAdsBannerView" ;
6268 private final String EVENT_AD_LOADED = "onAdLoaded" ;
6369 private final String EVENT_AD_IMPRESSION = "onAdImpression" ;
@@ -68,7 +74,8 @@ public class ReactNativeGoogleMobileAdsBannerAdViewManager
6874 private final String EVENT_PAID = "onPaid" ;
6975 private final String EVENT_SIZE_CHANGE = "onSizeChange" ;
7076 private final String EVENT_APP_EVENT = "onAppEvent" ;
71- private final ViewManagerDelegate <ReactNativeAdView > delegate = new RNGoogleMobileAdsBannerViewManagerDelegate <>(this );
77+ private final ViewManagerDelegate <ReactNativeAdView > delegate = new RNGoogleMobileAdsBannerViewManagerDelegate <>(this );
78+
7279 @ Nonnull
7380 @ Override
7481 public String getName () {
@@ -140,7 +147,7 @@ public void setSizeConfig(ReactNativeAdView reactViewGroup, ReadableMap sizeConf
140147 if (sizesArray .getType (i ) == ReadableType .String ) {
141148 String sizeString = sizesArray .getString (i );
142149 AdSize adSize =
143- ReactNativeGoogleMobileAdsCommon .getAdSize (sizeString , reactViewGroup );
150+ ReactNativeGoogleMobileAdsCommon .getAdSize (sizeString , reactViewGroup );
144151 sizeList .add (adSize );
145152 }
146153 }
@@ -180,7 +187,7 @@ public void recordManualImpression(ReactNativeAdView view) {
180187 public void load (ReactNativeAdView view ) {
181188 BaseAdView adView = getAdView (view );
182189 AdRequest request = view .getRequest ();
183- if (adView !=null ) {
190+ if (adView != null ) {
184191 adView .loadAd (request );
185192 }
186193 }
@@ -226,95 +233,95 @@ private BaseAdView initAdView(ReactNativeAdView reactViewGroup) {
226233 if (currentActivity == null ) return null ;
227234
228235 BaseAdView adView =
229- ReactNativeGoogleMobileAdsCommon .isAdManagerUnit (reactViewGroup .getUnitId ())
230- ? new AdManagerAdView (currentActivity )
231- : new AdView (currentActivity );
236+ ReactNativeGoogleMobileAdsCommon .isAdManagerUnit (reactViewGroup .getUnitId ())
237+ ? new AdManagerAdView (currentActivity )
238+ : new AdView (currentActivity );
232239
233240 adView .setDescendantFocusability (ViewGroup .FOCUS_BLOCK_DESCENDANTS );
234241 adView .setOnPaidEventListener (
235- new OnPaidEventListener () {
236- @ Override
237- public void onPaidEvent (AdValue adValue ) {
238- WritableMap payload = Arguments .createMap ();
239- payload .putDouble ("value" , 1e-6 * adValue .getValueMicros ());
240- payload .putDouble ("precision" , adValue .getPrecisionType ());
241- payload .putString ("currency" , adValue .getCurrencyCode ());
242- sendEvent (reactViewGroup , EVENT_PAID , payload );
243- }
244- });
242+ new OnPaidEventListener () {
243+ @ Override
244+ public void onPaidEvent (AdValue adValue ) {
245+ WritableMap payload = Arguments .createMap ();
246+ payload .putDouble ("value" , 1e-6 * adValue .getValueMicros ());
247+ payload .putDouble ("precision" , adValue .getPrecisionType ());
248+ payload .putString ("currency" , adValue .getCurrencyCode ());
249+ sendEvent (reactViewGroup , EVENT_PAID , payload );
250+ }
251+ });
245252 adView .setAdListener (
246- new AdListener () {
247- @ Override
248- public void onAdLoaded () {
249- AdSize adSize = adView .getAdSize ();
250- int width , height ;
251- if (reactViewGroup .getIsFluid ()) {
252- width = reactViewGroup .getWidth ();
253- height = reactViewGroup .getHeight ();
254-
255- adView .addOnLayoutChangeListener (
256- (v , left , top , right , bottom , oldLeft , oldTop , oldRight , oldBottom ) -> {
257- WritableMap payload = Arguments .createMap ();
258- payload .putDouble ("width" , PixelUtil .toDIPFromPixel (right - left ));
259- payload .putDouble ("height" , PixelUtil .toDIPFromPixel (bottom - top ));
260- sendEvent (reactViewGroup , EVENT_SIZE_CHANGE , payload );
261- });
262- } else {
263- int left = adView .getLeft ();
264- int top = adView .getTop ();
265- width = adSize .getWidthInPixels (reactViewGroup .getContext ());
266- height = adSize .getHeightInPixels (reactViewGroup .getContext ());
267-
268- adView .measure (width , height );
269- adView .layout (left , top , left + width , top + height );
270- }
253+ new AdListener () {
254+ @ Override
255+ public void onAdLoaded () {
256+ AdSize adSize = adView .getAdSize ();
257+ int width , height ;
258+ if (reactViewGroup .getIsFluid ()) {
259+ width = reactViewGroup .getWidth ();
260+ height = reactViewGroup .getHeight ();
261+
262+ adView .addOnLayoutChangeListener (
263+ (v , left , top , right , bottom , oldLeft , oldTop , oldRight , oldBottom ) -> {
264+ WritableMap payload = Arguments .createMap ();
265+ payload .putDouble ("width" , PixelUtil .toDIPFromPixel (right - left ));
266+ payload .putDouble ("height" , PixelUtil .toDIPFromPixel (bottom - top ));
267+ sendEvent (reactViewGroup , EVENT_SIZE_CHANGE , payload );
268+ });
269+ } else {
270+ int left = adView .getLeft ();
271+ int top = adView .getTop ();
272+ width = adSize .getWidthInPixels (reactViewGroup .getContext ());
273+ height = adSize .getHeightInPixels (reactViewGroup .getContext ());
274+
275+ adView .measure (width , height );
276+ adView .layout (left , top , left + width , top + height );
277+ }
271278
272- WritableMap payload = Arguments .createMap ();
273- payload .putDouble ("width" , PixelUtil .toDIPFromPixel (width ));
274- payload .putDouble ("height" , PixelUtil .toDIPFromPixel (height ));
279+ WritableMap payload = Arguments .createMap ();
280+ payload .putDouble ("width" , PixelUtil .toDIPFromPixel (width ));
281+ payload .putDouble ("height" , PixelUtil .toDIPFromPixel (height ));
275282
276- sendEvent (reactViewGroup , EVENT_AD_LOADED , payload );
277- }
283+ sendEvent (reactViewGroup , EVENT_AD_LOADED , payload );
284+ }
278285
279- @ Override
280- public void onAdFailedToLoad (@ NonNull LoadAdError loadAdError ) {
281- int errorCode = loadAdError .getCode ();
282- WritableMap payload = ReactNativeGoogleMobileAdsCommon .errorCodeToMap (errorCode );
283- sendEvent (reactViewGroup , EVENT_AD_FAILED_TO_LOAD , payload );
284- }
286+ @ Override
287+ public void onAdFailedToLoad (@ NonNull LoadAdError loadAdError ) {
288+ int errorCode = loadAdError .getCode ();
289+ WritableMap payload = ReactNativeGoogleMobileAdsCommon .errorCodeToMap (errorCode );
290+ sendEvent (reactViewGroup , EVENT_AD_FAILED_TO_LOAD , payload );
291+ }
285292
286- @ Override
287- public void onAdOpened () {
288- sendEvent (reactViewGroup , EVENT_AD_OPENED , null );
289- }
293+ @ Override
294+ public void onAdOpened () {
295+ sendEvent (reactViewGroup , EVENT_AD_OPENED , null );
296+ }
290297
291- @ Override
292- public void onAdClosed () {
293- sendEvent (reactViewGroup , EVENT_AD_CLOSED , null );
294- }
298+ @ Override
299+ public void onAdClosed () {
300+ sendEvent (reactViewGroup , EVENT_AD_CLOSED , null );
301+ }
295302
296- @ Override
297- public void onAdImpression () {
298- sendEvent (reactViewGroup , EVENT_AD_IMPRESSION , null );
299- }
303+ @ Override
304+ public void onAdImpression () {
305+ sendEvent (reactViewGroup , EVENT_AD_IMPRESSION , null );
306+ }
300307
301- @ Override
302- public void onAdClicked () {
303- sendEvent (reactViewGroup , EVENT_AD_CLICKED , null );
304- }
305- });
308+ @ Override
309+ public void onAdClicked () {
310+ sendEvent (reactViewGroup , EVENT_AD_CLICKED , null );
311+ }
312+ });
306313 if (adView instanceof AdManagerAdView ) {
307314 ((AdManagerAdView ) adView )
308- .setAppEventListener (
309- new AppEventListener () {
310- @ Override
311- public void onAppEvent (@ NonNull String name , @ Nullable String data ) {
312- WritableMap payload = Arguments .createMap ();
313- payload .putString ("name" , name );
314- payload .putString ("data" , data );
315- sendEvent (reactViewGroup , EVENT_APP_EVENT , payload );
316- }
317- });
315+ .setAppEventListener (
316+ new AppEventListener () {
317+ @ Override
318+ public void onAppEvent (@ NonNull String name , @ Nullable String data ) {
319+ WritableMap payload = Arguments .createMap ();
320+ payload .putString ("name" , name );
321+ payload .putString ("data" , data );
322+ sendEvent (reactViewGroup , EVENT_APP_EVENT , payload );
323+ }
324+ });
318325 }
319326 reactViewGroup .addView (adView );
320327 return adView ;
@@ -366,7 +373,7 @@ private void sendEvent(ReactNativeAdView reactViewGroup, String type, WritableMa
366373
367374 ThemedReactContext themedReactContext = ((ThemedReactContext ) reactViewGroup .getContext ());
368375 EventDispatcher eventDispatcher =
369- UIManagerHelper .getEventDispatcherForReactTag (themedReactContext , reactViewGroup .getId ());
376+ UIManagerHelper .getEventDispatcherForReactTag (themedReactContext , reactViewGroup .getId ());
370377 if (eventDispatcher != null ) {
371378 eventDispatcher .dispatchEvent (new OnNativeEvent (reactViewGroup .getId (), event ));
372379 }
0 commit comments