|
| 1 | +package com.play.library_base.utils; |
| 2 | + |
| 3 | +import android.annotation.TargetApi; |
| 4 | +import android.app.Activity; |
| 5 | +import android.os.Build; |
| 6 | +import android.view.View; |
| 7 | +import android.view.Window; |
| 8 | +import android.view.WindowManager; |
| 9 | + |
| 10 | +import java.lang.reflect.Field; |
| 11 | +import java.lang.reflect.Method; |
| 12 | + |
| 13 | +public class LightStatusBarUtils { |
| 14 | + public static void setLightStatusBarAboveAPI23(Activity activity, boolean isMarginStatusBar |
| 15 | + , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean dark) { |
| 16 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 17 | + setLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public static void setLightStatusBar(Activity activity, boolean dark) { |
| 22 | + setLightStatusBar(activity, false, false, false, dark); |
| 23 | + } |
| 24 | + |
| 25 | + public static void setLightStatusBar(Activity activity, boolean isMarginStatusBar |
| 26 | + , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean dark) { |
| 27 | + switch (RomUtils.getLightStatausBarAvailableRomType()) { |
| 28 | + case RomUtils.AvailableRomType.MIUI: |
| 29 | + if (RomUtils.getMIUIVersionCode() >= 7) { |
| 30 | + setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark); |
| 31 | + } else { |
| 32 | + setMIUILightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark); |
| 33 | + } |
| 34 | + break; |
| 35 | + |
| 36 | + case RomUtils.AvailableRomType.FLYME: |
| 37 | + setFlymeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark); |
| 38 | + break; |
| 39 | + |
| 40 | + case RomUtils.AvailableRomType.ANDROID_NATIVE: |
| 41 | + setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark); |
| 42 | + break; |
| 43 | + |
| 44 | + case RomUtils.AvailableRomType.NA: |
| 45 | + // N/A do nothing |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + private static boolean setMIUILightStatusBar(Activity activity, boolean isMarginStatusBar |
| 52 | + , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean darkmode) { |
| 53 | + initStatusBarStyle(activity, isMarginStatusBar, isMarginNavigationBar); |
| 54 | + |
| 55 | + Class<? extends Window> clazz = activity.getWindow().getClass(); |
| 56 | + try { |
| 57 | + int darkModeFlag = 0; |
| 58 | + Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); |
| 59 | + Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); |
| 60 | + darkModeFlag = field.getInt(layoutParams); |
| 61 | + Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); |
| 62 | + extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag); |
| 63 | + return true; |
| 64 | + } catch (Exception e) { |
| 65 | + setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, darkmode); |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean setFlymeLightStatusBar(Activity activity, boolean isMarginStatusBar |
| 71 | + , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean dark) { |
| 72 | + boolean result = false; |
| 73 | + if (activity != null) { |
| 74 | + initStatusBarStyle(activity, isMarginStatusBar, isMarginNavigationBar); |
| 75 | + try { |
| 76 | + WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); |
| 77 | + Field darkFlag = WindowManager.LayoutParams.class |
| 78 | + .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); |
| 79 | + Field meizuFlags = WindowManager.LayoutParams.class |
| 80 | + .getDeclaredField("meizuFlags"); |
| 81 | + darkFlag.setAccessible(true); |
| 82 | + meizuFlags.setAccessible(true); |
| 83 | + int bit = darkFlag.getInt(null); |
| 84 | + int value = meizuFlags.getInt(lp); |
| 85 | + if (dark) { |
| 86 | + value |= bit; |
| 87 | + } else { |
| 88 | + value &= ~bit; |
| 89 | + } |
| 90 | + meizuFlags.setInt(lp, value); |
| 91 | + activity.getWindow().setAttributes(lp); |
| 92 | + result = true; |
| 93 | + |
| 94 | + if (RomUtils.getFlymeVersion() >= 7) { |
| 95 | + setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark); |
| 96 | + |
| 97 | +// if (dark && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 98 | +// activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 99 | +//// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 100 | +// | View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
| 101 | +// | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| 102 | +// } else { |
| 103 | +// activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 104 | +//// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 105 | +// | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 106 | +// } |
| 107 | + } |
| 108 | + } catch (Exception e) { |
| 109 | + setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark); |
| 110 | + } |
| 111 | + } |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + @TargetApi(11) |
| 116 | + private static void setAndroidNativeLightStatusBar(Activity activity, boolean isMarginStatusBar |
| 117 | + , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean isDarkStatusBarIcon) { |
| 118 | + |
| 119 | + try { |
| 120 | + if (isTransStatusBar) { |
| 121 | + Window window = activity.getWindow(); |
| 122 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 123 | + if (isMarginStatusBar && isMarginNavigationBar) { |
| 124 | + //5.0版本及以上 |
| 125 | + if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 126 | + window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
| 127 | + | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| 128 | + } else { |
| 129 | + window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 130 | + } |
| 131 | + } else if (!isMarginStatusBar && !isMarginNavigationBar) { |
| 132 | + |
| 133 | + if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 134 | + window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 135 | +// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 136 | + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
| 137 | + | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| 138 | + } else { |
| 139 | + window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 140 | +// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 141 | + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + } else if (!isMarginStatusBar && isMarginNavigationBar) { |
| 146 | + if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 147 | + window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 148 | + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
| 149 | + | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| 150 | + } else { |
| 151 | + window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 152 | + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + } else { |
| 157 | + //留出来状态栏 不留出来导航栏 没找到办法。。 |
| 158 | + return; |
| 159 | + } |
| 160 | + } |
| 161 | + } else { |
| 162 | + View decor = activity.getWindow().getDecorView(); |
| 163 | + if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 164 | + decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| 165 | + } else { |
| 166 | + // We want to change tint color to white again. |
| 167 | + // You can also record the flags in advance so that you can turn UI back completely if |
| 168 | + // you have set other flags before, such as translucent or full screen. |
| 169 | + decor.setSystemUiVisibility(0); |
| 170 | + } |
| 171 | + } |
| 172 | + } catch (Exception e) { |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static void initStatusBarStyle(Activity activity, boolean isMarginStatusBar |
| 177 | + , boolean isMarginNavigationBar) { |
| 178 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { |
| 179 | + if (isMarginStatusBar && isMarginNavigationBar) { |
| 180 | + activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 181 | + } else if (!isMarginStatusBar && !isMarginNavigationBar) { |
| 182 | + activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 183 | +// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 184 | + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 185 | + } else if (!isMarginStatusBar && isMarginNavigationBar) { |
| 186 | + |
| 187 | + activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 188 | + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 189 | + } else { |
| 190 | + //留出来状态栏 不留出来导航栏 没找到办法。。 |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | +} |
0 commit comments