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