可以适配Android 11的沉浸式。状态栏完全透明,没有半透明灰色。
- package com.iflytek.jzapp.utils.immersive;
-
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Build;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.FrameLayout;
-
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.view.ViewCompat;
-
-
- /**
- * @author:luck
- * @data:2018/3/28 下午1:00
- * @描述: 沉浸式相关
- */
-
- public class ImmersiveManager {
-
- /**
- * 注意:使用最好将布局xml 跟布局加入 android:fitsSystemWindows="true" ,这样可以避免有些手机上布局顶边的问题
- *
- * @param baseActivity 这个会留出来状态栏和底栏的空白
- * @param statusBarColor 状态栏的颜色
- * @param navigationBarColor 导航栏的颜色
- * @param isDarkStatusBarIcon 状态栏图标颜色是否是深(黑)色 false状态栏图标颜色为白色
- */
- public static void immersiveAboveAPI23(AppCompatActivity baseActivity, int statusBarColor, int navigationBarColor, boolean isDarkStatusBarIcon) {
- immersiveAboveAPI23(baseActivity, false, false, statusBarColor, navigationBarColor, isDarkStatusBarIcon);
- }
-
-
- /**
- * @param baseActivity
- * @param statusBarColor 状态栏的颜色
- * @param navigationBarColor 导航栏的颜色
- */
- public static void immersiveAboveAPI23(AppCompatActivity baseActivity, boolean isMarginStatusBar
- , boolean isMarginNavigationBar, int statusBarColor, int navigationBarColor, boolean isDarkStatusBarIcon) {
- try {
- Window window = baseActivity.getWindow();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
- //4.4版本及以上 5.0版本及以下
- if (isDarkStatusBarIcon) {
- initBarBelowLOLLIPOP(baseActivity);
- } else {
- window.setFlags(
- WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
- WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- }
- } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- if (isMarginStatusBar && isMarginNavigationBar) {
- //5.0版本及以上
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
- | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- LightStatusBarUtils.setLightStatusBar(baseActivity, true
- , true
- , statusBarColor == Color.TRANSPARENT
- , isDarkStatusBarIcon);
-
- window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- } else if (!isMarginStatusBar && !isMarginNavigationBar) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && isDarkStatusBarIcon) {
- initBarBelowLOLLIPOP(baseActivity);
- } else {
- window.requestFeature(Window.FEATURE_NO_TITLE);
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
- | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
-
- LightStatusBarUtils.setLightStatusBar(baseActivity, false
- , false
- , statusBarColor == Color.TRANSPARENT
- , isDarkStatusBarIcon);
-
- window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- }
- } else if (!isMarginStatusBar) {
- window.requestFeature(Window.FEATURE_NO_TITLE);
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
- | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- LightStatusBarUtils.setLightStatusBar(baseActivity, false
- , true
- , statusBarColor == Color.TRANSPARENT
- , isDarkStatusBarIcon);
-
- window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- } else {
- //留出来状态栏 不留出来导航栏 没找到办法。。
- return;
- }
-
- window.setStatusBarColor(statusBarColor);
- window.setNavigationBarColor(navigationBarColor);
-
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- private final static String TAG_FAKE_STATUS_BAR_VIEW = "TAG_FAKE_STATUS_BAR_VIEW";
- private final static String TAG_MARGIN_ADDED = "TAG_MARGIN_ADDED";
- private final static String TAG_NAVIGATION_BAR_VIEW = "TAG_NAVIGATION_BAR_VIEW";
-
- /**
- * 透明状态栏
- *
- * @param activity
- * @param isDarkStatusBarBlack
- */
- public static void translucentStatusBar(Activity activity, boolean isDarkStatusBarBlack) {
- Window window = activity.getWindow();
- //添加Flag把状态栏设为可绘制模式
- window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- //如果为全透明模式,取消设置Window半透明的Flag
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- //设置状态栏为透明
- window.setStatusBarColor(Color.TRANSPARENT);
- View decor = window.getDecorView();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- //设置window的状态栏不可见,且状态栏字体是白色
- if (isDarkStatusBarBlack) {
- decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
- } else {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
- }
- } else {
- //初始化5.0以下,4.4以上沉浸式
- if (isDarkStatusBarBlack) {
- initBarBelowLOLLIPOP(activity);
- } else {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
- }
- }
- //view不根据系统窗口来调整自己的布局
- ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
- View mChildView = mContentView.getChildAt(0);
- if (mChildView != null) {
- mChildView.setFitsSystemWindows(false);
- ViewCompat.requestApplyInsets(mChildView);
- }
- }
-
- private static void initBarBelowLOLLIPOP(Activity activity) {
- //透明状态栏
- Window mWindow = activity.getWindow();
- mWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- //创建一个假的状态栏
- setupStatusBarView(activity);
- //判断是否存在导航栏,是否禁止设置导航栏
- if (DensityUtil.isNavBarVisible(activity)) {
- //透明导航栏,设置这个,如果有导航栏,底部布局会被导航栏遮住
- mWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- //创建一个假的导航栏
- setupNavBarView(activity);
- }
- }
-
- private static void setupStatusBarView(Activity activity) {
- Window mWindow = activity.getWindow();
- View statusBarView = mWindow.getDecorView().findViewWithTag(TAG_FAKE_STATUS_BAR_VIEW);
- if (statusBarView == null) {
- statusBarView = new View(activity);
- FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
- DensityUtil.getStatusBarHeight(activity));
- params.gravity = Gravity.TOP;
- statusBarView.setLayoutParams(params);
- statusBarView.setVisibility(View.VISIBLE);
- statusBarView.setTag(TAG_MARGIN_ADDED);
- ((ViewGroup) mWindow.getDecorView()).addView(statusBarView);
- }
- statusBarView.setBackgroundColor(Color.TRANSPARENT);
- }
-
- private static void setupNavBarView(Activity activity) {
- Window window = activity.getWindow();
- View navigationBarView = window.getDecorView().findViewWithTag(TAG_NAVIGATION_BAR_VIEW);
- if (navigationBarView == null) {
- navigationBarView = new View(activity);
- navigationBarView.setTag(TAG_NAVIGATION_BAR_VIEW);
- ((ViewGroup) window.getDecorView()).addView(navigationBarView);
- }
-
- FrameLayout.LayoutParams params;
- if (DensityUtil.isNavigationAtBottom(activity)) {
- params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, DensityUtil.getNavigationBarHeight(activity));
- params.gravity = Gravity.BOTTOM;
- } else {
- params = new FrameLayout.LayoutParams(DensityUtil.getNavigationBarWidth(activity), FrameLayout.LayoutParams.MATCH_PARENT);
- params.gravity = Gravity.END;
- }
- navigationBarView.setLayoutParams(params);
- navigationBarView.setBackgroundColor(Color.TRANSPARENT);
- navigationBarView.setVisibility(View.VISIBLE);
- }
- }
- package com.iflytek.jzapp.utils.immersive;
-
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.os.Build;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
-
- /**
- * @author:luck
- * @data:2018/3/28 下午1:01
- * @描述: 沉浸式
- */
-
- public class LightStatusBarUtils {
- public static void setLightStatusBarAboveAPI23(Activity activity, boolean isMarginStatusBar
- , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean dark) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- setLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark);
- }
- }
-
- public static void setLightStatusBar(Activity activity, boolean dark) {
- setLightStatusBar(activity, false, false, false, dark);
- }
-
- public static void setLightStatusBar(Activity activity, boolean isMarginStatusBar
- , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean dark) {
- switch (RomUtils.getLightStatausBarAvailableRomType()) {
- case RomUtils.AvailableRomType.MIUI:
- if (RomUtils.getMIUIVersionCode() >= 7) {
- setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark);
- } else {
- setMIUILightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark);
- }
- break;
-
- case RomUtils.AvailableRomType.FLYME:
- setFlymeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark);
- break;
-
- case RomUtils.AvailableRomType.ANDROID_NATIVE:
- setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark);
- break;
-
- case RomUtils.AvailableRomType.NA:
- // N/A do nothing
- break;
- }
- }
-
-
- private static boolean setMIUILightStatusBar(Activity activity, boolean isMarginStatusBar
- , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean darkmode) {
- initStatusBarStyle(activity, isMarginStatusBar, isMarginNavigationBar);
-
- Class<? extends Window> clazz = activity.getWindow().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);
- extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
- return true;
- } catch (Exception e) {
- setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, darkmode);
- }
- return false;
- }
-
- private static boolean setFlymeLightStatusBar(Activity activity, boolean isMarginStatusBar
- , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean dark) {
- boolean result = false;
- if (activity != null) {
- initStatusBarStyle(activity, isMarginStatusBar, isMarginNavigationBar);
- try {
- WindowManager.LayoutParams lp = activity.getWindow().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);
- activity.getWindow().setAttributes(lp);
- result = true;
-
- if (RomUtils.getFlymeVersion() >= 7) {
- setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark);
- }
- } catch (Exception e) {
- setAndroidNativeLightStatusBar(activity, isMarginStatusBar, isMarginNavigationBar, isTransStatusBar, dark);
- }
- }
- return result;
- }
-
- @TargetApi(11)
- private static void setAndroidNativeLightStatusBar(Activity activity, boolean isMarginStatusBar
- , boolean isMarginNavigationBar, boolean isTransStatusBar, boolean isDarkStatusBarIcon) {
-
- try {
- if (isTransStatusBar) {
- Window window = activity.getWindow();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- if (isMarginStatusBar && isMarginNavigationBar) {
- //5.0版本及以上
- if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
- } else {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- }
- } else if (!isMarginStatusBar && !isMarginNavigationBar) {
-
- if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
- } else {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- }
-
-
- } else if (!isMarginStatusBar && isMarginNavigationBar) {
- if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
- } else {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- }
-
-
- } else {
- //留出来状态栏 不留出来导航栏 没找到办法。。
- return;
- }
- }
- } else {
- View decor = activity.getWindow().getDecorView();
- if (isDarkStatusBarIcon && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
- } else {
- // We want to change tint color to white again.
- // You can also record the flags in advance so that you can turn UI back completely if
- // you have set other flags before, such as translucent or full screen.
- decor.setSystemUiVisibility(0);
- }
- }
- } catch (Exception e) {
- }
- }
-
- private static void initStatusBarStyle(Activity activity, boolean isMarginStatusBar
- , boolean isMarginNavigationBar) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- if (isMarginStatusBar && isMarginNavigationBar) {
- activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- } else if (!isMarginStatusBar && !isMarginNavigationBar) {
- activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- } else if (!isMarginStatusBar && isMarginNavigationBar) {
-
- activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- } else {
- //留出来状态栏 不留出来导航栏 没找到办法。。
- }
- }
-
- }
- }
- package com.iflytek.jzapp.utils.immersive;
-
- import android.annotation.SuppressLint;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.Configuration;
- import android.content.res.Resources;
- import android.graphics.Point;
- import android.os.Build;
- import android.provider.Settings;
- import android.util.DisplayMetrics;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.WindowManager;
-
- /**
- * @author:luck
- * @date:2021/11/17 11:48 上午
- * @describe:DensityUtil
- */
- public class DensityUtil {
- /**
- * 获取屏幕真实宽度
- *
- * @param context
- * @return
- */
- public static int getRealScreenWidth(Context context) {
- WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
- Point point = new Point();
- wm.getDefaultDisplay().getRealSize(point);
- return point.x;
- }
-
- /**
- * 获取屏幕真实高度
- *
- * @param context
- * @return
- */
- public static int getRealScreenHeight(Context context) {
- WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
- Point point = new Point();
- wm.getDefaultDisplay().getRealSize(point);
- return point.y;
- }
-
-
- /**
- * 获取屏幕高度(不包含状态栏高度)
- *
- * @param context
- * @return
- */
- public static int getScreenHeight(Context context) {
- return getRealScreenHeight(context) - getStatusNavigationBarHeight(context);
- }
-
- /**
- * 获取状态栏和导航栏高度
- *
- * @param context
- * @return
- */
- private static int getStatusNavigationBarHeight(Context context) {
- if (isNavBarVisible(context)) {
- return getStatusBarHeight(context) + getNavigationBarHeight(context);
- } else {
- return getStatusBarHeight(context);
- }
- }
-
- /**
- * 获取状态栏高度
- */
- public static int getStatusBarHeight(Context context) {
- int result = 0;
- int resourceId = Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android");
- if (resourceId > 0) {
- result = context.getResources().getDimensionPixelSize(resourceId);
- }
- return result == 0 ? dip2px(context, 26) : result;
- }
-
-
- /**
- * Return whether the navigation bar visible.
- * <p>Call it in onWindowFocusChanged will get right result.</p>
- *
- * @param
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isNavBarVisible(Context context) {
- boolean isVisible = false;
- if (!(context instanceof Activity)) {
- return false;
- }
- Activity activity = (Activity) context;
- Window window = activity.getWindow();
- ViewGroup decorView = (ViewGroup) window.getDecorView();
- for (int i = 0, count = decorView.getChildCount(); i < count; i++) {
- final View child = decorView.getChildAt(i);
- final int id = child.getId();
- if (id != View.NO_ID) {
- String resourceEntryName = getResNameById(activity, id);
- if ("navigationBarBackground".equals(resourceEntryName)
- && child.getVisibility() == View.VISIBLE) {
- isVisible = true;
- break;
- }
- }
- }
- if (isVisible) {
- // 对于三星手机,android10以下非OneUI2的版本,比如 s8,note8 等设备上,
- // 导航栏显示存在bug:"当用户隐藏导航栏时显示输入法的时候导航栏会跟随显示",会导致隐藏输入法之后判断错误
- // 这个问题在 OneUI 2 & android 10 版本已修复
- if (RomUtils.isSamsung()
- && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
- && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
- try {
- return Settings.Global.getInt(activity.getContentResolver(), "navigationbar_hide_bar_enabled") == 0;
- } catch (Exception ignore) {
- }
- }
-
- int visibility = decorView.getSystemUiVisibility();
- isVisible = (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
- }
-
- return isVisible;
- }
-
- /**
- * getResNameById
- *
- * @param context
- * @param id
- * @return
- */
- private static String getResNameById(Context context, int id) {
- try {
- return context.getResources().getResourceEntryName(id);
- } catch (Exception ignore) {
- return "";
- }
- }
-
-
- /**
- * 获取导航栏宽度
- *
- * @param context
- * @return
- */
- @TargetApi(14)
- public static int getNavigationBarWidth(Context context) {
- int result = 0;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
- if (isNavBarVisible(context)) {
- return getInternalDimensionSize(context, "navigation_bar_width");
- }
- }
- return result;
- }
-
- /**
- * 获取导航栏高度
- *
- * @param context
- * @return
- */
- @TargetApi(14)
- public static int getNavigationBarHeight(Context context) {
- int result = 0;
- Resources res = context.getResources();
- boolean mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
- if (isNavBarVisible(context)) {
- String key;
- if (mInPortrait) {
- key = "navigation_bar_height";
- } else {
- key = "navigation_bar_height_landscape";
- }
- return getInternalDimensionSize(context, key);
- }
- return result;
- }
-
-
- private static int getInternalDimensionSize(Context context, String key) {
- int result = 0;
- try {
- int resourceId = Resources.getSystem().getIdentifier(key, "dimen", "android");
- if (resourceId > 0) {
- int sizeOne = context.getResources().getDimensionPixelSize(resourceId);
- int sizeTwo = Resources.getSystem().getDimensionPixelSize(resourceId);
-
- if (sizeTwo >= sizeOne) {
- return sizeTwo;
- } else {
- float densityOne = context.getResources().getDisplayMetrics().density;
- float densityTwo = Resources.getSystem().getDisplayMetrics().density;
- float f = sizeOne * densityTwo / densityOne;
- return (int) ((f >= 0) ? (f + 0.5f) : (f - 0.5f));
- }
- }
- } catch (Resources.NotFoundException ignored) {
- return 0;
- }
- return result;
- }
-
- @SuppressLint("NewApi")
- private static float getSmallestWidthDp(Activity activity) {
- DisplayMetrics metrics = new DisplayMetrics();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
- } else {
- activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
- }
- float widthDp = metrics.widthPixels / metrics.density;
- float heightDp = metrics.heightPixels / metrics.density;
- return Math.min(widthDp, heightDp);
- }
-
- public static boolean isNavigationAtBottom(Activity activity) {
- Resources res = activity.getResources();
- boolean mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
- return (getSmallestWidthDp(activity) >= 600 || mInPortrait);
- }
-
- /**
- * dp2px
- */
- public static int dip2px(Context context, float dpValue) {
- final float scale = context.getApplicationContext().getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
- }
- }
- package com.iflytek.jzapp.utils.immersive;
-
- import android.os.Build;
- import android.text.TextUtils;
-
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.regex.Pattern;
-
- /**
- * @author:luck
- * @data:2018/3/28 下午1:02
- * @描述: Rom版本管理
- */
-
- public class RomUtils {
-
- private static final String[] ROM_SAMSUNG = {"samsung"};
- private final static String UNKNOWN = "unknown";
-
- public static class AvailableRomType {
- public static final int MIUI = 1;
- public static final int FLYME = 2;
- public static final int ANDROID_NATIVE = 3;
- public static final int NA = 4;
- }
-
-
- private static Integer romType;
-
- public static int getLightStatausBarAvailableRomType() {
- if (romType != null) {
- return romType;
- }
-
- if (isMIUIV6OrAbove()) {
- romType = AvailableRomType.MIUI;
- return romType;
- }
-
- if (isFlymeV4OrAbove()) {
- romType = AvailableRomType.FLYME;
- return romType;
- }
-
- if (isAndroid5OrAbove()) {
- romType = AvailableRomType.ANDROID_NATIVE;
- return romType;
- }
-
- romType = AvailableRomType.NA;
- return romType;
- }
-
- //Flyme V4的displayId格式为 [Flyme OS 4.x.x.xA]
- //Flyme V5的displayId格式为 [Flyme 5.x.x.x beta]
- private static boolean isFlymeV4OrAbove() {
- return (getFlymeVersion() >= 4);
- }
-
-
- //Flyme V4的displayId格式为 [Flyme OS 4.x.x.xA]
- //Flyme V5的displayId格式为 [Flyme 5.x.x.x beta]
- public static int getFlymeVersion() {
- String displayId = Build.DISPLAY;
- if (!TextUtils.isEmpty(displayId) && displayId.contains("Flyme")) {
- displayId = displayId.replaceAll("Flyme", "");
- displayId = displayId.replaceAll("OS", "");
- displayId = displayId.replaceAll(" ", "");
-
-
- String version = displayId.substring(0, 1);
-
- return stringToInt(version);
- }
- return 0;
- }
-
- //MIUI V6对应的versionCode是4
- //MIUI V7对应的versionCode是5
- private static boolean isMIUIV6OrAbove() {
- String miuiVersionCodeStr = getSystemProperty();
- if (!TextUtils.isEmpty(miuiVersionCodeStr)) {
- try {
- int miuiVersionCode = ValueOf.toInt(miuiVersionCodeStr);
- if (miuiVersionCode >= 4) {
- return true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return false;
- }
-
-
- public static int getMIUIVersionCode() {
- String miuiVersionCodeStr = getSystemProperty();
- int miuiVersionCode = 0;
- if (!TextUtils.isEmpty(miuiVersionCodeStr)) {
- try {
- miuiVersionCode = ValueOf.toInt(miuiVersionCodeStr);
- return miuiVersionCode;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return miuiVersionCode;
- }
-
-
- //Android Api 23以上
- private static boolean isAndroid5OrAbove() {
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
- }
-
-
- private static String getSystemProperty() {
- String line;
- BufferedReader input = null;
- try {
- Process p = Runtime.getRuntime().exec("getprop " + "ro.miui.ui.version.code");
- input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
- line = input.readLine();
- input.close();
- } catch (IOException ex) {
- return null;
- } finally {
- if (input != null) {
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return line;
- }
-
- /**
- * Return whether the rom is made by samsung.
- *
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean isSamsung() {
- final String brand = getBrand();
- final String manufacturer = getManufacturer();
- return isRightRom(brand, manufacturer, ROM_SAMSUNG);
- }
-
- private static boolean isRightRom(final String brand, final String manufacturer, final String... names) {
- for (String name : names) {
- if (brand.contains(name) || manufacturer.contains(name)) {
- return true;
- }
- }
- return false;
- }
-
- private static String getManufacturer() {
- try {
- String manufacturer = Build.MANUFACTURER;
- if (!TextUtils.isEmpty(manufacturer)) {
- return manufacturer.toLowerCase();
- }
- } catch (Throwable ignore) {/**/}
- return UNKNOWN;
- }
- private static String getBrand() {
- try {
- String brand = Build.BRAND;
- if (!TextUtils.isEmpty(brand)) {
- return brand.toLowerCase();
- }
- } catch (Throwable ignore) {/**/}
- return UNKNOWN;
- }
-
- /**
- * 匹配数值
- *
- * @param str
- * @return
- */
- public static int stringToInt(String str) {
- Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
- return pattern.matcher(str).matches() ? ValueOf.toInt(str) : 0;
- }
-
- }
- package com.iflytek.jzapp.utils.immersive;
-
- /**
- * @author:luck
- * @date:2019-11-12 14:27
- * @describe:类型转换工具类
- */
- public class ValueOf {
- public static String toString(Object o) {
- String value = "";
- try {
- value = o.toString();
- } catch (Exception e) {
- }
-
- return value;
- }
-
-
- public static double toDouble(Object o) {
-
- return toDouble(o, 0);
- }
-
- public static double toDouble(Object o, int defaultValue) {
- if (o == null) {
- return defaultValue;
- }
-
- double value;
- try {
- value = Double.parseDouble(o.toString().trim());
- } catch (Exception e) {
- value = defaultValue;
- }
-
- return value;
- }
-
- public static long toLong(Object o, long defaultValue) {
- if (o == null) {
- return defaultValue;
- }
- long value = 0;
- try {
- String s = o.toString().trim();
- if (s.contains(".")) {
- value = Long.parseLong(s.substring(0, s.lastIndexOf(".")));
- } else {
- value = Long.parseLong(s);
- }
- } catch (Exception e) {
- value = defaultValue;
- }
-
-
- return value;
- }
-
- public static long toLong(Object o) {
- return toLong(o, 0);
- }
-
-
- public static float toFloat(Object o, long defaultValue) {
- if (o == null) {
- return defaultValue;
- }
- float value = 0;
- try {
- String s = o.toString().trim();
- value = Float.parseFloat(s);
- } catch (Exception e) {
- value = defaultValue;
- }
-
-
- return value;
- }
-
- public static float toFloat(Object o) {
- return toFloat(o, 0);
- }
-
-
- public static int toInt(Object o, int defaultValue) {
- if (o == null) {
- return defaultValue;
- }
- int value;
- try {
- String s = o.toString().trim();
- if (s.contains(".")) {
- value = Integer.parseInt(s.substring(0, s.lastIndexOf(".")));
- } else {
- value = Integer.parseInt(s);
- }
- } catch (Exception e) {
- value = defaultValue;
- }
-
- return value;
- }
-
- public static int toInt(Object o) {
- return toInt(o, 0);
- }
-
- public static boolean toBoolean(Object o) {
- return toBoolean(o, false);
-
- }
-
-
- public static boolean toBoolean(Object o, boolean defaultValue) {
- if (o == null) {
- return false;
- }
- boolean value;
- try {
- String s = o.toString().trim();
- if ("false".equals(s.trim())) {
- value = false;
- } else {
- value = true;
- }
- } catch (Exception e) {
- value = defaultValue;
- }
-
- return value;
- }
-
-
- public static <T> T to(Object o, T defaultValue) {
- if (o == null) {
- return defaultValue;
- }
- T value = (T)o;
- return (T) value;
- }
- }