Skip to content

Commit e8ff15e

Browse files
committed
[MOB-11508] Some fixes
1 parent d0a4547 commit e8ff15e

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableAuthManager.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,33 @@ void clearRefreshTimer() {
256256

257257
@Override
258258
public void onSwitchToBackground() {
259-
IterableLogger.v(TAG, "App switched to background. Clearing auth refresh timer.");
260-
clearRefreshTimer();
259+
try {
260+
IterableLogger.v(TAG, "App switched to background. Clearing auth refresh timer.");
261+
clearRefreshTimer();
262+
} catch (Exception e) {
263+
IterableLogger.e(TAG, "Error in onSwitchToBackground", e);
264+
}
261265
}
262266

263267
@Override
264268
public void onSwitchToForeground() {
265-
IterableLogger.v(TAG, "App switched to foreground. Re-evaluating auth token refresh.");
266-
String authToken = api.getAuthToken();
267-
268-
if (authToken != null) {
269-
// Try to queue normal expiration refresh - if token is expired, this will handle it appropriately
270-
queueExpirationRefresh(authToken);
271-
} else if ((api.getEmail() != null || api.getUserId() != null) && !pendingAuth) {
272-
// No token but user is set - request new token
273-
IterableLogger.d(TAG, "App foregrounded, user identified, no auth token present. Requesting new token.");
274-
requestNewAuthToken(false, null, true);
269+
try {
270+
IterableLogger.v(TAG, "App switched to foreground. Re-evaluating auth token refresh.");
271+
String authToken = api.getAuthToken();
272+
273+
if (authToken != null) {
274+
queueExpirationRefresh(authToken);
275+
// If queueExpirationRefresh didn't schedule a timer (expired token case), request new token
276+
if (!isTimerScheduled && !pendingAuth) {
277+
IterableLogger.d(TAG, "Token expired, requesting new token on foreground");
278+
requestNewAuthToken(false, null, true);
279+
}
280+
} else if ((api.getEmail() != null || api.getUserId() != null) && !pendingAuth) {
281+
IterableLogger.d(TAG, "App foregrounded, user identified, no auth token present. Requesting new token.");
282+
requestNewAuthToken(false, null, true);
283+
}
284+
} catch (Exception e) {
285+
IterableLogger.e(TAG, "Error in onSwitchToForeground", e);
275286
}
276287
}
277288
}

iterableapi/src/test/java/com/iterable/iterableapi/IterableActivityMonitorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,21 @@ public IterableConfig.Builder run(IterableConfig.Builder builder) {
9898
return builder.setAuthHandler(mockAuthHandler);
9999
}
100100
}, null);
101-
101+
102102
IterableApi.getInstance().setEmail("[email protected]");
103103
IterableAuthManager authManager = IterableApi.getInstance().getAuthManager();
104-
104+
105105
// Verify AuthManager is registered as a callback
106106
ActivityController<Activity> activity = Robolectric.buildActivity(Activity.class).create().start().resume();
107107
Robolectric.flushForegroundThreadScheduler();
108-
108+
109109
// Verify we can trigger lifecycle methods without errors
110110
authManager.onSwitchToBackground();
111111
authManager.onSwitchToForeground();
112-
112+
113113
// Test that reset() unregisters the callback
114114
authManager.reset();
115-
115+
116116
// After reset, lifecycle methods should still work (no exceptions)
117117
authManager.onSwitchToBackground();
118118
authManager.onSwitchToForeground();

iterableapi/src/test/java/com/iterable/iterableapi/IterableApiAuthTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,29 +483,29 @@ public void testRegisterForPushInvokedAfterTokenRefresh() throws InterruptedExce
483483
@Test
484484
public void testAuthTokenRefreshPausesOnBackground() throws Exception {
485485
IterableApi.initialize(getContext(), "apiKey");
486-
486+
487487
IterableAuthManager authManager = IterableApi.getInstance().getAuthManager();
488-
488+
489489
// Set up a valid token and user to trigger normal expiration refresh
490490
doReturn(validJWT).when(authHandler).onAuthTokenRequested();
491491
IterableApi.getInstance().setEmail("[email protected]");
492492
shadowOf(getMainLooper()).runToEndOfTasks();
493-
493+
494494
// Request auth token which should set a timer for expiration refresh
495495
authManager.requestNewAuthToken(false);
496496
shadowOf(getMainLooper()).runToEndOfTasks();
497-
497+
498498
// The timer might be null if the token is considered expired, so let's test the behavior
499-
// rather than the internal timer state. We'll check that onSwitchToBackground and
499+
// rather than the internal timer state. We'll check that onSwitchToBackground and
500500
// onSwitchToForeground can be called without exceptions
501-
501+
502502
// Simulate app going to background - should clear any timer
503503
authManager.onSwitchToBackground();
504-
504+
505505
// Simulate app coming to foreground - should re-evaluate token state
506506
authManager.onSwitchToForeground();
507507
shadowOf(getMainLooper()).runToEndOfTasks();
508-
508+
509509
// Test passes if no exceptions were thrown and lifecycle methods executed successfully
510510
}
511511

0 commit comments

Comments
 (0)