Skip to content

Commit 1f39b71

Browse files
Googlera-maurice
authored andcommitted
Handling IllegalStateException when loading or creating interstitial ads.
(reported in #121 New error code and message for the default fallback case in switch statements- ConstantsHelper.CALLBACK_ERROR_UNKOWN and ConstantsHelper.CALLBACK_ERROR_MESSAGE_UNKNOWN. Corresponding new error code added to C++ side (admob/types.h) Removed a redundant overridden method as it was just calling its base class method directly. Updated release notes with this bugfix PiperOrigin-RevId: 333125360
1 parent a0c292a commit 1f39b71

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

admob/src/include/firebase/admob/types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ enum AdMobError {
7272
/// An attempt has been made to show an ad on an Android Activity that has
7373
/// no window token (such as one that's not done initializing).
7474
kAdMobErrorNoWindowToken,
75+
/// Fallback error for any unidentified cases.
76+
kAdMobErrorUnknown,
7577
};
7678
#ifdef INTERNAL_EXPERIMENTAL
7779
// LINT.ThenChange(//depot_firebase_cpp/admob/client/cpp/src_java/com/google/firebase/admob/internal/cpp/ConstantsHelper.java)

admob/src_java/com/google/firebase/admob/internal/cpp/ConstantsHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public final class ConstantsHelper {
4040
public static final int CALLBACK_ERROR_NO_FILL = 7;
4141

4242
public static final int CALLBACK_ERROR_NO_WINDOW_TOKEN = 8;
43+
44+
public static final int CALLBACK_ERROR_UNKNOWN = 9;
4345
// LINT.ThenChange(//depot_firebase_cpp/admob/client/cpp/src/include/firebase/admob/types.h)
4446

4547
/**
@@ -68,6 +70,8 @@ public final class ConstantsHelper {
6870

6971
public static final String CALLBACK_ERROR_MESSAGE_NO_WINDOW_TOKEN =
7072
"Android Activity does not have a window token.";
73+
74+
public static final String CALLBACK_ERROR_MESSAGE_UNKNOWN = "Unknown error occurred.";
7175
// LINT.ThenChange(//depot_firebase_cpp/admob/client/cpp/src/include/firebase/admob/types.h)
7276

7377
/** Types of notifications to send back to the C++ side for listeners updates. */

admob/src_java/com/google/firebase/admob/internal/cpp/InterstitialAdHelper.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,22 @@ public void run() {
9292
int errorCode;
9393
String errorMessage;
9494
if (mInterstitial == null) {
95-
errorCode = ConstantsHelper.CALLBACK_ERROR_NONE;
96-
errorMessage = ConstantsHelper.CALLBACK_ERROR_MESSAGE_NONE;
97-
mInterstitial = new InterstitialAd(mActivity);
98-
mInterstitial.setAdUnitId(mAdUnitId);
99-
mInterstitial.setAdListener(new InterstitialAdListener());
95+
try {
96+
mInterstitial = new InterstitialAd(mActivity);
97+
mInterstitial.setAdUnitId(mAdUnitId);
98+
mInterstitial.setAdListener(new InterstitialAdListener());
99+
errorCode = ConstantsHelper.CALLBACK_ERROR_NONE;
100+
errorMessage = ConstantsHelper.CALLBACK_ERROR_MESSAGE_NONE;
101+
} catch (IllegalStateException e) {
102+
mInterstitial = null;
103+
// This exception can be thrown if the ad unit ID was already set.
104+
errorCode = ConstantsHelper.CALLBACK_ERROR_ALREADY_INITIALIZED;
105+
errorMessage = ConstantsHelper.CALLBACK_ERROR_MESSAGE_ALREADY_INITIALIZED;
106+
}
100107
} else {
101108
errorCode = ConstantsHelper.CALLBACK_ERROR_ALREADY_INITIALIZED;
102109
errorMessage = ConstantsHelper.CALLBACK_ERROR_MESSAGE_ALREADY_INITIALIZED;
110+
103111
}
104112

105113
completeInterstitialAdFutureCallback(callbackDataPtr, errorCode, errorMessage);
@@ -152,7 +160,17 @@ public void run() {
152160
mLoadAdCallbackDataPtr = CPP_NULLPTR;
153161
}
154162
} else {
155-
mInterstitial.loadAd(request);
163+
try {
164+
mInterstitial.loadAd(request);
165+
} catch (IllegalStateException e) {
166+
synchronized (mInterstitialLock) {
167+
completeInterstitialAdFutureCallback(
168+
mLoadAdCallbackDataPtr,
169+
ConstantsHelper.CALLBACK_ERROR_UNINITIALIZED,
170+
ConstantsHelper.CALLBACK_ERROR_MESSAGE_UNINITIALIZED);
171+
mLoadAdCallbackDataPtr = CPP_NULLPTR;
172+
}
173+
}
156174
}
157175
}
158176
});
@@ -222,6 +240,9 @@ public void onAdFailedToLoad(int errorCode) {
222240
callbackErrorCode = ConstantsHelper.CALLBACK_ERROR_NO_FILL;
223241
callbackErrorMessage = ConstantsHelper.CALLBACK_ERROR_MESSAGE_NO_FILL;
224242
break;
243+
default:
244+
callbackErrorCode = ConstantsHelper.CALLBACK_ERROR_UNKNOWN;
245+
callbackErrorMessage = ConstantsHelper.CALLBACK_ERROR_MESSAGE_UNKNOWN;
225246
}
226247

227248
synchronized (mInterstitialLock) {
@@ -233,11 +254,6 @@ public void onAdFailedToLoad(int errorCode) {
233254
super.onAdFailedToLoad(errorCode);
234255
}
235256

236-
@Override
237-
public void onAdLeftApplication() {
238-
super.onAdLeftApplication();
239-
}
240-
241257
@Override
242258
public void onAdLoaded() {
243259
synchronized (mInterstitialLock) {

0 commit comments

Comments
 (0)