Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.webkit.WebViewCompat;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
Expand Down Expand Up @@ -157,6 +158,17 @@ public void setStyle(final PluginCall call) {
});
}

@PluginMethod
public void getStyle(final PluginCall call) {
String bar = call.getString("bar", BAR_STATUS_BAR);

getBridge().executeOnMainThread(() -> {
JSObject ret = new JSObject();
ret.put("style", bar.equals(BAR_GESTURE_BAR) ? currentGestureBarStyle : currentStatusBarStyle);
call.resolve(ret);
});
}

@PluginMethod
public void show(final PluginCall call) {
String bar = call.getString("bar", "");
Expand Down Expand Up @@ -299,20 +311,18 @@ private void injectSafeAreaCSS(WindowInsetsCompat insets) {
}

private void setStyle(String style, String bar) {
if (style.equals(STYLE_DEFAULT)) {
style = getStyleForTheme();
}
String resolvedStyle = style.equals(STYLE_DEFAULT) ? getStyleForTheme() : style;

Window window = getActivity().getWindow();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, window.getDecorView());
if (bar.isEmpty() || bar.equals(BAR_STATUS_BAR)) {
currentStatusBarStyle = style;
windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals(STYLE_DARK));
windowInsetsControllerCompat.setAppearanceLightStatusBars(!resolvedStyle.equals(STYLE_DARK));
}

if (bar.isEmpty() || bar.equals(BAR_GESTURE_BAR)) {
currentGestureBarStyle = style;
windowInsetsControllerCompat.setAppearanceLightNavigationBars(!style.equals(STYLE_DARK));
windowInsetsControllerCompat.setAppearanceLightNavigationBars(!resolvedStyle.equals(STYLE_DARK));
}

getActivity().getWindow().getDecorView().setBackgroundColor(getThemeColor(getContext(), android.R.attr.windowBackground));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.getcapacitor.plugin;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -15,8 +22,12 @@
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import com.getcapacitor.Bridge;
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import java.lang.reflect.Method;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;

public class SystemBarsTest {
Expand Down Expand Up @@ -48,6 +59,111 @@ public void showWithNavigationBarShowsOnlyNavigationBars() throws Exception {
verify(controller, never()).show(WindowInsetsCompat.Type.statusBars());
}

@Test
public void getStyleReturnsDefaultAfterSettingDefault() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("DEFAULT", "");

verify(fixture.controller).setAppearanceLightStatusBars(true);
assertEquals("DEFAULT", fixture.getStyle("StatusBar"));
assertEquals("DEFAULT", fixture.getStyle("NavigationBar"));
}
}

@Test
public void getStyleReturnsStyleOfRequestedBar() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("LIGHT", "");
fixture.setStyle("DARK", "NavigationBar");

assertEquals("LIGHT", fixture.getStyle("StatusBar"));
assertEquals("DARK", fixture.getStyle("NavigationBar"));
}
}

@Test
public void getStyleDefaultsToStatusBar() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("DARK", "StatusBar");

assertEquals("DARK", fixture.getStyle(null));
}
}

@Test
public void configurationChangeReappliesDefaultStyleForNewTheme() throws Exception {
try (StyleFixture fixture = new StyleFixture()) {
fixture.setStyle("DEFAULT", "");
verify(fixture.controller).setAppearanceLightStatusBars(true);
verify(fixture.controller).setAppearanceLightNavigationBars(true);

fixture.configuration.uiMode = Configuration.UI_MODE_NIGHT_YES;
fixture.plugin.handleOnConfigurationChanged(fixture.configuration);

verify(fixture.controller).setAppearanceLightStatusBars(false);
verify(fixture.controller).setAppearanceLightNavigationBars(false);
}
}

