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.util.ArraySet; 22 import android.util.Log; 23 24 25 /** 26 * Handles getting/changing the whitelist for the exceptions to battery saving features. 27 */ 28 public class PowerWhitelistBackend { 29 30 private static final String TAG = "PowerWhitelistBackend"; 31 32 private static final String DEVICE_IDLE_SERVICE = "deviceidle"; 33 34 private static final PowerWhitelistBackend INSTANCE = new PowerWhitelistBackend(); 35 36 private final IDeviceIdleController mDeviceIdleService; 37 private final ArraySet<String> mWhitelistedApps = new ArraySet<>(); 38 private final ArraySet<String> mSysWhitelistedApps = new ArraySet<>(); 39 40 public PowerWhitelistBackend() { 41 mDeviceIdleService = IDeviceIdleController.Stub.asInterface( 42 ServiceManager.getService(DEVICE_IDLE_SERVICE)); 43 refreshList(); 44 } 45 46 public int getWhitelistSize() { 47 return mWhitelistedApps.size(); 48 } 49 50 public boolean isSysWhitelisted(String pkg) { 51 return mSysWhitelistedApps.contains(pkg); 52 } 53 54 public boolean isWhitelisted(String pkg) { 55 return mWhitelistedApps.contains(pkg); 56 } 57 58 public void addApp(String pkg) { 59 try { 60 mDeviceIdleService.addPowerSaveWhitelistApp(pkg); 61 mWhitelistedApps.add(pkg); 62 } catch (RemoteException e) { 63 Log.w(TAG, "Unable to reach IDeviceIdleController", e); 64 } 65 } 66 67 public void removeApp(String pkg) { 68 try { 69 mDeviceIdleService.removePowerSaveWhitelistApp(pkg); 70 mWhitelistedApps.remove(pkg); 71 } catch (RemoteException e) { 72 Log.w(TAG, "Unable to reach IDeviceIdleController", e); 73 } 74 } 75 76 private void refreshList() { 77 mSysWhitelistedApps.clear(); 78 mWhitelistedApps.clear(); 79 try { 80 String[] whitelistedApps = mDeviceIdleService.getFullPowerWhitelist(); 81 for (String app : whitelistedApps) { 82 mWhitelistedApps.add(app); 83 } 84 String[] sysWhitelistedApps = mDeviceIdleService.getSystemPowerWhitelist(); 85 for (String app : sysWhitelistedApps) { 86 mSysWhitelistedApps.add(app); 87 } 88 } catch (RemoteException e) { 89 Log.w(TAG, "Unable to reach IDeviceIdleController", e); 90 } 91 } 92 93 public static PowerWhitelistBackend getInstance() { 94 return INSTANCE; 95 } 96 97 } 98