Skip to content

Commit

Permalink
fix(admob): add null checks for getCurrentActivity() usages (#2913)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanamd authored and Salakar committed Nov 23, 2019
1 parent 227ab63 commit 1fb296d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ private void sendInterstitialEvent(String type, int requestId, String adUnitId,

@ReactMethod
public void interstitialLoad(int requestId, String adUnitId, ReadableMap adRequestOptions) {
if (getCurrentActivity() == null) {
WritableMap error = Arguments.createMap();
error.putString("code", "null-activity");
error.putString("message", "Interstitial ad attempted to load but the current Activity was null.");
sendInterstitialEvent(AD_ERROR, requestId, adUnitId, error);
return;
}
getCurrentActivity().runOnUiThread(() -> {
InterstitialAd interstitialAd = new InterstitialAd(getApplicationContext());
interstitialAd.setAdUnitId(adUnitId);
Expand Down Expand Up @@ -112,6 +119,10 @@ public void onAdClosed() {

@ReactMethod
public void interstitialShow(int requestId, ReadableMap showOptions, Promise promise) {
if (getCurrentActivity() == null) {
rejectPromiseWithCodeAndMessage(promise, "null-activity", "Interstitial ad attempted to show but the current Activity was null.");
return;
}
getCurrentActivity().runOnUiThread(() -> {
InterstitialAd interstitialAd = interstitialAdArray.get(requestId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ private void sendRewardedEvent(String type, int requestId, String adUnitId, @Nul

@ReactMethod
public void rewardedLoad(int requestId, String adUnitId, ReadableMap adRequestOptions) {
if (getCurrentActivity() == null) {
WritableMap error = Arguments.createMap();
error.putString("code", "null-activity");
error.putString("message", "Rewarded ad attempted to load but the current Activity was null.");
sendRewardedEvent(AD_ERROR, requestId, adUnitId, error, null);
return;
}
getCurrentActivity().runOnUiThread(() -> {
RewardedAd rewardedAd = new RewardedAd(getApplicationContext(), adUnitId);

Expand Down Expand Up @@ -80,6 +87,10 @@ public void onRewardedAdFailedToLoad(int errorCode) {

@ReactMethod
public void rewardedShow(int requestId, String adUnitId, ReadableMap showOptions, Promise promise) {
if (getCurrentActivity() == null) {
rejectPromiseWithCodeAndMessage(promise, "null-activity", "Rewarded ad attempted to show but the current Activity was null.");
return;
}
getCurrentActivity().runOnUiThread(() -> {
RewardedAd rewardedAd = rewardedAdArray.get(requestId);

Expand Down

0 comments on commit 1fb296d

Please sign in to comment.