private static class StyleFixture implements AutoCloseable {

final SystemBars plugin = new SystemBars();
final Configuration configuration = mock(Configuration.class);
final WindowInsetsControllerCompat controller = mock(WindowInsetsControllerCompat.class);
private final MockedStatic<WindowCompat> windowCompat = mockStatic(WindowCompat.class);
private final MockedConstruction<TypedValue> typedValues = mockConstruction(TypedValue.class);

StyleFixture() {
Bridge bridge = mock(Bridge.class);
AppCompatActivity activity = mock(AppCompatActivity.class);
Resources resources = mock(Resources.class);
Context context = mock(Context.class);
Window window = mock(Window.class);
View decorView = mock(View.class);

configuration.uiMode = Configuration.UI_MODE_NIGHT_NO;
when(bridge.getActivity()).thenReturn(activity);
when(bridge.getContext()).thenReturn(context);
when(context.getTheme()).thenReturn(mock(Resources.Theme.class));
when(activity.getResources()).thenReturn(resources);
when(resources.getConfiguration()).thenReturn(configuration);
when(activity.getWindow()).thenReturn(window);
when(window.getDecorView()).thenReturn(decorView);
doAnswer((invocation) -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(bridge)
.executeOnMainThread(any(Runnable.class));
windowCompat.when(() -> WindowCompat.getInsetsController(window, decorView)).thenReturn(controller);

plugin.setBridge(bridge);
}

void setStyle(String style, String bar) throws Exception {
Method setStyle = SystemBars.class.getDeclaredMethod("setStyle", String.class, String.class);
setStyle.setAccessible(true);
setStyle.invoke(plugin, style, bar);
}

String getStyle(String bar) {
PluginCall call = mock(PluginCall.class);
when(call.getString("bar", "StatusBar")).thenReturn(bar == null ? "StatusBar" : bar);

plugin.getStyle(call);

ArgumentCaptor<JSObject> result = ArgumentCaptor.forClass(JSObject.class);
verify(call).resolve(result.capture());
return result.getValue().getString("style");
}

@Override
public void close() {
typedValues.close();
windowCompat.close();
}
}

@Test
public void bottomInsetIsNavigationBarWhenKeyboardHidden() throws Exception {
assertEquals(126, invokeGetBottomInset(126, 0, false));
Expand Down
53 changes: 52 additions & 1 deletion core/src/core-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin } from './definitions';
import type { Plugin, PluginListenerHandle } from './definitions';
import { registerPlugin } from './global';
import { WebPlugin } from './web-plugin';

Expand Down Expand Up @@ -604,6 +604,27 @@ export interface SystemBarsAnimationOptions {
animation: SystemBarsAnimation;
}

export interface SystemBarsGetStyleOptions {
/**
* The system bar to get the style of.
*
* On iOS, only the status bar has a style, so this option is ignored.
*
* @since 9.0.0
* @default 'StatusBar'
*/
bar?: SystemBarType;
}

export interface SystemBarsGetStyleResult {
/**
* The style last set for the system bar, using `setStyle()` or the `style` configuration value.
*
* @since 9.0.0
*/
style: SystemBarsStyle;
}

export interface SystemBarsPlugin {
/**
* Set the current style of the system bars.
Expand All @@ -612,6 +633,16 @@ export interface SystemBarsPlugin {
*/
setStyle(options: SystemBarsStyleOptions): Promise<void>;

/**
* Get the current style of a system bar.
*
* Returns the style that was requested, so `DEFAULT` is returned as `DEFAULT`
* rather than the `LIGHT` or `DARK` style it currently resolves to.
*
* @since 9.0.0
*/
getStyle(options?: SystemBarsGetStyleOptions): Promise<SystemBarsGetStyleResult>;

/**
* Show the system bars.
*
Expand All @@ -634,13 +665,33 @@ export interface SystemBarsPlugin {
* @since 8.0.0
*/
setAnimation(options: SystemBarsAnimationOptions): Promise<void>;

/**
* Listen for taps on the status bar.
*
* Only available on iOS.
*
* @since 9.0.0
*/
addListener(eventName: 'statusTap', listenerFunc: () => void): Promise<PluginListenerHandle>;

/**
* Remove all listeners for this plugin.
*
* @since 9.0.0
*/
removeAllListeners(): Promise<void>;
}

export class SystemBarsPluginWeb extends WebPlugin implements SystemBarsPlugin {
async setStyle(): Promise<void> {
this.unavailable('not available for web');
}

async getStyle(): Promise<SystemBarsGetStyleResult> {
throw this.unavailable('not available for web');
}

async setAnimation(): Promise<void> {
this.unavailable('not available for web');
}
Expand Down
Loading