Merge cherrypicks of ['googleplex-android-review.googlesource.com/39892028'] into 26Q2-release.

Change-Id: Icfcf4010fa82e60a6562431f3102e0c3d94b8a2b
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index 48daee4..f7ebc6c 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -290,18 +290,13 @@
                 final NfcVerboseVendorLogPreferenceController nfcVerboseLogController =
                         getDevelopmentOptionsController(
                                 NfcVerboseVendorLogPreferenceController.class);
-                final GraphicsDriverEnableAngleAsSystemDriverController enableAngleController =
-                        getDevelopmentOptionsController(
-                                GraphicsDriverEnableAngleAsSystemDriverController.class);
                 // If hardware offload isn't default value, we must reboot after disable
                 // developer options. Show a dialog for the user to confirm.
                 if ((a2dpController == null || a2dpController.isDefaultValue())
                         && (leAudioController == null || leAudioController.isDefaultValue())
                         && (nfcSnoopLogController == null || nfcSnoopLogController.isDefaultValue())
                         && (nfcVerboseLogController == null
-                        || nfcVerboseLogController.isDefaultValue())
-                        && (enableAngleController == null
-                        || enableAngleController.isDefaultValue())) {
+                                || nfcVerboseLogController.isDefaultValue())) {
                     disableDeveloperOptions();
                 } else {
                     // Disabling developer options in page-agnostic mode isn't supported as device
diff --git a/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java b/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java
index 1ffbb50..ae18777 100644
--- a/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java
+++ b/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverController.java
@@ -59,6 +59,8 @@
     static final String PROPERTY_DEBUG_ANGLE_DEVELOPER_OPTION =
             "debug.graphics.angle.developeroption.enable";
 
+    @VisibleForTesting static final String PROPERTY_VENDOR_API_LEVEL = "ro.vendor.api_level";
+
     @VisibleForTesting static final String ANGLE_DRIVER_SUFFIX = "angle";
 
     @VisibleForTesting
@@ -79,6 +81,11 @@
                 public boolean getBoolean(String key, boolean def) {
                     return SystemProperties.getBoolean(key, def);
                 }
+
+                @Override
+                public int getInt(String key, int def) {
+                    return SystemProperties.getInt(key, def);
+                }
             };
         }
     }
@@ -121,6 +128,16 @@
                 + persistGraphicsEglValue);
     }
 
+    // On devices where vendor API level is >= 202604, hide the switch from UI.
+    // This prevents users from changing the value of "persist.graphics.egl" through developer
+    // option menu UI on newly released devices.
+    @Override
+    public boolean isAvailable() {
+        final boolean isVendorAPILevelQualify =
+                (mSystemProperties.getInt(PROPERTY_VENDOR_API_LEVEL, 0) < 202604);
+        return isVendorAPILevelQualify;
+    }
+
     @Override
     public String getPreferenceKey() {
         return ENABLE_ANELE_AS_SYSTEM_DRIVER_KEY;
@@ -146,14 +163,6 @@
                 this);
     }
 
