Send the network logging enabled notification to the current user.

It was sending it to the system user, which wouldn't work on systems
with headless system user.

Test: atest FrameworksServicesTests:DevicePolicyManagerTest
Test: manual verification with TestDpc and CtsVerifier on phone and automotive
Test: adb shell dumpsys device_policy | grep mNetworkLoggingNotificationUserId

Bug: 205190291

Change-Id: I0c060a9d9f1c41f63f9bcebd3c1848963ff12764
This commit is contained in:
Felipe Leme
2021-11-11 17:44:50 -08:00
parent e171c540da
commit 3106ae05e2

View File

@@ -705,6 +705,12 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
@GuardedBy("getLockObject()")
private @UserIdInt int mLogoutUserId = UserHandle.USER_NULL;
/**
* User the network logging notification was sent to.
*/
// Guarded by mHandler
private @UserIdInt int mNetworkLoggingNotificationUserId = UserHandle.USER_NULL;
private static final boolean ENABLE_LOCK_GUARD = true;
/**
@@ -9575,7 +9581,15 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
private @UserIdInt int getCurrentForegroundUserId() {
try {
return mInjector.getIActivityManager().getCurrentUser().id;
UserInfo currentUser = mInjector.getIActivityManager().getCurrentUser();
if (currentUser == null) {
// TODO(b/206107460): should not happen on production, but it's happening on unit
// tests that are not properly setting the expectation (because they don't need it)
Slogf.wtf(LOG_TAG, "getCurrentForegroundUserId(): mInjector.getIActivityManager()"
+ ".getCurrentUser() returned null, please ignore when running unit tests");
return ActivityManager.getCurrentUser();
}
return currentUser.id;
} catch (RemoteException e) {
Slogf.wtf(LOG_TAG, "cannot get current user");
}
@@ -9709,10 +9723,18 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
mStateCache.dump(pw);
pw.println();
}
mHandler.post(() -> handleDump(pw));
dumpResources(pw);
}
}
// Dump state that is guarded by the handler
private void handleDump(IndentingPrintWriter pw) {
if (mNetworkLoggingNotificationUserId != UserHandle.USER_NULL) {
pw.println("mNetworkLoggingNotificationUserId: " + mNetworkLoggingNotificationUserId);
}
}
private void dumpImmutableState(IndentingPrintWriter pw) {
pw.println("Immutable state:");
pw.increaseIndent();
@@ -15183,11 +15205,10 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
}
if (active) {
if (shouldSendNotification) {
mHandler.post(() -> sendNetworkLoggingNotification());
mHandler.post(() -> handleSendNetworkLoggingNotification());
}
} else {
mHandler.post(() -> mInjector.getNotificationManager().cancel(
SystemMessage.NOTE_NETWORK_LOGGING));
mHandler.post(() -> handleCancelNetworkLoggingNotification());
}
});
}
@@ -15378,10 +15399,11 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
return true;
}
private void sendNetworkLoggingNotification() {
private void handleSendNetworkLoggingNotification() {
final PackageManagerInternal pm = mInjector.getPackageManagerInternal();
final Intent intent = new Intent(DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG);
intent.setPackage(pm.getSystemUiServiceComponent().getPackageName());
mNetworkLoggingNotificationUserId = getCurrentForegroundUserId();
// Simple notification clicks are immutable
final PendingIntent pendingIntent = PendingIntent.getBroadcastAsUser(mContext, 0, intent,
PendingIntent.FLAG_IMMUTABLE, UserHandle.CURRENT);
@@ -15396,7 +15418,26 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
.setStyle(new Notification.BigTextStyle()
.bigText(mContext.getString(R.string.network_logging_notification_text)))
.build();
mInjector.getNotificationManager().notify(SystemMessage.NOTE_NETWORK_LOGGING, notification);
Slogf.i(LOG_TAG, "Sending network logging notification to user %d",
mNetworkLoggingNotificationUserId);
mInjector.getNotificationManager().notifyAsUser(/* tag= */ null,
SystemMessage.NOTE_NETWORK_LOGGING, notification,
UserHandle.of(mNetworkLoggingNotificationUserId));
}
private void handleCancelNetworkLoggingNotification() {
if (mNetworkLoggingNotificationUserId == UserHandle.USER_NULL) {
// Happens when setNetworkLoggingActive(false) is called before called with true
Slogf.d(LOG_TAG, "Not cancelling network logging notification for USER_NULL");
return;
}
Slogf.i(LOG_TAG, "Cancelling network logging notification for user %d",
mNetworkLoggingNotificationUserId);
mInjector.getNotificationManager().cancelAsUser(/* tag= */ null,
SystemMessage.NOTE_NETWORK_LOGGING,
UserHandle.of(mNetworkLoggingNotificationUserId));
mNetworkLoggingNotificationUserId = UserHandle.USER_NULL;
}
/**