Merge "[Ravenwood] Always provide main thread" into main
diff --git a/core/java/Android.bp b/core/java/Android.bp
index ce9dedc..7e8a309 100644
--- a/core/java/Android.bp
+++ b/core/java/Android.bp
@@ -19,6 +19,7 @@
     srcs: [
         "**/*.java",
         "**/*.aidl",
+        ":systemfeatures-gen-srcs",
         ":framework-nfc-non-updatable-sources",
         ":ranging_stack_mock_initializer",
     ],
@@ -637,3 +638,29 @@
 }
 
 // protolog end
+
+// Whether to enable read-only system feature codegen.
+gen_readonly_feature_apis = select(release_flag("RELEASE_USE_SYSTEM_FEATURE_BUILD_FLAGS"), {
+    true: "true",
+    false: "false",
+    default: "false",
+})
+
+// Generates com.android.internal.pm.RoSystemFeatures, optionally compiling in
+// details about fixed system features defined by build flags. When disabled,
+// the APIs are simply passthrough stubs with no meaningful side effects.
+genrule {
+    name: "systemfeatures-gen-srcs",
+    cmd: "$(location systemfeatures-gen-tool) com.android.internal.pm.RoSystemFeatures " +
+        // --readonly=false (default) makes the codegen an effective no-op passthrough API.
+        " --readonly=" + gen_readonly_feature_apis +
+        // For now, only export "android.hardware.type.*" system features APIs.
+        // TODO(b/203143243): Use an intermediate soong var that aggregates all declared
+        // RELEASE_SYSTEM_FEATURE_* declarations into a single arg.
+        " --feature-apis=AUTOMOTIVE,WATCH,TELEVISION,EMBEDDED,PC" +
+        " > $(out)",
+    out: [
+        "RoSystemFeatures.java",
+    ],
+    tools: ["systemfeatures-gen-tool"],
+}
diff --git a/core/java/android/app/OWNERS b/core/java/android/app/OWNERS
index d363e19..ba71afb 100644
--- a/core/java/android/app/OWNERS
+++ b/core/java/android/app/OWNERS
@@ -97,6 +97,7 @@
 
 # Performance
 per-file PropertyInvalidatedCache.java = file:/PERFORMANCE_OWNERS
+per-file performance.aconfig = file:/PERFORMANCE_OWNERS
 
 # Pinner
 per-file pinner-client.aconfig = file:/core/java/android/app/pinner/OWNERS
diff --git a/core/java/android/util/NtpTrustedTime.java b/core/java/android/util/NtpTrustedTime.java
index 9f54d9f..3adbd68 100644
--- a/core/java/android/util/NtpTrustedTime.java
+++ b/core/java/android/util/NtpTrustedTime.java
@@ -24,7 +24,7 @@
 import android.content.res.Resources;
 import android.net.ConnectivityManager;
 import android.net.Network;
-import android.net.NetworkCapabilities;
+import android.net.NetworkInfo;
 import android.net.SntpClient;
 import android.os.Build;
 import android.os.SystemClock;
@@ -687,16 +687,8 @@
             if (connectivityManager == null) {
                 return false;
             }
-            final NetworkCapabilities networkCapabilities =
-                    connectivityManager.getNetworkCapabilities(network);
-            if (networkCapabilities == null) {
-                if (LOGD) Log.d(TAG, "getNetwork: failed to get network capabilities");
-                return false;
-            }
-            final boolean isConnectedToInternet = networkCapabilities.hasCapability(
-                    NetworkCapabilities.NET_CAPABILITY_INTERNET)
-                    && networkCapabilities.hasCapability(
-                    NetworkCapabilities.NET_CAPABILITY_VALIDATED);
+            final NetworkInfo ni = connectivityManager.getNetworkInfo(network);
+
             // This connectivity check is to avoid performing a DNS lookup for the time server on a
             // unconnected network. There are races to obtain time in Android when connectivity
             // changes, which means that forceRefresh() can be called by various components before
@@ -706,8 +698,8 @@
             // A side effect of check is that tests that run a fake NTP server on the device itself
             // will only be able to use it if the active network is connected, even though loopback
             // addresses are actually reachable.
