Skip to content

Commit ce245f5

Browse files
committed
Fix IAM redisplay when no dismiss
* If user navigates with backpress while an IAM is being display, IAM should continue displaying until user dismiss it. * Create new scenario for displaying IAM again when changing activity
1 parent 09ebc87 commit ce245f5

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/ActivityLifecycleHandler.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract static class ActivityAvailableListener {
4949
void available(@NonNull Activity activity) {
5050
}
5151

52-
void stopped() {
52+
void stopped(@NonNull Activity activity) {
5353
}
5454

5555
void lostFocus() {
@@ -67,11 +67,11 @@ void lostFocus() {
6767
private Activity curActivity = null;
6868
private boolean nextResumeIsFirstActivity = false;
6969

70-
void onConfigurationChanged(Configuration newConfig) {
70+
void onConfigurationChanged(Configuration newConfig, Activity activity) {
7171
// If Activity contains the configChanges orientation flag, re-create the view this way
7272
if (curActivity != null && OSUtils.hasConfigChangeFlag(curActivity, ActivityInfo.CONFIG_ORIENTATION)) {
73-
logOrientationChange(newConfig.orientation);
74-
onOrientationChanged();
73+
logOrientationChange(newConfig.orientation, activity);
74+
onOrientationChanged(activity);
7575
}
7676
}
7777

@@ -107,7 +107,7 @@ void onActivityStopped(Activity activity) {
107107
}
108108

109109
for (Map.Entry<String, ActivityAvailableListener> entry : sActivityAvailableListeners.entrySet()) {
110-
entry.getValue().stopped();
110+
entry.getValue().stopped(activity);
111111
}
112112

113113
logCurActivity();
@@ -129,12 +129,12 @@ private void logCurActivity() {
129129
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "curActivity is NOW: " + (curActivity != null ? "" + curActivity.getClass().getName() + ":" + curActivity : "null"));
130130
}
131131

132-
private void logOrientationChange(int orientation) {
132+
private void logOrientationChange(int orientation, Activity activity) {
133133
// Log device orientation change
134134
if (orientation == Configuration.ORIENTATION_LANDSCAPE)
135-
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "Configuration Orientation Change: LANDSCAPE (" + orientation + ")");
135+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "Configuration Orientation Change: LANDSCAPE (" + orientation + ") on activity: " + activity);
136136
else if (orientation == Configuration.ORIENTATION_PORTRAIT)
137-
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "Configuration Orientation Change: PORTRAIT (" + orientation + ")");
137+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "Configuration Orientation Change: PORTRAIT (" + orientation + ") on activity: " + activity);
138138
}
139139

140140
/**
@@ -143,11 +143,11 @@ else if (orientation == Configuration.ORIENTATION_PORTRAIT)
143143
* This fix was originally implemented for In App Messages not being re-shown when orientation
144144
* was changed on wrapper SDK apps
145145
*/
146-
private void onOrientationChanged() {
146+
private void onOrientationChanged(Activity activity) {
147147
// Remove view
148148
handleLostFocus();
149149
for (Map.Entry<String, ActivityAvailableListener> entry : sActivityAvailableListeners.entrySet()) {
150-
entry.getValue().stopped();
150+
entry.getValue().stopped(activity);
151151
}
152152

153153
// Show view

OneSignalSDK/onesignal/src/main/java/com/onesignal/ActivityLifecycleListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static void registerActivityLifecycleCallbacks(@NonNull final Application applic
6161
configuration = new ComponentCallbacks() {
6262
@Override
6363
public void onConfigurationChanged(Configuration newConfig) {
64-
activityLifecycleHandler.onConfigurationChanged(newConfig);
64+
activityLifecycleHandler.onConfigurationChanged(newConfig, activityLifecycleHandler.getCurActivity());
6565
}
6666

6767
@Override

OneSignalSDK/onesignal/src/main/java/com/onesignal/WebViewManager.java

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.json.JSONObject;
1919

2020
import java.io.UnsupportedEncodingException;
21-
import java.lang.ref.WeakReference;
2221

2322
import static com.onesignal.OSViewUtils.dpToPx;
2423

@@ -64,7 +63,8 @@ boolean isBanner() {
6463
@NonNull private Activity activity;
6564
@NonNull private OSInAppMessage message;
6665

67-
private boolean firstShow = true;
66+
private String currentActivityName = null;
67+
private Integer lastPageHeight = null;
6868

6969
interface OneSignalGenericCallback {
7070
void onComplete();
@@ -275,40 +275,49 @@ private void calculateHeightAndShowWebViewAfterNewActivity() {
275275

276276
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "In app message new activity, calculate height and show ");
277277

278-
// Using post to ensure that the status bar inset is already added to the view
279-
OSViewUtils.decorViewReady(activity, new Runnable() {
280-
@Override
281-
public void run() {
282-
// At time point the webView isn't attached to a view
283-
// Set the WebView to the max screen size then run JS to evaluate the height.
284-
setWebViewToMaxSize(activity);
285-
webView.evaluateJavascript(OSJavaScriptInterface.GET_PAGE_META_DATA_JS_FUNCTION, new ValueCallback<String>() {
286-
@Override
287-
public void onReceiveValue(final String value) {
288-
try {
289-
int pagePxHeight = pageRectToViewHeight(activity, new JSONObject(value));
290-
showMessageView(pagePxHeight);
291-
} catch (JSONException e) {
292-
e.printStackTrace();
278+
// Using post to ensure that the status bar inset is already added to the view
279+
OSViewUtils.decorViewReady(activity, new Runnable() {
280+
@Override
281+
public void run() {
282+
// At time point the webView isn't attached to a view
283+
// Set the WebView to the max screen size then run JS to evaluate the height.
284+
setWebViewToMaxSize(activity);
285+
webView.evaluateJavascript(OSJavaScriptInterface.GET_PAGE_META_DATA_JS_FUNCTION, new ValueCallback<String>() {
286+
@Override
287+
public void onReceiveValue(final String value) {
288+
try {
289+
int pagePxHeight = pageRectToViewHeight(activity, new JSONObject(value));
290+
showMessageView(pagePxHeight);
291+
} catch (JSONException e) {
292+
e.printStackTrace();
293+
}
293294
}
294-
}
295-
});
296-
}
295+
});
296+
}
297297
});
298298
}
299299

300300
@Override
301301
void available(final @NonNull Activity activity) {
302+
String lastActivityName = this.currentActivityName;
302303
this.activity = activity;
303-
if (firstShow)
304+
this.currentActivityName = activity.getLocalClassName();
305+
306+
if (lastActivityName == null)
304307
showMessageView(null);
305-
else
308+
else if (!lastActivityName.equals(currentActivityName)) {
309+
// Navigate to new activity while displaying current IAM
310+
if (messageView != null)
311+
messageView.removeAllViews();
312+
showMessageView(lastPageHeight);
313+
} else
306314
calculateHeightAndShowWebViewAfterNewActivity();
307315
}
308316

309317
@Override
310-
void stopped() {
311-
if (messageView != null)
318+
void stopped(Activity activity) {
319+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "In app message activity stopped, cleaning views");
320+
if (messageView != null && currentActivityName.equals(activity.getLocalClassName()))
312321
messageView.removeAllViews();
313322
}
314323

@@ -327,8 +336,10 @@ private void showMessageView(@Nullable Integer newHeight) {
327336

328337
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "In app message, showing first one with height: " + newHeight);
329338
messageView.setWebView(webView);
330-
if (newHeight != null)
339+
if (newHeight != null) {
340+
lastPageHeight = newHeight;
331341
messageView.updateHeight(newHeight);
342+
}
332343
messageView.showView(activity);
333344
messageView.checkIfShouldDismiss();
334345
}
@@ -377,11 +388,11 @@ private void setWebViewToMaxSize(Activity activity) {
377388
}
378389

379390
private void createNewInAppMessageView(@NonNull Position displayLocation, int pageHeight, boolean dragToDismissDisabled) {
391+
lastPageHeight = pageHeight;
380392
messageView = new InAppMessageView(webView, displayLocation, pageHeight, message.getDisplayDuration(), dragToDismissDisabled);
381393
messageView.setMessageController(new InAppMessageView.InAppMessageViewListener() {
382394
@Override
383395
public void onMessageWasShown() {
384-
firstShow = false;
385396
OneSignal.getInAppMessageController().onMessageWasShown(message);
386397
}
387398

0 commit comments

Comments
 (0)