Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ab05b143 authored by Fyodor Kupolov's avatar Fyodor Kupolov
Browse files

Added SQLiteOpenHelper.setOpenParams

Using the new constructor can be difficult as it requires an app to
make a decision which version of constructor to use before instantiating
the object. By adding setOpenParams method, apps can keep the old
structure of the code and only add API level check after instantiating
a helper object.

Test: SQLiteOpenHelperTest
Bug: 70863722
Change-Id: I1705dd790e3549d0d8e75eb33b1d9b5bdadcb3c9
parent df210b43
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12549,6 +12549,7 @@ package android.database.sqlite {
    method public abstract void onUpgrade(android.database.sqlite.SQLiteDatabase, int, int);
    method public void setIdleConnectionTimeout(long);
    method public void setLookasideConfig(int, int);
    method public void setOpenParams(android.database.sqlite.SQLiteDatabase.OpenParams);
    method public void setWriteAheadLoggingEnabled(boolean);
  }
+26 −3
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ public abstract class SQLiteOpenHelper {

    private SQLiteDatabase mDatabase;
    private boolean mIsInitializing;
    private final SQLiteDatabase.OpenParams.Builder mOpenParamsBuilder;
    private SQLiteDatabase.OpenParams.Builder mOpenParamsBuilder;

    /**
     * Create a helper object to create, open, and/or manage a database.
@@ -163,8 +163,7 @@ public abstract class SQLiteOpenHelper {
        mName = name;
        mNewVersion = version;
        mMinimumSupportedVersion = Math.max(0, minimumSupportedVersion);
        mOpenParamsBuilder = openParamsBuilder;
        mOpenParamsBuilder.addOpenFlags(SQLiteDatabase.CREATE_IF_NECESSARY);
        setOpenParamsBuilder(openParamsBuilder);
    }

    /**
@@ -229,6 +228,30 @@ public abstract class SQLiteOpenHelper {
        }
    }

    /**
     * Sets configuration parameters that are used for opening {@link SQLiteDatabase}.
     * <p>Please note that {@link SQLiteDatabase#CREATE_IF_NECESSARY} flag will always be set when
     * opening the database
     *
     * @param openParams configuration parameters that are used for opening {@link SQLiteDatabase}.
     * @throws IllegalStateException if the database is already open
     */
    public void setOpenParams(@NonNull SQLiteDatabase.OpenParams openParams) {
        Preconditions.checkNotNull(openParams);
        synchronized (this) {
            if (mDatabase != null && mDatabase.isOpen()) {
                throw new IllegalStateException(
                        "OpenParams cannot be set after opening the database");
            }
            setOpenParamsBuilder(new SQLiteDatabase.OpenParams.Builder(openParams));
        }
    }

    private void setOpenParamsBuilder(SQLiteDatabase.OpenParams.Builder openParamsBuilder) {
        mOpenParamsBuilder = openParamsBuilder;
        mOpenParamsBuilder.addOpenFlags(SQLiteDatabase.CREATE_IF_NECESSARY);
    }

    /**
     * Sets the maximum number of milliseconds that SQLite connection is allowed to be idle
     * before it is closed and removed from the pool.