Merge "Extend evs_app configuration" into rvc-qpr-dev
diff --git a/tests/CarDeveloperOptions/src/com/android/car/developeroptions/development/AppsNotRespondingPreferenceController.java b/tests/CarDeveloperOptions/src/com/android/car/developeroptions/development/AppsNotRespondingPreferenceController.java
index f504d1b..86d89f4 100644
--- a/tests/CarDeveloperOptions/src/com/android/car/developeroptions/development/AppsNotRespondingPreferenceController.java
+++ b/tests/CarDeveloperOptions/src/com/android/car/developeroptions/development/AppsNotRespondingPreferenceController.java
@@ -16,6 +16,7 @@
 package com.android.car.developeroptions.development;
 
 import android.content.Context;
+import android.os.UserHandle;
 import android.provider.Settings;
 
 import androidx.annotation.VisibleForTesting;
@@ -47,24 +48,31 @@
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         final boolean isEnabled = (Boolean) newValue;
-        Settings.Secure.putInt(mContext.getContentResolver(),
+        // Set for system user since Android framework will read it as that user.
+        // Note that this will enable/disable this option for all users.
+        Settings.Secure.putIntForUser(mContext.getContentResolver(),
                 Settings.Secure.ANR_SHOW_BACKGROUND,
-                isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
+                isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF,
+                UserHandle.SYSTEM.getIdentifier());
         return true;
     }
 
     @Override
     public void updateState(Preference preference) {
-        final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
-                Settings.Secure.ANR_SHOW_BACKGROUND, SETTING_VALUE_OFF);
+        final int mode = Settings.Secure.getIntForUser(mContext.getContentResolver(),
+                Settings.Secure.ANR_SHOW_BACKGROUND,
+                SETTING_VALUE_OFF, UserHandle.SYSTEM.getIdentifier());
         ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
     }
 
     @Override
     protected void onDeveloperOptionsSwitchDisabled() {
         super.onDeveloperOptionsSwitchDisabled();
-        Settings.Secure.putInt(mContext.getContentResolver(),
-                Settings.Secure.ANR_SHOW_BACKGROUND, SETTING_VALUE_OFF);
+        // Set for system user since Android framework will read it as that user.
+        // Note that this will enable/disable this option for all users.
+        Settings.Secure.putIntForUser(mContext.getContentResolver(),
+                Settings.Secure.ANR_SHOW_BACKGROUND,
+                SETTING_VALUE_OFF, UserHandle.SYSTEM.getIdentifier());
         ((SwitchPreference) mPreference).setChecked(false);
     }
 }
diff --git a/watchdog/server/src/WatchdogBinderMediator.cpp b/watchdog/server/src/WatchdogBinderMediator.cpp
index 1dc6873..fd0dd8e 100644
--- a/watchdog/server/src/WatchdogBinderMediator.cpp
+++ b/watchdog/server/src/WatchdogBinderMediator.cpp
@@ -54,7 +54,7 @@
         "%s or %s: Displays this help text.\n"
         "When no options are specified, carwatchdog report is generated.\n";
 
