Skip to content
Merged
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 @@ -49,6 +49,11 @@
*/
public interface StatusBarService {

enum APPEARANCE {
LIGHT,
DARK
}

/**
* Returns an instance of {@link StatusBarService}.
* @return An instance of {@link StatusBarService}.
Expand All @@ -59,15 +64,17 @@ static Optional<StatusBarService> create() {

/**
* Sets the color of the status bar to the specified color.
* Up until Android 14
* @param color The color to set the status bar to.
*/
void setColor(Color color);


/**
* Sets the color of the status bar to the specified color, on Android 15+
* @param statusBarColor The color to set the status bar to.
* @param navigationBarColor The color to set the navigation bar to.
* Sets the appearance of the icons of the system bars to light or dark,
* so these have enough contrast and can be easily read
* Only applies to Android.
* @param statusBarAppearance the light or dark appearance of the status bar
* @param navigationBarAppearance the light or dark appearance of the navigation bar
*/
void setSystemBarsColor(Color statusBarColor, Color navigationBarColor);
void setSystemBarsAppearance(APPEARANCE statusBarAppearance, APPEARANCE navigationBarAppearance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void setColor(Color color) {
}

@Override
public void setSystemBarsColor(Color statusBarColor, Color navigationBarColor) {
setNativeSystemBarsColor(getIntColor(statusBarColor), getIntColor(navigationBarColor));
public void setSystemBarsAppearance(APPEARANCE statusBarAppearance, APPEARANCE navigationBarAppearance) {
setNativeSystemBarsAppearance(statusBarAppearance == APPEARANCE.DARK, navigationBarAppearance == APPEARANCE.DARK);
}

private static int getIntColor(Color color) {
Expand All @@ -59,6 +59,5 @@ private static int getIntColor(Color color) {
}

private native void setNativeColor(int color);
private native void setNativeSystemBarsColor(int statusBarColor, int navigationBarColor);

private native void setNativeSystemBarsAppearance(boolean darkStatusBar, boolean darkNavigationBar);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2025, Gluon
* Copyright (c) 2016, 2019, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -42,8 +42,8 @@ public void setColor(Color color) {
}

@Override
public void setSystemBarsColor(Color statusBarColor, Color navigationBarColor) {
// to-do
public void setSystemBarsAppearance(APPEARANCE statusBarAppearance, APPEARANCE navigationBarAppearance) {
// no-op
}

private native void setNativeColor(double red, double green, double blue, double opacity);
Expand Down
17 changes: 9 additions & 8 deletions modules/statusbar/src/main/native/android/c/statusbar.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Gluon
* Copyright (c) 2020, 2021, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -30,14 +30,14 @@
static jclass jStatusBarServiceClass;
static jobject jDalvikStatusBarService;
static jmethodID jStatusBarServiceColorMethod;
static jmethodID jSystemBarsServiceColorMethod;
static jmethodID jStatusBarServiceDarkAppearanceMethod;

static void initializeStatusBarDalvikHandles() {
jStatusBarServiceClass = GET_REGISTER_DALVIK_CLASS(jStatusBarServiceClass, "com/gluonhq/helloandroid/DalvikStatusBarService");
ATTACH_DALVIK();
jmethodID jStatusBarServiceInitMethod = (*dalvikEnv)->GetMethodID(dalvikEnv, jStatusBarServiceClass, "<init>", "(Landroid/app/Activity;)V");
jStatusBarServiceColorMethod = (*dalvikEnv)->GetMethodID(dalvikEnv, jStatusBarServiceClass, "setColor", "(I)V");
jSystemBarsServiceColorMethod = (*dalvikEnv)->GetMethodID(dalvikEnv, jStatusBarServiceClass, "setSystemBarsColor", "(II)V");
jStatusBarServiceDarkAppearanceMethod = (*dalvikEnv)->GetMethodID(dalvikEnv, jStatusBarServiceClass, "setSystemBarsAppearance", "(ZZ)V");

jobject jActivity = substrateGetActivity();
jobject jtmpobj = (*dalvikEnv)->NewObject(dalvikEnv, jStatusBarServiceClass, jStatusBarServiceInitMethod, jActivity);
Expand Down Expand Up @@ -81,13 +81,14 @@ JNIEXPORT void JNICALL Java_com_gluonhq_attach_statusbar_impl_AndroidStatusBarSe
DETACH_DALVIK();
}

JNIEXPORT void JNICALL Java_com_gluonhq_attach_statusbar_impl_AndroidStatusBarService_setNativeSystemBarsColor
(JNIEnv *env, jclass jClass, jint statusBarColor, jint navigationBarColor)
JNIEXPORT void JNICALL Java_com_gluonhq_attach_statusbar_impl_AndroidStatusBarService_setNativeSystemBarsAppearance
(JNIEnv *env, jclass jClass, jboolean darkStatusBar, jboolean darkNavigationBar)
{
ATTACH_DALVIK();
if (isDebugAttach()) {
ATTACH_LOG_FINE("Set native system bars color, values: %d %d", statusBarColor, navigationBarColor);
ATTACH_LOG_FINE("Set native status bar appearance dark: %s, navigation bar appearance dark: %s, ",
darkStatusBar ? "true" : "false", darkNavigationBar ? "true" : "false");
}
(*dalvikEnv)->CallVoidMethod(dalvikEnv, jDalvikStatusBarService, jSystemBarsServiceColorMethod, statusBarColor, navigationBarColor);
(*dalvikEnv)->CallVoidMethod(dalvikEnv, jDalvikStatusBarService, jStatusBarServiceDarkAppearanceMethod, darkStatusBar, darkNavigationBar);
DETACH_DALVIK();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowInsetsController;
import android.view.WindowManager;

import androidx.core.view.WindowCompat;

public class DalvikStatusBarService {

private static final String TAG = Util.TAG;
Expand All @@ -50,11 +50,7 @@ public DalvikStatusBarService(Activity activity) {

private void setColor(final int color) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // < 21
Log.e(TAG, "setColor is not supported for the current Android version");
return;
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // > 34
Log.e(TAG, "setColor is not supported for the current Android version. " +
"Use setSystemBarsColor instead");
Log.e(TAG, "StatusBar service is not supported for the current Android version");
return;
}
if (activity == null) {
Expand All @@ -70,19 +66,25 @@ private void setColor(final int color) {
@Override
public void run() {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// make status bar transparent
window.setStatusBarColor(0x00000000);
// paint window
window.setBackgroundDrawable(new ColorDrawable(color));
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // <= 34
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// make status bar transparent
window.setStatusBarColor(0x00000000);
// paint window
window.setBackgroundDrawable(new ColorDrawable(color));
} else { // >= 35
View decorView = window.getDecorView();

// Apply color
decorView.setBackground(new ColorDrawable(color));
}
}
});
}

private void setSystemBarsColor(final int statusBarColor, final int navigationBarColor) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // < 34
Log.e(TAG, "setSystemBarsColor is not supported for the current Android version. " +
"Use setColor instead");
private void setSystemBarsAppearance(final boolean darkStatusBar, final boolean darkNavigationBar) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // < 21
Log.e(TAG, "StatusBar service is not supported for the current Android version");
return;
}
if (activity == null) {
Expand All @@ -92,44 +94,17 @@ private void setSystemBarsColor(final int statusBarColor, final int navigationBa
}

if (Util.isDebug()) {
Log.v(TAG, "Set SystemBars color, values: " + statusBarColor + ", " + navigationBarColor);
Log.v(TAG, "Set StatusBar appearance: " + (darkStatusBar ? "dark" : "light") + ", NavigationBar appearance: " + (darkNavigationBar ? "dark" : "light"));
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Window window = activity.getWindow();
View decorView = window.getDecorView();

// Get insets, before applying the color, as it will reset them
int top = decorView.getPaddingTop();
int right = decorView.getPaddingRight();
int bottom = decorView.getPaddingBottom();
int left = decorView.getPaddingLeft();

WindowInsetsController windowInsetsController = decorView.getWindowInsetsController();
if (windowInsetsController != null) {
// background light color requires dark icons, dark light color, white icons
int bitset = WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS;
windowInsetsController.setSystemBarsAppearance(getLuma(statusBarColor) > 128 ?
bitset : 0, bitset);
}

// Apply colors
decorView.setBackground(new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{statusBarColor, navigationBarColor}));

// Restore insets
decorView.setPadding(left, top, right, bottom);
WindowCompat.getInsetsController(window, decorView).setAppearanceLightStatusBars(darkStatusBar);
WindowCompat.getInsetsController(window, decorView).setAppearanceLightNavigationBars(darkNavigationBar);
}
});
}

private static double getLuma(int color) {
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = (color >> 0) & 0xff;
return 0.2126 * r + 0.7152 * g + 0.0722 * b; // 0 darkest - 255 lightest
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation 'androidx.core:core:1.13.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apply plugin: 'com.android.library'

android {

namespace 'com.gluonhq.helloandroid'

defaultConfig {
minSdkVersion 21
compileSdk 35
targetSdkVersion 35
}

dependencies {
compileOnly fileTree(dir: '../libs', include: '*.jar')
implementation 'androidx.core:core:1.13.0'
}

buildFeatures {
buildConfig = false
resValues = false
}
}