-    /** Return the default value of "persist.graphics.egl" */
-    public boolean isDefaultValue() {
-        final String currentGlesDriver =
-                mSystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
-        // default value of "persist.graphics.egl" is ""
-        return TextUtils.isEmpty(currentGlesDriver);
-    }
-
     @Override
     public void updateState(Preference preference) {
         super.updateState(preference);
@@ -173,12 +182,8 @@
 
     @Override
     protected void onDeveloperOptionsSwitchDisabled() {
-        // 1) disable the switch
+        // disable the switch
         super.onDeveloperOptionsSwitchDisabled();
-        // 2) set the persist.graphics.egl empty string
-        GraphicsEnvironment.getInstance().toggleAngleAsSystemDriver(false);
-        // 3) reset the switch
-        ((TwoStatePreference) mPreference).setChecked(false);
     }
 
     void toggleSwitchBack() {
diff --git a/src/com/android/settings/development/graphicsdriver/GraphicsDriverSystemPropertiesWrapper.java b/src/com/android/settings/development/graphicsdriver/GraphicsDriverSystemPropertiesWrapper.java
index 33badd1..22191de 100644
--- a/src/com/android/settings/development/graphicsdriver/GraphicsDriverSystemPropertiesWrapper.java
+++ b/src/com/android/settings/development/graphicsdriver/GraphicsDriverSystemPropertiesWrapper.java
@@ -19,6 +19,7 @@
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+
 /**
  * Wrapper interface to access {@link SystemProperties}.
  *
@@ -52,4 +53,13 @@
      * @return if the {@code key} isn't found, return {@code def}.
      */
     boolean getBoolean(@NonNull String key, @NonNull boolean def);
+
+    /**
+     * Get the int value for the given {@code key}.
+     *
+     * @param key the key to lookup
+     * @param def the default value in case the property is not set or empty
+     * @return if the {@code key} isn't found, return {@code def}.
+     */
+    int getInt(@NonNull String key, @NonNull int def);
 }
diff --git a/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerTest.java b/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerTest.java
index 4fbd4e2..b0647b2 100644
--- a/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerTest.java
+++ b/tests/robotests/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerTest.java
@@ -128,23 +128,4 @@
         mController.updateState(mPreference);
         verify(mPreference).setChecked(false);
     }
-
-    @Test
-    public void onDeveloperOptionSwitchDisabled_shouldDisableAngleAsSystemDriver() {
-        mController.onDeveloperOptionsSwitchDisabled();
-        final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
-        assertThat(systemEGLDriver).isEqualTo("");
-    }
-
-    @Test
-    public void onDeveloperOptionSwitchDisabled_preferenceShouldNotBeChecked() {
-        mController.onDeveloperOptionsSwitchDisabled();
-        verify(mPreference).setChecked(false);
-    }
-
-    @Test
-    public void onDeveloperOptionsSwitchDisabled_preferenceShouldNotBeEnabled() {
-        mController.onDeveloperOptionsSwitchDisabled();
-        verify(mPreference).setEnabled(false);
-    }
 }
diff --git a/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java b/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java
index e65a249..941d9ef 100644
--- a/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java
+++ b/tests/unit/src/com/android/settings/development/graphicsdriver/GraphicsDriverEnableAngleAsSystemDriverControllerJUnitTest.java
@@ -20,19 +20,23 @@
 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController.Injector;
 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController.PROPERTY_DEBUG_ANGLE_DEVELOPER_OPTION;
 import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController.PROPERTY_PERSISTENT_GRAPHICS_EGL;
+import static com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController.PROPERTY_VENDOR_API_LEVEL;
 
 import static com.google.common.truth.Truth.assertThat;
 
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.when;
 
 import android.content.Context;
 import android.os.Looper;
 import android.os.SystemProperties;
+import android.text.TextUtils;
 
 import androidx.preference.PreferenceManager;
 import androidx.preference.PreferenceScreen;
@@ -42,6 +46,7 @@
 
 import com.android.settings.development.DevelopmentSettingsDashboardFragment;
 
+import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -59,6 +64,10 @@
 
     private GraphicsDriverEnableAngleAsSystemDriverController mController;
 
