Detailed explanation of android screen brightness adjustment methods

Source: Internet
Author: User

Automatic screen brightness adjustment:
It is mainly to separate the LIGHT Sensor from the Sensor analysis, so we can analyze the automatic adjustment of the screen brightness (the mobile phone adjusts itself with the LIGHT intensity, that is, when the light is on, the screen is automatically brightened. when the light is on, the screen is automatically dimmed, saving the light from eye irritation ).....

(I have learned about the process since I experienced setbacks.) Now I will talk about the process. If there is something wrong with it, I hope you can point it out...

Start with Sttings. There is screen brightness adjustment in diaplay, there is a progress adjustment, and there is a checkbox (automatically adjusts the screen brightness) on it ),

Of course, we went to the file under settings to start reading it. First, we found
In the DiaplaySettings. java file and the display_setting.xml file, the corresponding components can be found in display_setting.xml, but the code for calling the corresponding components cannot be found in the DiaplaySettings. java file,

Then we can find the BrightnessPreference. java file in settings. Yes, this file is the file that automatically adjusts the brightness. Let's first look at the Code:
The constructor contains mAutomaticAvailable = context. getResources (). getBoolean (com. android. internal. R. bool. config_automatic_brightness_available );
At first, the mAutomaticAvailabl value (true, in config. xml) is used to determine whether to automatically adjust the brightness, because when I followed up with PowerManagerService. java, in 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); // call the getAutoBrightnessValue (int sensorValue, int [] values) method to obtain the automatically adjusted value.
MLcdBacklightValues = resources. getIntArray (
Com. android. internal. R. array. config_autoBrightnessLcdBacklightValues );
MButtonBacklightValues = resources. getIntArray (
Com. android. internal. R. array. config_autoBrightnessButtonBacklightValues );
MKeyboardBacklightValues = resources. getIntArray (.....
...... Then there will be no automatically adjusted checkbox in the screen brightness adjustment), which can also explain the value of mAutomaticAvailabl initialized by the constructor at the beginning.

Next, the BrightnessPreference. java file contains the onProgressChanged, onStartTrackingTouch (SeekBar seekBar), and onStopTrackingTouch (SeekBar seekBar) methods. These methods are not described at the first glance, but the onBindDialogView Method
MCheckBox = (CheckBox) view. findViewById (R. id. automatic_mode );
If (mAutomaticAvailable) {// when automatic adjustment is available
MCheckBox. setOnCheckedChangeListener (this); // Add a listener for the 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 );
}
}
Here is the checkbox where you choose to automatically adjust the brightness. If you select it, setMode (Settings. System. SCREEN_BRIGHTNESS_MODE_AUTOMATIC) and store the data in the database.

Here is the focus: There is a database. When the value in the database is monitored to change to Settings. System. SCREEN_BRIGHTNESS_MODE_AUTOMATIC, it will jump
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 );
The event triggered when the data in the database changes.

In systemReady () of powerManagerService. java (no longer called only once upon startup)
If (mUseSoftwareAutoBrightness ){
Log. I ("frist", "frist =" + "aaaa ");
MLightSensor = mSensorManager. getdefasensensor (Sensor. TYPE_LIGHT );
EnableLightSensor (true );
}
While mUseSoftwareAutoBrightness gets the true value in the initInThread () thread, so it goes to enableLightSensor (true );
If (enable ){
MSensorManager. registerListener (mLightListener, mLightSensor,
SensorManager. SENSOR_DELAY_NORMAL );
}
If the passed value is true, that is, enable = true, the listener listens and uses the sensor SENSOR_DELAY_NORMAL that matches the change in the screen direction.
Let's look at the mLightListener event: SensorEventListener mLightListener = new SensorEventListener (){
Public void onSensorChanged (SensorEvent event ){
Synchronized (mLocks ){
Int value = (int) event. values [0]; // obtain the current data from the 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 is the current screen brightness
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 is the brightness to be set.
MHandler. postDelayed (mAutoBrightnessTask, LIGHT_SENSOR_DELAY); // Go To The mAutoBrightnessTask thread to complete automatic adjustment
}
} Else {
MLightSensorPendingValue =-1;
}
}
}
The current screen brightness is definitely not equal to-1, so do mHandler. postDelayed (mAutoBrightnessTask, LIGHT_SENSOR_DELAY) in else, then we will go to mAutoBrightnessTask
Thread view: 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 );
}
}
}
}; Is constantly calling 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); // obtain the values in mLcdBacklightValues.
Int buttonValue = getAutoBrightnessValue (value, mButtonBacklightValues );
Int keyboardValue;
If (mKeyboardVisible ){
KeyboardValue = getAutoBrightnessValue (value, mKeyboardBacklightValues );
} Else {
KeyboardValue = 0;
}
...
If (mAutoBrightessEnabled & mScreenBrightnessOverride <0) {// sets the screen brightness
MScreenBrightness. setTargetLocked (lcdValue, AUTOBRIGHTNESS_ANIM_STEPS,
INITIAL_SCREEN_BRIGHTNESS, (int) mScreenBrightness. curValue );
}
.....
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.