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: Ia29e6d9aa1994b6ff56d7678592115b63724aa26
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:50:42 +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 packageName The package name of the process.
* @param uid The UID of the process. * @param uid The UID of the process.
* @param foregroundId The current foreground service notification ID, a negative value * @param foregroundId The current foreground service notification ID.
* means this notification is being removed. * @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.GuardedBy;
import com.android.internal.annotations.VisibleForTesting; 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.AppFGSPolicy;
import com.android.server.am.AppFGSTracker.PackageDurations; import com.android.server.am.AppFGSTracker.PackageDurations;
import com.android.server.am.AppRestrictionController.TrackerType; import com.android.server.am.AppRestrictionController.TrackerType;
@@ -112,9 +113,14 @@ final class AppFGSTracker extends BaseAppStateDurationsTracker<AppFGSPolicy, Pac
@Override @Override
public void onForegroundServiceNotificationUpdated(String packageName, int uid, public void onForegroundServiceNotificationUpdated(String packageName, int uid,
int foregroundId) { int foregroundId, boolean canceling) {
mHandler.obtainMessage(MyHandler.MSG_FOREGROUND_SERVICES_NOTIFICATION_UPDATED, final SomeArgs args = SomeArgs.obtain();
uid, foregroundId, packageName).sendToTarget(); 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 { private static class MyHandler extends Handler {
@@ -149,8 +155,10 @@ final class AppFGSTracker extends BaseAppStateDurationsTracker<AppFGSPolicy, Pac
(String) msg.obj, msg.arg1, msg.arg2); (String) msg.obj, msg.arg1, msg.arg2);
break; break;
case MSG_FOREGROUND_SERVICES_NOTIFICATION_UPDATED: case MSG_FOREGROUND_SERVICES_NOTIFICATION_UPDATED:
final SomeArgs args = (SomeArgs) msg.obj;
mTracker.handleForegroundServiceNotificationUpdated( mTracker.handleForegroundServiceNotificationUpdated(
(String) msg.obj, msg.arg1, msg.arg2); (String) args.arg1, args.argi1, args.argi2, (Boolean) args.arg2);
args.recycle();
break; break;
case MSG_CHECK_LONG_RUNNING_FGS: case MSG_CHECK_LONG_RUNNING_FGS:
mTracker.checkLongRunningFgs(); mTracker.checkLongRunningFgs();
@@ -241,18 +249,18 @@ final class AppFGSTracker extends BaseAppStateDurationsTracker<AppFGSPolicy, Pac
} }
private void handleForegroundServiceNotificationUpdated(String packageName, int uid, private void handleForegroundServiceNotificationUpdated(String packageName, int uid,
int notificationId) { int notificationId, boolean canceling) {
synchronized (mLock) { synchronized (mLock) {
SparseBooleanArray notificationIDs = mFGSNotificationIDs.get(uid, packageName); SparseBooleanArray notificationIDs = mFGSNotificationIDs.get(uid, packageName);
if (notificationId > 0) { if (!canceling) {
if (notificationIDs == null) { if (notificationIDs == null) {
notificationIDs = new SparseBooleanArray(); notificationIDs = new SparseBooleanArray();
mFGSNotificationIDs.put(uid, packageName, notificationIDs); mFGSNotificationIDs.put(uid, packageName, notificationIDs);
} }
notificationIDs.put(notificationId, false); notificationIDs.put(notificationId, false);
} else if (notificationId < 0) { } else {
if (notificationIDs != null) { if (notificationIDs != null) {
final int indexOfKey = notificationIDs.indexOfKey(-notificationId); final int indexOfKey = notificationIDs.indexOfKey(notificationId);
if (indexOfKey >= 0) { if (indexOfKey >= 0) {
final boolean wasVisible = notificationIDs.valueAt(indexOfKey); final boolean wasVisible = notificationIDs.valueAt(indexOfKey);
notificationIDs.removeAt(indexOfKey); notificationIDs.removeAt(indexOfKey);

View File

@@ -1975,6 +1975,9 @@ public final class AppRestrictionController {
} }
try { try {
final PackageInfo pkg = pm.getPackageInfo(packageName, 0 /* flags */); 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; final int targetSdk = pkg.applicationInfo.targetSdkVersion;
if (targetSdk < Build.VERSION_CODES.S) { if (targetSdk < Build.VERSION_CODES.S) {
return FrameworkStatsLog.APP_BACKGROUND_RESTRICTIONS_INFO__TARGET_SDK__SDK_PRE_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 foregroundNoti = localForegroundNoti; // save it for amending next time
signalForegroundServiceNotification(packageName, appInfo.uid, signalForegroundServiceNotification(packageName, appInfo.uid,
localForegroundId); localForegroundId, false /* canceling */);
} catch (RuntimeException e) { } catch (RuntimeException e) {
Slog.w(TAG, "Error showing notification for service", 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) { } catch (RuntimeException e) {
Slog.w(TAG, "Error canceling notification for service", 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, private void signalForegroundServiceNotification(String packageName, int uid,
int foregroundId) { int foregroundId, boolean canceling) {
synchronized (ams) { synchronized (ams) {
for (int i = ams.mForegroundServiceStateListeners.size() - 1; i >= 0; i--) { for (int i = ams.mForegroundServiceStateListeners.size() - 1; i >= 0; i--) {
ams.mForegroundServiceStateListeners.get(i).onForegroundServiceNotificationUpdated( 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, mAppFGSTracker.onForegroundServiceStateChanged(testPkgName, testUid,
testPid, true); testPid, true);
mAppFGSTracker.onForegroundServiceNotificationUpdated( mAppFGSTracker.onForegroundServiceNotificationUpdated(
testPkgName, testUid, notificationId); testPkgName, testUid, notificationId, false);
mAppFGSTracker.mNotificationListener.onNotificationPosted(new StatusBarNotification( mAppFGSTracker.mNotificationListener.onNotificationPosted(new StatusBarNotification(
testPkgName, null, notificationId, null, testUid, testPid, testPkgName, null, notificationId, null, testUid, testPid,
new Notification(), UserHandle.of(testUser), null, mCurrentTimeMillis), null); new Notification(), UserHandle.of(testUser), null, mCurrentTimeMillis), null);
@@ -848,7 +848,7 @@ public final class BackgroundRestrictionTest {
// Pretend we have the notification dismissed. // Pretend we have the notification dismissed.
mAppFGSTracker.onForegroundServiceNotificationUpdated( mAppFGSTracker.onForegroundServiceNotificationUpdated(
testPkgName, testUid, -notificationId); testPkgName, testUid, notificationId, true);
clearInvocations(mInjector.getAppStandbyInternal()); clearInvocations(mInjector.getAppStandbyInternal());
clearInvocations(mInjector.getNotificationManager()); clearInvocations(mInjector.getNotificationManager());
clearInvocations(mBgRestrictionController); clearInvocations(mBgRestrictionController);
@@ -885,7 +885,7 @@ public final class BackgroundRestrictionTest {
// Pretend notification is back on. // Pretend notification is back on.
mAppFGSTracker.onForegroundServiceNotificationUpdated( mAppFGSTracker.onForegroundServiceNotificationUpdated(
testPkgName, testUid, notificationId); testPkgName, testUid, notificationId, false);
// Now we'll prompt the user even it has a FGS with notification. // Now we'll prompt the user even it has a FGS with notification.
bgPromptFgsWithNotiToBgRestricted.set(true); bgPromptFgsWithNotiToBgRestricted.set(true);
clearInvocations(mInjector.getAppStandbyInternal()); clearInvocations(mInjector.getAppStandbyInternal());
@@ -1224,7 +1224,7 @@ public final class BackgroundRestrictionTest {
mAppFGSTracker.onForegroundServiceStateChanged(testPkgName1, testUid1, mAppFGSTracker.onForegroundServiceStateChanged(testPkgName1, testUid1,
testPid1, true); testPid1, true);
mAppFGSTracker.onForegroundServiceNotificationUpdated( mAppFGSTracker.onForegroundServiceNotificationUpdated(
testPkgName1, testUid1, fgsNotificationId); testPkgName1, testUid1, fgsNotificationId, false);
mAppFGSTracker.mNotificationListener.onNotificationPosted(new StatusBarNotification( mAppFGSTracker.mNotificationListener.onNotificationPosted(new StatusBarNotification(
testPkgName1, null, fgsNotificationId, null, testUid1, testPid1, testPkgName1, null, fgsNotificationId, null, testUid1, testPid1,
new Notification(), UserHandle.of(testUser1), null, mCurrentTimeMillis), null); new Notification(), UserHandle.of(testUser1), null, mCurrentTimeMillis), null);
@@ -1235,7 +1235,7 @@ public final class BackgroundRestrictionTest {
// Pretend we have the notification dismissed. // Pretend we have the notification dismissed.
mAppFGSTracker.onForegroundServiceNotificationUpdated( mAppFGSTracker.onForegroundServiceNotificationUpdated(
testPkgName1, testUid1, -fgsNotificationId); testPkgName1, testUid1, fgsNotificationId, true);
// Verify we have the notification. // Verify we have the notification.
notificationId = checkNotificationShown( notificationId = checkNotificationShown(
@@ -1500,7 +1500,7 @@ public final class BackgroundRestrictionTest {
if (withNotification) { if (withNotification) {
final int notificationId = 1000; final int notificationId = 1000;
mAppFGSTracker.onForegroundServiceNotificationUpdated( mAppFGSTracker.onForegroundServiceNotificationUpdated(
packageName, uid, notificationId); packageName, uid, notificationId, false);
final StatusBarNotification noti = new StatusBarNotification( final StatusBarNotification noti = new StatusBarNotification(
packageName, null, notificationId, null, uid, pid, packageName, null, notificationId, null, uid, pid,
new Notification(), UserHandle.of(UserHandle.getUserId(uid)), new Notification(), UserHandle.of(UserHandle.getUserId(uid)),