Skip to content

Commit

Permalink
Merge pull request #120 from NZME/feature/upgrade-to-support-newest-g…
Browse files Browse the repository at this point in the history
…ma-sdk-possible

upgrade to support newest GMA SDK version possible on both Android and iOS
  • Loading branch information
mharrison-nzme authored Mar 11, 2024
2 parents 8baa226 + 1618dcf commit 38af969
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 92 deletions.
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def DEFAULT_COMPILE_SDK_VERSION = 29
def DEFAULT_COMPILE_SDK_VERSION = 33
// def DEFAULT_BUILD_TOOLS_VERSION = '29.0.2'
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 29
def DEFAULT_MIN_SDK_VERSION = 24
def DEFAULT_TARGET_SDK_VERSION = 33

buildscript {
if (project == rootProject) {
Expand Down Expand Up @@ -60,6 +60,6 @@ repositories {
dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
implementation 'com.google.android.gms:play-services-ads:20.5.0'
implementation 'com.google.android.gms:play-services-ads:22.4.0'
implementation 'com.google.ads.mediation:facebook:6.8.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ public void loadBanner() {
if (publisherProvidedID != null) {
adRequestBuilder.setPublisherProvidedId(publisherProvidedID);
}
if (location != null) {
adRequestBuilder.setLocation(location);
}

// setLocation() became obsolete since GMA SDK version 21.0.0, link reference below:
// https://developers.google.com/admob/android/rel-notes
//if (location != null) {
// adRequestBuilder.setLocation(location);
//}
}

AdManagerAdRequest adRequest = adRequestBuilder.build();
Expand Down
146 changes: 72 additions & 74 deletions android/src/main/java/com/matejdr/admanager/BannerAdView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import com.facebook.react.modules.core.DeviceEventManagerModule;


class BannerAdView extends ReactViewGroup implements AppEventListener, LifecycleEventListener {
protected AdManagerAdView adManagerAdView;
ReactApplicationContext currentRNcontext;
Expand Down Expand Up @@ -63,9 +62,9 @@ public BannerAdView(final Context context, ReactApplicationContext applicationCo
currentActivityContext = applicationContext.getCurrentActivity();
applicationContext.addLifecycleEventListener(this);
this.createAdView();
} catch (Exception exception) {
} catch (Exception exception) {
this.onException(exception);
}
}
}

private void onException(Exception exception) {
Expand All @@ -82,15 +81,15 @@ private void onException(Exception exception) {
try {
this.adManagerAdView.destroy();
} catch (Exception innerIgnored) {
//ignore it
// ignore it
}
this.adManagerAdView = null;
this.adManagerAdView = null;
}

} catch (Exception e) {
// ignore it
}
}
}

private boolean isFluid() {
return AdSize.FLUID.equals(this.adSize);
Expand All @@ -104,16 +103,17 @@ public void requestLayout() {

private void createAdView() {
try {
if (this.adManagerAdView != null) this.adManagerAdView.destroy();
if (this.currentActivityContext == null) return;
if (this.adManagerAdView != null)
this.adManagerAdView.destroy();
if (this.currentActivityContext == null)
return;

this.adManagerAdView = new AdManagerAdView(currentActivityContext);

if (isFluid()) {
AdManagerAdView.LayoutParams layoutParams = new AdManagerAdView.LayoutParams(
ReactViewGroup.LayoutParams.MATCH_PARENT,
ReactViewGroup.LayoutParams.WRAP_CONTENT
);
ReactViewGroup.LayoutParams.MATCH_PARENT,
ReactViewGroup.LayoutParams.WRAP_CONTENT);
this.adManagerAdView.setLayoutParams(layoutParams);
}

Expand Down Expand Up @@ -205,9 +205,9 @@ public void onAdImpression() {
});
this.addView(this.adManagerAdView);
} catch (Exception e) {
sendErrorEvent("✅💪Error found at ad manager when createAdView(): "+e.getMessage()+"!");
sendErrorEvent("✅💪Error found at ad manager when createAdView(): " + e.getMessage() + "!");
this.onException(e);
}
}
} // end of createAdView

private void sendOnSizeChangeEvent() {
Expand All @@ -223,20 +223,20 @@ private void sendOnSizeChangeEvent() {
event.putDouble("height", height);
sendEvent(RNAdManagerBannerViewManager.EVENT_SIZE_CHANGE, event);
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when sendOnSizeChangeEvent(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when sendOnSizeChangeEvent(): " + e.getMessage() + "!");
}
}

private void sendEvent(String name, @Nullable WritableMap event) {
try {
ReactContext reactContext = (ReactContext) getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
getId(),
name,
event);
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when sendEvent(): "+e.getMessage()+"!");
}
getId(),
name,
event);
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when sendEvent(): " + e.getMessage() + "!");
}
}

public void loadBanner() {
Expand Down Expand Up @@ -271,10 +271,9 @@ public void loadBanner() {
}
testDevicesList.add(testDevice);
}
RequestConfiguration requestConfiguration
= new RequestConfiguration.Builder()
.setTestDeviceIds(testDevicesList)
.build();
RequestConfiguration requestConfiguration = new RequestConfiguration.Builder()
.setTestDeviceIds(testDevicesList)
.build();
MobileAds.setRequestConfiguration(requestConfiguration);
}

Expand All @@ -286,7 +285,6 @@ public void loadBanner() {

adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);


