From 6b9ec6e616d5401e300152b379aef6ab52220b87 Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Fri, 24 Feb 2023 01:59:28 +0000 Subject: [PATCH] Fix NPE. Avoid an NPE in the shell command handler when ActivityManagerService doesn't have a top app record. Bug: 261999509 Test: atest FrameworksServicesTests:ActivityManagerServiceTest Change-Id: I0bcc45873a4672c59769dbc67b164bb9bbece8cd --- .../com/android/server/am/ActivityManagerService.java | 1 + .../server/am/ActivityManagerShellCommand.java | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 6aa49d276391d..cd29d52ebaed4 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -16325,6 +16325,7 @@ public class ActivityManagerService extends IActivityManager.Stub // TODO(b/111541062): This method is only used for updating OOM adjustments. We need to update // the logic there and in mBatteryStatsService to make them aware of multiple resumed activities + @Nullable ProcessRecord getTopApp() { final WindowProcessController wpc = mAtmInternal != null ? mAtmInternal.getTopApp() : null; final ProcessRecord r = wpc != null ? (ProcessRecord) wpc.mOwner : null; diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java index aa9d4ccc61abb..72d6ca9fd7619 100644 --- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java +++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java @@ -3531,9 +3531,14 @@ final class ActivityManagerShellCommand extends ShellCommand { if (foregroundActivities) { try { int prcState = mIam.getUidProcessState(uid, "android"); - int topPid = mInternal.getTopApp().getPid(); - if (prcState == ProcessStateEnum.TOP && topPid == pid) { - mPw.println("New foreground process: " + pid); + ProcessRecord topApp = mInternal.getTopApp(); + if (topApp == null) { + mPw.println("No top app found"); + } else { + int topPid = topApp.getPid(); + if (prcState == ProcessStateEnum.TOP && topPid == pid) { + mPw.println("New foreground process: " + pid); + } } mPw.flush(); } catch (RemoteException e) {