21
21
import java .io .UnsupportedEncodingException ;
22
22
23
23
import static com .onesignal .OSViewUtils .dpToPx ;
24
+ import static com .onesignal .OSViewUtils .getFullbleedWindowWidth ;
24
25
25
26
// Manages WebView instances by pre-loading them, displaying them, and closing them when dismissed.
26
27
// Includes a static map for pre-loading, showing, and dismissed so these events can't be duplicated.
@@ -132,7 +133,20 @@ static void dismissCurrentInAppMessage() {
132
133
}
133
134
}
134
135
135
- private static void initInAppMessage (@ NonNull final Activity currentActivity , @ NonNull OSInAppMessageInternal message , @ NonNull OSInAppMessageContent content ) {
136
+ private static void setContentSafeAreaInsets (OSInAppMessageContent content , @ NonNull final Activity activity ) {
137
+ String html = content .getContentHtml ();
138
+ String safeAreaInsetsScript = OSJavaScriptInterface .SET_SAFE_AREA_INSETS_SCRIPT ;
139
+ int [] insets = OSViewUtils .getCutoutAndStatusBarInsets (activity );
140
+ String safeAreaJSObject = String .format (OSJavaScriptInterface .SAFE_AREA_JS_OBJECT , insets [0 ] ,insets [1 ],insets [2 ],insets [3 ]);
141
+ safeAreaInsetsScript = String .format (safeAreaInsetsScript , safeAreaJSObject );
142
+ html += safeAreaInsetsScript ;
143
+ content .setContentHtml (html );
144
+ }
145
+
146
+ private static void initInAppMessage (@ NonNull final Activity currentActivity , @ NonNull OSInAppMessageInternal message , @ NonNull final OSInAppMessageContent content ) {
147
+ if (content .isFullBleed ()) {
148
+ setContentSafeAreaInsets (content , currentActivity );
149
+ }
136
150
try {
137
151
final String base64Str = Base64 .encodeToString (
138
152
content .getContentHtml ().getBytes ("UTF-8" ),
@@ -148,7 +162,7 @@ private static void initInAppMessage(@NonNull final Activity currentActivity, @N
148
162
public void run () {
149
163
// Handles exception "MissingWebViewPackageException: Failed to load WebView provider: No WebView installed"
150
164
try {
151
- webViewManager .setupWebView (currentActivity , base64Str );
165
+ webViewManager .setupWebView (currentActivity , base64Str , content . isFullBleed () );
152
166
} catch (Exception e ) {
153
167
// Need to check error message to only catch MissingWebViewPackageException as it isn't public
154
168
if (e .getMessage () != null && e .getMessage ().contains ("No WebView installed" )) {
@@ -170,9 +184,21 @@ class OSJavaScriptInterface {
170
184
171
185
static final String JS_OBJ_NAME = "OSAndroid" ;
172
186
static final String GET_PAGE_META_DATA_JS_FUNCTION = "getPageMetaData()" ;
187
+ static final String SET_SAFE_AREA_INSETS_JS_FUNCTION = "setSafeAreaInsets(%s)" ;
188
+ static final String SAFE_AREA_JS_OBJECT = "{\n " +
189
+ " top: %d,\n " +
190
+ " bottom: %d,\n " +
191
+ " right: %d,\n " +
192
+ " left: %d,\n " +
193
+ "}" ;
194
+ static final String SET_SAFE_AREA_INSETS_SCRIPT = "\n \n " +
195
+ "<script>\n " +
196
+ " setSafeAreaInsets(%s);\n " +
197
+ "</script>" ;
173
198
174
199
static final String EVENT_TYPE_KEY = "type" ;
175
200
static final String EVENT_TYPE_RENDERING_COMPLETE = "rendering_complete" ;
201
+ static final String EVENT_TYPE_RESIZE = "resize" ;
176
202
static final String EVENT_TYPE_ACTION_TAKEN = "action_taken" ;
177
203
static final String EVENT_TYPE_PAGE_CHANGE = "page_change" ;
178
204
@@ -197,6 +223,8 @@ public void postMessage(String message) {
197
223
if (!messageView .isDragging ())
198
224
handleActionTaken (jsonObject );
199
225
break ;
226
+ case EVENT_TYPE_RESIZE :
227
+ break ;
200
228
case EVENT_TYPE_PAGE_CHANGE :
201
229
handlePageChange (jsonObject );
202
230
break ;
@@ -219,7 +247,7 @@ private void handleRenderComplete(JSONObject jsonObject) {
219
247
220
248
private int getPageHeightData (JSONObject jsonObject ) {
221
249
try {
222
- return WebViewManager . pageRectToViewHeight (activity , jsonObject .getJSONObject (IAM_PAGE_META_DATA_KEY ));
250
+ return pageRectToViewHeight (activity , jsonObject .getJSONObject (IAM_PAGE_META_DATA_KEY ));
223
251
} catch (JSONException e ) {
224
252
return -1 ;
225
253
}
@@ -266,7 +294,7 @@ private void handlePageChange(JSONObject jsonObject) throws JSONException {
266
294
}
267
295
}
268
296
269
- private static int pageRectToViewHeight (final @ NonNull Activity activity , @ NonNull JSONObject jsonObject ) {
297
+ private int pageRectToViewHeight (final @ NonNull Activity activity , @ NonNull JSONObject jsonObject ) {
270
298
try {
271
299
int pageHeight = jsonObject .getJSONObject ("rect" ).getInt ("height" );
272
300
int pxHeight = OSViewUtils .dpToPx (pageHeight );
@@ -285,14 +313,26 @@ private static int pageRectToViewHeight(final @NonNull Activity activity, @NonNu
285
313
}
286
314
}
287
315
316
+ private void updateSafeAreaInsets () {
317
+ OSUtils .runOnMainUIThread (new Runnable () {
318
+ @ Override
319
+ public void run () {
320
+ int [] insets = OSViewUtils .getCutoutAndStatusBarInsets (activity );
321
+ String safeAreaInsetsObject = String .format (OSJavaScriptInterface .SAFE_AREA_JS_OBJECT , insets [0 ], insets [1 ], insets [2 ], insets [3 ]);
322
+ String safeAreaInsetsFunction = String .format (OSJavaScriptInterface .SET_SAFE_AREA_INSETS_JS_FUNCTION , safeAreaInsetsObject );
323
+ webView .evaluateJavascript (safeAreaInsetsFunction , null );
324
+ }
325
+ });
326
+ }
327
+
288
328
// Every time an Activity is shown we update the height of the WebView since the available
289
329
// screen size may have changed. (Expect for Fullscreen)
290
330
private void calculateHeightAndShowWebViewAfterNewActivity () {
291
331
if (messageView == null )
292
332
return ;
293
333
294
- // Don't need a CSS / HTML height update for fullscreen
295
- if (messageView .getDisplayPosition () == Position .FULL_SCREEN ) {
334
+ // Don't need a CSS / HTML height update for fullscreen unless its fullbleed
335
+ if (messageView .getDisplayPosition () == Position .FULL_SCREEN && ! messageContent . isFullBleed () ) {
296
336
showMessageView (null );
297
337
return ;
298
338
}
@@ -306,6 +346,10 @@ public void run() {
306
346
// At time point the webView isn't attached to a view
307
347
// Set the WebView to the max screen size then run JS to evaluate the height.
308
348
setWebViewToMaxSize (activity );
349
+ if (messageContent .isFullBleed ()) {
350
+ updateSafeAreaInsets ();
351
+ }
352
+
309
353
webView .evaluateJavascript (OSJavaScriptInterface .GET_PAGE_META_DATA_JS_FUNCTION , new ValueCallback <String >() {
310
354
@ Override
311
355
public void onReceiveValue (final String value ) {
@@ -373,7 +417,7 @@ private void showMessageView(@Nullable Integer newHeight) {
373
417
}
374
418
375
419
@ SuppressLint ({"SetJavaScriptEnabled" , "AddJavascriptInterface" })
376
- private void setupWebView (@ NonNull final Activity currentActivity , final @ NonNull String base64Message ) {
420
+ private void setupWebView (@ NonNull final Activity currentActivity , final @ NonNull String base64Message , final boolean isFullScreen ) {
377
421
enableWebViewRemoteDebugging ();
378
422
379
423
webView = new OSWebView (currentActivity );
@@ -385,7 +429,14 @@ private void setupWebView(@NonNull final Activity currentActivity, final @NonNul
385
429
386
430
// Setup receiver for page events / data from JS
387
431
webView .addJavascriptInterface (new OSJavaScriptInterface (), OSJavaScriptInterface .JS_OBJ_NAME );
388
-
432
+ if (isFullScreen ) {
433
+ webView .setSystemUiVisibility (View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
434
+ View .SYSTEM_UI_FLAG_IMMERSIVE |
435
+ View .SYSTEM_UI_FLAG_HIDE_NAVIGATION );
436
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
437
+ webView .setFitsSystemWindows (false );
438
+ }
439
+ }
389
440
blurryRenderingWebViewForKitKatWorkAround (webView );
390
441
391
442
OSViewUtils .decorViewReady (currentActivity , new Runnable () {
@@ -457,12 +508,17 @@ private static void enableWebViewRemoteDebugging() {
457
508
}
458
509
}
459
510
460
- private static int getWebViewMaxSizeX (Activity activity ) {
461
- return OSViewUtils .getWindowWidth (activity ) - (MARGIN_PX_SIZE * 2 );
511
+ private int getWebViewMaxSizeX (Activity activity ) {
512
+ if (messageContent .isFullBleed ()) {
513
+ return getFullbleedWindowWidth (activity );
514
+ }
515
+ int margin = (MARGIN_PX_SIZE * 2 );
516
+ return OSViewUtils .getWindowWidth (activity ) - margin ;
462
517
}
463
518
464
- private static int getWebViewMaxSizeY (Activity activity ) {
465
- return OSViewUtils .getWindowHeight (activity ) - (MARGIN_PX_SIZE * 2 );
519
+ private int getWebViewMaxSizeY (Activity activity ) {
520
+ int margin = messageContent .isFullBleed () ? 0 : (MARGIN_PX_SIZE * 2 );
521
+ return OSViewUtils .getWindowHeight (activity ) - margin ;
466
522
}
467
523
468
524
private void removeActivityListener () {
0 commit comments