content: package com.termux.app.terminal.utils;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Point;
  import android.os.Build;
  import android.util.TypedValue;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.Window;
  import android.view.WindowManager;
  
  import androidx.annotation.NonNull;
  
  public class ScreenUtils {
      /**
       *get ActionBar height
       */
      public static int getActionBarHeight(Context context) {
          TypedValue tv = new TypedValue();
          if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
              return TypedValue.complexToDimensionPixelSize(
                  tv.data, context.getResources().getDisplayMetrics()
              );
          }
          return 0;
      }
  
      /**
       * get status bar height
       */
      public static int getStatusHeight() {
          Resources resources = Resources.getSystem();
          int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
          return resources.getDimensionPixelSize(resourceId);
      }
  
      /**
       * get navigation bar 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;
          }
      }
  
      /**
       * get visibility og navigation bar
       */
      public static boolean isNavBarVisible(@NonNull final Activity activity) {
          return isNavBarVisible(activity.getWindow());
      }
  
      /**
       * get visibility og navigation bar
       */
      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,window);
                  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, Window window) {
          try {
              return window.getContext().getResources().getResourceEntryName(id);
          } catch (Exception ignore) {
              return "";
          }
      }
  
      /**
       * get screen width
       */
      public static int getScreenWidth(Context context) {
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (wm == null) return -1;
          Point point = new Point();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              //Andoird 4.0 and later, navigation bar was introduced
              wm.getDefaultDisplay().getRealSize(point);
          } else {
              wm.getDefaultDisplay().getSize(point);
          }
          return point.x;
      }
  
      /**
       * get screen height
       */
      public static int getScreenHeight(Context context) {
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (wm == null) return -1;
          Point point = new Point();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              wm.getDefaultDisplay().getRealSize(point);
          } else {
              wm.getDefaultDisplay().getSize(point);
          }
          return point.y;
      }
  
      /**
       * get screen width (exclude decorate element, such as navigation bar below), not exact
       */
      public static int getAppScreenWidth(Context context) {
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (wm == null) return -1;
          Point point = new Point();
          wm.getDefaultDisplay().getSize(point);
          return point.x;
      }
  
      /**
       * get screen height (exclude decorate element, such as navigation bar below), not exact
       */
      public static int getAppScreenHeight(Context context) {
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (wm == null) return -1;
          Point point = new Point();
          wm.getDefaultDisplay().getSize(point);
          return point.y;
      }
  
      /**
       * get ContentView width
       */
      public static int getContentViewWidth(Activity activity) {
          View contentView = activity.findViewById(Window.ID_ANDROID_CONTENT);
          return contentView.getWidth();
      }
  
      /**
       * get ContentView height
       */
      public static int getContentViewHeight(Activity activity) {
          View contentView = activity.findViewById(Window.ID_ANDROID_CONTENT);
          return contentView.getHeight();
      }
  }
  
  
  