Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.settingslib.fuelgauge; |
| 18 | |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
Lei Yu | ae76865 | 2018-04-05 13:44:51 -0700 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 22 | import android.os.IDeviceIdleController; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
| 25 | import android.support.annotation.VisibleForTesting; |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 26 | import android.telecom.DefaultDialerManager; |
| 27 | import android.text.TextUtils; |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 28 | import android.util.ArraySet; |
| 29 | import android.util.Log; |
| 30 | |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 31 | import com.android.internal.telephony.SmsApplication; |
Lei Yu | ae76865 | 2018-04-05 13:44:51 -0700 | [diff] [blame] | 32 | import com.android.internal.util.ArrayUtils; |
| 33 | |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Handles getting/changing the whitelist for the exceptions to battery saving features. |
| 36 | */ |
| 37 | public class PowerWhitelistBackend { |
| 38 | |
| 39 | private static final String TAG = "PowerWhitelistBackend"; |
| 40 | |
| 41 | private static final String DEVICE_IDLE_SERVICE = "deviceidle"; |
| 42 | |
| 43 | private static PowerWhitelistBackend sInstance; |
| 44 | |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 45 | private final Context mAppContext; |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 46 | private final IDeviceIdleController mDeviceIdleService; |
| 47 | private final ArraySet<String> mWhitelistedApps = new ArraySet<>(); |
| 48 | private final ArraySet<String> mSysWhitelistedApps = new ArraySet<>(); |
Lei Yu | e93e77b | 2018-03-12 13:34:36 -0700 | [diff] [blame] | 49 | private final ArraySet<String> mSysWhitelistedAppsExceptIdle = new ArraySet<>(); |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 50 | |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 51 | public PowerWhitelistBackend(Context context) { |
| 52 | this(context, IDeviceIdleController.Stub.asInterface( |
| 53 | ServiceManager.getService(DEVICE_IDLE_SERVICE))); |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @VisibleForTesting |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 57 | PowerWhitelistBackend(Context context, IDeviceIdleController deviceIdleService) { |
| 58 | mAppContext = context.getApplicationContext(); |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 59 | mDeviceIdleService = deviceIdleService; |
| 60 | refreshList(); |
| 61 | } |
| 62 | |
| 63 | public int getWhitelistSize() { |
| 64 | return mWhitelistedApps.size(); |
| 65 | } |
| 66 | |
| 67 | public boolean isSysWhitelisted(String pkg) { |
| 68 | return mSysWhitelistedApps.contains(pkg); |
| 69 | } |
| 70 | |
| 71 | public boolean isWhitelisted(String pkg) { |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 72 | if (mWhitelistedApps.contains(pkg)) { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // Additionally, check if pkg is default dialer/sms. They are considered essential apps and |
| 77 | // should be automatically whitelisted (otherwise user may be able to set restriction on |
| 78 | // them, leading to bad device behavior.) |
| 79 | if (!mAppContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) { |
| 80 | return false; |
| 81 | } |
| 82 | final ComponentName defaultSms = SmsApplication.getDefaultSmsApplication(mAppContext, |
| 83 | true /* updateIfNeeded */); |
| 84 | if (defaultSms != null && TextUtils.equals(pkg, defaultSms.getPackageName())) { |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | final String defaultDialer = DefaultDialerManager.getDefaultDialerApplication(mAppContext); |
| 89 | if (TextUtils.equals(pkg, defaultDialer)) { |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Lei Yu | 3aec6d1 | 2018-05-08 18:09:51 -0700 | [diff] [blame] | 95 | public boolean isWhitelisted(String[] pkgs) { |
| 96 | if (ArrayUtils.isEmpty(pkgs)) { |
| 97 | return false; |
| 98 | } |
| 99 | for (String pkg : pkgs) { |
| 100 | if (isWhitelisted(pkg)) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | |
Lei Yu | e93e77b | 2018-03-12 13:34:36 -0700 | [diff] [blame] | 108 | public boolean isSysWhitelistedExceptIdle(String pkg) { |
| 109 | return mSysWhitelistedAppsExceptIdle.contains(pkg); |
| 110 | } |
| 111 | |
Lei Yu | ae76865 | 2018-04-05 13:44:51 -0700 | [diff] [blame] | 112 | public boolean isSysWhitelistedExceptIdle(String[] pkgs) { |
| 113 | if (ArrayUtils.isEmpty(pkgs)) { |
| 114 | return false; |
| 115 | } |
| 116 | for (String pkg : pkgs) { |
| 117 | if (isSysWhitelistedExceptIdle(pkg)) { |
| 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 125 | public void addApp(String pkg) { |
| 126 | try { |
| 127 | mDeviceIdleService.addPowerSaveWhitelistApp(pkg); |
| 128 | mWhitelistedApps.add(pkg); |
| 129 | } catch (RemoteException e) { |
| 130 | Log.w(TAG, "Unable to reach IDeviceIdleController", e); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | public void removeApp(String pkg) { |
| 135 | try { |
| 136 | mDeviceIdleService.removePowerSaveWhitelistApp(pkg); |
| 137 | mWhitelistedApps.remove(pkg); |
| 138 | } catch (RemoteException e) { |
| 139 | Log.w(TAG, "Unable to reach IDeviceIdleController", e); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @VisibleForTesting |
| 144 | public void refreshList() { |
| 145 | mSysWhitelistedApps.clear(); |
Lei Yu | e93e77b | 2018-03-12 13:34:36 -0700 | [diff] [blame] | 146 | mSysWhitelistedAppsExceptIdle.clear(); |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 147 | mWhitelistedApps.clear(); |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 148 | if (mDeviceIdleService == null) { |
| 149 | return; |
| 150 | } |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 151 | try { |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 152 | final String[] whitelistedApps = mDeviceIdleService.getFullPowerWhitelist(); |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 153 | for (String app : whitelistedApps) { |
| 154 | mWhitelistedApps.add(app); |
| 155 | } |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 156 | final String[] sysWhitelistedApps = mDeviceIdleService.getSystemPowerWhitelist(); |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 157 | for (String app : sysWhitelistedApps) { |
| 158 | mSysWhitelistedApps.add(app); |
| 159 | } |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 160 | final String[] sysWhitelistedAppsExceptIdle = |
Lei Yu | e93e77b | 2018-03-12 13:34:36 -0700 | [diff] [blame] | 161 | mDeviceIdleService.getSystemPowerWhitelistExceptIdle(); |
| 162 | for (String app : sysWhitelistedAppsExceptIdle) { |
| 163 | mSysWhitelistedAppsExceptIdle.add(app); |
| 164 | } |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 165 | } catch (RemoteException e) { |
| 166 | Log.w(TAG, "Unable to reach IDeviceIdleController", e); |
| 167 | } |
| 168 | } |
| 169 | |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 170 | public static PowerWhitelistBackend getInstance(Context context) { |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 171 | if (sInstance == null) { |
Fan Zhang | 091a057 | 2018-05-29 10:55:51 -0700 | [diff] [blame] | 172 | sInstance = new PowerWhitelistBackend(context); |
Tony Mantler | 70676c1 | 2017-12-15 10:09:37 -0800 | [diff] [blame] | 173 | } |
| 174 | return sInstance; |
| 175 | } |
| 176 | |
| 177 | } |