Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

(android) added bannerHeight to banner LOAD event (#361) #373

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/android/banner/BannerExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class BannerExecutor extends AbstractExecutor {
* The adView to display to the user.
*/
private AdView adView;

public AdView getAdView() {
return adView;
}
/**
* if want banner view overlap webview, we will need this layout
*/
Expand All @@ -35,8 +39,11 @@ public class BannerExecutor extends AbstractExecutor {

boolean bannerVisible = false;

public AdMob plugin;

public BannerExecutor(AdMob plugin) {
super(plugin);
this.plugin = plugin;
}

@Override
Expand Down
32 changes: 30 additions & 2 deletions src/android/banner/BannerListener.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package name.ratson.cordova.admob.banner;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewTreeObserver;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdView;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -43,14 +46,39 @@ public void onAdLeftApplication() {
executor.fireAdEvent("onLeaveToAd", data);
}

private int pxToDp(int px) {
DisplayMetrics displayMetrics = executor.plugin.cordova.getActivity().getApplicationContext().getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}

@Override
public void onAdLoaded() {
Log.w("AdMob", "BannerAdLoaded");
if (executor.shouldAutoShow() && !executor.bannerVisible) {
executor.showAd(true, null);
}
executor.fireAdEvent("admob.banner.events.LOAD");
executor.fireAdEvent("onReceiveAd");

AdView view = executor.getAdView();
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
JSONObject data = new JSONObject();
try {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int heightPx = view.getHeight(); //height is ready
int heightDp = pxToDp(heightPx);

data.put("bannerHeight", heightDp);
} catch (JSONException e) {
e.printStackTrace();
Copy link

Choose a reason for hiding this comment

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

It would be nice if this propagates to the webview console too.

}

executor.fireAdEvent("admob.banner.events.LOAD", data);
executor.fireAdEvent("onReceiveAd");
}
});

}

@Override
Expand Down