package com.yydcdut.note.utils; import android.annotation.TargetApi; import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.ColorRes; import android.support.annotation.DrawableRes; import android.view.View; import android.view.Window; import android.view.WindowManager; import java.lang.reflect.Method; /** * Created by yuyidong on 15/10/14. */ public class AppCompat { public static boolean AFTER_ICE_CREAM = afterIceCream(); private static boolean afterIceCream() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; } public static int getColor(@ColorRes int id, Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return context.getResources().getColor(id); } else { return context.getResources().getColor(id, context.getTheme()); } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static Drawable getDrawable(Context context, @DrawableRes int resId) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return context.getResources().getDrawable(resId); } else { return context.getDrawable(resId); } } public static void setBackgroundDrawable(View view, Drawable drawable) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.setBackground(drawable); } else { view.setBackgroundDrawable(drawable); } } /** * 5.0之后的SDK */ public static final boolean AFTER_LOLLIPOP = afterLollipop(); private static boolean afterLollipop() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; } public static final boolean AFTER_KITKAT = afterKitkat(); private static boolean afterKitkat() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static void setElevation(View view, float elevation) { if (AFTER_LOLLIPOP) { view.setElevation(elevation); } } public static void setFullWindow(Window window) { if (AppCompat.AFTER_LOLLIPOP) { window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | 128); } else { window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } } public static void setFullWindowWithoutNavigationBar(Window window, Context context) { if (AppCompat.AFTER_LOLLIPOP && hasNavigationBar(context)) { window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } } public static boolean hasNavigationBar(Context context) { boolean hasNavigationBar = false; Resources rs = context.getResources(); int id = rs.getIdentifier("config_showNavigationBar", "bool", "android"); if (id > 0) { hasNavigationBar = 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)) { hasNavigationBar = false; } else if ("0".equals(navBarOverride)) { hasNavigationBar = true; } } catch (Exception e) { YLog.wtf("AppCompat", e.getMessage()); } return hasNavigationBar; } public static int getNavigationBarHeight(Context context) { int navigationBarHeight = 0; Resources rs = context.getResources(); int id = rs.getIdentifier("navigation_bar_height", "dimen", "android"); if (id > 0 && hasNavigationBar(context)) { navigationBarHeight = rs.getDimensionPixelSize(id); } return navigationBarHeight; } }