Android O 的Doze模式白名单路径


Doze 模式列表

Android 功耗优化(5)---Android O 的Doze模式白名单路径_第1张图片

上述备注规则如下

if(powerWhitelist.isSysWhitelisted(pkg)) {    // Summary of app which doesn't have a battery optimization setting    show:Battery optimization not available} else {    if(powerWhitelist.isWhitelisted(pkg)) {    // Summary of app allowed to use a lot of power    show:Not optimized    } else {    // Summary of app which doesn't have a battery optimization setting    show:Optimizing battery use    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

具体读取路径源码如下

系统路径

package com.android.server;/** * Loads global system configuration info. */public class SystemConfig {    static final String TAG = "SystemConfig";    SystemConfig() {        // Read configuration from system        readPermissions(Environment.buildPath(                Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);        // Read configuration from the old permissions dir        readPermissions(Environment.buildPath(                Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);        // Allow Vendor to customize system configs around libs, features, permissions and apps        int vendorPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_PERMISSIONS |                ALLOW_APP_CONFIGS;        readPermissions(Environment.buildPath(                Environment.getVendorDirectory(), "etc", "sysconfig"), vendorPermissionFlag);        readPermissions(Environment.buildPath(                Environment.getVendorDirectory(), "etc", "permissions"), vendorPermissionFlag);        // Allow ODM to customize system configs around libs, features and apps        int odmPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_APP_CONFIGS;        readPermissions(Environment.buildPath(                Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);        readPermissions(Environment.buildPath(                Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);        // Only allow OEM to customize features        readPermissions(Environment.buildPath(                Environment.getOemDirectory(), "etc", "sysconfig"), ALLOW_FEATURES);        readPermissions(Environment.buildPath(                Environment.getOemDirectory(), "etc", "permissions"), ALLOW_FEATURES);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

由于Android O 解耦的思想,上述源码即如下路径 
1.RootDirectory 
etc/sysconfig/ 
etc/permissions/

我的机器没有etc/sysconfig/路径Z50:/etc/permissions # lslsandroid.software.live_wallpaper.xml  mediatek-packages-teleservice.xmlandroid.software.webview.xml         platform.xmlcom.android.location.provider.xml    pms_sysapp_removable_system_list.txtcom.android.media.remotedisplay.xml  privapp-permissions-google.xmlcom.android.mediadrm.signer.xml      privapp-permissions-mediatek.xmlcom.google.android.maps.xml          privapp-permissions-platform.xmlcom.google.android.media.effects.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.VendorDirectory 
vendor/etc/sysconfig/ 
vendor/etc/permissions/

我的机器没有vendor/etc/sysconfig/路径Z50:/vendor/etc/permissions # lslsandroid.hardware.audio.low_latency.xmlandroid.hardware.bluetooth.xmlandroid.hardware.bluetooth_le.xmlandroid.hardware.camera.xmlandroid.hardware.faketouch.xmlandroid.hardware.location.gps.xmlandroid.hardware.microphone.xmlandroid.hardware.sensor.accelerometer.xmlandroid.hardware.sensor.light.xmlandroid.hardware.sensor.proximity.xmlandroid.hardware.telephony.gsm.xmlandroid.hardware.touchscreen.multitouch.distinct.xmlandroid.hardware.touchscreen.multitouch.jazzhand.xmlandroid.hardware.touchscreen.multitouch.xmlandroid.hardware.touchscreen.xmlandroid.hardware.usb.accessory.xmlandroid.hardware.usb.host.xmlandroid.hardware.wifi.direct.xmlandroid.hardware.wifi.xmlandroid.software.live_wallpaper.xmlandroid.software.midi.xmlhandheld_core_hardware.xmlpms_sysapp_removable_vendor_list.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3.OdmDirectory 
odm/etc/sysconfig/ 
odm/etc/permissions/

很可惜,都没有,这里主要给ODM自己建立对应文件夹进行配置2|Z50:/ # lslsacct       data             init.preload.rc      nvdata    sdcardbugreports default.prop     init.rc              oem       storagecache      dev              init.usb.configfs.rc proc      syscharger    etc              init.usb.rc          protect_f systemconfig     fstab.enableswap init.zygote32.rc     protect_s ueventd.rccustom     init             mnt                  root      vendord          init.environ.rc  nvcfg                sbin
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.oemDirectory 
oem/etc/sysconfig/ 
oem/etc/permissions/

没有具体文件Z50:/oem # lsls
  • 1
  • 2
  • 3

具体文件

具体源码

    void readPermissions(File libraryDir, int permissionFlag) {        // Read permissions from given directory.        if (!libraryDir.exists() || !libraryDir.isDirectory()) {            if (permissionFlag == ALLOW_ALL) {                Slog.w(TAG, "No directory " + libraryDir + ", skipping");            }            return;        }        if (!libraryDir.canRead()) {            Slog.w(TAG, "Directory " + libraryDir + " cannot be read");            return;        }        // Iterate over the files in the directory and scan .xml files        File platformFile = null;        for (File f : libraryDir.listFiles()) {            // We'll read platform.xml last            // 这个文件最后会读的            if (f.getPath().endsWith("etc/permissions/platform.xml")) {                platformFile = f;                continue;            }            if (!f.getPath().endsWith(".xml")) {                Slog.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring");                continue;            }            if (!f.canRead()) {                Slog.w(TAG, "Permissions library file " + f + " cannot be read");                continue;            }            readPermissionsFromXml(f, permissionFlag);        }        // Read platform permissions last so it will take precedence        // 我就是最后        if (platformFile != null) {            readPermissionsFromXml(platformFile, permissionFlag);        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

接下来很明显就是读取各种XML了

private void readPermissionsFromXml(File permFile, int permissionFlag) {我们重点关注下面的即可...                } else if ("allow-in-power-save-except-idle".equals(name) && allowAll) {                    String pkgname = parser.getAttributeValue(null, "package");                    if (pkgname == null) {                        Slog.w(TAG, " without package in "                                + permFile + " at " + parser.getPositionDescription());                    } else {                        mAllowInPowerSaveExceptIdle.add(pkgname);                    }                    XmlUtils.skipCurrentTag(parser);                    continue;                } else if ("allow-in-power-save".equals(name) && allowAll) {                    String pkgname = parser.getAttributeValue(null, "package");                    if (pkgname == null) {                        Slog.w(TAG, " without package in " + permFile + " at "                                + parser.getPositionDescription());                    } else {                        mAllowInPowerSave.add(pkgname);                    }                    XmlUtils.skipCurrentTag(parser);                    continue;...                    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

取出白名单

手机路径 /etc/permissions/platform.xml

对应源代码路径如下

        <allow-in-power-save package="com.android.providers.downloads" />        <allow-in-power-save package="com.android.cellbroadcastreceiver" />    <allow-in-power-save package="com.android.shell" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

系统源码路径 frameworks\base\data\etc\platform.xml

当然内容是一样的啦        <allow-in-power-save package="com.android.providers.downloads" />        <allow-in-power-save package="com.android.cellbroadcastreceiver" />    <allow-in-power-save package="com.android.shell" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

手机路径 /vendor/etc/permissions/抱歉这里没有

系统源码路径 vendor\mediatek\proprietary\frameworks\base\data\etc\抱歉这里也没有,需要自行配置

系统源码路径 \vendor\partner_gms\etc\sysconfig\google.xml

这里是亲爱的Gms包设置    Line 23:     <allow-in-power-save package="com.google.android.gms" />    Line 28:     <allow-in-power-save-except-idle package="com.google.android.apps.work.oobconfig" />    Line 41:     <allow-in-power-save-except-idle package="com.android.vending" />    Line 44:     <allow-in-power-save package="com.google.android.volta" />    Line 48:     <allow-in-power-save package="com.google.android.ims" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

软件接口设置

    // Doze 模式新增白名单    public void addApp(String pkg) {        try {            mDeviceIdleService.addPowerSaveWhitelistApp(pkg);            mWhitelistedApps.add(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    // Doze 模式移除白名单    public void removeApp(String pkg) {        try {            mDeviceIdleService.removePowerSaveWhitelistApp(pkg);            mWhitelistedApps.remove(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

具体工具类如下

/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.lava.powersave.util;import android.os.IDeviceIdleController;import android.os.RemoteException;import android.os.ServiceManager;import android.util.ArraySet;import android.util.Log;/** * Handles getting/changing the whitelist for the exceptions to battery saving features. */public class PowerWhitelistBackend {    private static final String TAG = "WhitelistBackend";    private static final String DEVICE_IDLE_SERVICE = "deviceidle";    private static final PowerWhitelistBackend INSTANCE = new PowerWhitelistBackend();    private final IDeviceIdleController mDeviceIdleService;    private final ArraySet mWhitelistedApps = new ArraySet<>();    private final ArraySet mSysWhitelistedApps = new ArraySet<>();    public PowerWhitelistBackend() {        mDeviceIdleService = IDeviceIdleController.Stub.asInterface(                ServiceManager.getService(DEVICE_IDLE_SERVICE));        refreshList();    }    public int getWhitelistSize() {        return mWhitelistedApps.size();    }    public boolean isSysWhitelisted(String pkg) {        return mSysWhitelistedApps.contains(pkg);    }    public boolean isWhitelisted(String pkg) {        return mWhitelistedApps.contains(pkg);    }    public void addApp(String pkg) {        try {            mDeviceIdleService.addPowerSaveWhitelistApp(pkg);            mWhitelistedApps.add(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    public void removeApp(String pkg) {        try {            mDeviceIdleService.removePowerSaveWhitelistApp(pkg);            mWhitelistedApps.remove(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    private void refreshList() {        mSysWhitelistedApps.clear();        mWhitelistedApps.clear();        try {            String[] whitelistedApps = mDeviceIdleService.getFullPowerWhitelist();            for (String app : whitelistedApps) {                mWhitelistedApps.add(app);            }            String[] sysWhitelistedApps = mDeviceIdleService.getSystemPowerWhitelist();            for (String app : sysWhitelistedApps) {                mSysWhitelistedApps.add(app);            }        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    public static PowerWhitelistBackend getInstance() {        return INSTANCE;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

软件接口的配置也会体现这这个路径下 
data/system/deviceidle.xml

Z50:/data/system # cat deviceidle.xmlcat deviceidle.xml<?xml version='1.0' encoding='utf-8' standalone='yes' ?><config><wl n="com.google.android.apps.maps" /><wl n="com.google.android.gsf" />config>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

具体相关源码

 public final AtomicFile mConfigFile;    public DeviceIdleController(Context context) {        super(context);        mConfigFile = new AtomicFile(new File(getSystemDir(), "deviceidle.xml"));        mHandler = new MyHandler(BackgroundThread.getHandler().getLooper());    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

总结

Doze模式的白名单我们可以预设置还可以进行软件接口调用。 
1.主要预设置在 
frameworks\base\data\etc\platform.xml 
随着Android O 的代码控制雄心,以后路径可能需要厂商自己定义 
vendor\mediatek\proprietary\frameworks\base\data\etc\platform.xml

2.不要忘记GMS包里面也可以配置的 
\vendor\partner_gms\etc\sysconfig\google.xml

3.接口调用同样Google提供给我们了,使用起来同样简单粗暴,查看设置结果 data/system/deviceidle.xml

Android O 的Doze模式白名单路径

2017年11月17日 15:28:03 阅读数:1679

Doze 模式列表

Android 功耗优化(5)---Android O 的Doze模式白名单路径_第2张图片

上述备注规则如下

if(powerWhitelist.isSysWhitelisted(pkg)) {    // Summary of app which doesn't have a battery optimization setting    show:Battery optimization not available} else {    if(powerWhitelist.isWhitelisted(pkg)) {    // Summary of app allowed to use a lot of power    show:Not optimized    } else {    // Summary of app which doesn't have a battery optimization setting    show:Optimizing battery use    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

具体读取路径源码如下

系统路径

package com.android.server;/** * Loads global system configuration info. */public class SystemConfig {    static final String TAG = "SystemConfig";    SystemConfig() {        // Read configuration from system        readPermissions(Environment.buildPath(                Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);        // Read configuration from the old permissions dir        readPermissions(Environment.buildPath(                Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);        // Allow Vendor to customize system configs around libs, features, permissions and apps        int vendorPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_PERMISSIONS |                ALLOW_APP_CONFIGS;        readPermissions(Environment.buildPath(                Environment.getVendorDirectory(), "etc", "sysconfig"), vendorPermissionFlag);        readPermissions(Environment.buildPath(                Environment.getVendorDirectory(), "etc", "permissions"), vendorPermissionFlag);        // Allow ODM to customize system configs around libs, features and apps        int odmPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_APP_CONFIGS;        readPermissions(Environment.buildPath(                Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);        readPermissions(Environment.buildPath(                Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);        // Only allow OEM to customize features        readPermissions(Environment.buildPath(                Environment.getOemDirectory(), "etc", "sysconfig"), ALLOW_FEATURES);        readPermissions(Environment.buildPath(                Environment.getOemDirectory(), "etc", "permissions"), ALLOW_FEATURES);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

由于Android O 解耦的思想,上述源码即如下路径 
1.RootDirectory 
etc/sysconfig/ 
etc/permissions/

我的机器没有etc/sysconfig/路径Z50:/etc/permissions # lslsandroid.software.live_wallpaper.xml  mediatek-packages-teleservice.xmlandroid.software.webview.xml         platform.xmlcom.android.location.provider.xml    pms_sysapp_removable_system_list.txtcom.android.media.remotedisplay.xml  privapp-permissions-google.xmlcom.android.mediadrm.signer.xml      privapp-permissions-mediatek.xmlcom.google.android.maps.xml          privapp-permissions-platform.xmlcom.google.android.media.effects.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.VendorDirectory 
vendor/etc/sysconfig/ 
vendor/etc/permissions/

我的机器没有vendor/etc/sysconfig/路径Z50:/vendor/etc/permissions # lslsandroid.hardware.audio.low_latency.xmlandroid.hardware.bluetooth.xmlandroid.hardware.bluetooth_le.xmlandroid.hardware.camera.xmlandroid.hardware.faketouch.xmlandroid.hardware.location.gps.xmlandroid.hardware.microphone.xmlandroid.hardware.sensor.accelerometer.xmlandroid.hardware.sensor.light.xmlandroid.hardware.sensor.proximity.xmlandroid.hardware.telephony.gsm.xmlandroid.hardware.touchscreen.multitouch.distinct.xmlandroid.hardware.touchscreen.multitouch.jazzhand.xmlandroid.hardware.touchscreen.multitouch.xmlandroid.hardware.touchscreen.xmlandroid.hardware.usb.accessory.xmlandroid.hardware.usb.host.xmlandroid.hardware.wifi.direct.xmlandroid.hardware.wifi.xmlandroid.software.live_wallpaper.xmlandroid.software.midi.xmlhandheld_core_hardware.xmlpms_sysapp_removable_vendor_list.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3.OdmDirectory 
odm/etc/sysconfig/ 
odm/etc/permissions/

很可惜,都没有,这里主要给ODM自己建立对应文件夹进行配置2|Z50:/ # lslsacct       data             init.preload.rc      nvdata    sdcardbugreports default.prop     init.rc              oem       storagecache      dev              init.usb.configfs.rc proc      syscharger    etc              init.usb.rc          protect_f systemconfig     fstab.enableswap init.zygote32.rc     protect_s ueventd.rccustom     init             mnt                  root      vendord          init.environ.rc  nvcfg                sbin
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.oemDirectory 
oem/etc/sysconfig/ 
oem/etc/permissions/

没有具体文件Z50:/oem # lsls
  • 1
  • 2
  • 3

具体文件

具体源码

    void readPermissions(File libraryDir, int permissionFlag) {        // Read permissions from given directory.        if (!libraryDir.exists() || !libraryDir.isDirectory()) {            if (permissionFlag == ALLOW_ALL) {                Slog.w(TAG, "No directory " + libraryDir + ", skipping");            }            return;        }        if (!libraryDir.canRead()) {            Slog.w(TAG, "Directory " + libraryDir + " cannot be read");            return;        }        // Iterate over the files in the directory and scan .xml files        File platformFile = null;        for (File f : libraryDir.listFiles()) {            // We'll read platform.xml last            // 这个文件最后会读的            if (f.getPath().endsWith("etc/permissions/platform.xml")) {                platformFile = f;                continue;            }            if (!f.getPath().endsWith(".xml")) {                Slog.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring");                continue;            }            if (!f.canRead()) {                Slog.w(TAG, "Permissions library file " + f + " cannot be read");                continue;            }            readPermissionsFromXml(f, permissionFlag);        }        // Read platform permissions last so it will take precedence        // 我就是最后        if (platformFile != null) {            readPermissionsFromXml(platformFile, permissionFlag);        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

接下来很明显就是读取各种XML了

private void readPermissionsFromXml(File permFile, int permissionFlag) {我们重点关注下面的即可...                } else if ("allow-in-power-save-except-idle".equals(name) && allowAll) {                    String pkgname = parser.getAttributeValue(null, "package");                    if (pkgname == null) {                        Slog.w(TAG, " without package in "                                + permFile + " at " + parser.getPositionDescription());                    } else {                        mAllowInPowerSaveExceptIdle.add(pkgname);                    }                    XmlUtils.skipCurrentTag(parser);                    continue;                } else if ("allow-in-power-save".equals(name) && allowAll) {                    String pkgname = parser.getAttributeValue(null, "package");                    if (pkgname == null) {                        Slog.w(TAG, " without package in " + permFile + " at "                                + parser.getPositionDescription());                    } else {                        mAllowInPowerSave.add(pkgname);                    }                    XmlUtils.skipCurrentTag(parser);                    continue;...                    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

取出白名单

手机路径 /etc/permissions/platform.xml

对应源代码路径如下

        <allow-in-power-save package="com.android.providers.downloads" />        <allow-in-power-save package="com.android.cellbroadcastreceiver" />    <allow-in-power-save package="com.android.shell" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

系统源码路径 frameworks\base\data\etc\platform.xml

当然内容是一样的啦        <allow-in-power-save package="com.android.providers.downloads" />        <allow-in-power-save package="com.android.cellbroadcastreceiver" />    <allow-in-power-save package="com.android.shell" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

手机路径 /vendor/etc/permissions/抱歉这里没有

系统源码路径 vendor\mediatek\proprietary\frameworks\base\data\etc\抱歉这里也没有,需要自行配置

系统源码路径 \vendor\partner_gms\etc\sysconfig\google.xml

这里是亲爱的Gms包设置    Line 23:     <allow-in-power-save package="com.google.android.gms" />    Line 28:     <allow-in-power-save-except-idle package="com.google.android.apps.work.oobconfig" />    Line 41:     <allow-in-power-save-except-idle package="com.android.vending" />    Line 44:     <allow-in-power-save package="com.google.android.volta" />    Line 48:     <allow-in-power-save package="com.google.android.ims" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

软件接口设置

    // Doze 模式新增白名单    public void addApp(String pkg) {        try {            mDeviceIdleService.addPowerSaveWhitelistApp(pkg);            mWhitelistedApps.add(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    // Doze 模式移除白名单    public void removeApp(String pkg) {        try {            mDeviceIdleService.removePowerSaveWhitelistApp(pkg);            mWhitelistedApps.remove(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

具体工具类如下

/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.lava.powersave.util;import android.os.IDeviceIdleController;import android.os.RemoteException;import android.os.ServiceManager;import android.util.ArraySet;import android.util.Log;/** * Handles getting/changing the whitelist for the exceptions to battery saving features. */public class PowerWhitelistBackend {    private static final String TAG = "WhitelistBackend";    private static final String DEVICE_IDLE_SERVICE = "deviceidle";    private static final PowerWhitelistBackend INSTANCE = new PowerWhitelistBackend();    private final IDeviceIdleController mDeviceIdleService;    private final ArraySet mWhitelistedApps = new ArraySet<>();    private final ArraySet mSysWhitelistedApps = new ArraySet<>();    public PowerWhitelistBackend() {        mDeviceIdleService = IDeviceIdleController.Stub.asInterface(                ServiceManager.getService(DEVICE_IDLE_SERVICE));        refreshList();    }    public int getWhitelistSize() {        return mWhitelistedApps.size();    }    public boolean isSysWhitelisted(String pkg) {        return mSysWhitelistedApps.contains(pkg);    }    public boolean isWhitelisted(String pkg) {        return mWhitelistedApps.contains(pkg);    }    public void addApp(String pkg) {        try {            mDeviceIdleService.addPowerSaveWhitelistApp(pkg);            mWhitelistedApps.add(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    public void removeApp(String pkg) {        try {            mDeviceIdleService.removePowerSaveWhitelistApp(pkg);            mWhitelistedApps.remove(pkg);        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    private void refreshList() {        mSysWhitelistedApps.clear();        mWhitelistedApps.clear();        try {            String[] whitelistedApps = mDeviceIdleService.getFullPowerWhitelist();            for (String app : whitelistedApps) {                mWhitelistedApps.add(app);            }            String[] sysWhitelistedApps = mDeviceIdleService.getSystemPowerWhitelist();            for (String app : sysWhitelistedApps) {                mSysWhitelistedApps.add(app);            }        } catch (RemoteException e) {            Log.w(TAG, "Unable to reach IDeviceIdleController", e);        }    }    public static PowerWhitelistBackend getInstance() {        return INSTANCE;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

软件接口的配置也会体现这这个路径下 
data/system/deviceidle.xml

Z50:/data/system # cat deviceidle.xmlcat deviceidle.xml<?xml version='1.0' encoding='utf-8' standalone='yes' ?><config><wl n="com.google.android.apps.maps" /><wl n="com.google.android.gsf" />config>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

具体相关源码

 public final AtomicFile mConfigFile;    public DeviceIdleController(Context context) {        super(context);        mConfigFile = new AtomicFile(new File(getSystemDir(), "deviceidle.xml"));        mHandler = new MyHandler(BackgroundThread.getHandler().getLooper());    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

总结

Doze模式的白名单我们可以预设置还可以进行软件接口调用。 
1.主要预设置在 
frameworks\base\data\etc\platform.xml 
随着Android O 的代码控制雄心,以后路径可能需要厂商自己定义 
vendor\mediatek\proprietary\frameworks\base\data\etc\platform.xml

2.不要忘记GMS包里面也可以配置的 
\vendor\partner_gms\etc\sysconfig\google.xml

3.接口调用同样Google提供给我们了,使用起来同样简单粗暴,查看设置结果 data/system/deviceidle.xml

更多相关文章

  1. 如何正确获得Android内外SD卡路径
  2. android存储路径问题
  3. 最新下载 android 源码方法
  4. android完整资讯App、Kotlin新闻应用MVP + RxJava + Retrofit +
  5. Android源码阅读分析:ActivityManagerService分析(一)——启动流程
  6. 关于android 4.1源码调整asset相关目录的说明
  7. Android AsyncChannel源码分析
  8. android源码浅析--AlertController
  9. Android源码阅读分析:ActivityManagerService分析(二)——Activity

随机推荐

  1. 3.4 管理项目 - 创建Android库
  2. 基于Android的MediaPlayer的音乐播放器的
  3. AQuery简介:jQuery for Android
  4. Android应用如何支持屏幕多尺寸多分辨率
  5. Andriod编程基础(一):Andriod的优势及光明
  6. Android(安卓)连接WIF获取的信息剖析
  7. Android程序结构
  8. android 内存优化
  9. Android(二) 基于 eclipse 的 Android配置
  10. android BLE Peripheral 模拟 ibeacon 发