Add check to prevent resetting VPN always-on setting in safe mode

This change prevents the VPN always-on setting from being reset
in safe mode, where third-party VPNs may have limited
functionality in safe mode and can be deemed unsupported for
always-on use.

Bug: 397215751
Bug: 303408193
Test: manual, VpnTest
(cherry picked from https://android-review.googlesource.com/q/commit:cabbb7da639520633ad318655d1b5fe1c685c78e)
Merged-In: I03399c7c258ce5db67e81b4e51f25f2cb37700c9
Change-Id: I03399c7c258ce5db67e81b4e51f25f2cb37700c9
diff --git a/services/core/java/com/android/server/connectivity/Vpn.java b/services/core/java/com/android/server/connectivity/Vpn.java
index e3262cf..f5f13fa 100644
--- a/services/core/java/com/android/server/connectivity/Vpn.java
+++ b/services/core/java/com/android/server/connectivity/Vpn.java
@@ -1258,7 +1258,11 @@
             }
             // Remove always-on VPN if it's not supported.
             if (!isAlwaysOnPackageSupported(alwaysOnPackage)) {
-                setAlwaysOnPackage(null, false, null);
+                // Do not remove the always-on setting due to the restricted ability in safe mode.
+                // The always-on VPN can then start after the device reboots to normal mode.
+                if (!mContext.getPackageManager().isSafeMode()) {
+                    setAlwaysOnPackage(null, false, null);
+                }
                 return false;
             }
             // Skip if the service is already established. This isn't bulletproof: it's not bound