4 展訊Sprd設定-電池-PowerController.exitPowerSaveMode函式介面
阿新 • • 發佈:2018-11-21
0. 前言
- 上接展訊Sprd設定-電池-setPowerSaveMode
- 3 展訊Sprd設定-電池-onPowerSaveModeChanging介面
原始碼走讀的開始位置
- vendor/sprd/platform/frameworks/base/services/core/java/com/android/server/power/PowerController.java
1. PowerController.handlePowerSaveModeChanged
package com.android.server.power; public class PowerController //extends IPowerController.Stub private List<PowerSaveHelper> mHelpers; // handle the power save mode changed private void handlePowerSaveModeChanged(int newMode) { if (mInitFinished && mPowerSaveMode == newMode) return; // notify helpers power save mode changing for (int i = 0; i < mHelpers.size(); i++) { PowerSaveHelper helper = mHelpers.get(i); helper.onPowerSaveModeChanging(newMode); } exitPowerSaveMode(mPowerSaveMode); enterPowerSaveMode(newMode); if (mPowerSaveMode != newMode) { Slog.e(TAG, "Something error!! mPowerSaveMode change fail!!old mode:" + mPowerSaveMode + " new mode:" + newMode); } // notify helpers power save mode changed for (int i = 0; i < mHelpers.size(); i++) { PowerSaveHelper helper = mHelpers.get(i); helper.onPowerSaveModeChanged(newMode); } }
2. PowerController.exitPowerSaveMode
呼叫 exitPowerSaveMode 主要目的是設定 DeviceIdleController.forceDoze(false, true) 進入doze模式的介面
private void exitPowerSaveMode(int oldMode) { switch (oldMode) { case PowerManagerEx.MODE_LOWPOWER: // 省電模式 case PowerManagerEx.MODE_ULTRASAVING: // 超級省電模式 case PowerManagerEx.MODE_INVALID: // default mode is MODE_INVALID, so it must be the first time coming here. We should do reset lowpower mode // Google 原生的省電模式,這裡禁用這個模式 mPowerManager.setPowerSaveMode(false); exitForceIdleIfNeed(); break; case PowerManagerEx.MODE_PERFORMANCE: break; case PowerManagerEx.MODE_SMART: // 智慧省電模式 if (!mDozeEnabled) { exitForceIdleIfNeed(); } break; case PowerManagerEx.MODE_POWERSAVING: break; } }
3. PowerController.exitForceIdleIfNeed
package com.android.server.power;
public class PowerController //extends IPowerController.Stub
extends UsageStatsManagerInternal.AppStateEventChangeListener {
private DeviceIdleController.LocalService mLocalDeviceIdleController;
private void exitForceIdleIfNeed() {
if (mForceIdle) {
if (mLocalDeviceIdleController != null)
// forceDoze(是否進入強制進入doze模式,是否強制進入deep doze模式)
mLocalDeviceIdleController.forceDoze(false, true);
mForceIdle = false;
}
}
4. DeviceIdleController.forceDoze
- frameworks/base/services/core/java/com/android/server/DeviceIdleController.java
後續自己模組的省電管理科員借鑑下,進行強制進入省電模式
/**
* Keeps track of device idleness and drives low power mode based on that.
*/
public class DeviceIdleController extends SystemService
implements AnyMotionDetector.DeviceIdleCallback {
private static final String TAG = "DeviceIdleController";
private final PowerControllerHelper mPowerControllerHelper;
public class LocalService {
// duration in milliseconds
public boolean forceDoze(boolean enable, boolean bDeep) {
return mPowerControllerHelper.forceDoze(enable, bDeep);
}
5. PowerControllerHelper.forceDoze()介面
/**
* Keeps track of device idleness and drives low power mode based on that.
*/
public class DeviceIdleController extends SystemService
implements AnyMotionDetector.DeviceIdleCallback {
private static final String TAG = "DeviceIdleController";
private final PowerControllerHelper mPowerControllerHelper;
// impletement functions that need for PowerController
private final class PowerControllerHelper {
public boolean forceDoze(boolean enable, boolean bDeep) {
synchronized (this) {
if (enable) { // force doze
if (bDeep) { // force deep doze
mForceIdle = true;
forceBecomeInactiveIfAppropriateLocked();
int curState = mState;
while (curState != STATE_IDLE) {
stepIdleStateLocked("s:force");
if (curState == mState) {
Slog.w(TAG, "Unable to go deep idle; stopped at " + stateToString(mState));
exitForceIdleLocked();
return false;
}
curState = mState;
}
Slog.w(TAG, "Now forced in to deep idle mode");
} else { // force light doze
mForceIdle = true;
forceBecomeInactiveIfAppropriateLocked();
int curLightState = mLightState;
while (curLightState != LIGHT_STATE_IDLE) {
stepLightIdleStateLocked("s:force");
if (curLightState == mLightState) {
Slog.w(TAG, "Unable to go light idle; stopped at " + lightStateToString(mLightState));
exitForceIdleLocked();
return false;
}
curLightState = mLightState;
}
Slog.w(TAG, "Now forced in to light idle mode");
}
} else { // unforce doze ( whether light or deep)
exitForceIdleIgnoreDeviceStateLocked();
Slog.w(TAG, "Light state: " + lightStateToString(mLightState));
Slog.w(TAG, "deep state: " + stateToString(mState));
}
}
return true;
}