Skip to content

Commit

Permalink
fix(status-bar): not working on older devices (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Jul 11, 2022
1 parent 32240b6 commit 4e34977
Showing 1 changed file with 21 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import com.getcapacitor.Logger;

public class StatusBar {

private final String logTag = "Capacitor-StatusBar";
private int currentStatusbarColor;
private AppCompatActivity activity;
private String defaultStyle;
private int currentStatusBarColor;
private final AppCompatActivity activity;
private final String defaultStyle;

public StatusBar(AppCompatActivity activity) {
// save initial color of the status bar
this.activity = activity;
this.currentStatusbarColor = activity.getWindow().getStatusBarColor();
this.currentStatusBarColor = activity.getWindow().getStatusBarColor();
this.defaultStyle = getStyle();
}

Expand All @@ -32,12 +31,8 @@ public void setStyle(String style) {
style = this.defaultStyle;
}

WindowInsetsControllerCompat windowInsetsControllerCompat = ViewCompat.getWindowInsetsController(decorView);
if (windowInsetsControllerCompat != null) {
windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals("DARK"));
} else {
Logger.warn(logTag, "Could not get a WindowInsetsControllerCompat instance. Can not set status bar style.");
}
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, decorView);
windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals("DARK"));
}

@SuppressWarnings("deprecation")
Expand All @@ -47,50 +42,42 @@ public void setBackgroundColor(int color) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color);
// update the local color field as well
currentStatusbarColor = color;
currentStatusBarColor = color;
}

public void hide() {
View decorView = activity.getWindow().getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = ViewCompat.getWindowInsetsController(decorView);
if (windowInsetsControllerCompat != null) {
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.statusBars());
} else {
Logger.warn(logTag, "Could not get a WindowInsetsControllerCompat instance. Can not hide the status bar.");
}
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(activity.getWindow(), decorView);
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.statusBars());
}

public void show() {
View decorView = activity.getWindow().getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = ViewCompat.getWindowInsetsController(decorView);
if (windowInsetsControllerCompat != null) {
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.statusBars());
} else {
Logger.warn(logTag, "Could not get a WindowInsetsControllerCompat instance. Can not show the status bar.");
}
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(activity.getWindow(), decorView);
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.statusBars());
}

@SuppressWarnings("deprecation")
public void setOverlaysWebView(Boolean overlays) {
View decorView = activity.getWindow().getDecorView();
int uiOptions = decorView.getSystemUiVisibility();
if (overlays) {
// Sets the layout to a fullscreen one that does not hide the actual status bar, so the webview is displayed behind it.
// Sets the layout to a fullscreen one that does not hide the actual status bar, so the WebView is displayed behind it.
uiOptions = uiOptions | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
currentStatusbarColor = activity.getWindow().getStatusBarColor();
currentStatusBarColor = activity.getWindow().getStatusBarColor();
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
} else {
// Sets the layout to a normal one that displays the webview below the status bar.
// Sets the layout to a normal one that displays the WebView below the status bar.
uiOptions = uiOptions & ~View.SYSTEM_UI_FLAG_LAYOUT_STABLE & ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// recover the previous color of the status bar
activity.getWindow().setStatusBarColor(currentStatusbarColor);
activity.getWindow().setStatusBarColor(currentStatusBarColor);
}
}

@SuppressWarnings("deprecation")
private boolean getIsOverlayed() {
private boolean getIsOverlaid() {
return (
(activity.getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) ==
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
Expand All @@ -103,7 +90,7 @@ public StatusBarInfo getInfo() {
boolean isVisible = windowInsetsCompat != null && windowInsetsCompat.isVisible(WindowInsetsCompat.Type.statusBars());
StatusBarInfo info = new StatusBarInfo();
info.setStyle(getStyle());
info.setOverlays(getIsOverlayed());
info.setOverlays(getIsOverlaid());
info.setVisible(isVisible);
info.setColor(String.format("#%06X", (0xFFFFFF & window.getStatusBarColor())));
return info;
Expand All @@ -112,15 +99,9 @@ public StatusBarInfo getInfo() {
private String getStyle() {
View decorView = activity.getWindow().getDecorView();
String style = "DARK";
WindowInsetsControllerCompat windowInsetsControllerCompat = ViewCompat.getWindowInsetsController(decorView);
if (windowInsetsControllerCompat != null) {
if (windowInsetsControllerCompat.isAppearanceLightStatusBars()) {
style = "LIGHT";
} else {
style = "DARK";
}
} else {
Logger.warn(logTag, "Could not get a WindowInsetsControllerCompat instance. Can not get the status bar appearance.");
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(activity.getWindow(), decorView);
if (windowInsetsControllerCompat.isAppearanceLightStatusBars()) {
style = "LIGHT";
}
return style;
}
Expand Down

0 comments on commit 4e34977

Please sign in to comment.