-Status checkSystemPermission() {
+Status checkSystemUser() {
     if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
         return Status::fromExceptionCode(Status::EX_SECURITY,
                                          "Calling process does not have proper privilege");
@@ -151,7 +151,7 @@
 }
 
 Status WatchdogBinderMediator::registerMediator(const sp<ICarWatchdogClient>& mediator) {
-    Status status = checkSystemPermission();
+    Status status = checkSystemUser();
     if (!status.isOk()) {
         return status;
     }
@@ -159,29 +159,47 @@
 }
 
 Status WatchdogBinderMediator::unregisterMediator(const sp<ICarWatchdogClient>& mediator) {
-    Status status = checkSystemPermission();
+    Status status = checkSystemUser();
     if (!status.isOk()) {
         return status;
     }
     return mWatchdogProcessService->unregisterMediator(mediator);
 }
 Status WatchdogBinderMediator::registerMonitor(const sp<ICarWatchdogMonitor>& monitor) {
-    Status status = checkSystemPermission();
+    Status status = checkSystemUser();
     if (!status.isOk()) {
         return status;
     }
     return mWatchdogProcessService->registerMonitor(monitor);
 }
 Status WatchdogBinderMediator::unregisterMonitor(const sp<ICarWatchdogMonitor>& monitor) {
-    Status status = checkSystemPermission();
+    Status status = checkSystemUser();
     if (!status.isOk()) {
         return status;
     }
     return mWatchdogProcessService->unregisterMonitor(monitor);
 }
 
+Status WatchdogBinderMediator::tellMediatorAlive(const sp<ICarWatchdogClient>& mediator,
+                                                 const std::vector<int32_t>& clientsNotResponding,
+                                                 int32_t sessionId) {
+    Status status = checkSystemUser();
+    if (!status.isOk()) {
+        return status;
+    }
+    return mWatchdogProcessService->tellMediatorAlive(mediator, clientsNotResponding, sessionId);
+}
+Status WatchdogBinderMediator::tellDumpFinished(const android::sp<ICarWatchdogMonitor>& monitor,
+                                                int32_t pid) {
+    Status status = checkSystemUser();
+    if (!status.isOk()) {
+        return status;
+    }
+    return mWatchdogProcessService->tellDumpFinished(monitor, pid);
+}
+
 Status WatchdogBinderMediator::notifySystemStateChange(StateType type, int32_t arg1, int32_t arg2) {
-    Status status = checkSystemPermission();
+    Status status = checkSystemUser();
     if (!status.isOk()) {
         return status;
     }
diff --git a/watchdog/server/src/WatchdogBinderMediator.h b/watchdog/server/src/WatchdogBinderMediator.h
index 530659b..dbc26a3 100644
--- a/watchdog/server/src/WatchdogBinderMediator.h
+++ b/watchdog/server/src/WatchdogBinderMediator.h
@@ -61,14 +61,9 @@
     }
     binder::Status tellMediatorAlive(const sp<ICarWatchdogClient>& mediator,
                                      const std::vector<int32_t>& clientsNotResponding,
-                                     int32_t sessionId) override {
-        return mWatchdogProcessService->tellMediatorAlive(mediator, clientsNotResponding,
-                                                          sessionId);
-    }
+                                     int32_t sessionId) override;
     binder::Status tellDumpFinished(const android::sp<ICarWatchdogMonitor>& monitor,
-                                    int32_t pid) override {
-        return mWatchdogProcessService->tellDumpFinished(monitor, pid);
-    }
+                                    int32_t pid) override;
     binder::Status notifySystemStateChange(StateType type, int32_t arg1, int32_t arg2) override;
 
 protected:
diff --git a/watchdog/server/tests/WatchdogBinderMediatorTest.cpp b/watchdog/server/tests/WatchdogBinderMediatorTest.cpp
index a32dcca..7e44893 100644
--- a/watchdog/server/tests/WatchdogBinderMediatorTest.cpp
+++ b/watchdog/server/tests/WatchdogBinderMediatorTest.cpp
@@ -270,7 +270,16 @@
     ASSERT_TRUE(status.isOk()) << status;
 }
 
+TEST_F(WatchdogBinderMediatorTest, TestErrorOnTellMediatorAliveWithNonSystemCallingUid) {
+    sp<ICarWatchdogClient> mediator = new MockICarWatchdogClient();
+    std::vector clientsNotResponding = {123};
+    EXPECT_CALL(*mMockWatchdogProcessService, tellMediatorAlive(_, _, _)).Times(0);
+    Status status = mWatchdogBinderMediator->tellMediatorAlive(mediator, clientsNotResponding, 456);
+    ASSERT_FALSE(status.isOk()) << status;
+}
+
 TEST_F(WatchdogBinderMediatorTest, TestTellMediatorAlive) {
+    setSystemCallingUid();
     sp<ICarWatchdogClient> mediator = new MockICarWatchdogClient();
     std::vector clientsNotResponding = {123};
     EXPECT_CALL(*mMockWatchdogProcessService,
@@ -280,7 +289,15 @@
     ASSERT_TRUE(status.isOk()) << status;
 }
 
+TEST_F(WatchdogBinderMediatorTest, TestErrorOnTellDumpFinishedWithNonSystemCallingUid) {
+    sp<ICarWatchdogMonitor> monitor = new MockICarWatchdogMonitor();
+    EXPECT_CALL(*mMockWatchdogProcessService, tellDumpFinished(_, _)).Times(0);
+    Status status = mWatchdogBinderMediator->tellDumpFinished(monitor, 456);
+    ASSERT_FALSE(status.isOk()) << status;
+}
+
 TEST_F(WatchdogBinderMediatorTest, TestTellDumpFinished) {
+    setSystemCallingUid();
     sp<ICarWatchdogMonitor> monitor = new MockICarWatchdogMonitor();
     EXPECT_CALL(*mMockWatchdogProcessService, tellDumpFinished(monitor, 456))
             .WillOnce(Return(Status::ok()));