背景
今天分析LineageOS Project的 toxyc_device_motorola_potter 配置时看到特意处理qti_whitelist.xml
于是结合最新的android11 代码 梳理了一下流程 研究了下省电模式如何设置例外来对应用保活
qit whiltelist
allow-in-power-save : These are telephony components that need to freely run in the background
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<config>
<!-- These are telephony components that need to freely run in the background -->
<allow-in-power-save package="com.qualcomm.atfwd" />
<allow-in-power-save package="com.qualcomm.qti.telephonyservice" />
<allow-in-power-save package="com.qulacomm.qcrilmsgtunnel" />
<allow-in-power-save package="com.qualcomm.qti.ims" />
<allow-in-power-save package="com.qualcomm.qti.radioconfiginterface" />
<allow-in-power-save package="com.qualcomm.simcontacts" />
<allow-in-power-save package="com.qualcomm.qti.autoregistration" />
<allow-in-power-save package="com.qualcomm.qti.server.wigigapp" />
<allow-in-power-save package="com.qualcomm.qti.StatsPollManager" />
<allow-in-power-save package="com.qualcomm.qti.gsma.services.nfc" />
</config>
# Qualcomm
PRODUCT_COPY_FILES += \
$(LOCAL_PATH)/configs/privapp-permissions-oem.xml:system/etc/permissions/privapp-permissions-oem.xml \
$(LOCAL_PATH)/configs/qti_whitelist.xml:system/etc/sysconfig/qti_whitelist.xml
编译结果
系统解析
android/frameworks/base/core/java/com/android/server/SystemConfig.java
SystemConfig() {
// Read configuration from system
readPermissions(Environment.buildPath(
Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);
}
private void readPermissionsFromXml(File permFile, int permissionFlag) {
case "allow-in-power-save": {
if (allowAll) {
String pkgname = parser.getAttributeValue(null, "package");
if (pkgname == null) {
Slog.w(TAG, "<" + name + "> without package in "
+ permFile + " at " + parser.getPositionDescription());
} else {
mAllowInPowerSave.add(pkgname);
}
} else {
logNotAllowedInPartition(name, permFile, parser);
}
XmlUtils.skipCurrentTag(parser);
} break;
}
这里我们要注意mAllowInPowerSave 变量如何保证应用在后台运行
这里做了一个接口,可以将xml定义的应用传递给省电模式的管理
public ArraySet<String> getAllowInPowerSave() {
return mAllowInPowerSave;
}
android/frameworks/base/services/core/java/com/android/server/DeviceIdleController.java
@Override
public void onStart() {
synchronized (this) {
ArraySet<String> allowPower = sysConfig.getAllowInPowerSave();
for (int i=0; i<allowPower.size(); i++) {
String pkg = allowPower.valueAt(i);
try {
ApplicationInfo ai = pm.getApplicationInfo(pkg,
PackageManager.MATCH_SYSTEM_ONLY);
int appid = UserHandle.getAppId(ai.uid);
// These apps are on both the whitelist-except-idle as well
// as the full whitelist, so they apply in all cases.
mPowerSaveWhitelistAppsExceptIdle.put(ai.packageName, appid);
mPowerSaveWhitelistSystemAppIdsExceptIdle.put(appid, true);
mPowerSaveWhitelistApps.put(ai.packageName, appid);
mPowerSaveWhitelistSystemAppIds.put(appid, true);
} catch (PackageManager.NameNotFoundException e) {
}
}
这里定义了一个不受省电限制的app名称数组
/**
* Package names the user has white-listed to opt out of power save restrictions.
*/
private final ArrayMap<String, Integer> mPowerSaveWhitelistUserApps = new ArrayMap<>();
public boolean getPowerSaveWhitelistAppInternal(String name) {
synchronized (this) {
return mPowerSaveWhitelistUserApps.containsKey(name);
}
}
int onShellCommand(Shell shell, String cmd) {
//在此进行操作
}