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

Commit 497b5fab authored by Fyodor Kupolov's avatar Fyodor Kupolov
Browse files

Optimized database creation for a new user

If the file doesn't exist, database can be kept in memory. It's safe because
the database will be migrated and disposed of immediately after onCreate
finishes.

Bug: 26237300
Change-Id: Ib37c520f28960c8d6b9ce8dd719ffbcc11a97a58
parent 1d724e02
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -116,6 +116,12 @@ class DatabaseHelper extends SQLiteOpenHelper {
            // cleaned up automatically when the user is deleted.
            File databaseFile = new File(
                    Environment.getUserSystemDirectory(userHandle), DATABASE_NAME);
            // If databaseFile doesn't exist, database can be kept in memory. It's safe because the
            // database will be migrated and disposed of immediately after onCreate finishes
            if (!databaseFile.exists()) {
                Log.i(TAG, "No previous database file exists - running in in-memory mode");
                return null;
            }
            return databaseFile.getPath();
        }
    }
@@ -130,8 +136,16 @@ class DatabaseHelper extends SQLiteOpenHelper {
        return mValidTables.contains(name);
    }

    private boolean isInMemory() {
        return getDatabaseName() == null;
    }

    public void dropDatabase() {
        close();
        // No need to remove files if db is in memory
        if (isInMemory()) {
            return;
        }
        File databaseFile = mContext.getDatabasePath(getDatabaseName());
        if (databaseFile.exists()) {
            databaseFile.delete();
@@ -145,6 +159,10 @@ class DatabaseHelper extends SQLiteOpenHelper {

    public void backupDatabase() {
        close();
        // No need to backup files if db is in memory
        if (isInMemory()) {
            return;
        }
        File databaseFile = mContext.getDatabasePath(getDatabaseName());
        if (!databaseFile.exists()) {
            return;