-            if (!isConnectedToInternet) {
-                if (LOGD) Log.d(TAG, "getNetwork: no internet connectivity");
+            if (ni == null || !ni.isConnected()) {
+                if (LOGD) Log.d(TAG, "getNetwork: no connectivity");
                 return false;
             }
             return true;
diff --git a/core/tests/coretests/src/android/app/OWNERS b/core/tests/coretests/src/android/app/OWNERS
index 5636f9b..6d14ccb 100644
--- a/core/tests/coretests/src/android/app/OWNERS
+++ b/core/tests/coretests/src/android/app/OWNERS
@@ -14,3 +14,5 @@
 # Files related to background activity launches
 per-file Background*Start* = file:/BAL_OWNERS
 
+# Files related to caching
+per-file PropertyInvalidatedCache* = file:/PERFORMANCE_OWNERS
diff --git a/core/tests/coretests/src/android/os/OWNERS b/core/tests/coretests/src/android/os/OWNERS
index 1c00734..6149382 100644
--- a/core/tests/coretests/src/android/os/OWNERS
+++ b/core/tests/coretests/src/android/os/OWNERS
@@ -9,3 +9,6 @@
 
 # PerformanceHintManager
 per-file PerformanceHintManagerTest.java = file:/ADPF_OWNERS
+
+# Caching
+per-file IpcDataCache* = file:/PERFORMANCE_OWNERS
diff --git a/errorprone/Android.bp b/errorprone/Android.bp
index c1d2235..b559a15 100644
--- a/errorprone/Android.bp
+++ b/errorprone/Android.bp
@@ -22,6 +22,7 @@
 
     static_libs: [
         "annotations",
+        "jsr305",
         "framework-annotations-lib",
         "//external/error_prone:error_prone_core",
     ],
diff --git a/errorprone/OWNERS b/errorprone/OWNERS
index bddbdb3..aa8c126 100644
--- a/errorprone/OWNERS
+++ b/errorprone/OWNERS
@@ -1,2 +1 @@
-jsharkey@android.com
-jsharkey@google.com
+colefaust@google.com
diff --git a/errorprone/java/com/google/errorprone/bugpatterns/android/HideInCommentsChecker.java b/errorprone/java/com/google/errorprone/bugpatterns/android/HideInCommentsChecker.java
index 8dc9579..6d5e4484 100644
--- a/errorprone/java/com/google/errorprone/bugpatterns/android/HideInCommentsChecker.java
+++ b/errorprone/java/com/google/errorprone/bugpatterns/android/HideInCommentsChecker.java
@@ -29,6 +29,7 @@
 import com.google.errorprone.fixes.SuggestedFix;
 import com.google.errorprone.matchers.Description;
 import com.google.errorprone.util.ASTHelpers;
+import com.google.errorprone.util.ErrorProneComment;
 import com.google.errorprone.util.ErrorProneToken;
 import com.google.errorprone.util.ErrorProneTokens;
 import com.sun.source.tree.ClassTree;
@@ -37,7 +38,6 @@
 import com.sun.source.tree.NewClassTree;
 import com.sun.source.tree.Tree;
 import com.sun.source.tree.VariableTree;
-import com.sun.tools.javac.parser.Tokens;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -66,7 +66,7 @@
         final Map<Integer, Tree> javadocableTrees = findJavadocableTrees(tree, state);
         final String sourceCode = state.getSourceCode().toString();
         for (ErrorProneToken token : ErrorProneTokens.getTokens(sourceCode, state.context)) {
-            for (Tokens.Comment comment : token.comments()) {
+            for (ErrorProneComment comment : token.comments()) {
                 if (!javadocableTrees.containsKey(token.pos())) {
                     continue;
                 }
@@ -81,7 +81,7 @@
         return NO_MATCH;
     }
 
-    private static Optional<SuggestedFix> generateFix(Tokens.Comment comment) {
+    private static Optional<SuggestedFix> generateFix(ErrorProneComment comment) {
         final String text = comment.getText();
         if (text.startsWith("/**")) {
             return Optional.empty();
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractor.kt b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractor.kt
index 65b42e6..fcf486b 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractor.kt
@@ -23,6 +23,7 @@
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.keyevent.domain.interactor.SysUIKeyEventHandler.Companion.handleAction
 import com.android.systemui.media.controls.util.MediaSessionLegacyHelperWrapper
+import com.android.systemui.plugins.ActivityStarter.OnDismissAction
 import com.android.systemui.plugins.statusbar.StatusBarStateController
 import com.android.systemui.power.domain.interactor.PowerInteractor
 import com.android.systemui.shade.ShadeController
@@ -105,7 +106,15 @@
                 (statusBarStateController.state != StatusBarState.SHADE) &&
                 statusBarKeyguardViewManager.shouldDismissOnMenuPressed()
         if (shouldUnlockOnMenuPressed) {
-            shadeController.animateCollapseShadeForced()
+            statusBarKeyguardViewManager.dismissWithAction(
+                object : OnDismissAction {
+                    override fun onDismiss(): Boolean {
+                        return false
+                    }
+                },
+                null,
+                false,
+            )
             return true
         }
         return false
diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractorTest.kt
index 13f30f5..945e44a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/KeyguardKeyEventInteractorTest.kt
@@ -46,6 +46,7 @@
 import org.mockito.Mockito.never
 import org.mockito.Mockito.verify
 import org.mockito.junit.MockitoJUnit
+import org.mockito.kotlin.isNull
 
 @ExperimentalCoroutinesApi
 @SmallTest
@@ -96,7 +97,7 @@
             .sendVolumeKeyEvent(
                 eq(actionDownVolumeDownKeyEvent),
                 eq(AudioManager.USE_DEFAULT_STREAM_TYPE),
-                eq(true)
+                eq(true),
             )
 
         assertThat(underTest.dispatchKeyEvent(actionDownVolumeUpKeyEvent)).isTrue()
@@ -104,7 +105,7 @@
             .sendVolumeKeyEvent(
                 eq(actionDownVolumeUpKeyEvent),
                 eq(AudioManager.USE_DEFAULT_STREAM_TYPE),
-                eq(true)
+                eq(true),
             )
     }
 
@@ -117,7 +118,7 @@
             .sendVolumeKeyEvent(
                 eq(actionDownVolumeDownKeyEvent),
                 eq(AudioManager.USE_DEFAULT_STREAM_TYPE),
-                eq(true)
+                eq(true),
             )
 
         assertThat(underTest.dispatchKeyEvent(actionDownVolumeUpKeyEvent)).isFalse()
@@ -125,7 +126,7 @@
             .sendVolumeKeyEvent(
                 eq(actionDownVolumeUpKeyEvent),
                 eq(AudioManager.USE_DEFAULT_STREAM_TYPE),
-                eq(true)
+                eq(true),
             )
     }
 
@@ -135,7 +136,9 @@
         whenever(statusBarStateController.state).thenReturn(StatusBarState.SHADE_LOCKED)
         whenever(statusBarKeyguardViewManager.shouldDismissOnMenuPressed()).thenReturn(true)
 
-        verifyActionUpCollapsesTheShade(KeyEvent.KEYCODE_MENU)
+        val actionUpMenuKeyEvent = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU)
+        assertThat(underTest.dispatchKeyEvent(actionUpMenuKeyEvent)).isTrue()
+        verify(statusBarKeyguardViewManager).dismissWithAction(any(), isNull(), eq(false))
     }
 
     @Test
diff --git a/services/core/java/com/android/server/SystemConfig.java b/services/core/java/com/android/server/SystemConfig.java
index d80e40c..8b619a4 100644
--- a/services/core/java/com/android/server/SystemConfig.java
+++ b/services/core/java/com/android/server/SystemConfig.java
@@ -46,6 +46,7 @@
 import android.util.Xml;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.pm.RoSystemFeatures;
 import com.android.internal.util.XmlUtils;
 import com.android.modules.utils.build.UnboundedSdkLevel;
 import com.android.server.pm.permission.PermissionAllowlist;
@@ -212,6 +213,30 @@
         }
     }
 
