diff --git a/core/java/android/app/ActivityManagerInternal.java b/core/java/android/app/ActivityManagerInternal.java index faf64c2ec22b0..9c391abe62043 100644 --- a/core/java/android/app/ActivityManagerInternal.java +++ b/core/java/android/app/ActivityManagerInternal.java @@ -792,10 +792,11 @@ public abstract class ActivityManagerInternal { * * @param packageName The package name of the process. * @param uid The UID of the process. - * @param foregroundId The current foreground service notification ID, a negative value - * means this notification is being removed. + * @param foregroundId The current foreground service notification ID. + * @param canceling The given notification is being canceled. */ - void onForegroundServiceNotificationUpdated(String packageName, int uid, int foregroundId); + void onForegroundServiceNotificationUpdated(String packageName, int uid, int foregroundId, + boolean canceling); } /** diff --git a/services/core/java/com/android/server/am/AppFGSTracker.java b/services/core/java/com/android/server/am/AppFGSTracker.java index f8378c3a8a4e7..ddd27647c9701 100644 --- a/services/core/java/com/android/server/am/AppFGSTracker.java +++ b/services/core/java/com/android/server/am/AppFGSTracker.java @@ -52,6 +52,7 @@ import android.util.TimeUtils; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.os.SomeArgs; import com.android.server.am.AppFGSTracker.AppFGSPolicy; import com.android.server.am.AppFGSTracker.PackageDurations; import com.android.server.am.AppRestrictionController.TrackerType; @@ -112,9 +113,14 @@ final class AppFGSTracker extends BaseAppStateDurationsTracker 0) { + if (!canceling) { if (notificationIDs == null) { notificationIDs = new SparseBooleanArray(); mFGSNotificationIDs.put(uid, packageName, notificationIDs); } notificationIDs.put(notificationId, false); - } else if (notificationId < 0) { + } else { if (notificationIDs != null) { - final int indexOfKey = notificationIDs.indexOfKey(-notificationId); + final int indexOfKey = notificationIDs.indexOfKey(notificationId); if (indexOfKey >= 0) { final boolean wasVisible = notificationIDs.valueAt(indexOfKey); notificationIDs.removeAt(indexOfKey); diff --git a/services/core/java/com/android/server/am/AppRestrictionController.java b/services/core/java/com/android/server/am/AppRestrictionController.java index 708bd1662a77d..6f7435943d45f 100644 --- a/services/core/java/com/android/server/am/AppRestrictionController.java +++ b/services/core/java/com/android/server/am/AppRestrictionController.java @@ -1975,6 +1975,9 @@ public final class AppRestrictionController { } try { final PackageInfo pkg = pm.getPackageInfo(packageName, 0 /* flags */); + if (pkg == null || pkg.applicationInfo == null) { + return FrameworkStatsLog.APP_BACKGROUND_RESTRICTIONS_INFO__TARGET_SDK__SDK_UNKNOWN; + } final int targetSdk = pkg.applicationInfo.targetSdkVersion; if (targetSdk < Build.VERSION_CODES.S) { return FrameworkStatsLog.APP_BACKGROUND_RESTRICTIONS_INFO__TARGET_SDK__SDK_PRE_S; diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java index 639f56cb12ddd..5a55b8b33cad5 100644 --- a/services/core/java/com/android/server/am/ServiceRecord.java +++ b/services/core/java/com/android/server/am/ServiceRecord.java @@ -1112,7 +1112,7 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN foregroundNoti = localForegroundNoti; // save it for amending next time signalForegroundServiceNotification(packageName, appInfo.uid, - localForegroundId); + localForegroundId, false /* canceling */); } catch (RuntimeException e) { Slog.w(TAG, "Error showing notification for service", e); @@ -1147,17 +1147,18 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN } catch (RuntimeException e) { Slog.w(TAG, "Error canceling notification for service", e); } - signalForegroundServiceNotification(packageName, appInfo.uid, -localForegroundId); + signalForegroundServiceNotification(packageName, appInfo.uid, localForegroundId, + true /* canceling */); } }); } private void signalForegroundServiceNotification(String packageName, int uid, - int foregroundId) { + int foregroundId, boolean canceling) { synchronized (ams) { for (int i = ams.mForegroundServiceStateListeners.size() - 1; i >= 0; i--) { ams.mForegroundServiceStateListeners.get(i).onForegroundServiceNotificationUpdated( - packageName, appInfo.uid, foregroundId); + packageName, appInfo.uid, foregroundId, canceling); } } } diff --git a/services/tests/mockingservicestests/src/com/android/server/am/BackgroundRestrictionTest.java b/services/tests/mockingservicestests/src/com/android/server/am/BackgroundRestrictionTest.java index 77cf54363c629..c0b4f0fe58121 100644 --- a/services/tests/mockingservicestests/src/com/android/server/am/BackgroundRestrictionTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/am/BackgroundRestrictionTest.java @@ -671,7 +671,7 @@ public final class BackgroundRestrictionTest { mAppFGSTracker.onForegroundServiceStateChanged(testPkgName, testUid, testPid, true); mAppFGSTracker.onForegroundServiceNotificationUpdated( - testPkgName, testUid, notificationId); + testPkgName, testUid, notificationId, false); mAppFGSTracker.mNotificationListener.onNotificationPosted(new StatusBarNotification( testPkgName, null, notificationId, null, testUid, testPid, new Notification(), UserHandle.of(testUser), null, mCurrentTimeMillis), null); @@ -848,7 +848,7 @@ public final class BackgroundRestrictionTest { // Pretend we have the notification dismissed. mAppFGSTracker.onForegroundServiceNotificationUpdated( - testPkgName, testUid, -notificationId); + testPkgName, testUid, notificationId, true); clearInvocations(mInjector.getAppStandbyInternal()); clearInvocations(mInjector.getNotificationManager()); clearInvocations(mBgRestrictionController); @@ -885,7 +885,7 @@ public final class BackgroundRestrictionTest { // Pretend notification is back on. mAppFGSTracker.onForegroundServiceNotificationUpdated( - testPkgName, testUid, notificationId); + testPkgName, testUid, notificationId, false); // Now we'll prompt the user even it has a FGS with notification. bgPromptFgsWithNotiToBgRestricted.set(true); clearInvocations(mInjector.getAppStandbyInternal()); @@ -1224,7 +1224,7 @@ public final class BackgroundRestrictionTest { mAppFGSTracker.onForegroundServiceStateChanged(testPkgName1, testUid1, testPid1, true); mAppFGSTracker.onForegroundServiceNotificationUpdated( - testPkgName1, testUid1, fgsNotificationId); + testPkgName1, testUid1, fgsNotificationId, false); mAppFGSTracker.mNotificationListener.onNotificationPosted(new StatusBarNotification( testPkgName1, null, fgsNotificationId, null, testUid1, testPid1, new Notification(), UserHandle.of(testUser1), null, mCurrentTimeMillis), null); @@ -1235,7 +1235,7 @@ public final class BackgroundRestrictionTest { // Pretend we have the notification dismissed. mAppFGSTracker.onForegroundServiceNotificationUpdated( - testPkgName1, testUid1, -fgsNotificationId); + testPkgName1, testUid1, fgsNotificationId, true); // Verify we have the notification. notificationId = checkNotificationShown( @@ -1500,7 +1500,7 @@ public final class BackgroundRestrictionTest { if (withNotification) { final int notificationId = 1000; mAppFGSTracker.onForegroundServiceNotificationUpdated( - packageName, uid, notificationId); + packageName, uid, notificationId, false); final StatusBarNotification noti = new StatusBarNotification( packageName, null, notificationId, null, uid, pid, new Notification(), UserHandle.of(UserHandle.getUserId(uid)),