1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 package org.mozilla.gecko.util;
7 
8 import android.app.Activity;
9 import android.graphics.Color;
10 import android.os.Build;
11 import android.support.annotation.ColorInt;
12 import android.support.annotation.ColorRes;
13 import android.support.v4.content.ContextCompat;
14 import android.view.View;
15 import android.view.Window;
16 
17 import org.mozilla.gecko.GeckoApplication;
18 import org.mozilla.gecko.R;
19 import org.mozilla.gecko.lwt.LightweightTheme;
20 
21 public class WindowUtil {
22 
setTabsTrayStatusBarColor(final Activity activity)23     public static void setTabsTrayStatusBarColor(final Activity activity) {
24         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
25             return;
26         }
27         setStatusBarColorRes(activity, R.color.status_bar_bg_color_tabs_tray, true);
28     }
29 
setStatusBarColor(final Activity activity, final boolean isPrivate)30     public static void setStatusBarColor(final Activity activity, final boolean isPrivate) {
31         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
32             return;
33         }
34 
35         final @ColorInt int color;
36         final boolean isDarkTheme;
37 
38         if (HardwareUtils.isTablet()) {
39             color = ContextCompat.getColor(activity, R.color.status_bar_bg_color_tablet);
40             isDarkTheme = true;
41         } else {
42             LightweightTheme theme = ((GeckoApplication) activity.getApplication()).getLightweightTheme();
43             @ColorInt int themeColor = theme.getColor();
44             if (isPrivate) {
45                 color = ContextCompat.getColor(activity, R.color.status_bar_bg_color_private);
46                 isDarkTheme = true;
47             } else if (theme.isEnabled() && themeColor != Color.TRANSPARENT) {
48                 color = themeColor;
49                 isDarkTheme = !theme.isLightTheme();
50             } else {
51                 color = ContextCompat.getColor(activity, R.color.status_bar_bg_color);
52                 isDarkTheme = false;
53             }
54         }
55         setStatusBarColor(activity, color, isDarkTheme);
56     }
57 
setStatusBarColorRes(final Activity activity, final @ColorRes int colorResId, final boolean isDarkTheme)58     public static void setStatusBarColorRes(final Activity activity, final @ColorRes int colorResId,
59                                             final boolean isDarkTheme) {
60         final int backgroundColor = ContextCompat.getColor(activity, colorResId);
61         setStatusBarColor(activity, backgroundColor, isDarkTheme);
62     }
63 
setStatusBarColor(final Activity activity, final @ColorInt int backgroundColor, final boolean isDarkTheme)64     public static void setStatusBarColor(final Activity activity, final @ColorInt int backgroundColor,
65                                          final boolean isDarkTheme) {
66         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
67             return;
68         }
69 
70         final Window window = activity.getWindow();
71         window.setStatusBarColor(backgroundColor);
72 
73         final View view = window.getDecorView();
74         int flags = view.getSystemUiVisibility();
75         if (isDarkTheme) {
76             flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
77         } else {
78             flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
79         }
80         view.setSystemUiVisibility(flags);
81     }
82 }
83