package com.doumee.lib_coremodel.util; 
 | 
  
 | 
import android.annotation.TargetApi; 
 | 
import android.app.Activity; 
 | 
import android.content.Context; 
 | 
import android.graphics.Color; 
 | 
import android.os.Build; 
 | 
import android.view.View; 
 | 
import android.view.ViewGroup; 
 | 
import android.view.Window; 
 | 
import android.view.WindowManager; 
 | 
  
 | 
import java.io.IOException; 
 | 
import java.lang.reflect.Field; 
 | 
import java.lang.reflect.Method; 
 | 
  
 | 
import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; 
 | 
  
 | 
//状态栏工具 
 | 
public class StateBarUtils { 
 | 
     
 | 
    private Activity activity; 
 | 
  
 | 
    public StateBarUtils(Activity activity) { 
 | 
        this.activity = activity; 
 | 
    } 
 | 
  
 | 
    public void setStateBar(){ 
 | 
        transparencyBar(); 
 | 
        addStatusViewWithColor(Color.parseColor("#ffE91E63")); 
 | 
        setFitsSystemWindows(activity,true); 
 | 
    } 
 | 
  
 | 
    public void setStateBar(int color){ 
 | 
        transparencyBar(); 
 | 
        addStatusViewWithColor(color); 
 | 
        setFitsSystemWindows(activity,true); 
 | 
    } 
 | 
  
 | 
    //设置透明 
 | 
    public void setTransparentStateBar(){ 
 | 
        transparencyBar(); 
 | 
        addStatusViewWithColor(Color.parseColor("#ffffffff")); 
 | 
        setFitsSystemWindows(activity,true); 
 | 
        setStatusTextColor(true,activity); 
 | 
    } 
 | 
  
 | 
    //设置透明 
 | 
    public void setTransparentNoStateBar(){ 
 | 
        transparencyBar(); 
 | 
        setStatusTextColor(true,activity); 
 | 
        setFitsSystemWindows(activity,false); 
 | 
    } 
 | 
  
 | 
    public void setStartBarColor(int color){ 
 | 
        addStatusViewWithColor( color); 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    @TargetApi(19) 
 | 
    public void transparencyBar() { 
 | 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
 | 
            Window window = activity.getWindow(); 
 | 
            window.clearFlags(FLAG_TRANSLUCENT_STATUS); 
 | 
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
 | 
            window.setStatusBarColor(Color.TRANSPARENT); 
 | 
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
 | 
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
 | 
  
 | 
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
 | 
            Window window = activity.getWindow(); 
 | 
            window.setFlags(FLAG_TRANSLUCENT_STATUS, 
 | 
                    FLAG_TRANSLUCENT_STATUS); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 添加状态栏占位视图 
 | 
     * 
 | 
     */ 
 | 
    private void addStatusViewWithColor(int color) { 
 | 
        ViewGroup contentView = activity.findViewById(android.R.id.content); 
 | 
        View statusBarView = new View(activity); 
 | 
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
 | 
                getStatusBarHeight(activity)); 
 | 
        statusBarView.setBackgroundColor(color); 
 | 
        contentView.addView(statusBarView, lp); 
 | 
    } 
 | 
  
 | 
    public static int getStatusBarHeight(Context context) { 
 | 
        try { 
 | 
            int result = 0; 
 | 
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); 
 | 
            if (resourceId > 0) { 
 | 
                result = context.getResources().getDimensionPixelSize(resourceId); 
 | 
            } 
 | 
            return result; 
 | 
        }catch (Exception e){ 
 | 
  
 | 
        } 
 | 
        return 0; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 设置页面最外层布局 FitsSystemWindows 属性 
 | 
     * @param activity 
 | 
     * @param value 
 | 
     */ 
 | 
    public static void setFitsSystemWindows(Activity activity, boolean value) { 
 | 
        ViewGroup contentFrameLayout = (ViewGroup) activity.findViewById(android.R.id.content); 
 | 
        View parentView = contentFrameLayout.getChildAt(0); 
 | 
        if (parentView != null && Build.VERSION.SDK_INT >= 14) { 
 | 
            parentView.setFitsSystemWindows(value); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 改变魅族的状态栏字体为黑色,要求FlyMe4以上 
 | 
     */ 
 | 
    private static void processFlyMe(boolean isLightStatusBar, Activity activity) { 
 | 
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); 
 | 
        try { 
 | 
            Class<?> instance = Class.forName("android.view.WindowManager$LayoutParams"); 
 | 
            int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp); 
 | 
            Field field = instance.getDeclaredField("meizuFlags"); 
 | 
            field.setAccessible(true); 
 | 
            int origin = field.getInt(lp); 
 | 
            if (isLightStatusBar) { 
 | 
                field.set(lp, origin | value); 
 | 
            } else { 
 | 
                field.set(lp, (~value) & origin); 
 | 
            } 
 | 
        } catch (Exception ignored) { 
 | 
            ignored.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 改变小米的状态栏字体颜色为黑色, 要求MIUI6以上  lightStatusBar为真时表示黑色字体 
 | 
     */ 
 | 
    private static void processMIUI(boolean lightStatusBar, Activity activity) { 
 | 
        Class<? extends Window> clazz = activity.getWindow().getClass(); 
 | 
        try { 
 | 
            int darkModeFlag; 
 | 
            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(), lightStatusBar? darkModeFlag : 0, darkModeFlag); 
 | 
        } catch (Exception ignored) { 
 | 
            ignored.printStackTrace(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"; 
 | 
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name"; 
 | 
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage"; 
 | 
  
 | 
    /** 
 | 
     * 判断手机是否是小米 
 | 
     * @return 
 | 
     */ 
 | 
    public static boolean isMIUI() { 
 | 
        try { 
 | 
            final BuildProperties prop = BuildProperties.newInstance(); 
 | 
            return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null 
 | 
                    || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null 
 | 
                    || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null; 
 | 
        } catch (final IOException e) { 
 | 
            return false; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 判断手机是否是魅族 
 | 
     * @return 
 | 
     */ 
 | 
    public static boolean isFlyme() { 
 | 
        try { 
 | 
            // Invoke Build.hasSmartBar() 
 | 
            final Method method = Build.class.getMethod("hasSmartBar"); 
 | 
            return method != null; 
 | 
        } catch (final Exception e) { 
 | 
            return false; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 设置状态栏文字色值为深色调 
 | 
     * @param useDart 是否使用深色调 
 | 
     * @param activity 
 | 
     */ 
 | 
    public void setStatusTextColor(boolean useDart, Activity activity) { 
 | 
        if (isFlyme()) { 
 | 
            processFlyMe(useDart, activity); 
 | 
        } else if (isMIUI()) { 
 | 
            processMIUI(useDart, activity); 
 | 
        } else { 
 | 
            if (useDart) { 
 | 
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
 | 
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); 
 | 
                } 
 | 
            } else { 
 | 
                activity.getWindow().getDecorView().setSystemUiVisibility( 
 | 
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); 
 | 
            } 
 | 
            activity.getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, 0); 
 | 
        } 
 | 
    } 
 | 
} 
 |