电池指示灯
frameworks/base/core/res/res/values/config.xml
<!-- Default value for led color when battery is low on charge --> <integer name="config_notificationsBatteryLowARGB">0xFFFF0000</integer> <!-- Default value for led color when battery is medium charged --> <integer name="config_notificationsBatteryMediumARGB">0xFFFFFF00</integer> <!-- Default value for led color when battery is fully charged --> <integer name="config_notificationsBatteryFullARGB">0xFF00FF00</integer>0xAABBCCDD
AA(透明度)
BB(红色)
CC(绿色)
DD(蓝色)
frameworks/base/services/core/java/com/android/server/BatteryService.java
private final class Led { private final Light mBatteryLight; private final int mBatteryLowARGB; private final int mBatteryMediumARGB; private final int mBatteryFullARGB; private final int mBatteryLedOn; private final int mBatteryLedOff; public Led(Context context, LightsManager lights) { mBatteryLight = lights.getLight(LightsManager.LIGHT_ID_BATTERY); mBatteryLowARGB = context.getResources().getInteger( com.android.internal.R.integer.config_notificationsBatteryLowARGB); mBatteryMediumARGB = context.getResources().getInteger( com.android.internal.R.integer.config_notificationsBatteryMediumARGB); mBatteryFullARGB = context.getResources().getInteger( com.android.internal.R.integer.config_notificationsBatteryFullARGB); mBatteryLedOn = context.getResources().getInteger( com.android.internal.R.integer.config_notificationsBatteryLedOn); mBatteryLedOff = context.getResources().getInteger( com.android.internal.R.integer.config_notificationsBatteryLedOff); } /** * Synchronize on BatteryService. */ public void updateLightsLocked() { final int level = mHealthInfo.batteryLevel; final int status = mHealthInfo.batteryStatus; if (level < mLowBatteryWarningLevel) { if (status == BatteryManager.BATTERY_STATUS_CHARGING) { // Solid red when battery is charging mBatteryLight.setColor(mBatteryLowARGB); } else { // Flash red when battery is low and not charging mBatteryLight.setFlashing(mBatteryLowARGB, Light.LIGHT_FLASH_TIMED, mBatteryLedOn, mBatteryLedOff); } } else if (status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) { if (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90) { // Solid green when full or charging and nearly full mBatteryLight.setColor(mBatteryFullARGB); } else { // Solid orange when charging and halfway full mBatteryLight.setColor(mBatteryMediumARGB); } } else { // No lights if not charging and not low mBatteryLight.turnOff(); } } }//充电,没充满 红灯
//充电,达到90%的电量 绿灯
//低电量,没充电 闪红灯
呼吸灯
应用可以设置呼吸灯的颜色,比如在熄屏状态下,qq收到消息后,手机的绿灯会闪。
frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java
@GuardedBy("mNotificationLock") void updateLightsLocked() { // handle notification lights NotificationRecord ledNotification = null; while (ledNotification == null && !mLights.isEmpty()) { final String owner = mLights.get(mLights.size() - 1); ledNotification = mNotificationsByKey.get(owner); if (ledNotification == null) { Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); mLights.remove(owner); } } // Don't flash while we are in a call or screen is on if (ledNotification == null || mInCall || mScreenOn) { mNotificationLight.turnOff(); } else { NotificationRecord.Light light = ledNotification.getLight(); if (light != null && mNotificationPulseEnabled) { // pulse repeatedly mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, light.onMs, light.offMs); } } }有时需要固定呼吸灯的颜色,这样所有的呼吸灯颜色都是绿色了。
mNotificationLight.setFlashing(0xff00ff00, Light.LIGHT_FLASH_TIMED,light.onMs, light.offMs);
最终调到这里
frameworks/base/services/core/java/com/android/server/BatteryService.java
private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { if (shouldBeInLowPersistenceMode()) { brightnessMode = BRIGHTNESS_MODE_LOW_PERSISTENCE; } else if (brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE) { brightnessMode = mLastBrightnessMode; } if (!mInitialized || color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS || mBrightnessMode != brightnessMode) { if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#" + Integer.toHexString(color) + ": brightnessMode=" + brightnessMode); mInitialized = true; mLastColor = mColor; mColor = color; mMode = mode; mOnMS = onMS; mOffMS = offMS; mBrightnessMode = brightnessMode; Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x" + Integer.toHexString(color) + ")"); try { setLight_native(mId, color, mode, onMS, offMS, brightnessMode); } finally { Trace.traceEnd(Trace.TRACE_TAG_POWER); } } }再往下就是灯的hal层和三色灯(多数pmic有集成)驱动了。
还有一个就是关机充电的指示灯,充电红灯,充满点绿灯,该部分代码在一个可执行程序中。