content: package com.cicada.player.demo.util;
  
  import android.app.Activity;
  import android.content.ContentResolver;
  import android.content.Context;
  import android.provider.Settings;
  import android.util.DisplayMetrics;
  import android.view.Window;
  import android.view.WindowManager;
  
  /*
   * Copyright (C) 2010-2018 Alibaba Group Holding Limited.
   */
  
  /**
   * 屏幕相关的操作类
   */
  public class ScreenUtils {
      /**
       * 获取宽度
       *
       * @param activity 上下文
       * @return 宽度值，px
       */
      public static int getWidth(Activity activity) {
          DisplayMetrics dm = new DisplayMetrics();
          activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
          return dm.widthPixels;
      }
  
      /**
       * 获取高度
       *
       * @param activity 上下文
       * @return 高度值，px
       */
      public static int getHeight(Activity activity) {
          DisplayMetrics dm = new DisplayMetrics();
          activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
          return dm.heightPixels;
      }
  
      /**
       * 获取宽度
       *
       * @param mContext 上下文
       * @return 宽度值，px
       */
      public static int getWidth(Context mContext) {
          DisplayMetrics displayMetrics = new DisplayMetrics();
          ((WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))
                  .getDefaultDisplay().getMetrics(displayMetrics);
          return displayMetrics.widthPixels;
      }
  
      /**
       * 获取高度
       *
       * @param mContext 上下文
       * @return 高度值，px
       */
      public static int getHeight(Context mContext) {
          DisplayMetrics displayMetrics = new DisplayMetrics();
          ((WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))
                  .getDefaultDisplay().getMetrics(displayMetrics);
          return displayMetrics.heightPixels;
      }
  
  
      /**
       * 是否在屏幕右侧
       *
       * @param activity 上下文
       * @param xPos     位置的x坐标值
       * @return true：是。
       */
      public static boolean isInRight(Activity activity, int xPos) {
          return (xPos > getWidth(activity) / 2);
      }
  
      /**
       * 是否在屏幕左侧
       *
       * @param activity 上下文
       * @param xPos     位置的x坐标值
       * @return true：是。
       */
      public static boolean isInLeft(Activity activity, int xPos) {
          return (xPos < getWidth(activity) / 2);
      }
  
      /**
       * 设置屏幕亮度模式（设置Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL：值为0）
       * 才能进行手动调节亮度
       */
      public static void setScreenManualMode(Activity activity){
          ContentResolver contentResolver = activity.getContentResolver();
          try {
              int mode = Settings.System.getInt(contentResolver,
                      Settings.System.SCREEN_BRIGHTNESS_MODE);
              if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                  Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
                          Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
              }
          } catch (Settings.SettingNotFoundException e) {
              e.printStackTrace();
          }
      }
  
      /**
       * 设置屏幕亮度
       */
      public static void setScreenBrightness(int brightness,Activity activity){
          Window window = activity.getWindow();
          WindowManager.LayoutParams lp = window.getAttributes();
          lp.screenBrightness = brightness / 255.0f;
          window.setAttributes(lp);
      }
  
  }
  