fb-audience

Created Diff never expires
10 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
268 lines
12 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
271 lines
package com.ammarahmed.rnadmob.nativeads;
package com.ammarahmed.rnadmob.nativeads;
import android.content.Context;
import android.content.Context;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.VideoOptions;
import com.google.android.gms.ads.VideoOptions;
import com.google.android.gms.ads.formats.NativeAdOptions;
import com.google.android.gms.ads.formats.NativeAdOptions;
import com.google.android.gms.ads.formats.UnifiedNativeAd;
import com.google.android.gms.ads.formats.UnifiedNativeAd;
import com.google.android.gms.ads.formats.UnifiedNativeAdView;
import com.google.android.gms.ads.formats.UnifiedNativeAdView;
public class RNNativeAdWrapper extends LinearLayout {
public class RNNativeAdWrapper extends LinearLayout {
private final Runnable measureAndLayout = new Runnable() {
private final Runnable measureAndLayout = new Runnable() {
@Override
@Override
public void run() {
public void run() {
measure(
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
layout(getLeft(), getTop(), getRight(), getBottom());
layout(getLeft(), getTop(), getRight(), getBottom());
}
}
};
};
public int adRefreshInterval = 60000;
public int adRefreshInterval = 60000;
Context mContext;
Context mContext;
UnifiedNativeAdView nativeAdView;
UnifiedNativeAdView nativeAdView;
UnifiedNativeAd unifiedNativeAd;
UnifiedNativeAd unifiedNativeAd;
private int adChoicesPlacement = 1;
private int adChoicesPlacement = 1;
private boolean requestNonPersonalizedAdsOnly = false;
private boolean requestNonPersonalizedAdsOnly = false;
AdListener adListener = new AdListener() {
AdListener adListener = new AdListener() {
@Override
@Override
public void onAdFailedToLoad(int i) {
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
super.onAdFailedToLoad(i);
String errorMessage = "Unknown error";
String errorMessage = "Unknown error";
switch (i) {
switch (i) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
errorMessage = "Internal error, an invalid response was received from the ad server.";
errorMessage = "Internal error, an invalid response was received from the ad server.";
break;
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
case AdRequest.ERROR_CODE_INVALID_REQUEST:
errorMessage = "Invalid ad request, possibly an incorrect ad unit ID was given.";
errorMessage = "Invalid ad request, possibly an incorrect ad unit ID was given.";
break;
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
case AdRequest.ERROR_CODE_NETWORK_ERROR:
errorMessage = "The ad request was unsuccessful due to network connectivity.";
errorMessage = "The ad request was unsuccessful due to network connectivity.";
break;
break;
case AdRequest.ERROR_CODE_NO_FILL:
case AdRequest.ERROR_CODE_NO_FILL:
errorMessage = "The ad request was successful, but no ad was returned due to lack of ad inventory.";
errorMessage = "The ad request was successful, but no ad was returned due to lack of ad inventory.";
break;
break;
}
}
WritableMap event = Arguments.createMap();
WritableMap event = Arguments.createMap();
WritableMap error = Arguments.createMap();
WritableMap error = Arguments.createMap();
error.putString("message", errorMessage);
error.putString("message", errorMessage);
event.putMap("error", error);
event.putMap("error", error);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_FAILED_TO_LOAD, event);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_FAILED_TO_LOAD, event);
}
}
@Override
@Override
public void onAdClosed() {
public void onAdClosed() {
super.onAdClosed();
super.onAdClosed();
sendEvent(RNAdMobNativeViewManager.EVENT_AD_CLOSED, null);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_CLOSED, null);
}
}
@Override
@Override
public void onAdOpened() {
public void onAdOpened() {
super.onAdOpened();
super.onAdOpened();
sendEvent(RNAdMobNativeViewManager.EVENT_AD_OPENED, null);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_OPENED, null);
}
}
@Override
@Override
public void onAdClicked() {
public void onAdClicked() {
super.onAdClicked();
super.onAdClicked();
sendEvent(RNAdMobNativeViewManager.EVENT_AD_CLICKED, null);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_CLICKED, null);
}
}
@Override
@Override
public void onAdLoaded() {
public void onAdLoaded() {
super.onAdLoaded();
super.onAdLoaded();
sendEvent(RNAdMobNativeViewManager.EVENT_AD_LOADED, null);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_LOADED, null);
}
}
@Override
@Override
public void onAdImpression() {
public void onAdImpression() {
super.onAdImpression();
super.onAdImpression();
sendEvent(RNAdMobNativeViewManager.EVENT_AD_IMPRESSION, null);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_IMPRESSION, null);
}
}
@Override
@Override
public void onAdLeftApplication() {
public void onAdLeftApplication() {
super.onAdLeftApplication();
super.onAdLeftApplication();
sendEvent(RNAdMobNativeViewManager.EVENT_AD_LEFT_APPLICATION, null);
sendEvent(RNAdMobNativeViewManager.EVENT_AD_LEFT_APPLICATION, null);
}
}
};
};
private int loadWithDelay = 1000;
private int loadWithDelay = 1000;
private String admobAdUnitId = "";
private String admobAdUnitId = "";
private Handler handler;
private Handler handler;
UnifiedNativeAd.OnUnifiedNativeAdLoadedListener onUnifiedNativeAdLoadedListener = new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
UnifiedNativeAd.OnUnifiedNativeAdLoadedListener onUnifiedNativeAdLoadedListener = new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd nativeAd) {
public void onUnifiedNativeAdLoaded(UnifiedNativeAd nativeAd) {
if (nativeAd != null) {
if (nativeAd != null) {
unifiedNativeAd = nativeAd;
unifiedNativeAd = nativeAd;
nativeAdView.setNativeAd(unifiedNativeAd);
nativeAdView.setNativeAd(unifiedNativeAd);
}
}
setNativeAdToJS(nativeAd);
setNativeAdToJS(nativeAd);
}
}
};
};
public RNNativeAdWrapper(Context context) {
public RNNativeAdWrapper(Context context) {
super(context);
super(context);
mContext = context;
mContext = context;
createView(context);
createView(context);
handler = new Handler();
handler = new Handler();
}
}
public void createView(Context context) {
public void createView(Context context) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
LayoutInflater layoutInflater = LayoutInflater.from(context);
View viewRoot = layoutInflater.inflate(R.layout.rn_ad_unified_native_ad, this, true);
View viewRoot = layoutInflater.inflate(R.layout.rn_ad_unified_native_ad, this, true);
nativeAdView = (UnifiedNativeAdView) viewRoot.findViewById(R.id.native_ad_view);
nativeAdView = (UnifiedNativeAdView) viewRoot.findViewById(R.id.native_ad_view);
}
}
public void addMediaView(int id) {
public void addMediaView(int id) {
try {
try {
RNMediaView adMediaView = (RNMediaView) nativeAdView.findViewById(id);
RNMediaView adMediaView = (RNMediaView) nativeAdView.findViewById(id);
if (adMediaView != null) {
if (adMediaView != null) {
nativeAdView.setMediaView(adMediaView);
nativeAdView.setMediaView(adMediaView);
adMediaView.requestLayout();
adMediaView.requestLayout();
}
}
} catch (Exception e) {
} catch (Exception e) {
}
}
}
}
private void setNativeAdToJS(UnifiedNativeAd nativeAd) {
private void setNativeAdToJS(UnifiedNativeAd nativeAd) {
try {
try {
WritableMap args = Arguments.createMap();
WritableMap args = Arguments.createMap();
args.putString("headline", nativeAd.getHeadline());
args.putString("headline", nativeAd.getHeadline());
args.putString("tagline", nativeAd.getBody());
args.putString("tagline", nativeAd.getBody());
args.putString("advertiser", nativeAd.getAdvertiser());
args.putString("advertiser", nativeAd.getAdvertiser());
args.putString("callToAction", nativeAd.getCallToAction());
args.putString("callToAction", nativeAd.getCallToAction());
args.putBoolean("video", nativeAd.getMediaContent().hasVideoContent());
args.putBoolean("video", nativeAd.getMediaContent().hasVideoContent());
args.putString("price", nativeAd.getPrice());
args.putString("price", nativeAd.getPrice());
/* if (nativeAd.getStore() != null) {
if (nativeAd.getStore() != null) {
args.putString("store", nativeAd.getStore());
args.putString("store", nativeAd.getStore());
}
}
if (nativeAd.getStarRating() != null) {
if (nativeAd.getStarRating() != null) {
args.putInt("rating", nativeAd.getStarRating().intValue());
args.putInt("rating", nativeAd.getStarRating().intValue());
}*/
}
float aspectRatio = 1.0f;
float aspectRatio = 1.0f;
/* if (nativeAd.getMediaContent() != null) {
if (nativeAd.getMediaContent() != null) {
try {
try {
aspectRatio = nativeAd.getMediaContent().getAspectRatio();
aspectRatio = nativeAd.getMediaContent().getAspectRatio();
if (aspectRatio > 0) {
if (aspectRatio > 0) {
args.putString("aspectRatio", String.valueOf(aspectRatio));
args.putString("aspectRatio", String.valueOf(aspectRatio));
} else {
} else {
args.putString("aspectRatio", String.valueOf(1));
args.putString("aspectRatio", String.valueOf(aspectRatio));
}
}
} catch (Exception e) {
} catch (Exception e) {
args.putString("aspectRatio", String.valueOf(1));
aspectRatio = 1.0f;
args.putString("aspectRatio", String.valueOf(aspectRatio));
}
}
}*/
}
WritableArray images = Arguments.createArray();
WritableArray images = Arguments.createArray();
if (nativeAd.getImages() != null && nativeAd.getImages().size() > 0) {
if (nativeAd.getImages() != null && nativeAd.getImages().size() > 0) {
for (int i = 0; i < nativeAd.getImages().size(); i++) {
for (int i = 0; i < nativeAd.getImages().size(); i++) {
WritableMap map = Arguments.createMap();
WritableMap map = Arguments.createMap();
//if (nativeAd.getImages().get(i).getWidth() == -1) return;
map.putString("url", nativeAd.getImages().get(i).getUri().toString());
map.putInt("width", nativeAd.getImages().get(i).getWidth());
map.putInt("width", nativeAd.getImages().get(i).getWidth());
map.putInt("height", nativeAd.getImages().get(i).getHeight());
map.putInt("height", nativeAd.getImages().get(i).getHeight());
map.putString("url", nativeAd.getImages().get(i).getUri().toString());
images.pushMap(map);
images.pushMap(map);
}
}
}
}
if (images != null) {
if (images != null) {
args.putArray("images", images);
args.putArray("images", images);
} else {
args.putArray("images", null);
}
}
if (nativeAd.getIcon() != null && nativeAd.getIcon().getUri() != null) {
if (nativeAd.getIcon() != null || nativeAd.getIcon().getUri() != null) {
args.putString("icon", nativeAd.getIcon().getUri().toString());
args.putString("icon", nativeAd.getIcon().getUri().toString());
} else {
args.putString("icon", "empty");
}
}
//
sendEvent(RNAdMobNativeViewManager.EVENT_UNIFIED_NATIVE_AD_LOADED, args);
sendEvent(RNAdMobNativeViewManager.EVENT_UNIFIED_NATIVE_AD_LOADED, args);
} catch (Exception e) {
} catch (Exception e) {
}
}
if (handler != null) {
if (handler != null) {
handler.postDelayed(new Runnable() {
handler.postDelayed(new Runnable() {
@Override
@Override
public void run() {
public void run() {
loadAd();
loadAd();
}
}
}, adRefreshInterval);
}, adRefreshInterval);
}
}
}
}
@Override
@Override
protected void onAttachedToWindow() {
protected void onAttachedToWindow() {
super.onAttachedToWindow();
super.onAttachedToWindow();
}
}
@Override
@Override
protected void onDetachedFromWindow() {
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
super.onDetachedFromWindow();
}
}
private void sendEvent(String name, @Nullable WritableMap event) {
private void sendEvent(String name, @Nullable WritableMap event) {
ReactContext reactContext = (ReactContext) mContext;
ReactContext reactContext = (ReactContext) mContext;
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
getId(),
name,
name,
event);
event);
}
}
private void loadAd() {
private void loadAd() {
try {
try {
AdLoader.Builder builder = new AdLoader.Builder(mContext, admobAdUnitId);
AdLoader.Builder builder = new AdLoader.Builder(mContext, admobAdUnitId);
builder.forUnifiedNativeAd(onUnifiedNativeAdLoadedListener);
builder.forUnifiedNativeAd(onUnifiedNativeAdLoadedListener);
VideoOptions videoOptions = new VideoOptions.Builder()
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(true)
.setStartMuted(true)
.build();
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.setVideoOptions(videoOptions)
.setAdChoicesPlacement(adChoicesPlacement)
.setAdChoicesPlacement(adChoicesPlacement)
.build();
.build();
builder.withNativeAdOptions(adOptions);
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(adListener)
AdLoader adLoader = builder.withAdListener(adListener)
.build();
.build();
AdRequest adRequest;
AdRequest adRequest;
if (requestNonPersonalizedAdsOnly) {
if (requestNonPersonalizedAdsOnly) {
Bundle extras = new Bundle();
Bundle extras = new Bundle();
extras.putString("npa", "1");
extras.putString("npa", "1");
adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, extras).build();
adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, extras).build();
} else {
} else {
adRequest = new AdRequest.Builder().build();
adRequest = new AdRequest.Builder().build();
}
}
adLoader.loadAd(adRequest);
adLoader.loadAd(adRequest);
} catch (Exception e) {
} catch (Exception e) {
}
}
}
}
public void setLoadWithDelay(int delay) {
public void setLoadWithDelay(int delay) {
loadWithDelay = delay;
loadWithDelay = delay;
}
}
public void addNewView(View child, int index) {
public void addNewView(View child, int index) {
try {
try {
nativeAdView.addView(child, index);
nativeAdView.addView(child, index);
requestLayout();
requestLayout();
nativeAdView.requestLayout();
nativeAdView.requestLayout();
} catch (Exception e) {
} catch (Exception e) {
}
}
}
}
@Override
@Override
public void addView(View child) {
public void addView(View child) {
super.addView(child);
super.addView(child);
requestLayout();
requestLayout();
}
}
public void setAdRefreshInterval(int interval) {
public void setAdRefreshInterval(int interval) {
adRefreshInterval = interval;
adRefreshInterval = interval;
}
}
public void setAdUnitId(String id) {
public void setAdUnitId(String id) {
admobAdUnitId = id;
admobAdUnitId = id;
loadAd();
loadAd();
}
}
public void setAdChoicesPlacement(int location) {
public void setAdChoicesPlacement(int location) {
adChoicesPlacement = location;
adChoicesPlacement = location;
}
}
public void setRequestNonPersonalizedAdsOnly(boolean npa) {
public void setRequestNonPersonalizedAdsOnly(boolean npa) {
requestNonPersonalizedAdsOnly = npa;
requestNonPersonalizedAdsOnly = npa;
loadAd();
loadAd();
}
}
@Override
@Override
public void requestLayout() {
public void requestLayout() {
super.requestLayout();
super.requestLayout();
post(measureAndLayout);
post(measureAndLayout);
}
}
public void removeHandler() {
public void removeHandler() {
if (handler != null) {
if (handler != null) {
handler.removeCallbacks(null);
handler.removeCallbacks(null);
handler = null;
handler = null;
}
}
}
}
}
}