| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.RequiresPermission; |
| 23 |
import android.annotation.SystemApi; |
| 24 |
import android.annotation.SystemService; |
| 25 |
import android.content.Context; |
| 26 |
|
| 27 |
import java.lang.annotation.Retention; |
| 28 |
import java.lang.annotation.RetentionPolicy; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@SystemApi |
| 42 |
@Deprecated |
| 43 |
@SystemService(Context.POWER_WHITELIST_MANAGER) |
| 44 |
public class PowerWhitelistManager { |
| 45 |
private final Context mContext; |
| 46 |
|
| 47 |
|
| 48 |
private final IDeviceIdleController mService; |
| 49 |
|
| 50 |
private final PowerExemptionManager mPowerExemptionManager; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static final int EVENT_UNSPECIFIED = PowerExemptionManager.EVENT_UNSPECIFIED; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final int EVENT_SMS = PowerExemptionManager.EVENT_SMS; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static final int EVENT_MMS = PowerExemptionManager.EVENT_MMS; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
@Retention(RetentionPolicy.SOURCE) |
| 72 |
@IntDef(prefix = {"EVENT_"}, value = { |
| 73 |
EVENT_UNSPECIFIED, |
| 74 |
EVENT_SMS, |
| 75 |
EVENT_MMS, |
| 76 |
}) |
| 77 |
public @interface WhitelistEvent { |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static final int TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED = |
| 84 |
PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static final int TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED = |
| 90 |
PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
@IntDef(flag = true, prefix = { "TEMPORARY_ALLOWLIST_TYPE_" }, value = { |
| 97 |
TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, |
| 98 |
TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED, |
| 99 |
}) |
| 100 |
@Retention(RetentionPolicy.SOURCE) |
| 101 |
public @interface TempAllowListType {} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static final int REASON_DENIED = PowerExemptionManager.REASON_DENIED; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static final int REASON_UNKNOWN = PowerExemptionManager.REASON_UNKNOWN; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public static final int REASON_OTHER = PowerExemptionManager.REASON_OTHER; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static final int REASON_PROC_STATE_PERSISTENT = |
| 123 |
PowerExemptionManager.REASON_PROC_STATE_PERSISTENT; |
| 124 |
|
| 125 |
public static final int REASON_PROC_STATE_PERSISTENT_UI = |
| 126 |
PowerExemptionManager.REASON_PROC_STATE_PERSISTENT_UI; |
| 127 |
|
| 128 |
public static final int REASON_PROC_STATE_TOP = PowerExemptionManager.REASON_PROC_STATE_TOP; |
| 129 |
|
| 130 |
public static final int REASON_PROC_STATE_BTOP = PowerExemptionManager.REASON_PROC_STATE_BTOP; |
| 131 |
|
| 132 |
public static final int REASON_PROC_STATE_FGS = PowerExemptionManager.REASON_PROC_STATE_FGS; |
| 133 |
|
| 134 |
public static final int REASON_PROC_STATE_BFGS = PowerExemptionManager.REASON_PROC_STATE_BFGS; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public static final int REASON_UID_VISIBLE = PowerExemptionManager.REASON_UID_VISIBLE; |
| 139 |
|
| 140 |
public static final int REASON_SYSTEM_UID = PowerExemptionManager.REASON_SYSTEM_UID; |
| 141 |
|
| 142 |
public static final int REASON_ACTIVITY_STARTER = PowerExemptionManager.REASON_ACTIVITY_STARTER; |
| 143 |
|
| 144 |
public static final int REASON_START_ACTIVITY_FLAG = |
| 145 |
PowerExemptionManager.REASON_START_ACTIVITY_FLAG; |
| 146 |
|
| 147 |
public static final int REASON_FGS_BINDING = PowerExemptionManager.REASON_FGS_BINDING; |
| 148 |
|
| 149 |
public static final int REASON_DEVICE_OWNER = PowerExemptionManager.REASON_DEVICE_OWNER; |
| 150 |
|
| 151 |
public static final int REASON_PROFILE_OWNER = PowerExemptionManager.REASON_PROFILE_OWNER; |
| 152 |
|
| 153 |
public static final int REASON_COMPANION_DEVICE_MANAGER = |
| 154 |
PowerExemptionManager.REASON_COMPANION_DEVICE_MANAGER; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static final int REASON_BACKGROUND_ACTIVITY_PERMISSION = |
| 160 |
PowerExemptionManager.REASON_BACKGROUND_ACTIVITY_PERMISSION; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static final int REASON_BACKGROUND_FGS_PERMISSION = |
| 166 |
PowerExemptionManager.REASON_BACKGROUND_FGS_PERMISSION; |
| 167 |
|
| 168 |
public static final int REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION = |
| 169 |
PowerExemptionManager.REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION; |
| 170 |
|
| 171 |
public static final int REASON_INSTR_BACKGROUND_FGS_PERMISSION = |
| 172 |
PowerExemptionManager.REASON_INSTR_BACKGROUND_FGS_PERMISSION; |
| 173 |
|
| 174 |
public static final int REASON_SYSTEM_ALERT_WINDOW_PERMISSION = |
| 175 |
PowerExemptionManager.REASON_SYSTEM_ALERT_WINDOW_PERMISSION; |
| 176 |
|
| 177 |
public static final int REASON_DEVICE_DEMO_MODE = PowerExemptionManager.REASON_DEVICE_DEMO_MODE; |
| 178 |
|
| 179 |
public static final int REASON_ALLOWLISTED_PACKAGE = |
| 180 |
PowerExemptionManager.REASON_ALLOWLISTED_PACKAGE; |
| 181 |
|
| 182 |
public static final int REASON_APPOP = PowerExemptionManager.REASON_APPOP; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static final int REASON_GEOFENCING = PowerExemptionManager.REASON_GEOFENCING; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public static final int REASON_PUSH_MESSAGING = PowerExemptionManager.REASON_PUSH_MESSAGING; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public static final int REASON_PUSH_MESSAGING_OVER_QUOTA = |
| 199 |
PowerExemptionManager.REASON_PUSH_MESSAGING_OVER_QUOTA; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public static final int REASON_ACTIVITY_RECOGNITION = |
| 204 |
PowerExemptionManager.REASON_ACTIVITY_RECOGNITION; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static final int REASON_BOOT_COMPLETED = PowerExemptionManager.REASON_BOOT_COMPLETED; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static final int REASON_PRE_BOOT_COMPLETED = |
| 217 |
PowerExemptionManager.REASON_PRE_BOOT_COMPLETED; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public static final int REASON_LOCKED_BOOT_COMPLETED = |
| 223 |
PowerExemptionManager.REASON_LOCKED_BOOT_COMPLETED; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public static final int REASON_SYSTEM_ALLOW_LISTED = |
| 231 |
PowerExemptionManager.REASON_SYSTEM_ALLOW_LISTED; |
| 232 |
|
| 233 |
public static final int REASON_ALARM_MANAGER_ALARM_CLOCK = |
| 234 |
PowerExemptionManager.REASON_ALARM_MANAGER_ALARM_CLOCK; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static final int REASON_ALARM_MANAGER_WHILE_IDLE = |
| 240 |
PowerExemptionManager.REASON_ALARM_MANAGER_WHILE_IDLE; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public static final int REASON_SERVICE_LAUNCH = PowerExemptionManager.REASON_SERVICE_LAUNCH; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public static final int REASON_KEY_CHAIN = PowerExemptionManager.REASON_KEY_CHAIN; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public static final int REASON_PACKAGE_VERIFIER = PowerExemptionManager.REASON_PACKAGE_VERIFIER; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public static final int REASON_SYNC_MANAGER = PowerExemptionManager.REASON_SYNC_MANAGER; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public static final int REASON_DOMAIN_VERIFICATION_V1 = |
| 266 |
PowerExemptionManager.REASON_DOMAIN_VERIFICATION_V1; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public static final int REASON_DOMAIN_VERIFICATION_V2 = |
| 272 |
PowerExemptionManager.REASON_DOMAIN_VERIFICATION_V2; |
| 273 |
|
| 274 |
public static final int REASON_VPN = 309; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public static final int REASON_NOTIFICATION_SERVICE = |
| 280 |
PowerExemptionManager.REASON_NOTIFICATION_SERVICE; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public static final int REASON_PACKAGE_REPLACED = PowerExemptionManager.REASON_PACKAGE_REPLACED; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
@SystemApi |
| 291 |
public static final int REASON_LOCATION_PROVIDER = |
| 292 |
PowerExemptionManager.REASON_LOCATION_PROVIDER; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public static final int REASON_MEDIA_BUTTON = PowerExemptionManager.REASON_MEDIA_BUTTON; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public static final int REASON_EVENT_SMS = PowerExemptionManager.REASON_EVENT_SMS; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public static final int REASON_EVENT_MMS = PowerExemptionManager.REASON_EVENT_MMS; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public static final int REASON_SHELL = PowerExemptionManager.REASON_SHELL; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
@IntDef(flag = true, prefix = { "REASON_" }, value = { |
| 319 |
|
| 320 |
REASON_DENIED, |
| 321 |
REASON_UNKNOWN, |
| 322 |
REASON_OTHER, |
| 323 |
REASON_PROC_STATE_PERSISTENT, |
| 324 |
REASON_PROC_STATE_PERSISTENT_UI, |
| 325 |
REASON_PROC_STATE_TOP, |
| 326 |
REASON_PROC_STATE_BTOP, |
| 327 |
REASON_PROC_STATE_FGS, |
| 328 |
REASON_PROC_STATE_BFGS, |
| 329 |
REASON_UID_VISIBLE, |
| 330 |
REASON_SYSTEM_UID, |
| 331 |
REASON_ACTIVITY_STARTER, |
| 332 |
REASON_START_ACTIVITY_FLAG, |
| 333 |
REASON_FGS_BINDING, |
| 334 |
REASON_DEVICE_OWNER, |
| 335 |
REASON_PROFILE_OWNER, |
| 336 |
REASON_COMPANION_DEVICE_MANAGER, |
| 337 |
REASON_BACKGROUND_ACTIVITY_PERMISSION, |
| 338 |
REASON_BACKGROUND_FGS_PERMISSION, |
| 339 |
REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION, |
| 340 |
REASON_INSTR_BACKGROUND_FGS_PERMISSION, |
| 341 |
REASON_SYSTEM_ALERT_WINDOW_PERMISSION, |
| 342 |
REASON_DEVICE_DEMO_MODE, |
| 343 |
REASON_ALLOWLISTED_PACKAGE, |
| 344 |
REASON_APPOP, |
| 345 |
|
| 346 |
REASON_GEOFENCING, |
| 347 |
REASON_PUSH_MESSAGING, |
| 348 |
REASON_PUSH_MESSAGING_OVER_QUOTA, |
| 349 |
REASON_ACTIVITY_RECOGNITION, |
| 350 |
REASON_BOOT_COMPLETED, |
| 351 |
REASON_PRE_BOOT_COMPLETED, |
| 352 |
REASON_LOCKED_BOOT_COMPLETED, |
| 353 |
REASON_SYSTEM_ALLOW_LISTED, |
| 354 |
REASON_ALARM_MANAGER_ALARM_CLOCK, |
| 355 |
REASON_ALARM_MANAGER_WHILE_IDLE, |
| 356 |
REASON_SERVICE_LAUNCH, |
| 357 |
REASON_KEY_CHAIN, |
| 358 |
REASON_PACKAGE_VERIFIER, |
| 359 |
REASON_SYNC_MANAGER, |
| 360 |
REASON_DOMAIN_VERIFICATION_V1, |
| 361 |
REASON_DOMAIN_VERIFICATION_V2, |
| 362 |
REASON_VPN, |
| 363 |
REASON_NOTIFICATION_SERVICE, |
| 364 |
REASON_PACKAGE_REPLACED, |
| 365 |
REASON_LOCATION_PROVIDER, |
| 366 |
REASON_MEDIA_BUTTON, |
| 367 |
REASON_EVENT_SMS, |
| 368 |
REASON_EVENT_MMS, |
| 369 |
REASON_SHELL, |
| 370 |
}) |
| 371 |
@Retention(RetentionPolicy.SOURCE) |
| 372 |
public @interface ReasonCode {} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public PowerWhitelistManager(@NonNull Context context) { |
| 378 |
mContext = context; |
| 379 |
mService = context.getSystemService(DeviceIdleManager.class).getService(); |
| 380 |
mPowerExemptionManager = context.getSystemService(PowerExemptionManager.class); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
@Deprecated |
| 389 |
@RequiresPermission(android.Manifest.permission.DEVICE_POWER) |
| 390 |
public void addToWhitelist(@NonNull String packageName) { |
| 391 |
mPowerExemptionManager.addToPermanentAllowList(packageName); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
@Deprecated |
| 400 |
@RequiresPermission(android.Manifest.permission.DEVICE_POWER) |
| 401 |
public void addToWhitelist(@NonNull List<String> packageNames) { |
| 402 |
mPowerExemptionManager.addToPermanentAllowList(packageNames); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
@Deprecated |
| 415 |
@NonNull |
| 416 |
public int[] getWhitelistedAppIds(boolean includingIdle) { |
| 417 |
return mPowerExemptionManager.getAllowListedAppIds(includingIdle); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
@Deprecated |
| 430 |
public boolean isWhitelisted(@NonNull String packageName, boolean includingIdle) { |
| 431 |
return mPowerExemptionManager.isAllowListed(packageName, includingIdle); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
@Deprecated |
| 443 |
@RequiresPermission(android.Manifest.permission.DEVICE_POWER) |
| 444 |
public void removeFromWhitelist(@NonNull String packageName) { |
| 445 |
mPowerExemptionManager.removeFromPermanentAllowList(packageName); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
@Deprecated |
| 459 |
@RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) |
| 460 |
public void whitelistAppTemporarily(@NonNull String packageName, long durationMs, |
| 461 |
@ReasonCode int reasonCode, @Nullable String reason) { |
| 462 |
mPowerExemptionManager.addToTemporaryAllowList(packageName, reasonCode, reason, durationMs); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
@Deprecated |
| 474 |
@RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) |
| 475 |
public void whitelistAppTemporarily(@NonNull String packageName, long durationMs) { |
| 476 |
mPowerExemptionManager.addToTemporaryAllowList( |
| 477 |
packageName, REASON_UNKNOWN, packageName, durationMs); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
@Deprecated |
| 494 |
@RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) |
| 495 |
public long whitelistAppTemporarilyForEvent(@NonNull String packageName, |
| 496 |
@WhitelistEvent int event, @Nullable String reason) { |
| 497 |
return mPowerExemptionManager.addToTemporaryAllowListForEvent( |
| 498 |
packageName, REASON_UNKNOWN, reason, event); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
@Deprecated |
| 516 |
@RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) |
| 517 |
public long whitelistAppTemporarilyForEvent(@NonNull String packageName, |
| 518 |
@WhitelistEvent int event, @ReasonCode int reasonCode, @Nullable String reason) { |
| 519 |
return mPowerExemptionManager.addToTemporaryAllowListForEvent( |
| 520 |
packageName, reasonCode, reason, event); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
@Deprecated |
| 529 |
public static @ReasonCode int getReasonCodeFromProcState(int procState) { |
| 530 |
return PowerExemptionManager.getReasonCodeFromProcState(procState); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
@Deprecated |
| 541 |
public static String reasonCodeToString(@ReasonCode int reasonCode) { |
| 542 |
return PowerExemptionManager.reasonCodeToString(reasonCode); |
| 543 |
} |
| 544 |
} |