android: Provide global database helper instance

This commit is contained in:
Markus Pfeiffer
2024-02-21 12:24:53 +01:00
committed by Tobias Brunner
parent 861ac0109a
commit 9a917252e2
4 changed files with 24 additions and 14 deletions
@@ -77,7 +77,7 @@ public class DatabaseHelper extends SQLiteOpenHelper
TABLES.add(TABLE_VPN_PROFILE); TABLES.add(TABLE_VPN_PROFILE);
} }
DatabaseHelper(Context context) public DatabaseHelper(Context context)
{ {
super(context, DATABASE_NAME, null, DATABASE_VERSION); super(context, DATABASE_NAME, null, DATABASE_VERSION);
} }
@@ -35,7 +35,7 @@ public class VpnProfileSource implements VpnProfileDataSource
{ {
mManagedConfigurationService = StrongSwanApplication.getInstance().getManagedConfigurationService(); mManagedConfigurationService = StrongSwanApplication.getInstance().getManagedConfigurationService();
vpnProfileSqlDataSource = new VpnProfileSqlDataSource(context); vpnProfileSqlDataSource = new VpnProfileSqlDataSource();
dataSources.add(vpnProfileSqlDataSource); dataSources.add(vpnProfileSqlDataSource);
dataSources.add(new VpnProfileManagedDataSource(context)); dataSources.add(new VpnProfileManagedDataSource(context));
@@ -19,38 +19,36 @@
package org.strongswan.android.data; package org.strongswan.android.data;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.SQLException; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import org.strongswan.android.logic.StrongSwanApplication;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class VpnProfileSqlDataSource implements VpnProfileDataSource public class VpnProfileSqlDataSource implements VpnProfileDataSource
{ {
private DatabaseHelper mDbHelper; private final DatabaseHelper mDbHelper;
private SQLiteDatabase mDatabase; private SQLiteDatabase mDatabase;
private final Context mContext;
/** /**
* Construct a new VPN profile data source. The context is used to * Construct a new VPN profile data source. The context is used to
* open/create the database. * open/create the database.
*
* @param context context used to access the database
*/ */
public VpnProfileSqlDataSource(Context context) public VpnProfileSqlDataSource()
{ {
this.mContext = context; mDbHelper = StrongSwanApplication.getInstance().getDatabaseHelper();
} }
@Override @Override
public VpnProfileDataSource open() throws SQLException public VpnProfileDataSource open() throws SQLException
{ {
if (mDbHelper == null) if (mDatabase == null)
{ {
mDbHelper = new DatabaseHelper(mContext);
mDatabase = mDbHelper.getWritableDatabase(); mDatabase = mDbHelper.getWritableDatabase();
} }
return this; return this;
@@ -59,10 +57,9 @@ public class VpnProfileSqlDataSource implements VpnProfileDataSource
@Override @Override
public void close() public void close()
{ {
if (mDbHelper != null) if (mDatabase != null)
{ {
mDbHelper.close(); mDatabase = null;
mDbHelper = null;
} }
} }
@@ -26,6 +26,7 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log; import android.util.Log;
import org.strongswan.android.data.DatabaseHelper;
import org.strongswan.android.data.ManagedConfigurationService; import org.strongswan.android.data.ManagedConfigurationService;
import org.strongswan.android.security.LocalCertificateKeyStoreProvider; import org.strongswan.android.security.LocalCertificateKeyStoreProvider;
import org.strongswan.android.utils.Constants; import org.strongswan.android.utils.Constants;
@@ -56,6 +57,8 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
private ManagedConfigurationService mManagedConfigurationService; private ManagedConfigurationService mManagedConfigurationService;
private DatabaseHelper mDatabaseHelper;
private final BroadcastReceiver mRestrictionsReceiver = new BroadcastReceiver() private final BroadcastReceiver mRestrictionsReceiver = new BroadcastReceiver()
{ {
@Override @Override
@@ -78,6 +81,8 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
StrongSwanApplication.mContext = getApplicationContext(); StrongSwanApplication.mContext = getApplicationContext();
StrongSwanApplication.mInstance = this; StrongSwanApplication.mInstance = this;
mDatabaseHelper = new DatabaseHelper(mContext);
mManagedConfigurationService = new ManagedConfigurationService(mContext); mManagedConfigurationService = new ManagedConfigurationService(mContext);
ProcessLifecycleOwner.get().getLifecycle().addObserver(this); ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
} }
@@ -162,6 +167,14 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
return mManagedConfigurationService; return mManagedConfigurationService;
} }
/**
* @return the application's database helper used to access its SQLite database
*/
public DatabaseHelper getDatabaseHelper()
{
return mDatabaseHelper;
}
/* /*
* The libraries are extracted to /data/data/org.strongswan.android/... * The libraries are extracted to /data/data/org.strongswan.android/...
* during installation. On newer releases most are loaded in JNI_OnLoad. * during installation. On newer releases most are loaded in JNI_OnLoad.