Notify all affected UIDs for user/audio restriction changes

Test: pass: adb shell am instrument -w com.android.frameworks.servicestests
                /android.support.test.runner.AndroidJUnitRunner
      pass: cts-tradefed run cts-dev -m CtsPermissionTestCases
                -t android.permission.cts.AppOpsTest

Bug: 75274546

Change-Id: I788823fcc3ade8a4cf752d64bd11b87b212e4d31
diff --git a/services/core/java/com/android/server/AppOpsService.java b/services/core/java/com/android/server/AppOpsService.java
index 9756d17..d46734c 100644
--- a/services/core/java/com/android/server/AppOpsService.java
+++ b/services/core/java/com/android/server/AppOpsService.java
@@ -97,6 +97,9 @@
     // Write at most every 30 minutes.
     static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
 
+    // Constant meaning that any UID should be matched when dispatching callbacks
+    private static final int UID_ANY = -2;
+
     Context mContext;
     final AtomicFile mFile;
     final Handler mHandler;
@@ -766,7 +769,7 @@
 
     private void notifyOpChanged(ModeCallback callback, int code,
             int uid, String packageName) {
-        if (callback.mUid >= 0 && callback.mUid != uid) {
+        if (uid != UID_ANY && callback.mUid >= 0 && callback.mUid != uid) {
             return;
         }
         // There are components watching for mode changes such as window manager
@@ -1114,7 +1117,7 @@
         }
 
         mHandler.sendMessage(PooledLambda.obtainMessage(
-                AppOpsService::notifyWatchersOfChange, this, code));
+                AppOpsService::notifyWatchersOfChange, this, code, UID_ANY));
     }
 
     @Override
@@ -2682,7 +2685,7 @@
 
             if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) {
                 mHandler.sendMessage(PooledLambda.obtainMessage(
-                        AppOpsService::notifyWatchersOfChange, this, code));
+                        AppOpsService::notifyWatchersOfChange, this, code, UID_ANY));
             }
 
             if (restrictionState.isDefault()) {
@@ -2692,7 +2695,7 @@
         }
     }
 
-    private void notifyWatchersOfChange(int code) {
+    private void notifyWatchersOfChange(int code, int uid) {
         final ArraySet<ModeCallback> clonedCallbacks;
         synchronized (this) {
             ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code);
@@ -2702,7 +2705,7 @@
             clonedCallbacks = new ArraySet<>(callbacks);
         }
 
-        notifyOpChanged(clonedCallbacks,  code, -1, null);
+        notifyOpChanged(clonedCallbacks,  code, uid, null);
     }
 
     @Override
@@ -2937,7 +2940,7 @@
                     for (int j = 0; j < restrictionCount; j++) {
                         if (restrictions[j]) {
                             final int changedCode = j;
-                            mHandler.post(() -> notifyWatchersOfChange(changedCode));
+                            mHandler.post(() -> notifyWatchersOfChange(changedCode, UID_ANY));
                         }
                     }
                 }