加载中…
个人资料
辛朝磊
辛朝磊
  • 博客等级:
  • 博客积分:0
  • 博客访问:59,525
  • 关注人气:10
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

【从应用层到Framework】android LCD、键盘灯的背光亮度设置

(2012-06-21 11:04:18)
标签:

android

lcd

led

键盘灯

屏幕

背光

亮度

设置

it

分类: Android源码学习

今天碰到一个键盘灯太亮的问题,整理了一下代码,发出来方便看流程。对比了一下android2.3和4.0的代码,关于这部分的代码没什么变化。

 

应用部分,进入设置->显示->亮度

\packages\apps\Settings\src\com\android\settings\BrightnessPreference.java

 

// Backlight range is from 0 - 255. Need to make sure that user

// doesn't set the backlight to 0 and get stuck

private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;

private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;

 

//手动模式下,通过进度条控件设置亮度

091     public void onProgressChanged(SeekBar seekBar, int progress,
092             boolean fromTouch) {
093         setBrightness(progress + MINIMUM_BACKLIGHT);
094     }

 

//选中自动或手动调整亮度

104     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
105         setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
106                 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
107         if (!isChecked) {
108             setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
109         }
110     }

 

//调用PowerManager的接口

    private void setBrightness(int brightness) {
131         try {
132             IPowerManager power = IPowerManager.Stub.asInterface(
133                     ServiceManager.getService("power"));
134             if (power != null) {
135                 power.setBacklightBrightness(brightness);
136             }
137         } catch (RemoteException doe) {
138               
139                
140     }

 

framework\base\core\java\android\os\PowerManager.java

 

//调用PowerManagerService的接口 

    public void setBacklightBrightness(int brightness)
417     {
418         try {
419             mService.setBacklightBrightness(brightness);
420         } catch (RemoteException e) {
421         }
422     }

 

framework\base\service\java\com\android\server\PowerManagerService.java

 

//PowerManagerService中定义的mButtonLight和mKeyboardLight都是键盘灯,具体使用哪一个,要看底层的平台厂商使用

0196     private LightsService.Light mLcdLight;
0197     private LightsService.Light mButtonLight;
0198     private LightsService.Light mKeyboardLight;

 

2921     public void setBacklightBrightness(int brightness) {
2922         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2923         // Don't let applications turn the screen all the way off
2924         synchronized (mLocks) {
2925             brightness = Math.max(brightness, mScreenBrightnessDim);
                    //以下设备分别调用了LightsService.light的接口
2926             mLcdLight.setBrightness(brightness);
2927             mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2928             mButtonLight.setBrightness(brightness);
2929             long identity = Binder.clearCallingIdentity();
2930             try {
2931                 mBatteryStats.noteScreenBrightness(brightness);
2932             } catch (RemoteException e) {
2933                 Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
2934             } finally {
2935                 Binder.restoreCallingIdentity(identity);
2936             }
2937   
2938             // update our animation state
2939             synchronized (mLocks) {
2940                 mScreenBrightness.targetValue = brightness;
2941                 mScreenBrightness.jumpToTargetLocked();
2942             }
2943         }
2944     }

 

framework\base\service\java\com\android\server\LightsService.java

 

066         public void setBrightness(int brightness) {
067             setBrightness(brightness, BRIGHTNESS_MODE_USER);
068         }
069   
070         public void setBrightness(int brightness, int brightnessMode) {
071             synchronized (this) {
072                 int color = brightness & 0x000000ff;
073                 color = 0xff000000 | (color << 16) | (color << 8) | color;
074                 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode);
075             }
076         }

 

116         private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {
117             if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) {
118                 mColor = color;
119                 mMode = mode;
120                 mOnMS = onMS;
121                 mOffMS = offMS;
                        //调用JNI的本地接口,mNativePointer对应具体设备,通过调用LighsService中
                    //的init接口,进一步调用init_native接口获取设备信息
122                 setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);
123             }
124         }

 

framework\base\services\jni\com_android_server_LightsService.cpp

  
106 static void setLight_native(JNIEnv *env, jobject clazz, int ptr,
107         int light, int colorARGB, int flashMode, int onMS, int offMS, int brightnessMode)
108 {
109     Devices* devices = (Devices*)ptr;
110     light_state_t state;
111   
112     if (light < 0 || light >= LIGHT_COUNT || devices->lights[light] == NULL) {
113         return ;
114     }
115   
116     memset(&state, 0, sizeof(light_state_t));
117     state.color = colorARGB;
118     state.flashMode = flashMode;
119     state.flashOnMS = onMS;
120     state.flashOffMS = offMS;
121     state.brightnessMode = brightnessMode;
122   
         //devices->lights是一个结构体,具体定义没找到,不过根据代码看,set_light应该是一个函数指针
         //这里会根据不同的设备devices调用对应的set_light指向的具体函数
123     devices->lights[light]->set_light(devices->lights[light], &state);
124 }

 

 有位土豆同学问到如何控制在不同界面控制键盘灯亮灭的问题,解答一下

在上面提到的PowerManagerService.java中的setBacklightBrightness函数中有如下这样一句代码

mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);

这里的这个mKeyboardVisible成员变量是很重要的,这个变量就是解决土豆同学问题的关键。

在接口LocalPowerManager.java中有下面这个函数声明

void setKeyboardVisibility(boolean visible);

这个函数的功能是设置键盘灯亮灭

PowerManagerService.java中有这个函数的具体实现,设置了mKeyboardVisible。PowerManager中如果没有实现这个函数,可以参考setBacklightBrightness函数自己实现,之后在应用中的代码调用PowerManager中的setKeyboardVisibility函数就可以了。

 

另外加句题外话,有些程序有在自己的界面调起来的时候自动调节屏幕亮度的功能,例如书旗网的小说软件,就是临时调用了setBacklightBrightness函数,传入自己想要的亮度值给这个函数,另外保存系统的亮度值,在界面退出时再用原来系统的屏幕亮度值恢复一下,就可以实现这个功能。但是要注意处理异常退出的情况,比如应用被内核发出的kill干掉,电池被拔掉,也要能够恢复系统原来的亮度值,activity中有一个onSavedInstance函数能够做到处理这种情况的。就是不知道机器直接拔电池的时候,onSavedInstance函数是否能够正常运行,有兴趣得同学可以试一下。 

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有