Skip to content

Commit

Permalink
feat(admob, android): add adaptive banner support (#4840)
Browse files Browse the repository at this point in the history
* added support for adaptive banner

Co-authored-by: Mike Hardy <github@mikehardy.net>
  • Loading branch information
melkayam92 and mikehardy authored Jan 29, 2021
1 parent 664962c commit 51edf96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void setRequest(ReactViewGroup reactViewGroup, ReadableMap value) {

@ReactProp(name = "size")
public void setSize(ReactViewGroup reactViewGroup, String value) {
size = ReactNativeFirebaseAdMobCommon.stringToAdSize(value);
size = ReactNativeFirebaseAdMobCommon.getAdSize(value, reactViewGroup);

int width;
int height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@

import android.location.Location;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.views.view.ReactViewGroup;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
Expand All @@ -39,6 +44,29 @@

public class ReactNativeFirebaseAdMobCommon {

static AdSize getAdSizeForAdaptiveBanner(ReactViewGroup reactViewGroup) {

try {
Display display = Objects.requireNonNull(((ReactContext) reactViewGroup.getContext()).getCurrentActivity()).getWindowManager().getDefaultDisplay();

DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
int adWidth = (int) (outMetrics.widthPixels / outMetrics.density);

return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(reactViewGroup.getContext(), adWidth);
} catch (Exception e) {
return AdSize.BANNER;
}
}

static AdSize getAdSize(String preDefinedAdSize, ReactViewGroup reactViewGroup) {
if ("ADAPTIVE_BANNER".equals(preDefinedAdSize)) {
return ReactNativeFirebaseAdMobCommon.getAdSizeForAdaptiveBanner(reactViewGroup);
} else {
return ReactNativeFirebaseAdMobCommon.stringToAdSize(preDefinedAdSize);
}
}

static AdSize stringToAdSize(String value) {
Pattern pattern = Pattern.compile("([0-9]+)x([0-9]+)");
Matcher matcher = pattern.matcher(value);
Expand Down
2 changes: 0 additions & 2 deletions packages/admob/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ export namespace FirebaseAdMobTypes {

/**
* A (next generation) dynamically sized banner that is full-width and auto-height.
*
* iOS only
*/
ADAPTIVE_BANNER: 'ADAPTIVE_BANNER';

Expand Down

1 comment on commit 51edf96

@vercel
Copy link

@vercel vercel bot commented on 51edf96 Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.