Android 13.0 系统framework修改低电量关机值为3%

1、讲在最前面

系统rom定制开发中,其中在低电量时,系统会自动关机,这个和不同的平台和底层驱动和硬件都有关系,需要结合这些来实际调整这个值,我们可以通过分析源码中电池服务的代码,然后进行修改如何实现这个功能。

2、系统中framework修改低电量关机值的核心类

kotlin 复制代码
frameworks\base\services\core\java\com\android\server\BatteryService.java
    private final class HealthHalCallback extends IHealthInfoCallback.Stub
            implements HealthServiceWrapper.Callback {
        @Override public void healthInfoChanged(android.hardware.health.V2_0.HealthInfo props) {
            BatteryService.this.update(props);
        }
 
    private void update(android.hardware.health.V2_0.HealthInfo info) {
        traceBegin("HealthInfoUpdate");
 
        Trace.traceCounter(Trace.TRACE_TAG_POWER, "BatteryChargeCounter",
                info.legacy.batteryChargeCounter);
        Trace.traceCounter(Trace.TRACE_TAG_POWER, "BatteryCurrent",
                info.legacy.batteryCurrent);
 
        synchronized (mLock) {
            mRealBatteryLevel = info.legacy.batteryLevel;
            if (!mUpdatesStopped) {
                mHealthInfo = info.legacy;
                // Process the new values.
                processValuesLocked(false);
                mLock.notifyAll(); // for any waiters on new info
            } else {
                copy(mLastHealthInfo, info.legacy);
            }
        }
        traceEnd();
    }








   private void processValuesLocked(boolean force) {
        boolean logOutlier = false;
        long dischargeDuration = 0;
 
        mBatteryLevelCritical =
            mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
            && mHealthInfo.batteryLevel <= mCriticalBatteryLevel;
        if (mHealthInfo.chargerAcOnline) {
            mPlugType = BatteryManager.BATTERY_PLUGGED_AC;
        } else if (mHealthInfo.chargerUsbOnline) {
            mPlugType = BatteryManager.BATTERY_PLUGGED_USB;
        } else if (mHealthInfo.chargerWirelessOnline) {
            mPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
        } else {
            mPlugType = BATTERY_PLUGGED_NONE;
        }
 
        if (DEBUG) {
            Slog.d(TAG, "Processing new values: "
                    + "info=" + mHealthInfo
                    + ", mBatteryLevelCritical=" + mBatteryLevelCritical
                    + ", mPlugType=" + mPlugType);
        }
 
        // Let the battery stats keep track of the current level.
        try {
            mBatteryStats.setBatteryState(mHealthInfo.batteryStatus, mHealthInfo.batteryHealth,
                    mPlugType, mHealthInfo.batteryLevel, mHealthInfo.batteryTemperature,
                    mHealthInfo.batteryVoltage, mHealthInfo.batteryChargeCounter,
                    mHealthInfo.batteryFullCharge);
        } catch (RemoteException e) {
            // Should never happen.
        }

...
}
 
  private void shutdownIfNoPowerLocked() {
        // shut down gracefully if our battery is critically low and we are not powered.
        // wait until the system has booted before attempting to display the shutdown dialog.
        if (shouldShutdownLocked()) {
            /*SPRD : add power debug log start*/
            Slog.d(TAG, "Low battery shutdown, batteryLevel : " + mHealthInfo.batteryLevel);
            /*SPRD : add power debug log end*/
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mActivityManagerInternal.isSystemReady()) {
                        Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
                        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
                        intent.putExtra(Intent.EXTRA_REASON,
                                PowerManager.SHUTDOWN_LOW_BATTERY);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivityAsUser(intent, UserHandle.CURRENT);
                    }
                }
            });
        }
    }
 
   private boolean shouldShutdownLocked() {
        if (mHealthInfo.batteryLevel > 0) {
            return false;
        }
 
        // Battery-less devices should not shutdown.
        if (!mHealthInfo.batteryPresent) {
            return false;
        }
 
        // add for bug#1021541
        boolean checkPlugState = (mUpdatesStopped && mHealthInfo.batteryLevel == mSetBatteryLevel
                && mSetBatteryLevel != mRealBatteryLevel);
        if (DEBUG) Slog.d(TAG, "shutdownIfNoPowerLocked: mUpdatesStopped: " + mUpdatesStopped
                + " mHealthInfo.batteryLevel:"  + mHealthInfo.batteryLevel
                + " mSetBatteryLevel:" + mSetBatteryLevel + " mRealBatteryLevel:" + mRealBatteryLevel);
 

        return !checkPlugState || mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_CHARGING;
    }


  private boolean shouldShutdownLocked() {
//add start
        if (mHealthInfo.batteryLevel <= 2) {
            return true;
        }
 
//add end
        if (mHealthInfo.batteryLevel > 0) {
            return false;
        }
 
        // Battery-less devices should not shutdown.
        if (!mHealthInfo.batteryPresent) {
            return false;
        }
 
        // add for bug#1021541
        boolean checkPlugState = (mUpdatesStopped && mHealthInfo.batteryLevel == mSetBatteryLevel
                && mSetBatteryLevel != mRealBatteryLevel);
        if (DEBUG) Slog.d(TAG, "shutdownIfNoPowerLocked: mUpdatesStopped: " + mUpdatesStopped
                + " mHealthInfo.batteryLevel:"  + mHealthInfo.batteryLevel
                + " mSetBatteryLevel:" + mSetBatteryLevel + " mRealBatteryLevel:" + mRealBatteryLevel);
 
        return !checkPlugState || mHealthInfo.batteryStatus != BatteryManager.BATTERY_STATUS_CHARGING;
    }

经过源码分析,如果满足shouldShutdownLocked(),如果满足关机执行 Intent.ACTION_REQUEST_SHUTDOWN

添加

   if (mHealthInfo.batteryLevel <= 2) {
        return true;
    }
相关推荐
2402_8575893620 分钟前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
人间有清欢43 分钟前
十、kotlin的协程
kotlin
吾爱星辰44 分钟前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer44 分钟前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
哎呦没1 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥2 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程3 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
莹雨潇潇3 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
网络研究院3 小时前
Android 安卓内存安全漏洞数量大幅下降的原因
android·安全·编程·安卓·内存·漏洞·技术
杨哥带你写代码3 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端