+    /**
+     * Utility class for testing interaction with compile-time defined system features.
+     * @hide
+    */
+    @VisibleForTesting
+    public static class Injector {
+        /** Whether a system feature is defined as enabled and available at compile-time. */
+        public boolean isReadOnlySystemEnabledFeature(String featureName, int version) {
+            return Boolean.TRUE.equals(RoSystemFeatures.maybeHasFeature(featureName, version));
+        }
+
+        /** Whether a system feature is defined as disabled and unavailable at compile-time. */
+        public boolean isReadOnlySystemDisabledFeature(String featureName, int version) {
+            return Boolean.FALSE.equals(RoSystemFeatures.maybeHasFeature(featureName, version));
+        }
+
+        /** The full set of system features defined as compile-time enabled and available. */
+        public ArrayMap<String, FeatureInfo> getReadOnlySystemEnabledFeatures() {
+            return RoSystemFeatures.getReadOnlySystemEnabledFeatures();
+        }
+    }
+
+    private final Injector mInjector;
+
     // These are the built-in shared libraries that were read from the
     // system configuration files. Keys are the library names; values are
     // the individual entries that contain information such as filename
@@ -220,7 +245,7 @@
 
     // These are the features this devices supports that were read from the
     // system configuration files.
-    final ArrayMap<String, FeatureInfo> mAvailableFeatures = new ArrayMap<>();
+    final ArrayMap<String, FeatureInfo> mAvailableFeatures;
 
     // These are the features which this device doesn't support; the OEM
     // partition uses these to opt-out of features from the system image.
