一、 powermanagerservice.java 1 、 nest class
1) 、 private class UnsynchronizedWakeLock
从类名可以看出这个类实现的是异步获取唤醒锁。当 PowerManager.WakeLock 与 synchronizing on mLocks 之间发生死锁的时候,这个类可以重新实现 PowerManager.WakeLock 。由于它没有外部同步块,所以只有当拥有锁的时候才调用这个类。
该类实现的方法有:
public void acquire() public void release() public boolean isHeld() public String toString() |
构造方法:
UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) |
2 )、 private final class BatteryReceiver extends BroadcastReceiver
这个类是个广播接收,至于广播的使用,这里不作深入的探讨。该类重载了接收的方法。当然,接收的过程是有约束的,即同步 mLocks 。
@Override public void onReceive(Context context, Intent intent) synchronized (mLocks) 即使当 keyguard is on synchronized (mLocks) { forceUserActivityLocked(); |
3 )、 private final class BootCompletedReceiver extends BroadcastReceiver
@Override public void onReceive(Context context, Intent intent) { bootCompleted(); } |
该广播能接收到启动完成后的消息,而后调用 bootCompleted() 。
4 )、 private final class DockReceiver extends BroadcastReceiver
5 )、 private class SettingsObserver implements Observer
6 )、 private class WakeLock implements IBinder.DeathRecipient
方法 public void binderDied() 会调用 releaseWakeLockLocked
synchronized (mLocks) { releaseWakeLockLocked(this.binder, 0, true); |
7 )、 private class PokeLock implements IBinder.DeathRecipient
方法 public void binderDied() 调用
setPokeLock(0,this.binder, this.tag);
8 )、 private class TimeoutTask implements Runnable
该类实现了 Runnable 接口,有一个 run 方法。
run 方法实现的是 Timeout 后进行的动作, setTimeoutLocked(now,SCREEN_DIM); ? setTimeoutLocked(now,SCREEN_OFF);
9 )、 class BrightnessState
该类定义的方法有
BrightnessState(int m) public void dump(PrintWriter pw, String prefix) boolean setTargetLocked(int target, int stepsToTarget, int initialValue,int nominalCurrentValue)
delta = (targetValue -(nominalCurrentValue >= 0 ?nominalCurrentValue : curValue))/ stepsToTarget;
boolean stepLocked() |
10 )、 private class LightAnimator implements Runnable
该类实现了 Runnable 接口,有一个 run 方法
run 方法实现的是亮度变化的控制:
ScreenBrightness KeyboardBrightness ButtonBrightness |
这三个设备亮度分步控制。
if (mKeyboardBrightness.stepLocked()) { more = true; } if (mButtonBrightness.stepLocked()) { more = true; } if (more) { mHandler.postAtTime(mLightAnimator, now+(1000/60)); |
11 )、 private class GservicesChangedReceiver extends BroadcastReceiver
该类继承了广播接口的类,方法 onReceive 调用了 updateGservicesValues() 。
该类当接收到 google service 改变的广播时,就受调用 updateGservicesValues() 。
12 )、 private class LockList extends ArrayList<WakeLock>
方法:
void addLock(WakeLock wl) WakeLock removeLock(IBinder binder) int getIndex(IBinder binder) int gatherState() int reactivateScreenLocksLocked() |
该类实践上是建立了一了锁队列 / 锁列表,该队列可以通过 addLock 新建一个锁,也可以通过 removeLock 从队列中摘除一个锁,可以通过 getIndex 查询指定锁在队列中的索引号
定义了一个该类的实例
private final LockList mLocks = new LockList();
整个 service 中方法的调用都同步这个对象 mLocks
synchronized (mLocks)
2 、相关的 service : BatteryService
mBatteryService.isPowered()
mBatteryService.getBatteryLevel()
BatteryStatsService.getService()
Gservices Hardwareservice ActivityService
3 、参数
//flags for setPowerState
private static final intSCREEN_ON_BIT = 0x00000001;
private static final intSCREEN_BRIGHT_BIT = 0x00000002;
private static final intBUTTON_BRIGHT_BIT = 0x00000004;
private static final intKEYBOARD_BRIGHT_BIT = 0x00000008;
private static final intBATTERY_LOW_BIT = 0x00000010;
//SCREEN_OFF == everything off
private static final intSCREEN_OFF = 0x00000000;
//SCREEN_DIM == screen on, screen backlight dim
private static final intSCREEN_DIM = SCREEN_ON_BIT;
//SCREEN_BRIGHT == screen on, screen backlight bright
private static final intSCREEN_BRIGHT = SCREEN_ON_BIT |SCREEN_BRIGHT_BIT;
//SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
private static final intSCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT |BUTTON_BRIGHT_BIT;
//SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlightsbright
private static final intALL_BRIGHT = SCREEN_BUTTON_BRIGHT| KEYBOARD_BRIGHT_BIT;
4 、提供的相关服务
提供的这些服务之间并不是独立的,而是相互交织在一起,共同完成电源管理的:
1 )、光感传感器
LIGHT_SENSOR
定义了光感传感器变化明暗变化的延时:
// How long to wait to debounce light sensor changes . private static final int LIGHT_SENSOR_DELAY = 2000; |
相关的参数有:
private Sensor mLightSensor; private boolean mLightSensorEnabled; private float mLightSensorValue = -1; private int mHighestLightSensorValue = -1; private float mLightSensorPendingValue = -1; private int mLightSensorBrightness = -1; private int mLightSensorWarmupTime; private static final boolean mDebugLightSensor = (false || mSpew); |
相关的方法:
private void enableLightSensor(boolean enable) private int setScreenStateLocked private int getPreferredBrightness() private Runnable mAutoBrightnessTask = new Runnable() private void lightSensorChangedLocked(int value) public void setKeyboardVisibility(boolean visible) private void setScreenBrightnessMode(int mode) private void enableLightSensor(boolean enable) SensorEventListener mLightListener = new SensorEventListener() |
// force recompute of backlight values if (mLightSensorValue >= 0) { int value = (int)mLightSensorValue; mLightSensorValue = -1; lightSensorChangedLocked(value); |
2 )、接近 / 距离传感器
PROXIMITY_SENSOR
定义了接近传感器变化明暗变化的延时:
// For debouncing the proximity sensor. private static final int PROXIMITY_SENSOR_DELAY = 1000; |
定义了接近传感器触发的最大距离:
// trigger proximity if distance is less than 5 cm private static final float PROXIMITY_THRESHOLD = 5.0f; |
在 powermanager 类中定义的与接近传感器相关的参数有:
private static final int WAKE_BIT_PROXIMITY_SCREEN_OFF = 32; public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = WAKE_BIT_PROXIMITY_SCREEN_OFF |
相关参数:
private int mProximityWakeLockCount = 0; private boolean mProximitySensorEnabled = false; private boolean mProximitySensorActive = false; private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active private long mLastProximityEventTime;
private UnsynchronizedWakeLock mProximityPartialLock; private Sensor mProximitySensor; |
相关方法:
SensorEventListener mProximityListener = new SensorEventListener() private Runnable mProximityTask = new Runnable() |
// 当拨出电话或接通电话时会调用该方法 private void enableProximityLockLocked() ...... // 传感器的注册,三个参数分别是接收信号 Listener 的实例、传感器类型的列表、接收频度; mSensorManager.registerListener(mProximityListener, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
// 当挂断电话时会调用该方法 private void disableProximityLockLocked() ...... // 缷载传感器 mSensorManager.unregisterListener(mProximityListener);
private void proximityChangedLocked(boolean active) { ...... // 小于触发距离触发接近传感器之后的动作_灭屏。如打电话时屏幕 LCD 灯的熄灭 if (active) { goToSleepLocked(SystemClock.uptimeMillis(), WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR); mProximitySensorActive = true; ...... SensorEventListener mProximityListener = new SensorEventListener() |
3 )、提供明亮度动态变化:
animation for brightness changes :
定义了可以明暗动态变化设备的宏,可以看出,不外乎这三个部分:屏幕灯、触摸按键灯和键盘灯
static final boolean ANIMATE_SCREEN_LIGHTS = true; static final boolean ANIMATE_BUTTON_LIGHTS = false;// 如果有该部分的灯并需要亮度调节,找开这个宏 static final boolean ANIMATE_KEYBOARD_LIGHTS = false;// 如果有该部分的灯并需要亮度调节,找开这个宏 |
明暗变化的级别,分普通级别的较慢级别:
static final int ANIM_STEPS = 60/4; // Slower animation for autobrightness changes static final int AUTOBRIGHTNESS_ANIM_STEPS = 60; |
相关类:
101010private class LightAnimator implements Runnable
相关方法:
private void updateLightsLocked(int newState, int forceState) private void lightSensorChangedLocked(int value) public void setBacklightBrightness(int brightness) |
4 )、允许用户用软件自动调节亮度
mUseSoftwareAutoBrightness
在 initThread 中初始化了这个变量
mUseSoftwareAutoBrightness =resources.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
当这个变量获取到非零值
if (mUseSoftwareAutoBrightness) { mAutoBrightnessLevels = resources.getIntArray( com.android.internal.R.array.config_autoBrightnessLevels); |
自动调节亮度的功能与 LIGHT_SENSOR 相关,当外界光线强时,屏幕会相应提升亮度。