+    private static Boolean sIsControllerAvailableOnDevice = null;
+
+    private static String sDevicePersistGraphicsEGLValue = null;
+
     // Signal to wait for SystemProperty values changed
     private static class PropertyChangeSignal {
         private CountDownLatch mCountDownLatch;
@@ -137,10 +146,55 @@
         mPreference.setKey(mController.getPreferenceKey());
         screen.addPreference(mPreference);
         mController.displayPreference(screen);
+
+        if (sIsControllerAvailableOnDevice == null) {
+            final boolean isVendorAPILevelQualify =
+                    (SystemProperties.getInt(PROPERTY_VENDOR_API_LEVEL, 0) < 202604);
+            sIsControllerAvailableOnDevice = isVendorAPILevelQualify;
+        }
+
+        if (sDevicePersistGraphicsEGLValue == null) {
+            sDevicePersistGraphicsEGLValue =
+                    SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
+        }
+    }
+
+    @After
+    public void restoreDeviceStatus() {
+        final String persistGraphicsEGLValueAfterTest =
+                SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
+        if (!TextUtils.equals(sDevicePersistGraphicsEGLValue, persistGraphicsEGLValueAfterTest)) {
+            PropertyChangeSignal propertyChangeSignal = new PropertyChangeSignal();
+            SystemProperties.addChangeCallback(propertyChangeSignal.getCountDownJob());
+            mController.onPreferenceChange(
+                    mPreference,
+                    TextUtils.equals(ANGLE_DRIVER_SUFFIX, sDevicePersistGraphicsEGLValue));
+            propertyChangeSignal.wait(100);
+            // Verify the SystemProperty is set to the original device value.
+            assertThat(SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL))
+                    .isEqualTo(sDevicePersistGraphicsEGLValue);
+        }
+    }
+
+    @Test
+    public void vendorAPILevelQualify_isAvailable_shouldReturnTrue() {
+        when(mSystemPropertiesMock.getInt(eq(PROPERTY_VENDOR_API_LEVEL), anyInt()))
+                .thenReturn(202504);
+        assertThat(mController.isAvailable()).isTrue();
+    }
+
+    @Test
+    public void vendorAPILevelNotQualify_isAvailable_shouldReturnFalse() {
+        when(mSystemPropertiesMock.getInt(eq(PROPERTY_VENDOR_API_LEVEL), anyInt()))
+                .thenReturn(202604);
+        assertThat(mController.isAvailable()).isFalse();
     }
 
     @Test
     public void onPreferenceChange_switchOn_shouldEnableAngleAsSystemDriver() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Step 1: toggle the switch "Enable ANGLE" on
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
@@ -161,6 +215,9 @@
 
     @Test
     public void onPreferenceChange_switchOff_shouldDisableAngleAsSystemDriver() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Step 1: toggle the switch "Enable ANGLE" off
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
@@ -180,13 +237,31 @@
     }
 
     @Test
-    public void updateState_PreferenceShouldEnabled() {
+    public void updateState_PreferenceShouldEnabled_AngleIsSystemDriver() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
+        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
+                .thenReturn(ANGLE_DRIVER_SUFFIX);
+        mController.updateState(mPreference);
+        assertThat(mPreference.isEnabled()).isTrue();
+    }
+
+    @Test
+    public void updateState_PreferenceShouldDisabled_AngleIsNotSystemDriver() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
+        when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any())).thenReturn("");
         mController.updateState(mPreference);
         assertThat(mPreference.isEnabled()).isFalse();
     }
 
     @Test
     public void updateState_angleIsSystemGLESDriver_PreferenceShouldBeChecked() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any()))
                 .thenReturn(ANGLE_DRIVER_SUFFIX);
         mController.updateState(mPreference);
@@ -195,13 +270,19 @@
 
     @Test
     public void updateState_angleIsNotSystemGLESDriver_PreferenceShouldNotBeChecked() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         when(mSystemPropertiesMock.get(eq(PROPERTY_PERSISTENT_GRAPHICS_EGL), any())).thenReturn("");
         mController.updateState(mPreference);
         assertThat(mPreference.isChecked()).isFalse();
     }
 
     @Test
