1 /* 2 * Copyright (C) 2020 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.server.am; 18 19 import static android.app.ProcessMemoryState.HOSTING_COMPONENT_TYPE_BOUND_SERVICE; 20 import static android.app.ProcessMemoryState.HOSTING_COMPONENT_TYPE_FOREGROUND_SERVICE; 21 22 import static com.android.server.am.Flags.serviceBindingOomAdjPolicy; 23 24 import android.annotation.Nullable; 25 import android.app.ActivityManager; 26 import android.content.Context; 27 import android.content.pm.ServiceInfo; 28 import android.os.IBinder; 29 import android.os.SystemClock; 30 import android.util.ArrayMap; 31 import android.util.ArraySet; 32 33 import com.android.internal.annotations.GuardedBy; 34 import com.android.server.wm.WindowProcessController; 35 36 import java.io.PrintWriter; 37 import java.util.ArrayList; 38 39 /** 40 * The state info of all services in the process. 41 */ 42 final class ProcessServiceRecord { 43 /** 44 * Are there any client services with activities? 45 */ 46 private boolean mHasClientActivities; 47 48 /** 49 * Running any services that are foreground? 50 */ 51 private boolean mHasForegroundServices; 52 53 /** 54 * Last reported state of whether it's running any services that are foreground. 55 */ 56 private boolean mRepHasForegroundServices; 57 58 /** 59 * Running any services that are almost perceptible (started with 60 * {@link Context#BIND_ALMOST_PERCEPTIBLE} while the app was on TOP)? 61 */ 62 private boolean mHasTopStartedAlmostPerceptibleServices; 63 64 /** 65 * The latest value of {@link ServiceRecord#lastTopAlmostPerceptibleBindRequestUptimeMs} among 66 * the currently running services. 67 */ 68 private long mLastTopStartedAlmostPerceptibleBindRequestUptimeMs; 69 70 /** 71 * Service that applied current connectionGroup/Importance. 72 */ 73 private ServiceRecord mConnectionService; 74 75 /** 76 * Last group set by a connection. 77 */ 78 private int mConnectionGroup; 79 80 /** 81 * Last importance set by a connection. 82 */ 83 private int mConnectionImportance; 84 85 /** 86 * The OR'ed foreground service types that are running on this process. 87 * Note, because TYPE_NONE (==0) is also a valid type for pre-U apps, this field doesn't tell 88 * if the process has any TYPE_NONE FGS or not, but {@link #mHasTypeNoneFgs} will be set 89 * in that case. 90 */ 91 private int mFgServiceTypes; 92 93 /** 94 * Whether the process has any foreground services of TYPE_NONE running. 95 * @see #mFgServiceTypes 96 */ 97 private boolean mHasTypeNoneFgs; 98 99 /** 100 * Last reported foreground service types. 101 */ 102 private int mRepFgServiceTypes; 103 104 /** 105 * Bound using BIND_ABOVE_CLIENT, so want to be lower. 106 */ 107 private boolean mHasAboveClient; 108 109 /** 110 * Bound using BIND_TREAT_LIKE_ACTIVITY. 111 */ 112 private boolean mTreatLikeActivity; 113 114 /** 115 * Do we need to be executing services in the foreground? 116 */ 117 private boolean mExecServicesFg; 118 119 /** 120 * App is allowed to manage allowlists such as temporary Power Save mode allowlist. 121 */ 122 boolean mAllowlistManager; 123 124 /** 125 * All ServiceRecord running in this process. 126 */ 127 final ArraySet<ServiceRecord> mServices = new ArraySet<>(); 128 129 /** 130 * Services that are currently executing code (need to remain foreground). 131 */ 132 private final ArraySet<ServiceRecord> mExecutingServices = new ArraySet<>(); 133 134 /** 135 * All ConnectionRecord this process holds. 136 */ 137 private final ArraySet<ConnectionRecord> mConnections = new ArraySet<>(); 138 139 /** 140 * All ConnectionRecord this process holds indirectly to SDK sandbox processes. 141 */ 142 private @Nullable ArraySet<ConnectionRecord> mSdkSandboxConnections; 143 144 /** 145 * A set of UIDs of all bound clients. 146 */ 147 private ArraySet<Integer> mBoundClientUids = new ArraySet<>(); 148 149 /** 150 * The process should schedule a service timeout timer but haven't done so. 151 */ 152 private boolean mScheduleServiceTimeoutPending; 153 154 final ProcessRecord mApp; 155 156 private final ActivityManagerService mService; 157 ProcessServiceRecord(ProcessRecord app)158 ProcessServiceRecord(ProcessRecord app) { 159 mApp = app; 160 mService = app.mService; 161 } 162 setHasClientActivities(boolean hasClientActivities)163 void setHasClientActivities(boolean hasClientActivities) { 164 mHasClientActivities = hasClientActivities; 165 mApp.getWindowProcessController().setHasClientActivities(hasClientActivities); 166 } 167 hasClientActivities()168 boolean hasClientActivities() { 169 return mHasClientActivities; 170 } 171 setHasForegroundServices(boolean hasForegroundServices, int fgServiceTypes, boolean hasTypeNoneFgs)172 void setHasForegroundServices(boolean hasForegroundServices, int fgServiceTypes, 173 boolean hasTypeNoneFgs) { 174 // hasForegroundServices should be the same as "either it has any FGS types, or none types". 175 // We still take this as a parameter because it's used in the callsite... 176 if (ActivityManagerDebugConfig.DEBUG_SERVICE 177 && hasForegroundServices != ((fgServiceTypes != 0) || hasTypeNoneFgs)) { 178 throw new IllegalStateException("hasForegroundServices mismatch"); 179 } 180 181 mHasForegroundServices = hasForegroundServices; 182 mFgServiceTypes = fgServiceTypes; 183 mHasTypeNoneFgs = hasTypeNoneFgs; 184 mApp.getWindowProcessController().setHasForegroundServices(hasForegroundServices); 185 if (hasForegroundServices) { 186 mApp.mProfile.addHostingComponentType(HOSTING_COMPONENT_TYPE_FOREGROUND_SERVICE); 187 } else { 188 mApp.mProfile.clearHostingComponentType(HOSTING_COMPONENT_TYPE_FOREGROUND_SERVICE); 189 } 190 } 191 192 /** 193 * @return true if this process has any foreground services (even timed-out short-FGS) 194 */ hasForegroundServices()195 boolean hasForegroundServices() { 196 return mHasForegroundServices; 197 } 198 setHasReportedForegroundServices(boolean hasForegroundServices)199 void setHasReportedForegroundServices(boolean hasForegroundServices) { 200 mRepHasForegroundServices = hasForegroundServices; 201 } 202 hasReportedForegroundServices()203 boolean hasReportedForegroundServices() { 204 return mRepHasForegroundServices; 205 } 206 207 /** 208 * Returns the FGS types, but it doesn't tell if the types include "NONE" or not, use 209 * {@link #hasForegroundServices()} 210 */ getForegroundServiceTypes()211 int getForegroundServiceTypes() { 212 return mHasForegroundServices ? mFgServiceTypes : 0; 213 } 214 areForegroundServiceTypesSame(@erviceInfo.ForegroundServiceType int types, boolean hasTypeNoneFgs)215 boolean areForegroundServiceTypesSame(@ServiceInfo.ForegroundServiceType int types, 216 boolean hasTypeNoneFgs) { 217 return ((getForegroundServiceTypes() & types) == types) 218 && (mHasTypeNoneFgs == hasTypeNoneFgs); 219 } 220 221 /** 222 * @return true if the fgs types includes any of the given types. 223 * (wouldn't work for TYPE_NONE, which is 0) 224 */ containsAnyForegroundServiceTypes(@erviceInfo.ForegroundServiceType int types)225 boolean containsAnyForegroundServiceTypes(@ServiceInfo.ForegroundServiceType int types) { 226 return (getForegroundServiceTypes() & types) != 0; 227 } 228 229 /** 230 * @return true if the process has any FGS that are _not_ a "short" FGS. 231 */ hasNonShortForegroundServices()232 boolean hasNonShortForegroundServices() { 233 if (!mHasForegroundServices) { 234 return false; // Process has no FGS running. 235 } 236 // Does the process has any FGS of TYPE_NONE? 237 if (mHasTypeNoneFgs) { 238 return true; 239 } 240 // If not, we can just check mFgServiceTypes. 241 return mFgServiceTypes != ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE; 242 } 243 244 /** 245 * @return if this process: 246 * - has at least one short-FGS 247 * - has no other types of FGS 248 * - and all the short-FGSes are procstate-timed out. 249 */ areAllShortForegroundServicesProcstateTimedOut(long nowUptime)250 boolean areAllShortForegroundServicesProcstateTimedOut(long nowUptime) { 251 if (!mHasForegroundServices) { // Process has no FGS? 252 return false; 253 } 254 if (hasNonShortForegroundServices()) { // Any non-short FGS running? 255 return false; 256 } 257 // Now we need to look at all short-FGS within the process and see if all of them are 258 // procstate-timed-out or not. 259 for (int i = mServices.size() - 1; i >= 0; i--) { 260 final ServiceRecord sr = mServices.valueAt(i); 261 if (!sr.isShortFgs() || !sr.hasShortFgsInfo()) { 262 continue; 263 } 264 if (sr.getShortFgsInfo().getProcStateDemoteTime() >= nowUptime) { 265 return false; 266 } 267 } 268 return true; 269 } 270 getReportedForegroundServiceTypes()271 int getReportedForegroundServiceTypes() { 272 return mRepFgServiceTypes; 273 } 274 setReportedForegroundServiceTypes(int foregroundServiceTypes)275 void setReportedForegroundServiceTypes(int foregroundServiceTypes) { 276 mRepFgServiceTypes = foregroundServiceTypes; 277 } 278 getNumForegroundServices()279 int getNumForegroundServices() { 280 int count = 0; 281 for (int i = 0, serviceCount = mServices.size(); i < serviceCount; i++) { 282 if (mServices.valueAt(i).isForeground) { 283 count++; 284 } 285 } 286 return count; 287 } 288 updateHasTopStartedAlmostPerceptibleServices()289 void updateHasTopStartedAlmostPerceptibleServices() { 290 mHasTopStartedAlmostPerceptibleServices = false; 291 mLastTopStartedAlmostPerceptibleBindRequestUptimeMs = 0; 292 for (int s = mServices.size() - 1; s >= 0; --s) { 293 final ServiceRecord sr = mServices.valueAt(s); 294 mLastTopStartedAlmostPerceptibleBindRequestUptimeMs = Math.max( 295 mLastTopStartedAlmostPerceptibleBindRequestUptimeMs, 296 sr.lastTopAlmostPerceptibleBindRequestUptimeMs); 297 if (!mHasTopStartedAlmostPerceptibleServices && isAlmostPerceptible(sr)) { 298 mHasTopStartedAlmostPerceptibleServices = true; 299 } 300 } 301 } 302 isAlmostPerceptible(ServiceRecord record)303 private boolean isAlmostPerceptible(ServiceRecord record) { 304 if (record.lastTopAlmostPerceptibleBindRequestUptimeMs <= 0) { 305 return false; 306 } 307 final ArrayMap<IBinder, ArrayList<ConnectionRecord>> serviceConnections = 308 record.getConnections(); 309 for (int m = serviceConnections.size() - 1; m >= 0; --m) { 310 final ArrayList<ConnectionRecord> clist = serviceConnections.valueAt(m); 311 312 for (int c = clist.size() - 1; c >= 0; --c) { 313 final ConnectionRecord cr = clist.get(c); 314 if (cr.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)) { 315 return true; 316 } 317 } 318 } 319 return false; 320 } 321 hasTopStartedAlmostPerceptibleServices()322 boolean hasTopStartedAlmostPerceptibleServices() { 323 return mHasTopStartedAlmostPerceptibleServices 324 || (mLastTopStartedAlmostPerceptibleBindRequestUptimeMs > 0 325 && SystemClock.uptimeMillis() - mLastTopStartedAlmostPerceptibleBindRequestUptimeMs 326 < mService.mConstants.mServiceBindAlmostPerceptibleTimeoutMs); 327 } 328 getConnectionService()329 ServiceRecord getConnectionService() { 330 return mConnectionService; 331 } 332 setConnectionService(ServiceRecord connectionService)333 void setConnectionService(ServiceRecord connectionService) { 334 mConnectionService = connectionService; 335 } 336 getConnectionGroup()337 int getConnectionGroup() { 338 return mConnectionGroup; 339 } 340 setConnectionGroup(int connectionGroup)341 void setConnectionGroup(int connectionGroup) { 342 mConnectionGroup = connectionGroup; 343 } 344 getConnectionImportance()345 int getConnectionImportance() { 346 return mConnectionImportance; 347 } 348 setConnectionImportance(int connectionImportance)349 void setConnectionImportance(int connectionImportance) { 350 mConnectionImportance = connectionImportance; 351 } 352 updateHasAboveClientLocked()353 void updateHasAboveClientLocked() { 354 mHasAboveClient = false; 355 for (int i = mConnections.size() - 1; i >= 0; i--) { 356 ConnectionRecord cr = mConnections.valueAt(i); 357 358 final boolean isSameProcess = cr.binding.service.app != null 359 && cr.binding.service.app.mServices == this; 360 if (!isSameProcess && cr.hasFlag(Context.BIND_ABOVE_CLIENT)) { 361 mHasAboveClient = true; 362 break; 363 } 364 } 365 } 366 setHasAboveClient(boolean hasAboveClient)367 void setHasAboveClient(boolean hasAboveClient) { 368 mHasAboveClient = hasAboveClient; 369 } 370 hasAboveClient()371 boolean hasAboveClient() { 372 return mHasAboveClient; 373 } 374 modifyRawOomAdj(int adj)375 int modifyRawOomAdj(int adj) { 376 if (mHasAboveClient) { 377 // If this process has bound to any services with BIND_ABOVE_CLIENT, 378 // then we need to drop its adjustment to be lower than the service's 379 // in order to honor the request. We want to drop it by one adjustment 380 // level... but there is special meaning applied to various levels so 381 // we will skip some of them. 382 if (adj < ProcessList.FOREGROUND_APP_ADJ) { 383 // System process will not get dropped, ever 384 } else if (adj < ProcessList.VISIBLE_APP_ADJ) { 385 adj = ProcessList.VISIBLE_APP_ADJ; 386 } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { 387 adj = ProcessList.PERCEPTIBLE_APP_ADJ; 388 } else if (adj < ProcessList.PERCEPTIBLE_LOW_APP_ADJ) { 389 adj = ProcessList.PERCEPTIBLE_LOW_APP_ADJ; 390 } else if (adj < ProcessList.CACHED_APP_MIN_ADJ) { 391 adj = ProcessList.CACHED_APP_MIN_ADJ; 392 } else if (adj < ProcessList.CACHED_APP_MAX_ADJ) { 393 adj++; 394 } 395 } 396 return adj; 397 } 398 isTreatedLikeActivity()399 boolean isTreatedLikeActivity() { 400 return mTreatLikeActivity; 401 } 402 setTreatLikeActivity(boolean treatLikeActivity)403 void setTreatLikeActivity(boolean treatLikeActivity) { 404 mTreatLikeActivity = treatLikeActivity; 405 } 406 shouldExecServicesFg()407 boolean shouldExecServicesFg() { 408 return mExecServicesFg; 409 } 410 setExecServicesFg(boolean execServicesFg)411 void setExecServicesFg(boolean execServicesFg) { 412 mExecServicesFg = execServicesFg; 413 } 414 415 /** 416 * Records a service as running in the process. Note that this method does not actually start 417 * the service, but records the service as started for bookkeeping. 418 * 419 * @return true if the service was added, false otherwise. 420 */ startService(ServiceRecord record)421 boolean startService(ServiceRecord record) { 422 if (record == null) { 423 return false; 424 } 425 boolean added = mServices.add(record); 426 if (added && record.serviceInfo != null) { 427 mApp.getWindowProcessController().onServiceStarted(record.serviceInfo); 428 updateHostingComonentTypeForBindingsLocked(); 429 } 430 if (record.lastTopAlmostPerceptibleBindRequestUptimeMs > 0) { 431 mLastTopStartedAlmostPerceptibleBindRequestUptimeMs = Math.max( 432 mLastTopStartedAlmostPerceptibleBindRequestUptimeMs, 433 record.lastTopAlmostPerceptibleBindRequestUptimeMs); 434 if (!mHasTopStartedAlmostPerceptibleServices) { 435 mHasTopStartedAlmostPerceptibleServices = isAlmostPerceptible(record); 436 } 437 } 438 return added; 439 } 440 441 /** 442 * Records a service as stopped. Note that like {@link #startService(ServiceRecord)} this method 443 * does not actually stop the service, but records the service as stopped for bookkeeping. 444 * 445 * @return true if the service was removed, false otherwise. 446 */ stopService(ServiceRecord record)447 boolean stopService(ServiceRecord record) { 448 final boolean removed = mServices.remove(record); 449 if (record.lastTopAlmostPerceptibleBindRequestUptimeMs > 0) { 450 updateHasTopStartedAlmostPerceptibleServices(); 451 } 452 if (removed) { 453 updateHostingComonentTypeForBindingsLocked(); 454 } 455 return removed; 456 } 457 458 /** 459 * The same as calling {@link #stopService(ServiceRecord)} on all current running services. 460 */ stopAllServices()461 void stopAllServices() { 462 mServices.clear(); 463 updateHasTopStartedAlmostPerceptibleServices(); 464 } 465 466 /** 467 * Returns the number of services added with {@link #startService(ServiceRecord)} and not yet 468 * removed by a call to {@link #stopService(ServiceRecord)} or {@link #stopAllServices()}. 469 * 470 * @see #startService(ServiceRecord) 471 * @see #stopService(ServiceRecord) 472 */ numberOfRunningServices()473 int numberOfRunningServices() { 474 return mServices.size(); 475 } 476 477 /** 478 * Returns the service at the specified {@code index}. 479 * 480 * @see #numberOfRunningServices() 481 */ getRunningServiceAt(int index)482 ServiceRecord getRunningServiceAt(int index) { 483 return mServices.valueAt(index); 484 } 485 startExecutingService(ServiceRecord service)486 void startExecutingService(ServiceRecord service) { 487 mExecutingServices.add(service); 488 } 489 stopExecutingService(ServiceRecord service)490 void stopExecutingService(ServiceRecord service) { 491 mExecutingServices.remove(service); 492 } 493 stopAllExecutingServices()494 void stopAllExecutingServices() { 495 mExecutingServices.clear(); 496 } 497 getExecutingServiceAt(int index)498 ServiceRecord getExecutingServiceAt(int index) { 499 return mExecutingServices.valueAt(index); 500 } 501 numberOfExecutingServices()502 int numberOfExecutingServices() { 503 return mExecutingServices.size(); 504 } 505 addConnection(ConnectionRecord connection)506 void addConnection(ConnectionRecord connection) { 507 mConnections.add(connection); 508 addSdkSandboxConnectionIfNecessary(connection); 509 } 510 removeConnection(ConnectionRecord connection)511 void removeConnection(ConnectionRecord connection) { 512 mConnections.remove(connection); 513 removeSdkSandboxConnectionIfNecessary(connection); 514 } 515 removeAllConnections()516 void removeAllConnections() { 517 for (int i = 0, size = mConnections.size(); i < size; i++) { 518 removeSdkSandboxConnectionIfNecessary(mConnections.valueAt(i)); 519 } 520 mConnections.clear(); 521 } 522 getConnectionAt(int index)523 ConnectionRecord getConnectionAt(int index) { 524 return mConnections.valueAt(index); 525 } 526 numberOfConnections()527 int numberOfConnections() { 528 return mConnections.size(); 529 } 530 addSdkSandboxConnectionIfNecessary(ConnectionRecord connection)531 private void addSdkSandboxConnectionIfNecessary(ConnectionRecord connection) { 532 final ProcessRecord attributedClient = connection.binding.attributedClient; 533 if (attributedClient != null && connection.binding.service.isSdkSandbox) { 534 if (attributedClient.mServices.mSdkSandboxConnections == null) { 535 attributedClient.mServices.mSdkSandboxConnections = new ArraySet<>(); 536 } 537 attributedClient.mServices.mSdkSandboxConnections.add(connection); 538 } 539 } 540 removeSdkSandboxConnectionIfNecessary(ConnectionRecord connection)541 private void removeSdkSandboxConnectionIfNecessary(ConnectionRecord connection) { 542 final ProcessRecord attributedClient = connection.binding.attributedClient; 543 if (attributedClient != null && connection.binding.service.isSdkSandbox) { 544 if (attributedClient.mServices.mSdkSandboxConnections != null) { 545 attributedClient.mServices.mSdkSandboxConnections.remove(connection); 546 } 547 } 548 } 549 removeAllSdkSandboxConnections()550 void removeAllSdkSandboxConnections() { 551 if (mSdkSandboxConnections != null) { 552 mSdkSandboxConnections.clear(); 553 } 554 } 555 getSdkSandboxConnectionAt(int index)556 ConnectionRecord getSdkSandboxConnectionAt(int index) { 557 return mSdkSandboxConnections != null ? mSdkSandboxConnections.valueAt(index) : null; 558 } 559 numberOfSdkSandboxConnections()560 int numberOfSdkSandboxConnections() { 561 return mSdkSandboxConnections != null ? mSdkSandboxConnections.size() : 0; 562 } 563 addBoundClientUid(int clientUid, String clientPackageName, long bindFlags)564 void addBoundClientUid(int clientUid, String clientPackageName, long bindFlags) { 565 mBoundClientUids.add(clientUid); 566 mApp.getWindowProcessController() 567 .addBoundClientUid(clientUid, clientPackageName, bindFlags); 568 } 569 updateBoundClientUids()570 void updateBoundClientUids() { 571 clearBoundClientUids(); 572 if (mServices.isEmpty()) { 573 return; 574 } 575 // grab a set of clientUids of all mConnections of all services 576 final ArraySet<Integer> boundClientUids = new ArraySet<>(); 577 final int serviceCount = mServices.size(); 578 WindowProcessController controller = mApp.getWindowProcessController(); 579 for (int j = 0; j < serviceCount; j++) { 580 final ArrayMap<IBinder, ArrayList<ConnectionRecord>> conns = 581 mServices.valueAt(j).getConnections(); 582 final int size = conns.size(); 583 for (int conni = 0; conni < size; conni++) { 584 ArrayList<ConnectionRecord> c = conns.valueAt(conni); 585 for (int i = 0; i < c.size(); i++) { 586 ConnectionRecord cr = c.get(i); 587 boundClientUids.add(cr.clientUid); 588 controller.addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.getFlags()); 589 } 590 } 591 } 592 mBoundClientUids = boundClientUids; 593 } 594 addBoundClientUidsOfNewService(ServiceRecord sr)595 void addBoundClientUidsOfNewService(ServiceRecord sr) { 596 if (sr == null) { 597 return; 598 } 599 ArrayMap<IBinder, ArrayList<ConnectionRecord>> conns = sr.getConnections(); 600 for (int conni = conns.size() - 1; conni >= 0; conni--) { 601 ArrayList<ConnectionRecord> c = conns.valueAt(conni); 602 for (int i = 0; i < c.size(); i++) { 603 ConnectionRecord cr = c.get(i); 604 mBoundClientUids.add(cr.clientUid); 605 mApp.getWindowProcessController() 606 .addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.getFlags()); 607 608 } 609 } 610 } 611 clearBoundClientUids()612 void clearBoundClientUids() { 613 mBoundClientUids.clear(); 614 mApp.getWindowProcessController().clearBoundClientUids(); 615 } 616 617 @GuardedBy("mService") updateHostingComonentTypeForBindingsLocked()618 void updateHostingComonentTypeForBindingsLocked() { 619 boolean hasBoundClient = false; 620 for (int i = numberOfRunningServices() - 1; i >= 0; i--) { 621 final ServiceRecord sr = getRunningServiceAt(i); 622 if (sr != null && !sr.getConnections().isEmpty()) { 623 hasBoundClient = true; 624 break; 625 } 626 } 627 if (hasBoundClient) { 628 mApp.mProfile.addHostingComponentType(HOSTING_COMPONENT_TYPE_BOUND_SERVICE); 629 } else { 630 mApp.mProfile.clearHostingComponentType(HOSTING_COMPONENT_TYPE_BOUND_SERVICE); 631 } 632 } 633 634 @GuardedBy("mService") incServiceCrashCountLocked(long now)635 boolean incServiceCrashCountLocked(long now) { 636 final boolean procIsBoundForeground = mApp.mState.getCurProcState() 637 == ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE; 638 boolean tryAgain = false; 639 // Bump up the crash count of any services currently running in the proc. 640 for (int i = numberOfRunningServices() - 1; i >= 0; i--) { 641 // Any services running in the application need to be placed 642 // back in the pending list. 643 ServiceRecord sr = getRunningServiceAt(i); 644 // If the service was restarted a while ago, then reset crash count, else increment it. 645 if (now > sr.restartTime + ActivityManagerConstants.MIN_CRASH_INTERVAL) { 646 sr.crashCount = 1; 647 } else { 648 sr.crashCount++; 649 } 650 // Allow restarting for started or bound foreground services that are crashing. 651 // This includes wallpapers. 652 if (sr.crashCount < mService.mConstants.BOUND_SERVICE_MAX_CRASH_RETRY 653 && (sr.isForeground || procIsBoundForeground)) { 654 tryAgain = true; 655 } 656 } 657 return tryAgain; 658 } 659 660 @GuardedBy("mService") onCleanupApplicationRecordLocked()661 void onCleanupApplicationRecordLocked() { 662 mTreatLikeActivity = false; 663 mHasAboveClient = false; 664 setHasClientActivities(false); 665 } 666 667 @GuardedBy("mService") noteScheduleServiceTimeoutPending(boolean pending)668 void noteScheduleServiceTimeoutPending(boolean pending) { 669 mScheduleServiceTimeoutPending = pending; 670 } 671 672 @GuardedBy("mService") isScheduleServiceTimeoutPending()673 boolean isScheduleServiceTimeoutPending() { 674 return mScheduleServiceTimeoutPending; 675 } 676 onProcessUnfrozen()677 void onProcessUnfrozen() { 678 synchronized (mService) { 679 scheduleServiceTimeoutIfNeededLocked(); 680 } 681 } 682 onProcessFrozenCancelled()683 void onProcessFrozenCancelled() { 684 synchronized (mService) { 685 scheduleServiceTimeoutIfNeededLocked(); 686 } 687 } 688 689 @GuardedBy("mService") scheduleServiceTimeoutIfNeededLocked()690 private void scheduleServiceTimeoutIfNeededLocked() { 691 if (!serviceBindingOomAdjPolicy()) { 692 return; 693 } 694 if (mScheduleServiceTimeoutPending && mExecutingServices.size() > 0) { 695 mService.mServices.scheduleServiceTimeoutLocked(mApp); 696 // We'll need to reset the executingStart since the app was frozen. 697 final long now = SystemClock.uptimeMillis(); 698 for (int i = 0, size = mExecutingServices.size(); i < size; i++) { 699 mExecutingServices.valueAt(i).executingStart = now; 700 } 701 } 702 } 703 dump(PrintWriter pw, String prefix, long nowUptime)704 void dump(PrintWriter pw, String prefix, long nowUptime) { 705 if (mHasForegroundServices || mApp.mState.getForcingToImportant() != null) { 706 pw.print(prefix); pw.print("mHasForegroundServices="); pw.print(mHasForegroundServices); 707 pw.print(" forcingToImportant="); pw.println(mApp.mState.getForcingToImportant()); 708 } 709 if (mHasTopStartedAlmostPerceptibleServices 710 || mLastTopStartedAlmostPerceptibleBindRequestUptimeMs > 0) { 711 pw.print(prefix); pw.print("mHasTopStartedAlmostPerceptibleServices="); 712 pw.print(mHasTopStartedAlmostPerceptibleServices); 713 pw.print(" mLastTopStartedAlmostPerceptibleBindRequestUptimeMs="); 714 pw.println(mLastTopStartedAlmostPerceptibleBindRequestUptimeMs); 715 } 716 if (mHasClientActivities || mHasAboveClient || mTreatLikeActivity) { 717 pw.print(prefix); pw.print("hasClientActivities="); pw.print(mHasClientActivities); 718 pw.print(" hasAboveClient="); pw.print(mHasAboveClient); 719 pw.print(" treatLikeActivity="); pw.println(mTreatLikeActivity); 720 } 721 if (mConnectionService != null || mConnectionGroup != 0) { 722 pw.print(prefix); pw.print("connectionGroup="); pw.print(mConnectionGroup); 723 pw.print(" Importance="); pw.print(mConnectionImportance); 724 pw.print(" Service="); pw.println(mConnectionService); 725 } 726 if (mAllowlistManager) { 727 pw.print(prefix); pw.print("allowlistManager="); pw.println(mAllowlistManager); 728 } 729 if (mServices.size() > 0) { 730 pw.print(prefix); pw.println("Services:"); 731 for (int i = 0, size = mServices.size(); i < size; i++) { 732 pw.print(prefix); pw.print(" - "); pw.println(mServices.valueAt(i)); 733 } 734 } 735 if (mExecutingServices.size() > 0) { 736 pw.print(prefix); pw.print("Executing Services (fg="); 737 pw.print(mExecServicesFg); pw.println(")"); 738 for (int i = 0, size = mExecutingServices.size(); i < size; i++) { 739 pw.print(prefix); pw.print(" - "); pw.println(mExecutingServices.valueAt(i)); 740 } 741 } 742 if (mConnections.size() > 0) { 743 pw.print(prefix); pw.println("mConnections:"); 744 for (int i = 0, size = mConnections.size(); i < size; i++) { 745 pw.print(prefix); pw.print(" - "); pw.println(mConnections.valueAt(i)); 746 } 747 } 748 if (serviceBindingOomAdjPolicy()) { 749 pw.print(prefix); 750 pw.print("scheduleServiceTimeoutPending="); 751 pw.println(mScheduleServiceTimeoutPending); 752 } 753 } 754 } 755