22
33import android .app .Activity ;
44import android .content .Context ;
5+ import android .os .Handler ;
6+ import android .os .Looper ;
57import com .getcapacitor .JSObject ;
68import com .getcapacitor .PluginCall ;
79
@@ -13,7 +15,15 @@ public interface EventNotifier {
1315
1416 private AppOpenAdManager appOpenAdManager ;
1517
16- public void loadAppOpen (Context context , PluginCall call , EventNotifier notifier ) {
18+ private static void runOnMain (Activity activity , Runnable runnable ) {
19+ if (activity != null ) {
20+ activity .runOnUiThread (runnable );
21+ } else {
22+ new Handler (Looper .getMainLooper ()).post (runnable );
23+ }
24+ }
25+
26+ public void loadAppOpen (Context context , Activity activity , PluginCall call , EventNotifier notifier ) {
1727 if (context == null ) {
1828 call .reject ("Context is not available" );
1929 return ;
@@ -24,21 +34,25 @@ public void loadAppOpen(Context context, PluginCall call, EventNotifier notifier
2434 call .reject ("adUnitId is required" );
2535 return ;
2636 }
27- if (appOpenAdManager == null ) {
28- appOpenAdManager = new AppOpenAdManager (adUnitId );
29- }
3037
31- appOpenAdManager .loadAd (
32- context ,
33- () -> {
34- notifier .notify ("appOpenAdLoaded" , new JSObject ());
35- call .resolve ();
36- },
37- () -> {
38- notifier .notify ("appOpenAdFailedToLoad" , new JSObject ());
39- call .reject ("Failed to load App Open Ad" );
38+ final Context appContext = context .getApplicationContext ();
39+ runOnMain (activity , () -> {
40+ if (appOpenAdManager == null || !adUnitId .equals (appOpenAdManager .getAdUnitId ())) {
41+ appOpenAdManager = new AppOpenAdManager (adUnitId );
4042 }
41- );
43+
44+ appOpenAdManager .loadAd (
45+ appContext ,
46+ () -> {
47+ notifier .notify ("appOpenAdLoaded" , new JSObject ());
48+ call .resolve ();
49+ },
50+ () -> {
51+ notifier .notify ("appOpenAdFailedToLoad" , new JSObject ());
52+ call .reject ("Failed to load App Open Ad" );
53+ }
54+ );
55+ });
4256 }
4357
4458 public void showAppOpen (Activity activity , PluginCall call , EventNotifier notifier ) {
@@ -47,31 +61,35 @@ public void showAppOpen(Activity activity, PluginCall call, EventNotifier notifi
4761 return ;
4862 }
4963
50- if (appOpenAdManager == null || !appOpenAdManager .isAdLoaded ()) {
51- call .reject ("App Open Ad is not loaded" );
52- return ;
53- }
54-
55- appOpenAdManager .showAdIfAvailable (
56- activity ,
57- () -> {
58- notifier .notify ("appOpenAdOpened" , new JSObject ());
59- },
60- () -> {
61- notifier .notify ("appOpenAdClosed" , new JSObject ());
62- call .resolve ();
63- },
64- () -> {
65- notifier .notify ("appOpenAdFailedToShow" , new JSObject ());
66- call .reject ("Failed to show App Open Ad" );
64+ activity .runOnUiThread (() -> {
65+ if (appOpenAdManager == null || !appOpenAdManager .isAdLoaded ()) {
66+ call .reject ("App Open Ad is not loaded" );
67+ return ;
6768 }
68- );
69+
70+ appOpenAdManager .showAdIfAvailable (
71+ activity ,
72+ () -> {
73+ notifier .notify ("appOpenAdOpened" , new JSObject ());
74+ },
75+ () -> {
76+ notifier .notify ("appOpenAdClosed" , new JSObject ());
77+ call .resolve ();
78+ },
79+ () -> {
80+ notifier .notify ("appOpenAdFailedToShow" , new JSObject ());
81+ call .reject ("Failed to show App Open Ad" );
82+ }
83+ );
84+ });
6985 }
7086
71- public void isAppOpenLoaded (PluginCall call ) {
72- boolean loaded = appOpenAdManager != null && appOpenAdManager .isAdLoaded ();
73- JSObject result = new JSObject ();
74- result .put ("value" , loaded );
75- call .resolve (result );
87+ public void isAppOpenLoaded (Activity activity , PluginCall call ) {
88+ runOnMain (activity , () -> {
89+ boolean loaded = appOpenAdManager != null && appOpenAdManager .isAdLoaded ();
90+ JSObject result = new JSObject ();
91+ result .put ("value" , loaded );
92+ call .resolve (result );
93+ });
7694 }
7795}
0 commit comments