|
|
package com.br_technology.securitytrain_master.util;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
import android.content.Context;
|
|
|
import android.content.res.Resources;
|
|
|
import android.graphics.Color;
|
|
|
import android.graphics.drawable.ColorDrawable;
|
|
|
import android.os.Build;
|
|
|
import android.util.DisplayMetrics;
|
|
|
import android.view.Display;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
import android.view.Window;
|
|
|
import android.view.WindowManager;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
|
|
|
import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
|
|
|
import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
|
|
|
import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
|
|
|
import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
|
|
|
|
|
|
public class SkinCompat {
|
|
|
|
|
|
public interface JYSkinDelegate {
|
|
|
|
|
|
boolean sbIsLight();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置状态栏黑色字体图标,
|
|
|
* 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
|
|
|
*
|
|
|
* @return 1:MIUUI 2:Flyme 3:android6.0
|
|
|
*/
|
|
|
public static int getStatusBarLightMode(Window window) {
|
|
|
int result = 0;
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
if (MIUISetStatusBarLightMode(window, true)) {
|
|
|
result = 1;
|
|
|
} else if (FlymeSetStatusBarLightMode(window, true)) {
|
|
|
result = 2;
|
|
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
|
|
result = 3;
|
|
|
} else {//5.0
|
|
|
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 已知系统类型时,设置状态栏黑色字体图标。
|
|
|
* 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
|
|
|
*/
|
|
|
public static void setStatusBarLightMode(Window window) {
|
|
|
int type = getStatusBarLightMode(window);
|
|
|
if (type == 1) {
|
|
|
MIUISetStatusBarLightMode(window, true);
|
|
|
} else if (type == 2) {
|
|
|
FlymeSetStatusBarLightMode(window, true);
|
|
|
} else if (type == 3) {
|
|
|
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
|
|
} else {//5.0
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清除MIUI或flyme或6.0以上版本状态栏黑色字体
|
|
|
*/
|
|
|
public static void StatusBarDarkMode(Window window) {
|
|
|
int type = getStatusBarLightMode(window);
|
|
|
if (type == 1) {
|
|
|
MIUISetStatusBarLightMode(window, false);
|
|
|
} else if (type == 2) {
|
|
|
FlymeSetStatusBarLightMode(window, false);
|
|
|
} else if (type == 3) {
|
|
|
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置状态栏图标为深色和魅族特定的文字风格
|
|
|
* 可以用来判断是否为Flyme用户
|
|
|
*
|
|
|
* @param window 需要设置的窗口
|
|
|
* @param dark 是否把状态栏字体及图标颜色设置为深色
|
|
|
* @return boolean 成功执行返回true
|
|
|
*/
|
|
|
public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
|
|
|
boolean result = false;
|
|
|
if (window != null) {
|
|
|
try {
|
|
|
WindowManager.LayoutParams lp = window.getAttributes();
|
|
|
Field darkFlag = WindowManager.LayoutParams.class
|
|
|
.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
|
|
|
Field meizuFlags = WindowManager.LayoutParams.class
|
|
|
.getDeclaredField("meizuFlags");
|
|
|
darkFlag.setAccessible(true);
|
|
|
meizuFlags.setAccessible(true);
|
|
|
int bit = darkFlag.getInt(null);
|
|
|
int value = meizuFlags.getInt(lp);
|
|
|
if (dark) {
|
|
|
value |= bit;
|
|
|
} else {
|
|
|
value &= ~bit;
|
|
|
}
|
|
|
meizuFlags.setInt(lp, value);
|
|
|
window.setAttributes(lp);
|
|
|
result = true;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置状态栏字体图标为深色,需要MIUIV6以上
|
|
|
*
|
|
|
* @param window 需要设置的窗口
|
|
|
* @param dark 是否把状态栏字体及图标颜色设置为深色
|
|
|
* @return boolean 成功执行返回true
|
|
|
*/
|
|
|
public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
|
|
|
boolean result = false;
|
|
|
if (window != null) {
|
|
|
Class clazz = window.getClass();
|
|
|
try {
|
|
|
int darkModeFlag = 0;
|
|
|
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
|
|
|
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
|
|
|
darkModeFlag = field.getInt(layoutParams);
|
|
|
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
|
|
|
if (dark) {
|
|
|
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
|
|
|
} else {
|
|
|
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
|
|
|
}
|
|
|
result = true;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
//SYSTEM_UI_FLAG_FULLSCREEN
|
|
|
public static void initFull(Activity activity) {
|
|
|
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
|
|
|
}
|
|
|
|
|
|
public static void initSystemUiVisibilityTrans(Activity activity, boolean backgroundIsLight) {
|
|
|
int version = Build.VERSION.SDK_INT;
|
|
|
int flags = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|
|
| SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
|
| FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
|
|
|
if (version >= Build.VERSION_CODES.M) {
|
|
|
if (backgroundIsLight) {
|
|
|
flags |= SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
|
|
|
} else {
|
|
|
flags &= ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
|
|
|
}
|
|
|
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
|
|
|
} else {
|
|
|
flags |= FLAG_TRANSLUCENT_STATUS;
|
|
|
// activity.getWindow().setStatusBarColor(Color.BLACK);
|
|
|
}
|
|
|
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
|
|
|
activity.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
|
|
|
activity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
|
|
}
|
|
|
|
|
|
public static void initSystemUiVisibility(Window window, boolean backgroundIsLight) {
|
|
|
int version = Build.VERSION.SDK_INT;
|
|
|
int flags = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|
|
| SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
|
| FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
|
|
|
if (version >= Build.VERSION_CODES.M) {
|
|
|
if (backgroundIsLight) {
|
|
|
flags |= SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
|
|
|
} else {
|
|
|
flags &= ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
|
|
|
}
|
|
|
window.setStatusBarColor(Color.TRANSPARENT);
|
|
|
} else {
|
|
|
flags |= FLAG_TRANSLUCENT_STATUS;
|
|
|
// window.setStatusBarColor(Color.BLACK);
|
|
|
}
|
|
|
window.getDecorView().setSystemUiVisibility(flags);
|
|
|
}
|
|
|
|
|
|
|
|
|
public boolean hasNavigationBarFun(Activity activity) {
|
|
|
Resources rs = activity.getResources();
|
|
|
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
|
|
|
boolean hasNavBarFun = false;
|
|
|
if (id > 0) {
|
|
|
hasNavBarFun = rs.getBoolean(id);
|
|
|
}
|
|
|
try {
|
|
|
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
|
|
|
Method m = systemPropertiesClass.getMethod("get", String.class);
|
|
|
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
|
|
|
if ("1".equals(navBarOverride)) {
|
|
|
hasNavBarFun = false;
|
|
|
} else if ("0".equals(navBarOverride)) {
|
|
|
hasNavBarFun = true;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
hasNavBarFun = false;
|
|
|
}
|
|
|
return hasNavBarFun;
|
|
|
}
|
|
|
|
|
|
//navigationBar是否有显示
|
|
|
public boolean isNavigationBarShow(Activity activity) {
|
|
|
DisplayMetrics dm = new DisplayMetrics();
|
|
|
Display display = activity.getWindowManager().getDefaultDisplay();
|
|
|
display.getMetrics(dm);
|
|
|
int screenHeight = dm.heightPixels;
|
|
|
|
|
|
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
|
|
display.getRealMetrics(realDisplayMetrics);
|
|
|
} else {
|
|
|
Class c;
|
|
|
try {
|
|
|
c = Class.forName("android.view.Display");
|
|
|
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
|
|
|
method.invoke(display, realDisplayMetrics);
|
|
|
} catch (Exception e) {
|
|
|
realDisplayMetrics.setToDefaults();
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int screenRealHeight = realDisplayMetrics.heightPixels;
|
|
|
return (screenRealHeight - screenHeight) > 0;
|
|
|
}
|
|
|
|
|
|
//
|
|
|
public static void setTopPaddingStatus(View view) {
|
|
|
view.setPadding(0,getStatusBarHeight(view.getContext()),0,0);
|
|
|
}
|
|
|
|
|
|
public static void setTopMarginStatus(View view) {
|
|
|
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).topMargin = getStatusBarHeight(view.getContext());
|
|
|
}
|
|
|
|
|
|
public static int getStatusBarHeight(Context context) {
|
|
|
try {
|
|
|
//获得系统中的状态栏高度配置
|
|
|
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
|
|
if (resourceId > 0) {
|
|
|
return context.getResources().getDimensionPixelSize(resourceId);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|