content: package cn.hotapk.fastandrutils.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.graphics.Bitmap;
  import android.graphics.Rect;
  import android.provider.Settings;
  import android.util.DisplayMetrics;
  import android.view.Surface;
  import android.view.View;
  import android.view.Window;
  import android.view.WindowManager;
  
  /**
   * @author laijian
   * @version 2017/9/12
   * @Copyright (C)上午10:23 , www.hotapk.cn
   * 获取分辨率 及 分辨率转换 工具类
   */
  public final class FScreenUtils {
  
      private FScreenUtils() {
  
      }
  
      /**
       * 获得屏幕宽高pix
       *
       * @param context
       * @return
       */
      public static int[] getScreen(Context context) {
          WindowManager wm = (WindowManager) FUtils.getAppContext().getSystemService(Context.WINDOW_SERVICE);
          DisplayMetrics displayMetrics = new DisplayMetrics();
          wm.getDefaultDisplay().getMetrics(displayMetrics);
          return new int[]{displayMetrics.widthPixels, displayMetrics.heightPixels};
      }
  
      /**
       * 获得状态栏的高度pix
       *
       * @param context
       * @return
       */
      public static int getStatusHeight(Context context) {
  
          int statusHeight = -1;
          try {
              Class<?> clazz = Class.forName("com.android.internal.R$dimen");
              Object object = clazz.newInstance();
              int height = Integer.parseInt(clazz.getField("status_bar_height")
                      .get(object).toString());
              statusHeight = context.getResources().getDimensionPixelSize(height);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return statusHeight;
      }
  
      /***
       * 获得标题栏的高度pix
       *
       * @param activity
       * @param context
       * @return
       */
      public static int getTitleHeight(Activity activity, Context context) {
          int contentTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
          int titleheight = contentTop - getStatusHeight(context);
          return titleheight;
      }
  
  
      /**
       * 获取当前屏幕截图，包含状态栏
       *
       * @param activity
       * @return
       */
      public static Bitmap snapShotWithStatusBar(Activity activity) {
          View view = activity.getWindow().getDecorView();
          view.setDrawingCacheEnabled(true);
          view.buildDrawingCache();
          Bitmap bmp = view.getDrawingCache();
          int[] screen = getScreen(activity);
          int width = screen[0];
          int height = screen[1];
          Bitmap bp = null;
          bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
          view.destroyDrawingCache();
          return bp;
      }
  
      /**
       * 获取当前屏幕截图，不包含状态栏
       *
       * @param activity
       * @return
       */
      public static Bitmap snapShotWithoutStatusBar(Activity activity) {
          View view = activity.getWindow().getDecorView();
          view.setDrawingCacheEnabled(true);
          view.buildDrawingCache();
          Bitmap bmp = view.getDrawingCache();
          Rect frame = new Rect();
          activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
          int statusBarHeight = frame.top;
          int[] screen = getScreen(activity);
          int width = screen[0];
          int height = screen[1];
          Bitmap bp = null;
          bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
                  - statusBarHeight);
          view.destroyDrawingCache();
          return bp;
      }
  
  
      /**
       * 设置屏幕为全屏
       * <p>需在 {@code setContentView} 之前调用</p>
       *
       * @param activity activity
       */
      public static void setFullScreen(final Activity activity) {
          activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
          activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
      }
  
      /**
       * 判断是否横屏
       *
       * @return {@code true}: 是<br>{@code false}: 否
       */
      public static boolean isLandscape() {
          return FUtils.getAppContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
      }
  
      /**
       * 设置屏幕为横屏
       * <p>还有一种就是在Activity中加属性android:screenOrientation="landscape"</p>
       * <p>不设置Activity的android:configChanges时，切屏会重新调用各个生命周期，切横屏时会执行一次，切竖屏时会执行两次</p>
       * <p>设置Activity的android:configChanges="orientation"时，切屏还是会重新调用各个生命周期，切横、竖屏时只会执行一次</p>
       * <p>设置Activity的android:configChanges="orientation|keyboardHidden|screenSize"（4.0以上必须带最后一个参数）时
       * 切屏不会重新调用各个生命周期，只会执行onConfigurationChanged方法</p>
       *
       * @param activity activity
       */
      public static void setLandscape(final Activity activity) {
          activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      }
  
      /**
       * 判断是否竖屏
       *
       * @return {@code true}: 是<br>{@code false}: 否
       */
      public static boolean isPortrait() {
          return FUtils.getAppContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
      }
  
      /**
       * 设置屏幕为竖屏
       *
       * @param activity activity
       */
      public static void setPortrait(final Activity activity) {
          activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      }
  
      /**
       * 获取屏幕旋转角度
       *
       * @param activity activity
       * @return 屏幕旋转角度
       */
      public static int getScreenRotation(final Activity activity) {
          switch (activity.getWindowManager().getDefaultDisplay().getRotation()) {
              default:
              case Surface.ROTATION_0:
                  return 0;
              case Surface.ROTATION_90:
                  return 90;
              case Surface.ROTATION_180:
                  return 180;
              case Surface.ROTATION_270:
                  return 270;
          }
      }
  
      /**
       * 判断是否锁屏
       *
       * @return {@code true}: 是<br>{@code false}: 否
       */
      public static boolean isScreenLock() {
          KeyguardManager km = (KeyguardManager) FUtils.getAppContext().getSystemService(Context.KEYGUARD_SERVICE);
          return km.inKeyguardRestrictedInputMode();
      }
  
      /**
       * 获取进入休眠时长
       *
       * @return 进入休眠时长，报错返回-123
       */
      public static int getSleepDuration() {
          try {
              return Settings.System.getInt(FUtils.getAppContext().getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
          } catch (Settings.SettingNotFoundException e) {
              e.printStackTrace();
              return -123;
          }
      }
  
      /**
       * 设置进入休眠时长
       * <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
       *
       * @param duration 时长
       */
      public static void setSleepDuration(final int duration) {
          Settings.System.putInt(FUtils.getAppContext().getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, duration);
      }
  
  }
  