// Targeting
if (hasTargeting) {
if (customTargeting != null && customTargeting.length > 0) {
Expand Down Expand Up @@ -323,34 +321,35 @@ public void loadBanner() {
if (publisherProvidedID != null) {
adRequestBuilder.setPublisherProvidedId(publisherProvidedID);
}
if (location != null) {
adRequestBuilder.setLocation(location);
}
// setLocation() became obsolete since GMA SDK version 21.0.0, link reference
// below:
// https://developers.google.com/admob/android/rel-notes
// if (location != null) {
// adRequestBuilder.setLocation(location);
// }
}

AdManagerAdRequest adRequest = adRequestBuilder.build();
this.adManagerAdView.loadAd(adRequest);
} catch (Exception e) {
sendErrorEvent("✅💪Error found at ad manager when loadBanner(): "+e.getMessage()+"!");
} catch (Exception e) {
sendErrorEvent("✅💪Error found at ad manager when loadBanner(): " + e.getMessage() + "!");
this.onException(e);
}
} //End of loadBanner()

}
} // End of loadBanner()

// bubble up error to JS/TS level.
public void sendErrorEvent(String errorMessage) {
try {
WritableMap params = Arguments.createMap();
params.putString("error", errorMessage);
currentRNcontext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("onError", params);
currentRNcontext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("onError", params);
} catch (Exception e) {
//non-critical error, so ignore.
// non-critical error, so ignore.
}

}

}

public void setAdUnitID(String adUnitID) {
try {
Expand All @@ -362,16 +361,16 @@ public void setAdUnitID(String adUnitID) {
this.adUnitID = adUnitID;
this.adManagerAdView.setAdUnitId(adUnitID);
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setAdUnitID(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setAdUnitID(): " + e.getMessage() + "!");
}
}

public void setTestDevices(String[] testDevices) {
try {
this.testDevices = testDevices;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setTestDevices(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setTestDevices(): " + e.getMessage() + "!");
}

}

Expand All @@ -380,72 +379,72 @@ public void setCustomTargeting(CustomTargeting[] customTargeting) {
try {
this.customTargeting = customTargeting;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setCustomTargeting(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setCustomTargeting(): " + e.getMessage() + "!");
}
}

public void setCategoryExclusions(String[] categoryExclusions) {
try {
this.categoryExclusions = categoryExclusions;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setCategoryExclusions(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setCategoryExclusions(): " + e.getMessage() + "!");
}
}

public void setKeywords(String[] keywords) {
try {
this.keywords = keywords;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setKeywords(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setKeywords(): " + e.getMessage() + "!");
}
}

public void setContentURL(String content_url) {
try {
this.content_url = content_url;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setContentURL(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setContentURL(): " + e.getMessage() + "!");
}
}

public void setPublisherProvidedID(String publisherProvidedID) {
try {
this.publisherProvidedID = publisherProvidedID;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setPublisherProvidedID(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setPublisherProvidedID(): " + e.getMessage() + "!");
}
}

public void setLocation(Location location) {
try {
this.location = location;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setLocation(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setLocation(): " + e.getMessage() + "!");
}
}

public void setAdSize(AdSize adSize) {
try {
this.adSize = adSize;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setAdSize(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setAdSize(): " + e.getMessage() + "!");
}
}

public void setValidAdSizes(AdSize[] adSizes) {
try {
this.validAdSizes = adSizes;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setValidAdSizes(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setValidAdSizes(): " + e.getMessage() + "!");
}
}

public void setCorrelator(String correlator) {
try {
this.correlator = correlator;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setCorrelator(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when setCorrelator(): " + e.getMessage() + "!");
}
}

@Override
Expand All @@ -456,8 +455,8 @@ public void onAppEvent(String name, String info) {
event.putString("info", info);
sendEvent(RNAdManagerBannerViewManager.EVENT_APP_EVENT, event);
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when onAppEvent(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when onAppEvent(): " + e.getMessage() + "!");
}
}

@Override
Expand All @@ -467,8 +466,8 @@ public void onHostResume() {
this.adManagerAdView.resume();
}
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when onHostResume(): "+e.getMessage()+"!");
}
sendErrorEvent("Error found at ad manager when onHostResume(): " + e.getMessage() + "!");
}
}

@Override
Expand All @@ -478,7 +477,7 @@ public void onHostPause() {
this.adManagerAdView.pause();
}
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when onHostResume(): "+e.getMessage()+"!");
sendErrorEvent("Error found at ad manager when onHostResume(): " + e.getMessage() + "!");
}
}

Expand All @@ -490,7 +489,7 @@ public void onHostDestroy() {
this.adManagerAdView.destroy();
}
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when onHostDestroy(): "+e.getMessage()+"!");
sendErrorEvent("Error found at ad manager when onHostDestroy(): " + e.getMessage() + "!");
}
}

Expand All @@ -500,17 +499,16 @@ public void run() {
try {
if (isFluid()) {
adManagerAdView.measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY)
);
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
} else {
adManagerAdView.measure(width, height);
}
adManagerAdView.layout(left, top, left + width, top + height);
} catch (Exception e) {
sendErrorEvent(
"Error found at ad manager when MeasureAndLayoutRunnable::run(): " + e.getMessage() + "!");
}
catch (Exception e) {
sendErrorEvent("Error found at ad manager when MeasureAndLayoutRunnable::run(): "+e.getMessage()+"!");
}
}
} // end of Runnable
}
Loading

0 comments on commit 38af969

Please sign in to comment.