Improved AbstractMasterSystemService to handle supported user types.

Also changed AutofillManagerService and ContentCaptureManagerService to take advantage of these methods.

Test: manual verification
Test: atest CtsAutoFillServiceTestCases CtsContentCaptureServiceTestCases # on phone and Automotive

Bug: 133242016

Change-Id: I3e7f9d65a6ef1e8e6ec886a41b35733e463a6389
diff --git a/services/core/java/com/android/server/infra/AbstractMasterSystemService.java b/services/core/java/com/android/server/infra/AbstractMasterSystemService.java
index 9782f30..a9c4d08 100644
--- a/services/core/java/com/android/server/infra/AbstractMasterSystemService.java
+++ b/services/core/java/com/android/server/infra/AbstractMasterSystemService.java
@@ -48,6 +48,7 @@
 import java.io.PrintWriter;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -167,6 +168,12 @@
     private SparseArray<String> mUpdatingPackageNames;
 
     /**
+     * Lazy-loadable reference to {@link UserManagerInternal}.
+     */
+    @Nullable
+    private UserManagerInternal mUm;
+
+    /**
      * Default constructor.
      *
      * <p>When using this constructor, the {@link AbstractPerUserSystemService} is removed from
@@ -222,9 +229,8 @@
         } else {
             mDisabledByUserRestriction = new SparseBooleanArray();
             // Hookup with UserManager to disable service when necessary.
-            final UserManager um = context.getSystemService(UserManager.class);
-            final UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class);
-            final List<UserInfo> users = um.getUsers();
+            final UserManagerInternal umi = getUserManagerInternal();
+            final List<UserInfo> users = getSupportedUsers();
             for (int i = 0; i < users.size(); i++) {
                 final int userId = users.get(i).id;
                 final boolean disabled = umi.getUserRestriction(userId, disallowProperty);
@@ -649,6 +655,36 @@
     }
 
     /**
+     * Gets a cached reference to {@link UserManagerInternal}.
+     */
+    @NonNull
+    protected UserManagerInternal getUserManagerInternal() {
+        if (mUm == null) {
+            if (verbose) Slog.v(mTag, "lazy-loading UserManagerInternal");
+            mUm = LocalServices.getService(UserManagerInternal.class);
+        }
+        return mUm;
+    }
+
+    /**
+     * Gets a list of all supported users (i.e., those that pass the {@link #isSupported(UserInfo)}
+     * check).
+     */
+    @NonNull
+    protected List<UserInfo> getSupportedUsers() {
+        final UserInfo[] allUsers = getUserManagerInternal().getUserInfos();
+        final int size = allUsers.length;
+        final List<UserInfo> supportedUsers = new ArrayList<>(size);
+        for (int i = 0; i < size; i++) {
+            final UserInfo userInfo = allUsers[i];
+            if (isSupported(userInfo)) {
+                supportedUsers.add(userInfo);
+            }
+        }
+        return supportedUsers;
+    }
+
+    /**
      * Asserts that the given package name is owned by the UID making this call.
      *
      * @throws SecurityException when it's not...
@@ -684,8 +720,7 @@
             if (mServiceNameResolver != null) {
                 pw.print(prefix); pw.print("Name resolver: ");
                 mServiceNameResolver.dumpShort(pw); pw.println();
-                final UserManager um = getContext().getSystemService(UserManager.class);
-                final List<UserInfo> users = um.getUsers();
+                final List<UserInfo> users = getSupportedUsers();
                 for (int i = 0; i < users.size(); i++) {
                     final int userId = users.get(i).id;
                     pw.print(prefix2); pw.print(userId); pw.print(": ");
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index 1fe5512..65fb35d 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -4180,6 +4180,17 @@
             }
             return userData == null ? null : userData.info;
         }
+
+        public @NonNull UserInfo[] getUserInfos() {
+            synchronized (mUsersLock) {
+                int userSize = mUsers.size();
+                UserInfo[] allInfos = new UserInfo[userSize];
+                for (int i = 0; i < userSize; i++) {
+                    allInfos[i] = mUsers.valueAt(i).info;
+                }
+                return allInfos;
+            }
+        }
     }
 
     /* Remove all the users except of the system one. */