android 屏幕亮度调节方法详解

屏幕亮度自动调节
主要是从Sensor分析之中分离出来分析LIGHT 光线感应器,因此就分析一下自动调节屏幕亮度(手机随着光线的强度自我调节,也就是在亮的光线下屏幕自动调亮一些,暗的时候就自动调暗一些,省得光线对眼睛有刺激).....

    (本人从历经挫折才大概了解流程),现在就开始讲一下流程,如果有不对地方希望牛人指出来。。。

    先从Sttings入手吧,在diaplay中有屏幕亮度调节,有一个进度调,上面有一个checkbox(自动调节屏幕亮度的),

那么我们当然去找settings下的文件开始看了,先找到
DiaplaySettings.java文件以及display_setting.xml文件,display_setting.xml里面能找到相应的组件,但是 在DiaplaySettings.java文件中却不能找到调用相应组件的代码,

那么我们再到settings中去找,能找到BrightnessPreference.java文件,没错这个文件就是自动调节亮度的文件,我们先看看代码:
构造函数中有mAutomaticAvailable = context.getResources().getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
开始以为mAutomaticAvailabl的值(为true,在config.xml中)是判断是否自动调节亮度,因为在我跟进到PowerManagerService.java中,在initInThread() 中
  mUseSoftwareAutoBrightness = resources.getBoolean(
                com.android.internal.R.bool.config_automatic_brightness_available);

        if (mUseSoftwareAutoBrightness) {
            mAutoBrightnessLevels = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessLevels);//得到自动化调节的值调用 getAutoBrightnessValue(int sensorValue, int[] values) 方法
            mLcdBacklightValues = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
            mButtonBacklightValues = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
            mKeyboardBacklightValues = resources.getIntArray(。。。。。
。。。。。。这里看起来好像是当mUseSoftwareAutoBrightness为true时就取得config.xml文件config_autoBrightnessLevels的值,再去根据光线自动调节,但是mAutomaticAvailabl的值只是说明有没有自动调节屏幕的功能(比如说你将config.xml文件的config_automatic_brightness_available的值改为false,编译替换framework-res.apk那么在屏幕亮度调节中将没有自动调节的checkbox),这也能解释构造函数一开始就初始化mAutomaticAvailabl的值。

接下来在BrightnessPreference.java文件中有onProgressChanged,onStartTrackingTouch(SeekBar seekBar),onStopTrackingTouch(SeekBar seekBar)方法,这些一看也能懂就不介绍了,在onBindDialogView(View view)方法中有
mCheckBox = (CheckBox)view.findViewById(R.id.automatic_mode);
        if (mAutomaticAvailable) {//当有自动调节功能
            mCheckBox.setOnCheckedChangeListener(this);//为checkbox添加监听事件
            try {
                mOldAutomatic = Settings.System.getInt(getContext().getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS_MODE);
。。。。。。
主要看public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
                : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        if (!isChecked) {
            setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
        }
    }
这里才是你选择自动调节亮度的checkbox,如果选择则setMode(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);并将数据存放到数据库中。

这里是重点:这里有个数据库,当监测到数据库中的值发生改变时也就是变为Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC时,就会跳到
powerManagerService.java的initInThread()中ContentResolver resolver = mContext.getContentResolver();
        Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
                "(" + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?) or ("
                        + Settings.System.NAME + "=?)",
                new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, XEC_DLS_CONTROL,
                        SCREEN_BRIGHTNESS_MODE, WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE},
                null);
        mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
        SettingsObserver settingsObserver = new SettingsObserver();
        mSettings.addObserver(settingsObserver);
数据库中数据发生改变时触发事件的发生。

在 powerManagerService.java的systemReady()(只在开机调用一次就不再调用)中
 if (mUseSoftwareAutoBrightness) {
            Log.i("frist","frist="+"aaaa");
            mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
            enableLightSensor(true);
        }
而mUseSoftwareAutoBrightness是在initInThread()线程中取得了true值,那么就到enableLightSensor(true);方法去看
 if (enable) {
                    mSensorManager.registerListener(mLightListener, mLightSensor,
                            SensorManager.SENSOR_DELAY_NORMAL);
                }
如果传递来的值为true也就是enable=true就监听而且用匹配屏幕方向变化的感应SENSOR_DELAY_NORMAL。
再看mLightListener事件:SensorEventListener mLightListener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent event) {
            synchronized (mLocks) {
                int value = (int)event.values[0];//从event里面获取当前数据
                Log.i("value","value="+value);       
                long milliseconds = SystemClock.elapsedRealtime();
                if (mDebugLightSensor) {
                    Slog.d(TAG, "onSensorChanged: light value: " + value);
                }
                mHandler.removeCallbacks(mAutoBrightnessTask);
                if (mLightSensorValue != value) {//mLightSensorValue是当前的屏幕亮度
                    if (mLightSensorValue == -1 ||
                            milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
                        // process the value immediately if screen has just turned on
                        lightSensorChangedLocked(value);
                    } else {
                        // delay processing to debounce the sensor
                        mLightSensorPendingValue = value;//mLightSensorPendingValue是准备要设置的亮度
                        mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);//到mAutoBrightnessTask线程去完成自动调节
                    }
                } else {
                    mLightSensorPendingValue = -1;
                }
            }
        }
当前屏幕亮度肯定不等与-1,所以做else中的mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY),那么我们再到mAutoBrightnessTask
线程看:private Runnable mAutoBrightnessTask = new Runnable() {
        public void run() {
            synchronized (mLocks) {
                int value = (int)mLightSensorPendingValue;
                Log.i("mLightSensorPendingValue","mLightSensorPendingValue"+mLightSensorPendingValue);
                if (value >= 0) {
                    mLightSensorPendingValue = -1;
                    lightSensorChangedLocked(value);
                }
            }
        }
    };就是不断的去调用lightSensorChangedLocked(value)
private void lightSensorChangedLocked(int value) {
。。。。。。。
if (mLightSensorValue != value) {
            mLightSensorValue = value;
            if ((mPowerState & BATTERY_LOW_BIT) == 0) {
                // Use light sensor value no matter it is in a dock or not.
                int lcdValue = getAutoBrightnessValue(
                        value,
                        mLcdBacklightValues);// 取得mLcdBacklightValues里面的值
                int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
                int keyboardValue;
                if (mKeyboardVisible) {
                    keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
                } else {
                    keyboardValue = 0;
                }
。。。
if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {//设置屏幕亮度
                    mScreenBrightness.setTargetLocked(lcdValue, AUTOBRIGHTNESS_ANIM_STEPS,
                            INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue);
                }
。。。。。
   }

代码技巧

转载请关注公众号:代码技巧 回复:授权

本文链接地址:https://www.oudahe.com/p/49713/