Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions iterableapi/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
android:name=".IterableTrampolineActivity"
android:exported="false"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"
android:theme="@style/TrampolineActivity.Transparent"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class IterableApi {
private IterableNotificationData _notificationData;
private String _deviceId;
private boolean _firstForegroundHandled;
private boolean _autoRetryOnJwtFailure;
private IterableHelper.SuccessHandler _setUserSuccessCallbackHandler;
private IterableHelper.FailureHandler _setUserFailureCallbackHandler;

Expand Down Expand Up @@ -104,6 +105,14 @@ public void execute(@Nullable String data) {
SharedPreferences sharedPref = sharedInstance.getMainActivityContext().getSharedPreferences(IterableConstants.SHARED_PREFS_SAVED_CONFIGURATION, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(IterableConstants.SHARED_PREFS_OFFLINE_MODE_KEY, offlineConfiguration);

// Parse autoRetry flag from remote config.
if (jsonData.has(IterableConstants.KEY_AUTO_RETRY)) {
boolean autoRetryRemote = jsonData.getBoolean(IterableConstants.KEY_AUTO_RETRY);
editor.putBoolean(IterableConstants.SHARED_PREFS_AUTO_RETRY_KEY, autoRetryRemote);
_autoRetryOnJwtFailure = autoRetryRemote;
}

editor.apply();
} catch (JSONException e) {
IterableLogger.e(TAG, "Failed to read remote configuration");
Expand Down Expand Up @@ -194,6 +203,15 @@ static void loadLastSavedConfiguration(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(IterableConstants.SHARED_PREFS_SAVED_CONFIGURATION, Context.MODE_PRIVATE);
boolean offlineMode = sharedPref.getBoolean(IterableConstants.SHARED_PREFS_OFFLINE_MODE_KEY, false);
sharedInstance.apiClient.setOfflineProcessingEnabled(offlineMode);

sharedInstance._autoRetryOnJwtFailure = sharedPref.getBoolean(IterableConstants.SHARED_PREFS_AUTO_RETRY_KEY, false);
}

/**
* Returns whether auto-retry on JWT failure is enabled, as determined by remote configuration.
*/
boolean isAutoRetryOnJwtFailure() {
return _autoRetryOnJwtFailure;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ private RequestProcessor getRequestProcessor() {
}

void setOfflineProcessingEnabled(boolean offlineMode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (offlineMode) {
if (this.requestProcessor == null || this.requestProcessor.getClass() != OfflineRequestProcessor.class) {
this.requestProcessor = new OfflineRequestProcessor(authProvider.getContext());
}
} else {
if (this.requestProcessor == null || this.requestProcessor.getClass() != OnlineRequestProcessor.class) {
this.requestProcessor = new OnlineRequestProcessor();
}
}
if (offlineMode && this.requestProcessor instanceof OfflineRequestProcessor) {
return;
}
if (!offlineMode && this.requestProcessor instanceof OnlineRequestProcessor) {
return;
}

if (this.requestProcessor instanceof OfflineRequestProcessor) {
((OfflineRequestProcessor) this.requestProcessor).dispose();
}

this.requestProcessor = offlineMode
? new OfflineRequestProcessor(authProvider.getContext())
: new OnlineRequestProcessor();
}

void getRemoteConfiguration(IterableHelper.IterableActionHandler actionHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
Expand All @@ -18,6 +19,25 @@ public class IterableAuthManager implements IterableActivityMonitor.AppStateCall
private static final String TAG = "IterableAuth";
private static final String expirationString = "exp";

/**
* Represents the state of the JWT auth token.
* VALID: Last request succeeded with this token.
* INVALID: A 401 JWT error was received; processing should pause.
* UNKNOWN: A new token was obtained but not yet verified by a request.
*/
enum AuthState {
VALID,
INVALID,
UNKNOWN
}

/**
* Listener interface for components that need to react when a new auth token is ready.
*/
interface AuthTokenReadyListener {
void onAuthTokenReady();
}

private final IterableApi api;
private final IterableAuthHandler authHandler;
private final long expiringAuthTokenRefreshPeriod;
Expand All @@ -34,6 +54,9 @@ public class IterableAuthManager implements IterableActivityMonitor.AppStateCall
private volatile boolean isTimerScheduled;
private volatile boolean isInForeground = true; // Assume foreground initially

private volatile AuthState authState = AuthState.UNKNOWN;
private final ArrayList<AuthTokenReadyListener> authTokenReadyListeners = new ArrayList<>();

private final ExecutorService executor = Executors.newSingleThreadExecutor();

IterableAuthManager(IterableApi api, IterableAuthHandler authHandler, RetryPolicy authRetryPolicy, long expiringAuthTokenRefreshPeriod) {
Expand All @@ -45,6 +68,58 @@ public class IterableAuthManager implements IterableActivityMonitor.AppStateCall
this.activityMonitor.addCallback(this);
}

void addAuthTokenReadyListener(AuthTokenReadyListener listener) {
authTokenReadyListeners.add(listener);
}

void removeAuthTokenReadyListener(AuthTokenReadyListener listener) {
authTokenReadyListeners.remove(listener);
}

/**
* Returns true if the auth token is in a state that allows requests to proceed.
* Requests can proceed when auth state is VALID or UNKNOWN (newly obtained token).
* If no authHandler is configured (JWT not used), this always returns true.
*/
boolean isAuthTokenReady() {
if (authHandler == null) {
return true;
}
return authState != AuthState.INVALID;
}

/**
* Marks the auth token as invalid. Called when a 401 JWT error is received.
*/
void setAuthTokenInvalid() {
setAuthState(AuthState.INVALID);
}

AuthState getAuthState() {
return authState;
}

/**
* Centralized auth state setter. Notifies AuthTokenReadyListeners only when
* transitioning from INVALID to a ready state (UNKNOWN or VALID), which means
* a new token has been obtained after a prior auth failure.
*/
private void setAuthState(AuthState newState) {
AuthState previousState = this.authState;
this.authState = newState;

if (previousState == AuthState.INVALID && newState != AuthState.INVALID) {
notifyAuthTokenReadyListeners();
}
}

private void notifyAuthTokenReadyListeners() {
ArrayList<AuthTokenReadyListener> listenersCopy = new ArrayList<>(authTokenReadyListeners);
for (AuthTokenReadyListener listener : listenersCopy) {
listener.onAuthTokenReady();
}
}

public synchronized void requestNewAuthToken(boolean hasFailedPriorAuth, IterableHelper.SuccessHandler successCallback) {
requestNewAuthToken(hasFailedPriorAuth, successCallback, true);
}
Expand All @@ -61,6 +136,9 @@ void reset() {

void setIsLastAuthTokenValid(boolean isValid) {
isLastAuthTokenValid = isValid;
if (isValid) {
setAuthState(AuthState.VALID);
}
}

void resetRetryCount() {
Expand Down Expand Up @@ -132,6 +210,9 @@ public void run() {

private void handleAuthTokenSuccess(String authToken, IterableHelper.SuccessHandler successCallback) {
if (authToken != null) {
// Token obtained but not yet verified by a request - set state to UNKNOWN.
// setAuthState will notify listeners only if previous state was INVALID.
setAuthState(AuthState.UNKNOWN);
IterableApi.getInstance().setAuthToken(authToken);
queueExpirationRefresh(authToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class IterableConstants {
public static final String KEY_INBOX_SESSION_ID = "inboxSessionId";
public static final String KEY_EMBEDDED_SESSION_ID = "id";
public static final String KEY_OFFLINE_MODE = "offlineMode";
public static final String KEY_AUTO_RETRY = "autoRetry";
public static final String KEY_FIRETV = "FireTV";
public static final String KEY_CREATE_NEW_FIELDS = "createNewFields";
public static final String KEY_IS_USER_KNOWN = "isUserKnown";
Expand Down Expand Up @@ -130,6 +131,7 @@ public final class IterableConstants {
public static final String SHARED_PREFS_FCM_MIGRATION_DONE_KEY = "itbl_fcm_migration_done";
public static final String SHARED_PREFS_SAVED_CONFIGURATION = "itbl_saved_configuration";
public static final String SHARED_PREFS_OFFLINE_MODE_KEY = "itbl_offline_mode";
public static final String SHARED_PREFS_AUTO_RETRY_KEY = "itbl_auto_retry";
public static final String SHARED_PREFS_EVENT_LIST_KEY = "itbl_event_list";
public static final String SHARED_PREFS_USER_UPDATE_OBJECT_KEY = "itbl_user_update_object";
public static final String SHARED_PREFS_UNKNOWN_SESSIONS = "itbl_unknown_sessions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
Expand Down Expand Up @@ -200,9 +201,12 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
applyWindowGravity(getDialog().getWindow(), "onCreateView");
}

webView = new IterableWebView(getContext());
webView = createWebViewSafely(getContext());
if (webView == null) {
dismissAllowingStateLoss();
return null;
}
webView.setId(R.id.webView);

webView.createWithHtml(this, htmlString);

if (orientationListener == null) {
Expand Down Expand Up @@ -324,7 +328,9 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
*/
@Override
public void onStop() {
orientationListener.disable();
if (orientationListener != null) {
orientationListener.disable();
}

super.onStop();
}
Expand Down Expand Up @@ -747,6 +753,18 @@ InAppLayout getInAppLayout(Rect padding) {
return InAppLayout.CENTER;
}
}

private IterableWebView createWebViewSafely(Context context) {
try {
return new IterableWebView(context);
} catch (Resources.NotFoundException e) {
IterableLogger.e(TAG, "Failed to create WebView - system WebView resource issue", e);
return null;
} catch (RuntimeException e) {
IterableLogger.e(TAG, "Failed to create WebView - unexpected error", e);
return null;
}
}
}

enum InAppLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private PendingIntent getPendingIntent(Context context, IterableNotificationData
if (button.openApp) {
IterableLogger.d(TAG, "Go through TrampolineActivity");
buttonIntent.setClass(context, IterableTrampolineActivity.class);
buttonIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
buttonIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingButtonIntent = PendingIntent.getActivity(context, buttonIntent.hashCode(),
buttonIntent, pendingIntentFlag);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public IterableNotificationBuilder createNotification(Context context, Bundle ex
trampolineActivityIntent.setClass(context, IterableTrampolineActivity.class);
trampolineActivityIntent.putExtras(extras);
trampolineActivityIntent.putExtra(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER, IterableConstants.ITERABLE_ACTION_DEFAULT);
trampolineActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
trampolineActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// Action buttons
if (notificationData.getActionButtons() != null) {
Expand Down
Loading
Loading