当前位置:   article > 正文

Android 12 设置默认的屏幕亮度百分比_getbrightnessconstraint

getbrightnessconstraint

1、安卓每个版本的更新, 其核心代码也会随之更新,本次为Android 12 版本默认屏幕亮度的修改。

其中涉及的核心代码主要有:

packages\apps\Settings\src\com\android\settings\display\BrightnessLevelPreferenceController.java
frameworks\base\services\core\java\com\android\server\power\PowerManagerService.java
frameworks/base/res/res/values/config.xml
  • 1
  • 2
  • 3

2、设置默认亮度的主要方法代码

BrightnessLevelPreferenceController中处理:

 public class BrightnessLevelPreferenceController extends AbstractPreferenceController implements
            PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
     
  
     
        private ContentObserver mBrightnessObserver =
                new ContentObserver(new Handler(Looper.getMainLooper())) {
                    @Override
                    public void onChange(boolean selfChange) {
                        updatedSummary(mPreference);
                    }
                };
     
        public BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle) {
            super(context);
            if (lifecycle != null) {
                lifecycle.addObserver(this);
            }
            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mMinBrightness = powerManager.getBrightnessConstraint(
                    PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM);
            mMaxBrightness = powerManager.getBrightnessConstraint(
                    PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM);
            mMinVrBrightness = powerManager.getBrightnessConstraint(
                    PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM_VR);
            mMaxVrBrightness = powerManager.getBrightnessConstraint(
                    PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM_VR);
            mContentResolver = mContext.getContentResolver();
            mDefaultBacklight = powerManager.getBrightnessConstraint(
                    PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

PowerManager得到相应的值:

  private void updatedSummary(Preference preference) {
            if (preference != null) {
                preference.setSummary(NumberFormat.getPercentInstance().format(getCurrentBrightness()));
            }
        }
     
        private double getCurrentBrightness() {
            final int value;
            if (isInVrMode()) {
                value = convertLinearToGammaFloat(System.getFloat(mContentResolver,
                        System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, mMaxBrightness),
                        mMinVrBrightness, mMaxVrBrightness);
            } else {
                value = convertLinearToGammaFloat(Settings.System.getFloat(mContentResolver,
                        System.SCREEN_BRIGHTNESS_FLOAT, mDefaultBacklight),
                        mMinBrightness, mMaxBrightness);
            }
            return getPercentage(value, GAMMA_SPACE_MIN, GAMMA_SPACE_MAX);
        }
     
        private double getPercentage(double value, int min, int max) {
            if (value > max) {
                return 1.0;
            }
            if (value < min) {
                return 0.0;
            }
            return (value - min) / (max - min);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

public final class PowerManagerService extends SystemService
            implements Watchdog.Monitor {
     
    @VisibleForTesting
        PowerManagerService(Context context, Injector injector) {
            super(context);
     
            mContext = context;
    ......
            // Save brightness values:
            // Get float values from config.
            // Store float if valid
            // Otherwise, get int values and convert to float and then store.
            final float min = mContext.getResources().getFloat(com.android.internal.R.dimen
                    .config_screenBrightnessSettingMinimumFloat);
            final float max = mContext.getResources().getFloat(com.android.internal.R.dimen
                    .config_screenBrightnessSettingMaximumFloat);
            final float def = mContext.getResources().getFloat(com.android.internal.R.dimen
                    .config_screenBrightnessSettingDefaultFloat);
            final float doze = mContext.getResources().getFloat(com.android.internal.R.dimen
                    .config_screenBrightnessDozeFloat);
            final float dim = mContext.getResources().getFloat(com.android.internal.R.dimen
                    .config_screenBrightnessDimFloat);
     
            if (min == INVALID_BRIGHTNESS_IN_CONFIG || max == INVALID_BRIGHTNESS_IN_CONFIG
                    || def == INVALID_BRIGHTNESS_IN_CONFIG) {
                mScreenBrightnessMinimum = BrightnessSynchronizer.brightnessIntToFloat(
                        mContext.getResources().getInteger(com.android.internal.R.integer
                                .config_screenBrightnessSettingMinimum),
                        PowerManager.BRIGHTNESS_OFF + 1, PowerManager.BRIGHTNESS_ON,
                        PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX);
                mScreenBrightnessMaximum = BrightnessSynchronizer.brightnessIntToFloat(
                        mContext.getResources().getInteger(com.android.internal.R.integer
                                .config_screenBrightnessSettingMaximum),
                        PowerManager.BRIGHTNESS_OFF + 1, PowerManager.BRIGHTNESS_ON,
                        PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX);
                mScreenBrightnessDefault = BrightnessSynchronizer.brightnessIntToFloat(
                        mContext.getResources().getInteger(com.android.internal.R.integer
                                .config_screenBrightnessSettingDefault),
                        PowerManager.BRIGHTNESS_OFF + 1, PowerManager.BRIGHTNESS_ON,
                        PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX);
            } else {
                mScreenBrightnessMinimum = min;
                mScreenBrightnessMaximum = max;
                mScreenBrightnessDefault = def;
            }
    ..
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

其中它是通过config中实现的:

 public float getBrightnessConstraint(int constraint) {
            switch (constraint) {
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM:
                    return mScreenBrightnessMinimum;
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM:
                    return mScreenBrightnessMaximum;
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT:
                    return mScreenBrightnessDefault;
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DIM:
                    return mScreenBrightnessDim;
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DOZE:
                    return mScreenBrightnessDoze;
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM_VR:
                    return mScreenBrightnessMinimumVr;
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM_VR:
                    return mScreenBrightnessMaximumVr;
                case PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT_VR:
                    return mScreenBrightnessDefaultVr;
                default:
                    return PowerManager.BRIGHTNESS_INVALID_FLOAT;
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3、修改代码的位置:

   --- a/frameworks/base/res/res/values/config.xml
    +++ b//frameworks/base/res/res/values/config.xml
    @@ -28,5 +28,5 @@
         <item name="config_screenBrightnessSettingMaximumFloat" format="float" type="dimen">1.0</item>
         <!-- Default screen brightness setting set.
         Set this to 0.4 for Default brightness Float.-->
    -    <item name="config_screenBrightnessSettingDefaultFloat" format="float" type="dimen">0.3</item>
    +    <item name="config_screenBrightnessSettingDefaultFloat" format="float" type="dimen">0.4</item>
     </resources>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/699930
推荐阅读
相关标签
  

闽ICP备14008679号