- 获取Doze白名单列表:DeviceIdleController.getUserPowerWhitelist
- 是否Doze白名单:DeviceIdleController.isPowerSaveWhitelistApp
- Doze白名单变化广播:PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED
- 设置Doze白名单:DeviceIdleController.addPowerSaveWhitelistApp
package com.android.server.doze;import android.app.usage.UsageStatsManager;
import android.app.usage.UsageStatsManagerInternal;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IDeviceIdleController;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemClock;
import android.util.ArrayMap;import com.android.server.LocalServices;
import com.android.server.usage.AppStandbyConfig;import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class UserWhiteList {public static final String TAG = "UserWhiteList";private static final ArrayMap<String, IWhiteListChangedListener> mListeners = new ArrayMap<>();private final IDeviceIdleController mDeviceIdleController;private final UsageStatsManagerInternal mUsageStatsService;private static final List<String> mWhiteAllList = new ArrayList<>();//监听的回调:private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if (null == intent) {return;}switch (intent.getAction()) {case PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED:synchronized (mListeners) {onUserWhiteListChanged();for (Map.Entry<String, IWhiteListChangedListener> entity : mListeners.entrySet()) {IWhiteListChangedListener lsr = entity.getValue();if (lsr != null) {lsr.onUserWhiteListChanged();}}List<String> dozeWhiteList = getUserPowerWhitelist(true);if (null != dozeWhiteList) {String dozeWhiteListInfo = "ACTION_POWER_SAVE_WHITELIST_CHANGED " +"onUserWhiteListChanged " + dozeWhiteList;PowerLog.getInstance().writePowerFeatureLog(dozeWhiteListInfo);}}break;case Intent.ACTION_PACKAGE_ADDED:final String packageName = intent.getData().getEncodedSchemeSpecificPart();if (null == packageName) {break;}// keep pre doze config while app update eventif (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {break;}// add app to doze white list while app install eventif (AppStandbyConfig.isDozePkgWhiteList(packageName)&& !isPowerSaveWhitelistApp(packageName)) {addPowerSaveWhitelistApp(packageName);String whiteAppInfo = "ACTION_PACKAGE_ADDED " +"addPowerSaveWhitelistApp " + "packageName:" + packageName;Nslog.d(TAG_DOZE, TAG, whiteAppInfo);PowerLog.getInstance().writePowerFeatureLog(whiteAppInfo);}break;default:break;}}};public UserWhiteList(Context context, Handler handler) {mDeviceIdleController = IDeviceIdleController.Stub.asInterface(ServiceManager.getService("deviceidle"));mUsageStatsService = LocalServices.getService(UsageStatsManagerInternal.class);IntentFilter intentFilter = new IntentFilter(PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED);context.registerReceiver(mBroadcastReceiver, intentFilter,null, handler);intentFilter = new IntentFilter();intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);intentFilter.addDataScheme("package");context.registerReceiver(mBroadcastReceiver, intentFilter,null, handler);Nslog.d(TAG_DOZE, TAG, "registerReceiver success");}public void registerWhiteListChangedListener(String feature, IWhiteListChangedListener listener) {synchronized (mListeners) {if (listener != null && feature != null) {mListeners.put(feature, listener);}}}public void onUserWhiteListChanged() {List<String> whitelist = getUserPowerWhitelist(true);mWhiteAllList.clear();mWhiteAllList.addAll(whitelist);}public List<String> getUserPowerWhitelist() {return mWhiteAllList;}/*** 获取所有白名单包名*/public List<String> getUserPowerWhitelist(boolean all) {List<String> whitelistPkgs = new ArrayList<>();try {String[] pkgs = mDeviceIdleController.getUserPowerWhitelist();for (int i = 0; i < pkgs.length; i++) {if (!all && AppStandbyConfig.isDozePkgWhiteList(pkgs[i])) {continue;}whitelistPkgs.add(pkgs[i]);}} catch (RemoteException e) {Nslog.d(TAG_DOZE, TAG, e.toString());}return whitelistPkgs;}/*** 判断是否白名单*/public boolean isPowerSaveWhitelistApp(String pkg) {boolean isWhite = false;try {isWhite = mDeviceIdleController.isPowerSaveWhitelistApp(pkg);} catch (RemoteException e) {Nslog.d(TAG_DOZE, TAG, e.toString());}return isWhite;}/*** 获取白名单大小, setting可使用这个方法*/public int getAppIdUserWhitelistSize() {try {int[] uids = mDeviceIdleController.getAppIdUserWhitelist();return uids.length;} catch (RemoteException e) {Nslog.d(TAG_DOZE, TAG, e.toString());}return 0;}public List<String> getRunningAppBuckets(List<String> run) {if (run == null) {return null;}List<String> list = new ArrayList<>();for (int i = 0; i < run.size(); i++) {int bucket = -1;if (null != mUsageStatsService) {bucket = mUsageStatsService.getAppStandbyBucket(run.get(i), 0, SystemClock.elapsedRealtime());}if (bucket == UsageStatsManager.STANDBY_BUCKET_RESTRICTED) {list.add(run.get(i));}}return list;}public boolean isInRestrictedPackage(String pkg) {boolean isRestricted = (UsageStatsManager.STANDBY_BUCKET_RESTRICTED <= getAppBucket(pkg));return isRestricted;}public int getAppBucket(String pkg) {int bucket = -1;if (null != mUsageStatsService) {bucket = mUsageStatsService.getAppStandbyBucket(pkg, 0, SystemClock.elapsedRealtime());}return bucket;}private void addPowerSaveWhitelistApp(String pkgName) {try {if (null == mDeviceIdleController) {return;}mDeviceIdleController.addPowerSaveWhitelistApp(pkgName);} catch (RemoteException e) {Nslog.d(TAG_DOZE, TAG, e.toString());}}}