/**
* Copyright 2016 JustWayward Team
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.codingbingo.fastreader.utils;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Window;
import android.view.WindowManager;
import com.codingbingo.fastreader.base.utils.BaseUtils;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 屏幕亮度工具类
*
* @author yuyh.
* @date 16/4/10.
*/
public class ScreenUtils extends BaseUtils {
public enum EScreenDensity {
XXHDPI, //超高分辨率 1080×1920
XHDPI, //超高分辨率 720×1280
HDPI, //高分辨率 480×800
MDPI, //中分辨率 320×480
}
public static EScreenDensity getDisply(Context context) {
EScreenDensity eScreenDensity;
//初始化屏幕密度
DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
if (densityDpi <= 160) {
eScreenDensity = EScreenDensity.MDPI;
} else if (densityDpi <= 240) {
eScreenDensity = EScreenDensity.HDPI;
} else if (densityDpi < 400) {
eScreenDensity = EScreenDensity.XHDPI;
} else {
eScreenDensity = EScreenDensity.XXHDPI;
}
return eScreenDensity;
}
/**
* 获取屏幕宽度
*
* @return
*/
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取屏幕高度
*
* @return
*/
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 将dp转换成px
*
* @param dp
* @return
*/
public static int dp2px(Context mContext, int dp) {
return (int) (dp * mContext.getResources().getDisplayMetrics().density + 0.5f);
}
/**
* 将px转换成dp
*
* @param px
* @return
*/
public static int pxToDpInt(Context mContext, float px) {
return (int) (px / mContext.getResources().getDisplayMetrics().density + 0.5f);
}
public static int getActionBarSize(Context context) {
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
return actionBarHeight;
}
return 0;
}
/**
* 获取titleBar的高度
* @param mContext
* @return
*/
public static int getTitleBarHeight(Context mContext) {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics());
}
return actionBarHeight;
}
/**
* 获取statusBar高度
* @param mContext
* @return
*/
public static int getStatusBarHeight(Context mContext) {
int height = 0;
try {
Class c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
height = mContext.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage());
}
return height;
}
/**
* 当前是否是横屏
*
* @param context context
* @return boolean
*/
public static final boolean isLandscape(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
/**
* 当前是否是竖屏
*
* @param context context
* @return boolean
*/
public static final boolean isPortrait(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
/**
* 调整窗口的透明度 1.0f,0.5f 变暗
*
* @param from from>=0&&from<=1.0f
* @param to to>=0&&to<=1.0f
* @param context 当前的activity
*/
public static void dimBackground(final float from, final float to, Activity context) {
final Window window = context.getWindow();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);
valueAnimator.setDuration(500);
valueAnimator.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
WindowManager.LayoutParams params = window.getAttributes();
params.alpha = (Float) animation.getAnimatedValue();
window.setAttributes(params);
}
});
valueAnimator.start();
}
/**
* 判断是否开启了自动亮度调节
*
* @param activity
* @return
*/
public static boolean isAutoBrightness(Activity activity) {
boolean isAutoAdjustBright = false;
try {
isAutoAdjustBright = Settings.System.getInt(
activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return isAutoAdjustBright;
}
/**
* 关闭亮度自动调节
*
* @param activity
*/
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/**
* 开启亮度自动调节
*
* @param activity
*/
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
/**
* 获得当前屏幕亮度值
*
* @param mContext
* @return 0~100
*/
public static float getScreenBrightness(Context mContext) {
int screenBrightness = 255;
try {
screenBrightness = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
return screenBrightness / 255.0F * 100;
}
/**
* 设置当前屏幕亮度值
*
* @param paramInt 0~100
* @param mContext
*/
public static void saveScreenBrightness(int paramInt, Context mContext) {
if (paramInt <= 5) {
paramInt = 5;
}
try {
float f = paramInt / 100.0F * 255;
Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) f);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置Activity的亮度
*
* @param paramInt
* @param mActivity
*/
public static void setScreenBrightness(int paramInt, Activity mActivity) {
if (paramInt <= 5) {
paramInt = 5;
}
Window localWindow = mActivity.getWindow();
WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
float f = paramInt / 100.0F;
localLayoutParams.screenBrightness = f;
localWindow.setAttributes(localLayoutParams);
}
}