Merge "Add a parameter to the internal FGS notification listener" into tm-dev am: e1eb2bf9a3

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17577223

Change-Id: I19d582f586c88163f922e124b560ef4033942374
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Jing Ji
2022-04-05 11:51:44 +00:00
committed by Automerger Merge Worker
5 changed files with 34 additions and 21 deletions

View File

@@ -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);
}
/**

View File

@@ -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<AppFGSPolicy, Pac
@Override
public void onForegroundServiceNotificationUpdated(String packageName, int uid,
int foregroundId) {
mHandler.obtainMessage(MyHandler.MSG_FOREGROUND_SERVICES_NOTIFICATION_UPDATED,
uid, foregroundId, packageName).sendToTarget();
int foregroundId, boolean canceling) {
final SomeArgs args = SomeArgs.obtain();
args.argi1 = uid;
args.argi2 = foregroundId;
args.arg1 = packageName;
args.arg2 = canceling ? Boolean.TRUE : Boolean.FALSE;
mHandler.obtainMessage(MyHandler.MSG_FOREGROUND_SERVICES_NOTIFICATION_UPDATED, args)
.sendToTarget();
}
private static class MyHandler extends Handler {
@@ -149,8 +155,10 @@ final class AppFGSTracker extends BaseAppStateDurationsTracker<AppFGSPolicy, Pac
(String) msg.obj, msg.arg1, msg.arg2);
break;
case MSG_FOREGROUND_SERVICES_NOTIFICATION_UPDATED:
final SomeArgs args = (SomeArgs) msg.obj;
mTracker.handleForegroundServiceNotificationUpdated(
(String) msg.obj, msg.arg1, msg.arg2);
(String) args.arg1, args.argi1, args.argi2, (Boolean) args.arg2);
args.recycle();
break;
case MSG_CHECK_LONG_RUNNING_FGS:
mTracker.checkLongRunningFgs();
@@ -241,18 +249,18 @@ final class AppFGSTracker extends BaseAppStateDurationsTracker<AppFGSPolicy, Pac
}
private void handleForegroundServiceNotificationUpdated(String packageName, int uid,
int notificationId) {
int notificationId, boolean canceling) {
synchronized (mLock) {
SparseBooleanArray notificationIDs = mFGSNotificationIDs.get(uid, packageName);
if (notificationId > 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);

View File

@@ -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;

View File

@@ -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);
}
}
}

View File

@@ -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)),