-    public void onDeveloperOptionSwitchDisabled_angleShouldNotBeSystemGLESDriver() {
+    public void onDeveloperOptionSwitchDisabled_persistGraphicsEGLValueUnchanged() {
+        final String persistGraphicsEGLValueBefore =
+                SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL, "");
+
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
         // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
@@ -209,30 +290,35 @@
         SystemProperties.addChangeCallback(propertyChangeSignal1.getCountDownJob());
 
         // Test that onDeveloperOptionSwitchDisabled,
-        // persist.graphics.egl updates to ""
+        // persist.graphics.egl remains unchanged
         mController.onDeveloperOptionsSwitchDisabled();
         propertyChangeSignal1.wait(100);
-        final String systemEGLDriver = SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
-        assertThat(systemEGLDriver).isEqualTo("");
+        final String persistGraphicsEGLValueAfter =
+                SystemProperties.get(PROPERTY_PERSISTENT_GRAPHICS_EGL);
+        assertThat(persistGraphicsEGLValueAfter).isEqualTo(persistGraphicsEGLValueBefore);
 
         // Done with the test, remove the callback
         SystemProperties.removeChangeCallback(propertyChangeSignal1.getCountDownJob());
     }
 
     @Test
-    public void onDeveloperOptionSwitchDisabled_PreferenceShouldNotBeChecked() {
+    public void onDeveloperOptionSwitchDisabled_PreferenceCheckedUnchanged() {
+        final boolean preferenceIsCheckedBefore = mPreference.isChecked();
         mController.onDeveloperOptionsSwitchDisabled();
-        assertThat(mPreference.isChecked()).isFalse();
+        assertThat(mPreference.isChecked()).isEqualTo(preferenceIsCheckedBefore);
     }
 
     @Test
-    public void onDeveloperOptionSwitchDisabled_PreferenceShouldDisabled() {
+    public void onDeveloperOptionSwitchDisabled_PreferenceIsDisabled() {
         mController.onDeveloperOptionsSwitchDisabled();
         assertThat(mPreference.isEnabled()).isFalse();
     }
 
     @Test
-    public void onRebootCancelled_ToggleSwitchFromOnToOff() {
+    public void onRebootCancelled_ToggleSwitchOnRevertToOff() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Step 1: Toggle the "Enable ANGLE" switch on
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
@@ -269,7 +355,10 @@
     }
 
     @Test
-    public void onRebootCancelled_ToggleSwitchFromOffToOn() {
+    public void onRebootCancelled_ToggleSwitchOffRevertToOn() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Step 1: Toggle off the switch "Enable ANGLE"
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
@@ -306,7 +395,10 @@
     }
 
     @Test
-    public void onRebootDialogDismissed_ToggleSwitchFromOnToOff() {
+    public void onRebootDialogDismissed_ToggleSwitchOnRevertToOff() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Step 1: Toggle on the switch "Enable ANGLE"
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
@@ -342,8 +434,11 @@
     }
 
     @Test
-    public void onRebootDialogDismissed_ToggleSwitchFromOffToOn() {
-        // Step 1: Toggle on the switch "Enable ANGLE"
+    public void onRebootDialogDismissed_ToggleSwitchOffRevertToOn() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
+        // Step 1: Toggle off the switch "Enable ANGLE"
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
         // GpuService::toggleAngleAsSystemDriver() updates the persist.graphics.egl.
@@ -379,6 +474,9 @@
 
     @Test
     public void onRebootDialogConfirmed_ToggleSwitchOnRemainsOn() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Step 1: Toggle on the switch "Enable ANGLE"
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
@@ -414,6 +512,9 @@
 
     @Test
     public void onRebootDialogConfirmed_ToggleSwitchOffRemainsOff() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Step 1: Toggle off the switch "Enable ANGLE"
         // Add a callback when SystemProperty changes.
         // This allows the thread to wait until
@@ -449,6 +550,9 @@
 
     @Test
     public void updateState_DeveloperOptionPropertyIsFalse() {
+        assumeTrue(
+                "Skipping test on device where the switch is not available",
+                sIsControllerAvailableOnDevice);
         // Test that when debug.graphics.angle.developeroption.enable is false:
         when(mSystemPropertiesMock.getBoolean(eq(PROPERTY_DEBUG_ANGLE_DEVELOPER_OPTION),
                                               anyBoolean())).thenReturn(false);