3030import android .app .Activity ;
3131import android .content .Context ;
3232import android .graphics .drawable .ColorDrawable ;
33+ import android .graphics .drawable .GradientDrawable ;
3334import android .os .Build ;
3435import android .util .Log ;
3536import android .view .View ;
3637import android .view .Window ;
38+ import android .view .WindowInsetsController ;
3739import android .view .WindowManager ;
3840
3941public class DalvikStatusBarService {
@@ -48,7 +50,11 @@ public DalvikStatusBarService(Activity activity) {
4850
4951 private void setColor (final int color ) {
5052 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) { // < 21
51- Log .e (TAG , "StatusBar service is not supported for the current Android version" );
53+ Log .e (TAG , "setColor is not supported for the current Android version" );
54+ return ;
55+ } else if (Build .VERSION .SDK_INT > Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) { // > 34
56+ Log .e (TAG , "setColor is not supported for the current Android version. " +
57+ "Use setSystemBarsColor instead" );
5258 return ;
5359 }
5460 if (activity == null ) {
@@ -64,29 +70,66 @@ private void setColor(final int color) {
6470 @ Override
6571 public void run () {
6672 Window window = activity .getWindow ();
67- if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) { // <= 34
68- window .addFlags (WindowManager .LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
69- // make status bar transparent
70- window .setStatusBarColor (0x00000000 );
71- // paint window
72- window .setBackgroundDrawable (new ColorDrawable (color ));
73- } else { // >= 35
74- View decorView = window .getDecorView ();
75-
76- // Get insets, before applying the color, as it will reset them
77- int top = decorView .getPaddingTop ();
78- int right = decorView .getPaddingRight ();
79- int bottom = decorView .getPaddingBottom ();
80- int left = decorView .getPaddingLeft ();
81-
82- // Apply color
83- decorView .setBackground (new ColorDrawable (color ));
84-
85- // Restore insets
86- decorView .setPadding (left , top , right , bottom );
73+ window .addFlags (WindowManager .LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
74+ // make status bar transparent
75+ window .setStatusBarColor (0x00000000 );
76+ // paint window
77+ window .setBackgroundDrawable (new ColorDrawable (color ));
78+ }
79+ });
80+ }
81+
82+ private void setSystemBarsColor (final int statusBarColor , final int navigationBarColor ) {
83+ if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) { // < 34
84+ Log .e (TAG , "setSystemBarsColor is not supported for the current Android version. " +
85+ "Use setColor instead" );
86+ return ;
87+ }
88+ if (activity == null ) {
89+ Log .e (TAG , "FXActivity not found. This service is not allowed when "
90+ + "running in background mode or from wearable" );
91+ return ;
92+ }
93+
94+ if (Util .isDebug ()) {
95+ Log .v (TAG , "Set SystemBars color, values: " + statusBarColor + ", " + navigationBarColor );
96+ }
97+ activity .runOnUiThread (new Runnable () {
98+ @ Override
99+ public void run () {
100+ Window window = activity .getWindow ();
101+ View decorView = window .getDecorView ();
102+
103+ // Get insets, before applying the color, as it will reset them
104+ int top = decorView .getPaddingTop ();
105+ int right = decorView .getPaddingRight ();
106+ int bottom = decorView .getPaddingBottom ();
107+ int left = decorView .getPaddingLeft ();
108+
109+ WindowInsetsController windowInsetsController = decorView .getWindowInsetsController ();
110+ if (windowInsetsController != null ) {
111+ // background light color requires dark icons, dark light color, white icons
112+ int bitset = WindowInsetsController .APPEARANCE_LIGHT_STATUS_BARS ;
113+ windowInsetsController .setSystemBarsAppearance (getLuma (statusBarColor ) > 128 ?
114+ bitset : 0 , bitset );
87115 }
116+
117+ // Apply colors
118+ decorView .setBackground (new GradientDrawable (
119+ GradientDrawable .Orientation .TOP_BOTTOM ,
120+ new int []{statusBarColor , navigationBarColor }));
121+
122+ // Restore insets
123+ decorView .setPadding (left , top , right , bottom );
88124 }
89125 });
90126 }
91127
128+ private static double getLuma (int color ) {
129+ int r = (color >> 16 ) & 0xff ;
130+ int g = (color >> 8 ) & 0xff ;
131+ int b = (color >> 0 ) & 0xff ;
132+ return 0.2126 * r + 0.7152 * g + 0.0722 * b ; // 0 darkest - 255 lightest
133+ }
134+
92135}
0 commit comments