-
Notifications
You must be signed in to change notification settings - Fork 1
AB#251996 Status bar color warning on API 35+ #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,7 +36,6 @@ | |||||||||
| import androidx.annotation.AnyThread; | ||||||||||
| import androidx.annotation.NonNull; | ||||||||||
| import androidx.annotation.Nullable; | ||||||||||
| import androidx.annotation.RequiresApi; | ||||||||||
| import androidx.annotation.UiThread; | ||||||||||
|
|
||||||||||
| import com.optimove.android.BuildConfig; | ||||||||||
|
|
@@ -161,27 +160,46 @@ private void sendToClient(String type, JSONObject data) { | |||||||||
|
|
||||||||||
| String script = "window.postHostMessage(" + j.toString() + ")"; | ||||||||||
|
|
||||||||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||||||||||
| wv.evaluateJavascript(script, null); | ||||||||||
| } else { | ||||||||||
| wv.loadUrl("javascript:" + script); | ||||||||||
| wv.evaluateJavascript(script, null); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static boolean isLegacyStatusBarColorSupported() { | ||||||||||
| return Build.VERSION.SDK_INT < 35; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static int getStatusBarColorLegacy(Window window) { | ||||||||||
| if (!isLegacyStatusBarColorSupported() || window == null) { | ||||||||||
| return 0; | ||||||||||
| } | ||||||||||
| try { | ||||||||||
| java.lang.reflect.Method method = Window.class.getMethod("getStatusBarColor"); | ||||||||||
| Object result = method.invoke(window); | ||||||||||
| if (result instanceof Integer) { | ||||||||||
| return (Integer) result; | ||||||||||
| } | ||||||||||
| } catch (Throwable ignored) {} | ||||||||||
| return 0; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @UiThread | ||||||||||
| @SuppressWarnings("deprecation") | ||||||||||
| private void setStatusBarColorForDialog(Activity currentActivity) { | ||||||||||
| if (currentActivity == null) { | ||||||||||
| private static void setStatusBarColorLegacy(Window window, int color) { | ||||||||||
| if (!isLegacyStatusBarColorSupported() || window == null) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| try { | ||||||||||
| java.lang.reflect.Method method = Window.class.getMethod("setStatusBarColor", int.class); | ||||||||||
| method.invoke(window, color); | ||||||||||
| } catch (Throwable ignored) {} | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | ||||||||||
| @UiThread | ||||||||||
| private void setStatusBarColorForDialog(Activity currentActivity) { | ||||||||||
| if (currentActivity == null) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Window window = currentActivity.getWindow(); | ||||||||||
|
|
||||||||||
| prevStatusBarColor = window.getStatusBarColor(); | ||||||||||
| prevStatusBarColor = getStatusBarColorLegacy(window); | ||||||||||
|
|
||||||||||
| int flags = window.getAttributes().flags; | ||||||||||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { | ||||||||||
|
|
@@ -198,18 +216,17 @@ private void setStatusBarColorForDialog(Activity currentActivity) { | |||||||||
| statusBarColor = currentActivity.getResources().getColor(R.color.statusBarColorForNotch); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| window.setStatusBarColor(statusBarColor); | ||||||||||
| setStatusBarColorLegacy(window, statusBarColor); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @UiThread | ||||||||||
| @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||||||||||
| private void unsetStatusBarColorForDialog(Activity dialogActivity) { | ||||||||||
| if (dialogActivity == null) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Window window = dialogActivity.getWindow(); | ||||||||||
| window.setStatusBarColor(prevStatusBarColor); | ||||||||||
| setStatusBarColorLegacy(window, prevStatusBarColor); | ||||||||||
|
|
||||||||||
| if (prevFlagTranslucentStatus) { | ||||||||||
| window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | ||||||||||
|
|
@@ -225,7 +242,7 @@ private void closeDialog(Activity dialogActivity) { | |||||||||
| if (dialog != null) { | ||||||||||
| dialog.setOnKeyListener(null); | ||||||||||
| dialog.dismiss(); | ||||||||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||||||||||
| if (Build.VERSION.SDK_INT < 35) { | ||||||||||
| unsetStatusBarColorForDialog(dialogActivity); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -243,7 +260,7 @@ private void closeDialog(Activity dialogActivity) { | |||||||||
| @UiThread | ||||||||||
| private void showWebView(@NonNull final Activity currentActivity) { | ||||||||||
| try { | ||||||||||
| if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||||||||||
| if (BuildConfig.DEBUG) { | ||||||||||
| WebView.setWebContentsDebuggingEnabled(true); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -286,9 +303,7 @@ private void showWebView(@NonNull final Activity currentActivity) { | |||||||||
|
|
||||||||||
| WebSettings settings = wv.getSettings(); | ||||||||||
| settings.setJavaScriptEnabled(true); | ||||||||||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | ||||||||||
| settings.setMediaPlaybackRequiresUserGesture(false); | ||||||||||
| } | ||||||||||
| settings.setMediaPlaybackRequiresUserGesture(false); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the API level check for
Suggested change
Finding type:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SDK's minimal API is 21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it! I'll remember that the SDK's minimal API is 21 after the PR is merged. |
||||||||||
|
|
||||||||||
| wv.addJavascriptInterface(this, JS_NAME); | ||||||||||
| wv.setWebViewClient(this); | ||||||||||
|
|
@@ -315,7 +330,10 @@ private void closeCurrentMessage() { | |||||||||
| @Override | ||||||||||
| public void onPageFinished(WebView view, String url) { | ||||||||||
| view.setBackgroundColor(android.graphics.Color.TRANSPARENT); | ||||||||||
| setStatusBarColorForDialog(currentActivity); | ||||||||||
| if (Build.VERSION.SDK_INT < 35) { | ||||||||||
| setStatusBarColorForDialog(currentActivity); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| pageFinished = true; | ||||||||||
|
|
||||||||||
| sendCurrentMessageToClient(); | ||||||||||
|
|
@@ -324,7 +342,6 @@ public void onPageFinished(WebView view, String url) { | |||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| @TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||||||||||
| public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { | ||||||||||
| super.onReceivedHttpError(view, request, errorResponse); | ||||||||||
|
|
||||||||||
|
|
@@ -458,7 +475,7 @@ private void maybeSetNotchInsets(Context context) { | |||||||||
| } | ||||||||||
|
|
||||||||||
| List<Rect> cutoutBoundingRectangles = displayCutout.getBoundingRects(); | ||||||||||
| if (cutoutBoundingRectangles.size() == 0) { | ||||||||||
| if (cutoutBoundingRectangles.isEmpty()) { | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the API level check for
evaluateJavascriptcould cause crashes on devices below API 19. Should we keep the fallback toloadUrl?Finding type:
Logical BugsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SDK's minimal API is 21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! I'll remember that the SDK's minimal API is 21 after the PR is merged.