|
| 1 | +package com.getcapacitor.plugin; |
| 2 | + |
| 3 | +import android.content.pm.PackageInfo; |
| 4 | +import android.content.res.Configuration; |
| 5 | +import android.view.View; |
| 6 | +import android.view.Window; |
| 7 | +import androidx.core.graphics.Insets; |
| 8 | +import androidx.core.view.ViewCompat; |
| 9 | +import androidx.core.view.WindowCompat; |
| 10 | +import androidx.core.view.WindowInsetsCompat; |
| 11 | +import androidx.core.view.WindowInsetsControllerCompat; |
| 12 | +import androidx.webkit.WebViewCompat; |
| 13 | +import com.getcapacitor.Plugin; |
| 14 | +import com.getcapacitor.PluginCall; |
| 15 | +import com.getcapacitor.PluginMethod; |
| 16 | +import com.getcapacitor.annotation.CapacitorPlugin; |
| 17 | +import java.util.Locale; |
| 18 | +import java.util.regex.Matcher; |
| 19 | +import java.util.regex.Pattern; |
| 20 | + |
| 21 | +@CapacitorPlugin |
| 22 | +public class SystemBars extends Plugin { |
| 23 | + |
| 24 | + static final String STYLE_LIGHT = "LIGHT"; |
| 25 | + static final String STYLE_DARK = "DARK"; |
| 26 | + static final String STYLE_DEFAULT = "DEFAULT"; |
| 27 | + static final String BAR_STATUS_BAR = "StatusBar"; |
| 28 | + static final String BAR_GESTURE_BAR = "NavigationBar"; |
| 29 | + |
| 30 | + static final String viewportMetaJSFunction = """ |
| 31 | + function capacitorSystemBarsCheckMetaViewport() { |
| 32 | + const meta = document.querySelectorAll("meta[name=viewport]"); |
| 33 | + if (meta.length == 0) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + // get the last found meta viewport tag |
| 37 | + const metaContent = meta[meta.length - 1].content; |
| 38 | + return metaContent.includes("viewport-fit=cover"); |
| 39 | + } |
| 40 | +
|
| 41 | + capacitorSystemBarsCheckMetaViewport(); |
| 42 | + """; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void load() { |
| 46 | + super.load(); |
| 47 | + initSystemBars(); |
| 48 | + } |
| 49 | + |
| 50 | + private boolean hasFixedWebView() { |
| 51 | + PackageInfo packageInfo = WebViewCompat.getCurrentWebViewPackage(bridge.getContext()); |
| 52 | + Pattern pattern = Pattern.compile("(\\d+)"); |
| 53 | + Matcher matcher = pattern.matcher(packageInfo.versionName); |
| 54 | + |
| 55 | + if (!matcher.find()) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + String majorVersionStr = matcher.group(0); |
| 60 | + int majorVersion = Integer.parseInt(majorVersionStr); |
| 61 | + |
| 62 | + return majorVersion >= 140; |
| 63 | + } |
| 64 | + |
| 65 | + private void initSystemBars() { |
| 66 | + String style = getConfig().getString("style", STYLE_DEFAULT).toUpperCase(Locale.US); |
| 67 | + boolean hidden = getConfig().getBoolean("hidden", false); |
| 68 | + boolean disableCSSInsets = getConfig().getBoolean("disableInsets", false); |
| 69 | + |
| 70 | + this.bridge.getWebView().evaluateJavascript(viewportMetaJSFunction, (res) -> { |
| 71 | + boolean hasMetaViewportCover = res.equals("true"); |
| 72 | + if (!disableCSSInsets) { |
| 73 | + setupSafeAreaInsets(this.hasFixedWebView(), hasMetaViewportCover); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + getBridge().executeOnMainThread(() -> { |
| 78 | + setStyle(style, ""); |
| 79 | + setHidden(hidden, ""); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @PluginMethod |
| 84 | + public void setStyle(final PluginCall call) { |
| 85 | + String bar = call.getString("bar", ""); |
| 86 | + String style = call.getString("style", STYLE_DEFAULT); |
| 87 | + |
| 88 | + getBridge().executeOnMainThread(() -> { |
| 89 | + setStyle(style, bar); |
| 90 | + call.resolve(); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @PluginMethod |
| 95 | + public void show(final PluginCall call) { |
| 96 | + String bar = call.getString("bar", ""); |
| 97 | + |
| 98 | + getBridge().executeOnMainThread(() -> { |
| 99 | + setHidden(false, bar); |
| 100 | + call.resolve(); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + @PluginMethod |
| 105 | + public void hide(final PluginCall call) { |
| 106 | + String bar = call.getString("bar", ""); |
| 107 | + |
| 108 | + getBridge().executeOnMainThread(() -> { |
| 109 | + setHidden(true, bar); |
| 110 | + call.resolve(); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + @PluginMethod |
| 115 | + public void setAnimation(final PluginCall call) { |
| 116 | + call.resolve(); |
| 117 | + } |
| 118 | + |
| 119 | + private void setupSafeAreaInsets(boolean hasFixedWebView, boolean hasMetaViewportCover) { |
| 120 | + ViewCompat.setOnApplyWindowInsetsListener((View) getBridge().getWebView().getParent(), (v, insets) -> { |
| 121 | + if (hasFixedWebView && hasMetaViewportCover) { |
| 122 | + return insets; |
| 123 | + } |
| 124 | + |
| 125 | + Insets safeArea = insets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout()); |
| 126 | + injectSafeAreaCSS(safeArea.top, safeArea.right, safeArea.bottom, safeArea.left); |
| 127 | + |
| 128 | + return WindowInsetsCompat.CONSUMED; |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + private void injectSafeAreaCSS(int top, int right, int bottom, int left) { |
| 133 | + // Convert pixels to density-independent pixels |
| 134 | + float density = getActivity().getResources().getDisplayMetrics().density; |
| 135 | + float topPx = top / density; |
| 136 | + float rightPx = right / density; |
| 137 | + float bottomPx = bottom / density; |
| 138 | + float leftPx = left / density; |
| 139 | + |
| 140 | + // Execute JavaScript to inject the CSS |
| 141 | + getBridge().executeOnMainThread(() -> { |
| 142 | + if (bridge != null && bridge.getWebView() != null) { |
| 143 | + String script = String.format( |
| 144 | + Locale.US, |
| 145 | + """ |
| 146 | + try { |
| 147 | + document.documentElement.style.setProperty("--safe-area-inset-top", "%dpx"); |
| 148 | + document.documentElement.style.setProperty("--safe-area-inset-right", "%dpx"); |
| 149 | + document.documentElement.style.setProperty("--safe-area-inset-bottom", "%dpx"); |
| 150 | + document.documentElement.style.setProperty("--safe-area-inset-left", "%dpx"); |
| 151 | + } catch(e) { console.error('Error injecting safe area CSS:', e); } |
| 152 | + """, |
| 153 | + (int) topPx, |
| 154 | + (int) rightPx, |
| 155 | + (int) bottomPx, |
| 156 | + (int) leftPx |
| 157 | + ); |
| 158 | + |
| 159 | + bridge.getWebView().evaluateJavascript(script, null); |
| 160 | + } |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + private void setStyle(String style, String bar) { |
| 165 | + if (style.equals(STYLE_DEFAULT)) { |
| 166 | + style = getStyleForTheme(); |
| 167 | + } |
| 168 | + |
| 169 | + Window window = getActivity().getWindow(); |
| 170 | + WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, window.getDecorView()); |
| 171 | + if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) { |
| 172 | + windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals(STYLE_DARK)); |
| 173 | + } |
| 174 | + |
| 175 | + if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) { |
| 176 | + windowInsetsControllerCompat.setAppearanceLightNavigationBars(!style.equals(STYLE_DARK)); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void setHidden(boolean hide, String bar) { |
| 181 | + Window window = getActivity().getWindow(); |
| 182 | + WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, window.getDecorView()); |
| 183 | + |
| 184 | + if (hide) { |
| 185 | + if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) { |
| 186 | + windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.statusBars()); |
| 187 | + } |
| 188 | + if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) { |
| 189 | + windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.navigationBars()); |
| 190 | + } |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) { |
| 195 | + windowInsetsControllerCompat.show(WindowInsetsCompat.Type.systemBars()); |
| 196 | + } |
| 197 | + if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) { |
| 198 | + windowInsetsControllerCompat.show(WindowInsetsCompat.Type.navigationBars()); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private String getStyleForTheme() { |
| 203 | + int currentNightMode = getActivity().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; |
| 204 | + if (currentNightMode != Configuration.UI_MODE_NIGHT_YES) { |
| 205 | + return STYLE_LIGHT; |
| 206 | + } |
| 207 | + return STYLE_DARK; |
| 208 | + } |
| 209 | +} |
0 commit comments