Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
fix(android): fix current activity not found
Browse files Browse the repository at this point in the history
Added custom ReactModule base class to keep track for current activity.

Refs: #30
  • Loading branch information
wjaykim committed Nov 28, 2021
1 parent cb28d45 commit 75562e4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.rnadmob.admob;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;

public abstract class ActivityAwareJavaModule extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks, LifecycleEventListener {

@Nullable
private Application application;

@Nullable
protected Activity currentActivity;

public ActivityAwareJavaModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addLifecycleEventListener(this);
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

@Override
public void onActivityStarted(@NonNull Activity activity) {
currentActivity = activity;
}

@Override
public void onActivityResumed(@NonNull Activity activity) {
currentActivity = activity;
}

@Override
public void onActivityStopped(Activity activity) {}

@Override
public void onActivityPaused(Activity activity) {}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}

@Override
public void onActivityDestroyed(@NonNull Activity activity) {
if (activity.equals(currentActivity)) {
currentActivity = null;
}
}

@Override
public void onHostResume() {
if (application == null) {
currentActivity = getCurrentActivity();
if (currentActivity != null) {
application = currentActivity.getApplication();
application.registerActivityLifecycleCallbacks(this);
}
}
}

@Override
public void onHostPause() {}

@Override
public void onHostDestroy() {
if (application != null) {
application.unregisterActivityLifecycleCallbacks(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.rnadmob.admob.RNAdMobEventModule.AD_FAILED_TO_PRESENT;

import android.app.Activity;
import android.os.Handler;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -97,7 +96,7 @@ public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
}

@Override
protected void show(AppOpenAd ad, Activity activity, int requestId) {
protected void show(AppOpenAd ad, int requestId) {
if (isAdExpired()) {
presentPromiseHolder.reject(requestId, "E_AD_NOT_READY", "Ad is expired.");
WritableMap error = Arguments.createMap();
Expand All @@ -106,7 +105,7 @@ protected void show(AppOpenAd ad, Activity activity, int requestId) {
requestAd(requestId, unitId, options, null);
return;
}
ad.show(activity);
ad.show(currentActivity);
}

private boolean isAdExpired() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
import static com.rnadmob.admob.RNAdMobEventModule.AD_LOADED;
import static com.rnadmob.admob.RNAdMobEventModule.AD_PRESENTED;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdLoadCallback;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.rnadmob.admob.ActivityAwareJavaModule;
import com.rnadmob.admob.RNAdMobAdHolder;
import com.rnadmob.admob.RNAdMobCommon;
import com.rnadmob.admob.RNAdMobEventModule;
Expand All @@ -30,7 +28,7 @@
import java.util.Locale;
import java.util.Objects;

public abstract class RNAdMobFullScreenAdModule<T> extends ReactContextBaseJavaModule {
public abstract class RNAdMobFullScreenAdModule<T> extends ActivityAwareJavaModule {

RNAdMobAdHolder<T> adHolder = new RNAdMobAdHolder<>();
RNAdMobPromiseHolder presentPromiseHolder = new RNAdMobPromiseHolder();
Expand All @@ -56,7 +54,7 @@ public void onCatalystInstanceDestroy() {

protected abstract void load(String unitId, AdManagerAdRequest adRequest, AdLoadCallback<T> adLoadCallback, FullScreenContentCallback fullScreenContentCallback);

protected abstract void show(T ad, Activity activity, int requestId);
protected abstract void show(T ad, int requestId);

protected void sendEvent(String eventName, int requestId, @Nullable WritableMap data) {
RNAdMobEventModule.sendEvent(eventName, getAdType(), requestId, data);
Expand Down Expand Up @@ -158,13 +156,12 @@ public void onAdShowedFullScreenContent() {
}

protected void requestAd(int requestId, String unitId, ReadableMap options, final Promise promise) {
Activity activity = getCurrentActivity(promise);
if (activity == null) {
if (currentActivity == null) {
return;
}

adHolder.remove(requestId);
activity.runOnUiThread(() -> {
currentActivity.runOnUiThread(() -> {
AdManagerAdRequest adRequest = RNAdMobCommon.buildAdRequest(Objects.requireNonNull(options.getMap("requestOptions")));
AdLoadCallback<T> adLoadCallback = getAdLoadCallback(requestId, options, promise);
FullScreenContentCallback fullScreenContentCallback = getFullScreenContentCallback(requestId, unitId, options);
Expand All @@ -173,16 +170,15 @@ protected void requestAd(int requestId, String unitId, ReadableMap options, fina
}

protected void presentAd(int requestId, final Promise promise) {
Activity activity = getCurrentActivity(promise);
if (activity == null) {
if (currentActivity == null) {
return;
}

activity.runOnUiThread(() -> {
currentActivity.runOnUiThread(() -> {
T ad = adHolder.get(requestId);
if (ad != null) {
presentPromiseHolder.add(requestId, promise);
show(ad, activity, requestId);
show(ad, requestId);
} else {
if (promise != null) {
promise.reject("E_AD_NOT_READY", "Ad is not ready.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
}

@Override
protected void show(InterstitialAd ad, Activity activity, int requestId) {
ad.show(activity);
protected void show(InterstitialAd ad, int requestId) {
ad.show(currentActivity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
}

@Override
protected void show(RewardedAd ad, Activity activity, int requestId) {
ad.show(activity, rewardItem -> {
protected void show(RewardedAd ad, int requestId) {
ad.show(currentActivity, rewardItem -> {
WritableMap reward = Arguments.createMap();
reward.putInt("amount", rewardItem.getAmount());
reward.putString("type", rewardItem.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
}

@Override
protected void show(RewardedInterstitialAd ad, Activity activity, int requestId) {
ad.show(activity, rewardItem -> {
protected void show(RewardedInterstitialAd ad, int requestId) {
ad.show(currentActivity, rewardItem -> {
WritableMap reward = Arguments.createMap();
reward.putInt("amount", rewardItem.getAmount());
reward.putString("type", rewardItem.getType());
Expand Down

0 comments on commit 75562e4

Please sign in to comment.