Introduced DISABLE_COMPATIBILITY_WAL flag

If set, Compatibility WAL will not be used.
Currently this flag is set if SQLiteOpenHelper.setWriteAheadLogging(false)
is called before opening the database. Previously this call was ignored
since ENABLE_WRITE_AHEAD_LOGGING flag is only set when setWriteAheadLogging(true)

Test: CtsDatabaseTestCases
Bug: 74116447
Change-Id: Ic486e178b9da5d747840be739303a2685b91f35b
diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java
index ae1f57d..b463d8d 100644
--- a/core/java/android/database/sqlite/SQLiteDatabase.java
+++ b/core/java/android/database/sqlite/SQLiteDatabase.java
@@ -253,6 +253,13 @@
     public static final int ENABLE_WRITE_AHEAD_LOGGING = 0x20000000;
 
     /**
+     * Open flag: Flag for {@link #openDatabase} to disable Compatibility WAL when opening database.
+     *
+     * @hide
+     */
+    public static final int DISABLE_COMPATIBILITY_WAL = 0x40000000;
+
+    /**
      * Absolute max value that can be set by {@link #setMaxSqlCacheSize(int)}.
      *
      * Each prepared-statement is between 1K - 6K, depending on the complexity of the
@@ -288,10 +295,10 @@
         mConfigurationLocked.idleConnectionTimeoutMs = effectiveTimeoutMs;
         mConfigurationLocked.journalMode = journalMode;
         mConfigurationLocked.syncMode = syncMode;
-        mConfigurationLocked.useCompatibilityWal = SQLiteGlobal.isCompatibilityWalSupported();
-        if (!mConfigurationLocked.isInMemoryDb() && SQLiteCompatibilityWalFlags.areFlagsSet()) {
-            mConfigurationLocked.useCompatibilityWal = SQLiteCompatibilityWalFlags
-                    .isCompatibilityWalSupported();
+        if (!SQLiteGlobal.isCompatibilityWalSupported() || (
+                SQLiteCompatibilityWalFlags.areFlagsSet() && !SQLiteCompatibilityWalFlags
+                        .isCompatibilityWalSupported())) {
+            mConfigurationLocked.openFlags |= DISABLE_COMPATIBILITY_WAL;
         }
     }
 
@@ -2082,21 +2089,21 @@
         synchronized (mLock) {
             throwIfNotOpenLocked();
 
-            final boolean oldUseCompatibilityWal = mConfigurationLocked.useCompatibilityWal;
             final int oldFlags = mConfigurationLocked.openFlags;
-            if (!oldUseCompatibilityWal && (oldFlags & ENABLE_WRITE_AHEAD_LOGGING) == 0) {
+            final boolean walDisabled = (oldFlags & ENABLE_WRITE_AHEAD_LOGGING) == 0;
+            final boolean compatibilityWalDisabled = (oldFlags & DISABLE_COMPATIBILITY_WAL) != 0;
+            if (walDisabled && compatibilityWalDisabled) {
                 return;
             }
 
             mConfigurationLocked.openFlags &= ~ENABLE_WRITE_AHEAD_LOGGING;
-            // If an app explicitly disables WAL, do not even use compatibility mode
-            mConfigurationLocked.useCompatibilityWal = false;
+            // If an app explicitly disables WAL, compatibility mode should be disabled too
+            mConfigurationLocked.openFlags |= DISABLE_COMPATIBILITY_WAL;
 
             try {
                 mConnectionPoolLocked.reconfigure(mConfigurationLocked);
             } catch (RuntimeException ex) {
                 mConfigurationLocked.openFlags = oldFlags;
-                mConfigurationLocked.useCompatibilityWal = oldUseCompatibilityWal;
                 throw ex;
             }
         }