Only current user can update GrammaticalInflection for MUMD

Android auto supports MUMD user model where background users have
UI access on assigned displays, a.k.a. visible background users.
In Automotive's multi-display where passengers
(modeled as visible background users) can interact with the display
in front of them concurrently with the driver (modeled as the the
current user) who interacts with driver's display.

The change for this CL will apply on the devices who enables
config_multiuserVisibleBackgroundUsers. Currently only android auto has this
enabled.

Android auto also doesn't support profiles so the change is only for current
user. After this change, on Andorid Auto's MUMD config, background visible users
can't change the GrammaticalInflection, only current user can change the
GrammaticalInflection. The change has no effect on other form factors.

Flag: EXEMPT minor fix

Bug: 352784696
Test: atest CtsGrammaticalInflectionTestCases
Change-Id: I6735e7b309971866aca406b1e2035a225df35bdf
diff --git a/services/core/java/com/android/server/grammaticalinflection/GrammaticalInflectionService.java b/services/core/java/com/android/server/grammaticalinflection/GrammaticalInflectionService.java
index e242164..e0aa9bf 100644
--- a/services/core/java/com/android/server/grammaticalinflection/GrammaticalInflectionService.java
+++ b/services/core/java/com/android/server/grammaticalinflection/GrammaticalInflectionService.java
@@ -21,11 +21,13 @@
 import static com.android.server.grammaticalinflection.GrammaticalInflectionUtils.checkSystemGrammaticalGenderPermission;
 
 import android.annotation.Nullable;
+import android.app.ActivityManager;
 import android.app.ActivityTaskManager;
 import android.app.GrammaticalInflectionManager;
 import android.app.IGrammaticalInflectionManager;
 import android.content.AttributionSource;
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.content.pm.PackageManagerInternal;
 import android.content.res.Configuration;
 import android.os.Binder;
@@ -36,6 +38,7 @@
 import android.os.ShellCallback;
 import android.os.SystemProperties;
 import android.os.Trace;
+import android.os.UserManager;
 import android.permission.PermissionManager;
 import android.util.AtomicFile;
 import android.util.Log;
@@ -271,6 +274,31 @@
                 throw new IllegalArgumentException("Unknown grammatical gender");
             }
 
+            // TODO(b/356895553): Don't allow profiles and background user to change system
+            //  grammaticalinflection
+            if (UserManager.isVisibleBackgroundUsersEnabled()
+                    && mContext.getPackageManager().hasSystemFeature(
+                    PackageManager.FEATURE_AUTOMOTIVE)) {
+                // The check is added only for automotive devices. On automotive devices, it is
+                // possible that multiple users are visible simultaneously using visible background
+                // users. In such cases, it is desired that only the current user (not the visible
+                // background user) can change the GrammaticalInflection of the device.
+                final long origId = Binder.clearCallingIdentity();
+                try {
+                    int currentUser = ActivityManager.getCurrentUser();
+                    if (userId != currentUser) {
+                        Log.w(TAG,
+                                "Only current user is allowed to update GrammaticalInflection if "
+                                        + "visible background users are enabled. Current User"
+                                        + currentUser + ". Calling User: " + userId);
+                        throw new SecurityException("Only current user is allowed to update "
+                                + "GrammaticalInflection.");
+                    }
+                } finally {
+                    Binder.restoreCallingIdentity(origId);
+                }
+            }
+
             final File file = getGrammaticalGenderFile(userId);
             synchronized (mLock) {
                 final AtomicFile atomicFile = new AtomicFile(file);