content: package com.test.banner.utils;
  
  import android.app.Activity;
  import android.app.KeyguardManager;
  import android.content.Context;
  import android.content.pm.ActivityInfo;
  import android.content.res.Configuration;
  import android.content.res.Resources;
  import android.graphics.Bitmap;
  import android.graphics.Point;
  import android.os.Build;
  import android.provider.Settings;
  import android.support.annotation.NonNull;
  import android.support.annotation.RequiresPermission;
  import android.util.DisplayMetrics;
  import android.view.Surface;
  import android.view.View;
  import android.view.Window;
  import android.view.WindowManager;
  
  import com.test.banner.application.AppContext;
  
  import static android.Manifest.permission.WRITE_SETTINGS;
  
  /**
   * 屏幕相关工具类
   */
  public final class ScreenUtils {
  
      private ScreenUtils() {
          throw new UnsupportedOperationException("u can't instantiate me...");
      }
  
      /**
       * 获取屏幕宽度
       *
       * @return the width of screen, in pixel
       */
      public static int getScreenWidth() {
          WindowManager wm = (WindowManager) AppContext.getInstance().getSystemService(Context.WINDOW_SERVICE);
          if (wm == null) {
              return AppContext.getInstance().getResources().getDisplayMetrics().widthPixels;
          }
          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.x;
      }
  
      /**
       * 获取屏幕高度
       *
       * @return the height of screen, in pixel
       */
      public static int getScreenHeight() {
          WindowManager wm = (WindowManager) AppContext.getInstance().getSystemService(Context.WINDOW_SERVICE);
          if (wm == null) {
              return AppContext.getInstance().getResources().getDisplayMetrics().heightPixels;
          }
          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;
      }
  
      /**
       * 屏幕密度
       *
       * @return the density of screen
       */
      public static float getScreenDensity() {
          return AppContext.getInstance().getResources().getDisplayMetrics().density;
      }
  
      /**
       * @return the screen density expressed as dots-per-inch
       */
      public static int getScreenDensityDpi() {
          return AppContext.getInstance().getResources().getDisplayMetrics().densityDpi;
      }
  
      /**
       * 设置全屏
       *
       * @param activity The activity.
       */
      public static void setFullScreen(@NonNull final Activity activity) {
          activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                  | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
      }
  
      /**
       * 取消全屏
       *
       * @param activity The activity.
       */
      public static void setNonFullScreen(@NonNull final Activity activity) {
          activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                  | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
      }
  
      /**
       * Toggle full screen.
       *
       * @param activity The activity.
       */
      public static void toggleFullScreen(@NonNull final Activity activity) {
          int fullScreenFlag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
          Window window = activity.getWindow();
          if ((window.getAttributes().flags & fullScreenFlag) == fullScreenFlag) {
              window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                      | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
          } else {
              window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                      | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
          }
      }
  
      /**
       * 是否全屏
       */
      public static boolean isFullScreen(@NonNull final Activity activity) {
          int fullScreenFlag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
          return (activity.getWindow().getAttributes().flags & fullScreenFlag) == fullScreenFlag;
      }
  
      /**
       * 横屏
       *
       * @param activity The activity.
       */
      public static void setLandscape(@NonNull final Activity activity) {
          activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      }
  
      /**
       * 竖屏
       *
       * @param activity The activity.
       */
      public static void setPortrait(@NonNull final Activity activity) {
          activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      }
  
      /**
       * Return whether screen is landscape.
       *
       * @return
       */
      public static boolean isLandscape() {
          return AppContext.getInstance().getResources().getConfiguration().orientation
                  == Configuration.ORIENTATION_LANDSCAPE;
      }
  
      /**
       * Return whether screen is portrait.
       *
       * @return
       */
      public static boolean isPortrait() {
          return AppContext.getInstance().getResources().getConfiguration().orientation
                  == Configuration.ORIENTATION_PORTRAIT;
      }
  
      /**
       * Return the rotation of screen.
       *
       * @param activity The activity.
       * @return the rotation of screen
       */
      public static int getScreenRotation(@NonNull final Activity activity) {
          switch (activity.getWindowManager().getDefaultDisplay().getRotation()) {
              case Surface.ROTATION_0:
                  return 0;
              case Surface.ROTATION_90:
                  return 90;
              case Surface.ROTATION_180:
                  return 180;
              case Surface.ROTATION_270:
                  return 270;
              default:
                  return 0;
          }
      }
  
      /**
       * 截屏
       *
       * @param activity The activity.
       * @return the bitmap of screen
       */
      public static Bitmap screenShot(@NonNull final Activity activity) {
          return screenShot(activity, false);
      }
  
      /**
       * 截屏
       *
       * @param activity          The activity.
       * @param isDeleteStatusBar True to delete status bar, false otherwise.
       * @return the bitmap of screen
       */
      public static Bitmap screenShot(@NonNull final Activity activity, boolean isDeleteStatusBar) {
          View decorView = activity.getWindow().getDecorView();
          decorView.setDrawingCacheEnabled(true);
          decorView.buildDrawingCache();
          Bitmap bmp = decorView.getDrawingCache();
          DisplayMetrics dm = new DisplayMetrics();
          activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
          Bitmap ret;
          if (isDeleteStatusBar) {
              Resources resources = activity.getResources();
              int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
              int statusBarHeight = resources.getDimensionPixelSize(resourceId);
              ret = Bitmap.createBitmap(
                      bmp,
                      0,
                      statusBarHeight,
                      dm.widthPixels,
                      dm.heightPixels - statusBarHeight
              );
          } else {
              ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
          }
          decorView.destroyDrawingCache();
          return ret;
      }
  
      /**
       * Return whether screen is locked.
       */
      public static boolean isScreenLock() {
          KeyguardManager km =
                  (KeyguardManager) AppContext.getInstance().getSystemService(Context.KEYGUARD_SERVICE);
          return km != null && km.inKeyguardRestrictedInputMode();
      }
  
      /**
       * Set the duration of sleep.
       * <p>Must hold {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
       *
       * @param duration The duration.
       */
      @RequiresPermission(WRITE_SETTINGS)
      public static void setSleepDuration(final int duration) {
          Settings.System.putInt(
                  AppContext.getInstance().getContentResolver(),
                  Settings.System.SCREEN_OFF_TIMEOUT,
                  duration
          );
      }
  
      /**
       * Return the duration of sleep.
       *
       * @return the duration of sleep.
       */
      public static int getSleepDuration() {
          try {
              return Settings.System.getInt(
                      AppContext.getInstance().getContentResolver(),
                      Settings.System.SCREEN_OFF_TIMEOUT
              );
          } catch (Settings.SettingNotFoundException e) {
              e.printStackTrace();
              return -123;
          }
      }
  
      /**
       * Return whether device is tablet.
       *
       * @return {@code true}: yes<br>{@code false}: no
       */
      public static boolean isTablet() {
          return (AppContext.getInstance().getResources().getConfiguration().screenLayout
                  & Configuration.SCREENLAYOUT_SIZE_MASK)
                  >= Configuration.SCREENLAYOUT_SIZE_LARGE;
      }
  
      /**
       * Reference from: https://mp.weixin.qq.com/s/d9QCoBP6kV9VSWvVldVVwA
       */
      private static void adaptScreen(final Activity activity,
                                      final float sizeInDp,
                                      final boolean isVerticalSlide) {
          final DisplayMetrics appDm = AppContext.getInstance().getResources().getDisplayMetrics();
          final DisplayMetrics activityDm = activity.getResources().getDisplayMetrics();
          if (isVerticalSlide) {
              activityDm.density = activityDm.widthPixels / sizeInDp;
          } else {
              activityDm.density = activityDm.heightPixels / sizeInDp;
          }
          activityDm.scaledDensity = activityDm.density * (appDm.scaledDensity / appDm.density);
          activityDm.densityDpi = (int) (160 * activityDm.density);
      }
  }
  