+ * author: Blankj
+ * blog : http://blankj.com
+ * time : 2016/09/23
+ * desc : utils about bar
+ *
+ */
+public final class BarUtils {
+
+ ///////////////////////////////////////////////////////////////////////////
+ // status bar
+ ///////////////////////////////////////////////////////////////////////////
+
+ private static final String TAG_STATUS_BAR = "TAG_STATUS_BAR";
+ private static final String TAG_OFFSET = "TAG_OFFSET";
+ private static final int KEY_OFFSET = -123;
+
+ private BarUtils() {
+ throw new UnsupportedOperationException("u can't instantiate me...");
+ }
+
+ /**
+ * Return the status bar's height.
+ *
+ * @return the status bar's height
+ */
+ public static int getStatusBarHeight() {
+ Resources resources = Resources.getSystem();
+ int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
+ return resources.getDimensionPixelSize(resourceId);
+ }
+
+ /**
+ * Set the status bar's visibility.
+ *
+ * @param activity The activity.
+ * @param isVisible True to set status bar visible, false otherwise.
+ */
+ public static void setStatusBarVisibility(@NonNull final Activity activity,
+ final boolean isVisible) {
+ setStatusBarVisibility(activity.getWindow(), isVisible);
+ }
+
+ /**
+ * Set the status bar's visibility.
+ *
+ * @param window The window.
+ * @param isVisible True to set status bar visible, false otherwise.
+ */
+ public static void setStatusBarVisibility(@NonNull final Window window,
+ final boolean isVisible) {
+ if (isVisible) {
+ window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+ showStatusBarView(window);
+ addMarginTopEqualStatusBarHeight(window);
+ } else {
+ window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+ hideStatusBarView(window);
+ subtractMarginTopEqualStatusBarHeight(window);
+ }
+ }
+
+ /**
+ * Return whether the status bar is visible.
+ *
+ * @param activity The activity.
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isStatusBarVisible(@NonNull final Activity activity) {
+ int flags = activity.getWindow().getAttributes().flags;
+ return (flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0;
+ }
+
+ /**
+ * Set the status bar's light mode.
+ *
+ * @param activity The activity.
+ * @param isLightMode True to set status bar light mode, false otherwise.
+ */
+ public static void setStatusBarLightMode(@NonNull final Activity activity,
+ final boolean isLightMode) {
+ setStatusBarLightMode(activity.getWindow(), isLightMode);
+ }
+
+ /**
+ * Set the status bar's light mode.
+ *
+ * @param window The window.
+ * @param isLightMode True to set status bar light mode, false otherwise.
+ */
+ public static void setStatusBarLightMode(@NonNull final Window window,
+ final boolean isLightMode) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ View decorView = window.getDecorView();
+ int vis = decorView.getSystemUiVisibility();
+ if (isLightMode) {
+ vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
+ } else {
+ vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
+ }
+ decorView.setSystemUiVisibility(vis);
+ }
+ }
+
+ /**
+ * Is the status bar light mode.
+ *
+ * @param activity The activity.
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isStatusBarLightMode(@NonNull final Activity activity) {
+ return isStatusBarLightMode(activity.getWindow());
+ }
+
+ /**
+ * Is the status bar light mode.
+ *
+ * @param window The window.
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isStatusBarLightMode(@NonNull final Window window) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ View decorView = window.getDecorView();
+ int vis = decorView.getSystemUiVisibility();
+ return (vis & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0;
+ }
+ return false;
+ }
+
+ /**
+ * Add the top margin size equals status bar's height for view.
+ *
+ * @param view The view.
+ */
+ public static void addMarginTopEqualStatusBarHeight(@NonNull View view) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ view.setTag(TAG_OFFSET);
+ Object haveSetOffset = view.getTag(KEY_OFFSET);
+ if (haveSetOffset != null && (Boolean) haveSetOffset) return;
+ MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
+ layoutParams.setMargins(layoutParams.leftMargin,
+ layoutParams.topMargin + getStatusBarHeight(),
+ layoutParams.rightMargin,
+ layoutParams.bottomMargin);
+ view.setTag(KEY_OFFSET, true);
+ }
+
+ /**
+ * Subtract the top margin size equals status bar's height for view.
+ *
+ * @param view The view.
+ */
+ public static void subtractMarginTopEqualStatusBarHeight(@NonNull View view) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ Object haveSetOffset = view.getTag(KEY_OFFSET);
+ if (haveSetOffset == null || !(Boolean) haveSetOffset) return;
+ MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
+ layoutParams.setMargins(layoutParams.leftMargin,
+ layoutParams.topMargin - getStatusBarHeight(),
+ layoutParams.rightMargin,
+ layoutParams.bottomMargin);
+ view.setTag(KEY_OFFSET, false);
+ }
+
+ private static void addMarginTopEqualStatusBarHeight(final Window window) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ View withTag = window.getDecorView().findViewWithTag(TAG_OFFSET);
+ if (withTag == null) return;
+ addMarginTopEqualStatusBarHeight(withTag);
+ }
+
+ private static void subtractMarginTopEqualStatusBarHeight(final Window window) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ View withTag = window.getDecorView().findViewWithTag(TAG_OFFSET);
+ if (withTag == null) return;
+ subtractMarginTopEqualStatusBarHeight(withTag);
+ }
+
+ /**
+ * Set the status bar's color.
+ *
+ * @param activity The activity.
+ * @param color The status bar's color.
+ */
+ public static View setStatusBarColor(@NonNull final Activity activity,
+ @ColorInt final int color) {
+ return setStatusBarColor(activity, color, false);
+ }
+
+ /**
+ * Set the status bar's color.
+ *
+ * @param activity The activity.
+ * @param color The status bar's color.
+ * @param isDecor True to add fake status bar in DecorView,
+ * false to add fake status bar in ContentView.
+ */
+ public static View setStatusBarColor(@NonNull final Activity activity,
+ @ColorInt final int color,
+ final boolean isDecor) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null;
+ transparentStatusBar(activity);
+ return applyStatusBarColor(activity, color, isDecor);
+ }
+
+
+ /**
+ * Set the status bar's color.
+ *
+ * @param window The window.
+ * @param color The status bar's color.
+ */
+ public static View setStatusBarColor(@NonNull final Window window,
+ @ColorInt final int color) {
+ return setStatusBarColor(window, color, false);
+ }
+
+ /**
+ * Set the status bar's color.
+ *
+ * @param window The window.
+ * @param color The status bar's color.
+ * @param isDecor True to add fake status bar in DecorView,
+ * false to add fake status bar in ContentView.
+ */
+ public static View setStatusBarColor(@NonNull final Window window,
+ @ColorInt final int color,
+ final boolean isDecor) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null;
+ transparentStatusBar(window);
+ return applyStatusBarColor(window, color, isDecor);
+ }
+
+ /**
+ * Set the status bar's color.
+ *
+ * @param fakeStatusBar The fake status bar view.
+ * @param color The status bar's color.
+ */
+ public static void setStatusBarColor(@NonNull final View fakeStatusBar,
+ @ColorInt final int color) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ Activity activity = ActivityUtils.getActivityByContext(fakeStatusBar.getContext());
+ if (activity == null) return;
+ transparentStatusBar(activity);
+ fakeStatusBar.setVisibility(View.VISIBLE);
+ ViewGroup.LayoutParams layoutParams = fakeStatusBar.getLayoutParams();
+ layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
+ layoutParams.height = getStatusBarHeight();
+ fakeStatusBar.setBackgroundColor(color);
+ }
+
+ /**
+ * Set the custom status bar.
+ *
+ * @param fakeStatusBar The fake status bar view.
+ */
+ public static void setStatusBarCustom(@NonNull final View fakeStatusBar) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ Activity activity = ActivityUtils.getActivityByContext(fakeStatusBar.getContext());
+ if (activity == null) return;
+ transparentStatusBar(activity);
+ fakeStatusBar.setVisibility(View.VISIBLE);
+ ViewGroup.LayoutParams layoutParams = fakeStatusBar.getLayoutParams();
+ if (layoutParams == null) {
+ layoutParams = new ViewGroup.LayoutParams(
+ ViewGroup.LayoutParams.MATCH_PARENT,
+ getStatusBarHeight()
+ );
+ fakeStatusBar.setLayoutParams(layoutParams);
+ } else {
+ layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
+ layoutParams.height = getStatusBarHeight();
+ }
+ }
+
+ /**
+ * Set the status bar's color for DrawerLayout.
+ *
DrawLayout must add {@code android:fitsSystemWindows="true"}
+ *
+ * @param drawer The DrawLayout.
+ * @param fakeStatusBar The fake status bar view.
+ * @param color The status bar's color.
+ */
+ public static void setStatusBarColor4Drawer(@NonNull final DrawerLayout drawer,
+ @NonNull final View fakeStatusBar,
+ @ColorInt final int color) {
+ setStatusBarColor4Drawer(drawer, fakeStatusBar, color, false);
+ }
+
+ /**
+ * Set the status bar's color for DrawerLayout.
+ *
DrawLayout must add {@code android:fitsSystemWindows="true"}
+ *
+ * @param drawer The DrawLayout.
+ * @param fakeStatusBar The fake status bar view.
+ * @param color The status bar's color.
+ * @param isTop True to set DrawerLayout at the top layer, false otherwise.
+ */
+ public static void setStatusBarColor4Drawer(@NonNull final DrawerLayout drawer,
+ @NonNull final View fakeStatusBar,
+ @ColorInt final int color,
+ final boolean isTop) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ Activity activity = ActivityUtils.getActivityByContext(fakeStatusBar.getContext());
+ if (activity == null) return;
+ transparentStatusBar(activity);
+ drawer.setFitsSystemWindows(false);
+ setStatusBarColor(fakeStatusBar, color);
+ for (int i = 0, count = drawer.getChildCount(); i < count; i++) {
+ drawer.getChildAt(i).setFitsSystemWindows(false);
+ }
+ if (isTop) {
+ hideStatusBarView(activity);
+ } else {
+ setStatusBarColor(activity, color, false);
+ }
+ }
+
+ private static View applyStatusBarColor(final Activity activity,
+ final int color,
+ boolean isDecor) {
+ return applyStatusBarColor(activity.getWindow(), color, isDecor);
+ }
+
+ private static View applyStatusBarColor(final Window window,
+ final int color,
+ boolean isDecor) {
+ ViewGroup parent = isDecor ?
+ (ViewGroup) window.getDecorView() :
+ (ViewGroup) window.findViewById(android.R.id.content);
+ View fakeStatusBarView = parent.findViewWithTag(TAG_STATUS_BAR);
+ if (fakeStatusBarView != null) {
+ if (fakeStatusBarView.getVisibility() == View.GONE) {
+ fakeStatusBarView.setVisibility(View.VISIBLE);
+ }
+ fakeStatusBarView.setBackgroundColor(color);
+ } else {
+ fakeStatusBarView = createStatusBarView(window.getContext(), color);
+ parent.addView(fakeStatusBarView);
+ }
+ return fakeStatusBarView;
+ }
+
+ private static void hideStatusBarView(final Activity activity) {
+ hideStatusBarView(activity.getWindow());
+ }
+
+ private static void hideStatusBarView(final Window window) {
+ ViewGroup decorView = (ViewGroup) window.getDecorView();
+ View fakeStatusBarView = decorView.findViewWithTag(TAG_STATUS_BAR);
+ if (fakeStatusBarView == null) return;
+ fakeStatusBarView.setVisibility(View.GONE);
+ }
+
+ private static void showStatusBarView(final Window window) {
+ ViewGroup decorView = (ViewGroup) window.getDecorView();
+ View fakeStatusBarView = decorView.findViewWithTag(TAG_STATUS_BAR);
+ if (fakeStatusBarView == null) return;
+ fakeStatusBarView.setVisibility(View.VISIBLE);
+ }
+
+ private static View createStatusBarView(final Context context,
+ final int color) {
+ View statusBarView = new View(context);
+ statusBarView.setLayoutParams(new ViewGroup.LayoutParams(
+ ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight()));
+ statusBarView.setBackgroundColor(color);
+ statusBarView.setTag(TAG_STATUS_BAR);
+ return statusBarView;
+ }
+
+ public static void transparentStatusBar(final Activity activity) {
+ transparentStatusBar(activity.getWindow());
+ }
+
+ public static void transparentStatusBar(final Window window) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
+ window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
+ int option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
+ int vis = window.getDecorView().getSystemUiVisibility();
+ window.getDecorView().setSystemUiVisibility(option | vis);
+ window.setStatusBarColor(Color.TRANSPARENT);
+ } else {
+ window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
+ }
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ // action bar
+ ///////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Return the action bar's height.
+ *
+ * @return the action bar's height
+ */
+ public static int getActionBarHeight() {
+ TypedValue tv = new TypedValue();
+ if (AppUtil.getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
+ return TypedValue.complexToDimensionPixelSize(
+ tv.data, Resources.getSystem().getDisplayMetrics()
+ );
+ }
+ return 0;
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ // notification bar
+ ///////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Set the notification bar's visibility.
+ *
Must hold {@code }
+ *
+ * @param isVisible True to set notification bar visible, false otherwise.
+ */
+ @RequiresPermission(EXPAND_STATUS_BAR)
+ public static void setNotificationBarVisibility(final boolean isVisible) {
+ String methodName;
+ if (isVisible) {
+ methodName = (Build.VERSION.SDK_INT <= 16) ? "expand" : "expandNotificationsPanel";
+ } else {
+ methodName = (Build.VERSION.SDK_INT <= 16) ? "collapse" : "collapsePanels";
+ }
+ invokePanels(methodName);
+ }
+
+ private static void invokePanels(final String methodName) {
+ try {
+ @SuppressLint("WrongConstant")
+ Object service = AppUtil.getContext().getSystemService("statusbar");
+ @SuppressLint("PrivateApi")
+ Class> statusBarManager = Class.forName("android.app.StatusBarManager");
+ Method expand = statusBarManager.getMethod(methodName);
+ expand.invoke(service);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ ///////////////////////////////////////////////////////////////////////////
+ // navigation bar
+ ///////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Return the navigation bar's height.
+ *
+ * @return the navigation bar's height
+ */
+ public static int getNavBarHeight() {
+ Resources res = Resources.getSystem();
+ int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
+ if (resourceId != 0) {
+ return res.getDimensionPixelSize(resourceId);
+ } else {
+ return 0;
+ }
+ }
+
+ /**
+ * Set the navigation bar's visibility.
+ *
+ * @param activity The activity.
+ * @param isVisible True to set navigation bar visible, false otherwise.
+ */
+ public static void setNavBarVisibility(@NonNull final Activity activity, boolean isVisible) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ setNavBarVisibility(activity.getWindow(), isVisible);
+
+ }
+
+ /**
+ * Set the navigation bar's visibility.
+ *
+ * @param window The window.
+ * @param isVisible True to set navigation bar visible, false otherwise.
+ */
+ public static void setNavBarVisibility(@NonNull final Window window, boolean isVisible) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
+ final 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(id);
+ if ("navigationBarBackground".equals(resourceEntryName)) {
+ child.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
+ }
+ }
+ }
+ final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
+ | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
+ | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
+ if (isVisible) {
+ decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~uiOptions);
+ } else {
+ decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() | uiOptions);
+ }
+ }
+
+ /**
+ * Return whether the navigation bar visible.
+ *
Call it in onWindowFocusChanged will get right result.
+ *
+ * @param activity The activity.
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isNavBarVisible(@NonNull final Activity activity) {
+ return isNavBarVisible(activity.getWindow());
+ }
+
+ /**
+ * Return whether the navigation bar visible.
+ *
Call it in onWindowFocusChanged will get right result.
+ *
+ * @param window The window.
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isNavBarVisible(@NonNull final Window window) {
+ boolean isVisible = false;
+ 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(id);
+ if ("navigationBarBackground".equals(resourceEntryName)
+ && child.getVisibility() == View.VISIBLE) {
+ isVisible = true;
+ break;
+ }
+ }
+ }
+ if (isVisible) {
+ int visibility = decorView.getSystemUiVisibility();
+ isVisible = (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
+ }
+ return isVisible;
+ }
+
+ private static String getResNameById(int id) {
+ try {
+ return AppUtil.getContext().getResources().getResourceEntryName(id);
+ } catch (Exception ignore) {
+ return "";
+ }
+ }
+
+ /**
+ * Set the navigation bar's color.
+ *
+ * @param activity The activity.
+ * @param color The navigation bar's color.
+ */
+ @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
+ public static void setNavBarColor(@NonNull final Activity activity, @ColorInt final int color) {
+ setNavBarColor(activity.getWindow(), color);
+ }
+
+ /**
+ * Set the navigation bar's color.
+ *
+ * @param window The window.
+ * @param color The navigation bar's color.
+ */
+ @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
+ public static void setNavBarColor(@NonNull final Window window, @ColorInt final int color) {
+ window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
+ window.setNavigationBarColor(color);
+ }
+
+ /**
+ * Return the color of navigation bar.
+ *
+ * @param activity The activity.
+ * @return the color of navigation bar
+ */
+ @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
+ public static int getNavBarColor(@NonNull final Activity activity) {
+ return getNavBarColor(activity.getWindow());
+ }
+
+ /**
+ * Return the color of navigation bar.
+ *
+ * @param window The window.
+ * @return the color of navigation bar
+ */
+ @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
+ public static int getNavBarColor(@NonNull final Window window) {
+ return window.getNavigationBarColor();
+ }
+
+ /**
+ * Return whether the navigation bar visible.
+ *
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isSupportNavBar() {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
+ WindowManager wm = (WindowManager) AppUtil.getContext().getSystemService(Context.WINDOW_SERVICE);
+ if (wm == null) return false;
+ Display display = wm.getDefaultDisplay();
+ Point size = new Point();
+ Point realSize = new Point();
+ display.getSize(size);
+ display.getRealSize(realSize);
+ return realSize.y != size.y || realSize.x != size.x;
+ }
+ boolean menu = ViewConfiguration.get(AppUtil.getContext()).hasPermanentMenuKey();
+ boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
+ return !menu && !back;
+ }
+
+ /**
+ * Set the nav bar's light mode.
+ *
+ * @param activity The activity.
+ * @param isLightMode True to set nav bar light mode, false otherwise.
+ */
+ public static void setNavBarLightMode(@NonNull final Activity activity,
+ final boolean isLightMode) {
+ setNavBarLightMode(activity.getWindow(), isLightMode);
+ }
+
+ /**
+ * Set the nav bar's light mode.
+ *
+ * @param window The window.
+ * @param isLightMode True to set nav bar light mode, false otherwise.
+ */
+ public static void setNavBarLightMode(@NonNull final Window window,
+ final boolean isLightMode) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ View decorView = window.getDecorView();
+ int vis = decorView.getSystemUiVisibility();
+ if (isLightMode) {
+ vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
+ } else {
+ vis &= ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
+ }
+ decorView.setSystemUiVisibility(vis);
+ }
+ }
+
+ /**
+ * Is the nav bar light mode.
+ *
+ * @param activity The activity.
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isNavBarLightMode(@NonNull final Activity activity) {
+ return isNavBarLightMode(activity.getWindow());
+ }
+
+ /**
+ * Is the nav bar light mode.
+ *
+ * @param window The window.
+ * @return {@code true}: yes {@code false}: no
+ */
+ public static boolean isNavBarLightMode(@NonNull final Window window) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ View decorView = window.getDecorView();
+ int vis = decorView.getSystemUiVisibility();
+ return (vis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0;
+ }
+ return false;
+ }
+}
diff --git a/app/src/main/java/com/pic/catcher/util/ClipboardUtil.java b/app/src/main/java/com/pic/catcher/util/ClipboardUtil.java
new file mode 100644
index 0000000..5578a2f
--- /dev/null
+++ b/app/src/main/java/com/pic/catcher/util/ClipboardUtil.java
@@ -0,0 +1,30 @@
+package com.pic.catcher.util;
+
+import android.content.ClipData;
+import android.content.ClipboardManager;
+import android.content.Context;
+
+import com.lu.magic.util.AppUtil;
+
+public class ClipboardUtil {
+ /**
+ * 复制内容到剪切板
+ *
+ * @param copyStr
+ * @return
+ */
+ public static boolean copy(CharSequence copyStr) {
+ try {
+ //获取剪贴板管理器
+ ClipboardManager cm = (ClipboardManager) AppUtil.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
+ // 创建普通字符型ClipData
+ ClipData mClipData = ClipData.newPlainText("Label", copyStr);
+ // 将ClipData内容放到系统剪贴板里。
+ cm.setPrimaryClip(mClipData);
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+}
diff --git a/app/src/main/java/com/pic/catcher/util/CodeUtil.java b/app/src/main/java/com/pic/catcher/util/CodeUtil.java
new file mode 100644
index 0000000..a2c5710
--- /dev/null
+++ b/app/src/main/java/com/pic/catcher/util/CodeUtil.java
@@ -0,0 +1,162 @@
+package com.pic.catcher.util;
+
+import android.content.Context;
+
+import com.lu.magic.util.ReflectUtil;
+import com.lu.magic.util.function.Consumer;
+import com.lu.magic.util.function.Predicate;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+
+import dalvik.system.DexFile;
+
+public class CodeUtil {
+ /**
+ * 获取应用程序下的所有Dex文件
+ *
+ * @param packageCodePath 包路径
+ * @return Set
+ */
+ private static List getDexFileList(String packageCodePath) {
+ List dexFiles = new ArrayList<>();
+ File dir = new File(packageCodePath).getParentFile();
+ File[] files = dir.listFiles();
+ for (File file : files) {
+ try {
+ String absolutePath = file.getAbsolutePath();
+ if (!absolutePath.contains(".")) continue;
+ String suffix = absolutePath.substring(absolutePath.lastIndexOf("."));
+ if (!suffix.equals(".apk")) continue;
+ DexFile dexFile = getDexFile(file.getAbsolutePath());
+ if (dexFile == null) continue;
+ dexFiles.add(dexFile);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ return dexFiles;
+ }
+
+ /**
+ * 获取DexFile文件
+ *
+ * @param path 路径
+ * @return DexFile
+ */
+ public static DexFile getDexFile(String path) {
+ try {
+ return new DexFile(path);
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+
+ public static List filterClass(Context context, Predicate func) {
+ List dexFiles = getDexFileListNewApi(context.getClassLoader());
+ if (dexFiles.isEmpty()) {
+ //老方法需要重新读取,耗时大
+ dexFiles = getDexFileList(context.getApplicationContext().getPackageCodePath());
+ }
+ return filterClassInternal(dexFiles.iterator(), func);
+ }
+
+ public static void eachClass(Context context, Consumer consumer) {
+ List dexFiles = getDexFileListNewApi(context.getClassLoader());
+ if (dexFiles.isEmpty()) {
+ //老方法需要重新读取,耗时大
+ dexFiles = getDexFileList(context.getApplicationContext().getPackageCodePath());
+ }
+ eachClassInternal(dexFiles.iterator(), consumer);
+ }
+
+ /**
+ * 读取类路径下的所有类
+ *
+ * @param context 上下文
+ * @param pkgName 包名
+ * @return List
+ */
+ public static List filterClass(Context context, String pkgName, boolean includeChildDir) {
+ List dexFiles = getDexFileListNewApi(context.getClassLoader());
+ if (dexFiles.isEmpty()) {
+ //老方法需要重新读取,耗时大
+ dexFiles = getDexFileList(context.getApplicationContext().getPackageCodePath());
+ }
+
+ return filterClassInternal(dexFiles.iterator(), currentClassPath -> {
+ if (!currentClassPath.startsWith(pkgName)) {
+ return false;
+ }
+ if (includeChildDir) {
+ return true;
+ } else {
+ return '.' == currentClassPath.charAt(pkgName.length());
+ }
+ });
+ }
+
+
+ public static List getDexFileListNewApi(ClassLoader classLoader) {
+ List result = new ArrayList<>();
+ try {
+ Object pathList = ReflectUtil.getFieldValue(classLoader, classLoader.getClass().getSuperclass(), "pathList");
+ Object[] dexElements = (Object[]) ReflectUtil.getFieldValue(pathList, "dexElements");
+ for (Object dexElement : dexElements) {
+ DexFile dexFile = (DexFile) ReflectUtil.getFieldValue(dexElement, "dexFile");
+ result.add(dexFile);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+
+ private static void eachClassInternal(Iterator dexFiles, Consumer func) {
+
+ while (dexFiles.hasNext()) {
+ DexFile dexFile = dexFiles.next();
+ if (dexFile == null) continue;
+
+ Enumeration entries = dexFile.entries();
+ while (entries.hasMoreElements()) {
+ try {
+ String currentClassPath = entries.nextElement();
+ if (currentClassPath == null || currentClassPath.isEmpty()) {
+ continue;
+ }
+ func.accept(currentClassPath);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ }
+
+ private static List filterClassInternal(Iterator dexFiles, Predicate predicateFunc) {
+ HashSet result = new HashSet<>();
+ eachClassInternal(dexFiles, s -> {
+ if (predicateFunc.test(s)) {
+ result.add(s);
+ }
+ });
+ return new ArrayList<>(result);
+ }
+
+
+ public static String getPackageName(String typeName) {
+ int index = typeName.lastIndexOf(".");
+ if (index == -1) {
+ return null;
+ }
+ return typeName.substring(0, index);
+ }
+}
diff --git a/app/src/main/java/com/pic/catcher/util/ColorUtilX.kt b/app/src/main/java/com/pic/catcher/util/ColorUtilX.kt
new file mode 100644
index 0000000..d2cfae2
--- /dev/null
+++ b/app/src/main/java/com/pic/catcher/util/ColorUtilX.kt
@@ -0,0 +1,32 @@
+package com.pic.catcher.util
+
+import android.graphics.Color
+import com.lu.magic.util.ColorUtil
+
+class ColorUtilX {
+ companion object {
+ /**
+ * 颜色取反
+ */
+ @JvmStatic
+ fun invertColor(color: Int): Int {
+ // 将颜色值转换为ARGB格式
+ val a = Color.alpha(color)
+ val r = Color.red(color)
+ val g = Color.green(color)
+ val b = Color.blue(color)
+
+ // 对RGB分量进行取反
+ val invR = 255 - r
+ val invG = 255 - g
+ val invB = 255 - b
+
+ // 构建新的颜色值
+ return Color.argb(a, invR, invG, invB)
+ }
+
+ }
+
+}
+
+fun ColorUtil.invertColor(color: Int) = ColorUtilX.invertColor(color)
\ No newline at end of file
diff --git a/app/src/main/java/com/pic/catcher/util/EachUtil.java b/app/src/main/java/com/pic/catcher/util/EachUtil.java
new file mode 100644
index 0000000..8151237
--- /dev/null
+++ b/app/src/main/java/com/pic/catcher/util/EachUtil.java
@@ -0,0 +1,30 @@
+package com.pic.catcher.util;
+
+import java.lang.reflect.Field;
+import java.util.function.Predicate;
+
+public class EachUtil {
+
+ public static void eachFieldValue(Object obj, Predicate