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