@@ -602,12 +627,26 @@
     public ArrayMap<String, Integer> getOemDefinedUids() {
         return mOemDefinedUids;
     }
+
     /**
      * Only use for testing. Do NOT use in production code.
      * @param readPermissions false to create an empty SystemConfig; true to read the permissions.
      */
     @VisibleForTesting
     public SystemConfig(boolean readPermissions) {
+        this(readPermissions, new Injector());
+    }
+
+    /**
+     * Only use for testing. Do NOT use in production code.
+     * @param readPermissions false to create an empty SystemConfig; true to read the permissions.
+     * @param injector Additional dependency injection for testing.
+     */
+    @VisibleForTesting
+    public SystemConfig(boolean readPermissions, Injector injector) {
+        mInjector = injector;
+        mAvailableFeatures = mInjector.getReadOnlySystemEnabledFeatures();
+
         if (readPermissions) {
             Slog.w(TAG, "Constructing a test SystemConfig");
             readAllPermissions();
@@ -617,6 +656,9 @@
     }
 
     SystemConfig() {
+        mInjector = new Injector();
+        mAvailableFeatures = mInjector.getReadOnlySystemEnabledFeatures();
+
         TimingsTraceLog log = new TimingsTraceLog(TAG, Trace.TRACE_TAG_SYSTEM_SERVER);
         log.traceBegin("readAllPermissions");
         try {
@@ -1777,6 +1819,10 @@
     }
 
     private void addFeature(String name, int version) {
+        if (mInjector.isReadOnlySystemDisabledFeature(name, version)) {
+            Slog.w(TAG, "Skipping feature addition for compile-time disabled feature: " + name);
+            return;
+        }
         FeatureInfo fi = mAvailableFeatures.get(name);
         if (fi == null) {
             fi = new FeatureInfo();
@@ -1789,6 +1835,10 @@
     }
 
     private void removeFeature(String name) {
+        if (mInjector.isReadOnlySystemEnabledFeature(name, /*version=*/0)) {
+            Slog.w(TAG, "Skipping feature removal for compile-time enabled feature: " + name);
+            return;
+        }
         if (mAvailableFeatures.remove(name) != null) {
             Slog.d(TAG, "Removed unavailable feature " + name);
         }
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index d7c43b5..f7a34cc 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -12689,7 +12689,7 @@
                         continue;
                     }
                     endTime = SystemClock.currentThreadTimeMillis();
-                    hasSwapPss = mi.hasSwappedOutPss;
+                    hasSwapPss = hasSwapPss || mi.hasSwappedOutPss;
                     memtrackGraphics = mi.getOtherPrivate(Debug.MemoryInfo.OTHER_GRAPHICS);
                     memtrackGl = mi.getOtherPrivate(Debug.MemoryInfo.OTHER_GL);
                 } else {
@@ -13367,7 +13367,7 @@
                     continue;
                 }
                 endTime = SystemClock.currentThreadTimeMillis();
-                hasSwapPss = mi.hasSwappedOutPss;
+                hasSwapPss = hasSwapPss || mi.hasSwappedOutPss;
             } else {
                 reportType = ProcessStats.ADD_PSS_EXTERNAL;
                 startTime = SystemClock.currentThreadTimeMillis();
diff --git a/services/tests/servicestests/src/com/android/server/systemconfig/SystemConfigReadOnlyFeaturesTest.kt b/services/tests/servicestests/src/com/android/server/systemconfig/SystemConfigReadOnlyFeaturesTest.kt
new file mode 100644
index 0000000..22d894a
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/systemconfig/SystemConfigReadOnlyFeaturesTest.kt
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.systemconfig
+
+import android.content.Context
+import android.content.pm.FeatureInfo
+import android.util.ArrayMap
+import android.util.Xml
+
+import androidx.test.filters.SmallTest
+import androidx.test.platform.app.InstrumentationRegistry
+import com.android.server.SystemConfig
+import com.google.common.truth.Truth.assertThat
+import org.junit.runner.RunWith
+import org.junit.Rule
+
+import org.junit.Test
+import org.junit.rules.TemporaryFolder
+import org.junit.runners.JUnit4
+
+@SmallTest
+@RunWith(JUnit4::class)
+class SystemConfigReadOnlyFeaturesTest {
+
+    companion object {
+        private const val FEATURE_ONE = "feature.test.1"
+        private const val FEATURE_TWO = "feature.test.2"
+        private const val FEATURE_RUNTIME_AVAILABLE_TEMPLATE =
+        """
+            <permissions>
+                <feature name="%s" />
+            </permissions>
+        """
+        private const val FEATURE_RUNTIME_DISABLED_TEMPLATE =
+        """
+            <permissions>
+                <Disabled-feature name="%s" />
+            </permissions>
+        """
+
+        fun featureInfo(featureName: String) = FeatureInfo().apply { name = featureName }
+    }
+
+    private val context: Context = InstrumentationRegistry.getInstrumentation().context
+
+    @get:Rule
+    val tempFolder = TemporaryFolder(context.filesDir)
+
+    private val injector = TestInjector()
+
+    private var uniqueCounter = 0
+
+    @Test
+    fun empty() {
+        assertFeatures().isEmpty()
+    }
+
+    @Test
+    fun readOnlyEnabled() {
+        addReadOnlyEnabledFeature(FEATURE_ONE)
+        addReadOnlyEnabledFeature(FEATURE_TWO)
+
+        assertFeatures().containsAtLeast(FEATURE_ONE, FEATURE_TWO)
+    }
+
+    @Test
+    fun readOnlyAndRuntimeEnabled() {
+        addReadOnlyEnabledFeature(FEATURE_ONE)
+        addRuntimeEnabledFeature(FEATURE_TWO)
+
+        // No issues with matching availability.
+        assertFeatures().containsAtLeast(FEATURE_ONE, FEATURE_TWO)
+    }
+
+    @Test
+    fun readOnlyEnabledRuntimeDisabled() {
+        addReadOnlyEnabledFeature(FEATURE_ONE)
+        addRuntimeDisabledFeature(FEATURE_ONE)
+
+        // Read-only feature availability should take precedence.
+        assertFeatures().contains(FEATURE_ONE)
+    }
+
+    @Test
+    fun readOnlyDisabled() {
+        addReadOnlyDisabledFeature(FEATURE_ONE)
+
+        assertFeatures().doesNotContain(FEATURE_ONE)
+    }
+
+    @Test
+    fun readOnlyAndRuntimeDisabled() {
+        addReadOnlyDisabledFeature(FEATURE_ONE)
+        addRuntimeDisabledFeature(FEATURE_ONE)
+
+        // No issues with matching (un)availability.
+        assertFeatures().doesNotContain(FEATURE_ONE)
+    }
+
+    @Test
+    fun readOnlyDisabledRuntimeEnabled() {
+        addReadOnlyDisabledFeature(FEATURE_ONE)
+        addRuntimeEnabledFeature(FEATURE_ONE)
+        addRuntimeEnabledFeature(FEATURE_TWO)
+
+        // Read-only feature (un)availability should take precedence.
+        assertFeatures().doesNotContain(FEATURE_ONE)
+        assertFeatures().contains(FEATURE_TWO)
+    }
+
+    fun addReadOnlyEnabledFeature(featureName: String) {
+        injector.readOnlyEnabledFeatures[featureName] = featureInfo(featureName)
+    }
+
+    fun addReadOnlyDisabledFeature(featureName: String) {
+        injector.readOnlyDisabledFeatures.add(featureName)
+    }
+
+    fun addRuntimeEnabledFeature(featureName: String) {
+        FEATURE_RUNTIME_AVAILABLE_TEMPLATE.format(featureName).write()
+    }
+
+    fun addRuntimeDisabledFeature(featureName: String) {
+        FEATURE_RUNTIME_DISABLED_TEMPLATE.format(featureName).write()
+    }
+
+    private fun String.write() = tempFolder.root.resolve("${uniqueCounter++}.xml")
+            .writeText(this.trimIndent())
+
+    private fun assertFeatures() = assertThat(availableFeatures().keys)
+
+    private fun availableFeatures() = SystemConfig(false, injector).apply {
+        val parser = Xml.newPullParser()
+        readPermissions(parser, tempFolder.root, /*Grant all permission flags*/ 0.inv())
+    }.let { it.availableFeatures }
+
+    internal class TestInjector() : SystemConfig.Injector() {
+        val readOnlyEnabledFeatures = ArrayMap<String, FeatureInfo>()
+        val readOnlyDisabledFeatures = mutableSetOf<String>()
+
+        override fun isReadOnlySystemEnabledFeature(featureName: String, version: Int): Boolean {
+            return readOnlyEnabledFeatures.containsKey(featureName)
+        }
+
+        override fun isReadOnlySystemDisabledFeature(featureName: String, version: Int): Boolean {
+            return readOnlyDisabledFeatures.contains(featureName)
+        }
+
+        override fun getReadOnlySystemEnabledFeatures(): ArrayMap<String, FeatureInfo> {
+            return readOnlyEnabledFeatures
+        }
+    }
+}
diff --git a/tools/systemfeatures/src/com/android/systemfeatures/SystemFeaturesGenerator.kt b/tools/systemfeatures/src/com/android/systemfeatures/SystemFeaturesGenerator.kt
index cba521e..196b5e7 100644
--- a/tools/systemfeatures/src/com/android/systemfeatures/SystemFeaturesGenerator.kt
+++ b/tools/systemfeatures/src/com/android/systemfeatures/SystemFeaturesGenerator.kt
@@ -22,8 +22,6 @@
 import com.squareup.javapoet.MethodSpec
 import com.squareup.javapoet.ParameterizedTypeName
 import com.squareup.javapoet.TypeSpec
-import java.util.HashMap
-import java.util.Map
 import javax.lang.model.element.Modifier
 
 /*
@@ -52,7 +50,7 @@
  *     public static boolean hasFeatureAutomotive(Context context);
  *     public static boolean hasFeatureLeanback(Context context);
  *     public static Boolean maybeHasFeature(String feature, int version);
- *     public static ArrayMap<String, FeatureInfo> getCompileTimeAvailableFeatures();
+ *     public static ArrayMap<String, FeatureInfo> getReadOnlySystemEnabledFeatures();
  * }
  * </pre>
  */
@@ -63,6 +61,7 @@
     private val PACKAGEMANAGER_CLASS = ClassName.get("android.content.pm", "PackageManager")
     private val CONTEXT_CLASS = ClassName.get("android.content", "Context")
     private val FEATUREINFO_CLASS = ClassName.get("android.content.pm", "FeatureInfo")
+    private val ARRAYMAP_CLASS = ClassName.get("android.util", "ArrayMap")
     private val ASSUME_TRUE_CLASS =
         ClassName.get("com.android.aconfig.annotations", "AssumeTrueForR8")
     private val ASSUME_FALSE_CLASS =
@@ -291,19 +290,19 @@
         features: Collection<FeatureInfo>,
     ) {
         val methodBuilder =
-                MethodSpec.methodBuilder("getCompileTimeAvailableFeatures")
+                MethodSpec.methodBuilder("getReadOnlySystemEnabledFeatures")
                 .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                 .addAnnotation(ClassName.get("android.annotation", "NonNull"))
                 .addJavadoc("Gets features marked as available at compile-time, keyed by name." +
                         "\n\n@hide")
                 .returns(ParameterizedTypeName.get(
-                        ClassName.get(Map::class.java),
+                        ARRAYMAP_CLASS,
                         ClassName.get(String::class.java),
                         FEATUREINFO_CLASS))
 
         val availableFeatures = features.filter { it.readonly && it.version != null }
-        methodBuilder.addStatement("Map<String, FeatureInfo> features = new \$T<>(\$L)",
-                HashMap::class.java, availableFeatures.size)
+        methodBuilder.addStatement("\$T<String, FeatureInfo> features = new \$T<>(\$L)",
+                ARRAYMAP_CLASS, ARRAYMAP_CLASS, availableFeatures.size)
         if (!availableFeatures.isEmpty()) {
             methodBuilder.addStatement("FeatureInfo fi = new FeatureInfo()")
         }
diff --git a/tools/systemfeatures/tests/golden/RoFeatures.java.gen b/tools/systemfeatures/tests/golden/RoFeatures.java.gen
index edbfc42..ee97b26 100644
--- a/tools/systemfeatures/tests/golden/RoFeatures.java.gen
+++ b/tools/systemfeatures/tests/golden/RoFeatures.java.gen
@@ -13,10 +13,9 @@
 import android.content.Context;
 import android.content.pm.FeatureInfo;
 import android.content.pm.PackageManager;
+import android.util.ArrayMap;
 import com.android.aconfig.annotations.AssumeFalseForR8;
 import com.android.aconfig.annotations.AssumeTrueForR8;
-import java.util.HashMap;
-import java.util.Map;
 
 /**
  * @hide
@@ -94,8 +93,8 @@
      * @hide
      */
     @NonNull
-    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
-        Map<String, FeatureInfo> features = new HashMap<>(2);
+    public static ArrayMap<String, FeatureInfo> getReadOnlySystemEnabledFeatures() {
+        ArrayMap<String, FeatureInfo> features = new ArrayMap<>(2);
         FeatureInfo fi = new FeatureInfo();
         fi.name = PackageManager.FEATURE_WATCH;
         fi.version = 1;
diff --git a/tools/systemfeatures/tests/golden/RoNoFeatures.java.gen b/tools/systemfeatures/tests/golden/RoNoFeatures.java.gen
index bf7a006..40c7db7 100644
--- a/tools/systemfeatures/tests/golden/RoNoFeatures.java.gen
+++ b/tools/systemfeatures/tests/golden/RoNoFeatures.java.gen
@@ -9,8 +9,7 @@
 import android.content.Context;
 import android.content.pm.FeatureInfo;
 import android.content.pm.PackageManager;
-import java.util.HashMap;
-import java.util.Map;
+import android.util.ArrayMap;
 
 /**
  * @hide
@@ -43,8 +42,8 @@
      * @hide
      */
     @NonNull
-    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
-        Map<String, FeatureInfo> features = new HashMap<>(0);
+    public static ArrayMap<String, FeatureInfo> getReadOnlySystemEnabledFeatures() {
+        ArrayMap<String, FeatureInfo> features = new ArrayMap<>(0);
         return features;
     }
 }
diff --git a/tools/systemfeatures/tests/golden/RwFeatures.java.gen b/tools/systemfeatures/tests/golden/RwFeatures.java.gen
index b20b228..7bf8961 100644
--- a/tools/systemfeatures/tests/golden/RwFeatures.java.gen
+++ b/tools/systemfeatures/tests/golden/RwFeatures.java.gen
@@ -12,8 +12,7 @@
 import android.content.Context;
 import android.content.pm.FeatureInfo;
 import android.content.pm.PackageManager;
-import java.util.HashMap;
-import java.util.Map;
+import android.util.ArrayMap;
 
 /**
  * @hide
@@ -73,8 +72,8 @@
      * @hide
      */
     @NonNull
-    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
-        Map<String, FeatureInfo> features = new HashMap<>(0);
+    public static ArrayMap<String, FeatureInfo> getReadOnlySystemEnabledFeatures() {
+        ArrayMap<String, FeatureInfo> features = new ArrayMap<>(0);
         return features;
     }
 }
diff --git a/tools/systemfeatures/tests/golden/RwNoFeatures.java.gen b/tools/systemfeatures/tests/golden/RwNoFeatures.java.gen
index d91f5b6..eb7ec63 100644
--- a/tools/systemfeatures/tests/golden/RwNoFeatures.java.gen
+++ b/tools/systemfeatures/tests/golden/RwNoFeatures.java.gen
@@ -7,8 +7,7 @@
 import android.annotation.Nullable;
 import android.content.Context;
 import android.content.pm.FeatureInfo;
-import java.util.HashMap;
-import java.util.Map;
+import android.util.ArrayMap;
 
 /**
  * @hide
@@ -32,8 +31,8 @@
      * @hide
      */
     @NonNull
-    public static Map<String, FeatureInfo> getCompileTimeAvailableFeatures() {
-        Map<String, FeatureInfo> features = new HashMap<>(0);
+    public static ArrayMap<String, FeatureInfo> getReadOnlySystemEnabledFeatures() {
+        ArrayMap<String, FeatureInfo> features = new ArrayMap<>(0);
         return features;
     }
 }
diff --git a/tools/systemfeatures/tests/src/ArrayMap.java b/tools/systemfeatures/tests/src/ArrayMap.java
new file mode 100644
index 0000000..a5ed9b0
--- /dev/null
+++ b/tools/systemfeatures/tests/src/ArrayMap.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.util;
+
+import java.util.HashMap;
+
+/** Stub for testing. */
+public final class ArrayMap<K, V> extends HashMap<K, V> {
+    public ArrayMap(int capacity) {
+        super(capacity);
+    }
+}
diff --git a/tools/systemfeatures/tests/src/SystemFeaturesGeneratorTest.java b/tools/systemfeatures/tests/src/SystemFeaturesGeneratorTest.java
index 39f8fc4..ed3f5c9 100644
--- a/tools/systemfeatures/tests/src/SystemFeaturesGeneratorTest.java
+++ b/tools/systemfeatures/tests/src/SystemFeaturesGeneratorTest.java
@@ -60,7 +60,7 @@
         assertThat(RwNoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isNull();
         assertThat(RwNoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isNull();
         assertThat(RwNoFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull();
-        assertThat(RwNoFeatures.getCompileTimeAvailableFeatures()).isEmpty();
+        assertThat(RwNoFeatures.getReadOnlySystemEnabledFeatures()).isEmpty();
     }
 
     @Test
@@ -72,7 +72,7 @@
         assertThat(RoNoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isNull();
         assertThat(RoNoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isNull();
         assertThat(RoNoFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull();
-        assertThat(RoNoFeatures.getCompileTimeAvailableFeatures()).isEmpty();
+        assertThat(RoNoFeatures.getReadOnlySystemEnabledFeatures()).isEmpty();
 
         // Also ensure we fall back to the PackageManager for feature APIs without an accompanying
         // versioned feature definition.
@@ -106,7 +106,7 @@
         assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isNull();
         assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isNull();
         assertThat(RwFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull();
-        assertThat(RwFeatures.getCompileTimeAvailableFeatures()).isEmpty();
+        assertThat(RwFeatures.getReadOnlySystemEnabledFeatures()).isEmpty();
     }
 
     @Test
@@ -163,7 +163,7 @@
         assertThat(RoFeatures.maybeHasFeature("com.arbitrary.feature", 100)).isNull();
         assertThat(RoFeatures.maybeHasFeature("", 0)).isNull();
 
-        Map<String, FeatureInfo> compiledFeatures = RoFeatures.getCompileTimeAvailableFeatures();
+        Map<String, FeatureInfo> compiledFeatures = RoFeatures.getReadOnlySystemEnabledFeatures();
         assertThat(compiledFeatures.keySet())
                 .containsExactly(PackageManager.FEATURE_WATCH, PackageManager.FEATURE_WIFI);
         assertThat(compiledFeatures.get(PackageManager.FEATURE_WATCH).version).isEqualTo(1);