]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Provide global database helper instance
authorMarkus Pfeiffer <markus.pfeiffer@relution.io>
Tue, 21 Nov 2023 14:37:23 +0000 (15:37 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 21 Feb 2024 11:24:53 +0000 (12:24 +0100)
src/frontends/android/app/src/main/java/org/strongswan/android/data/DatabaseHelper.java
src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfileSource.java
src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfileSqlDataSource.java
src/frontends/android/app/src/main/java/org/strongswan/android/logic/StrongSwanApplication.java

index 95efd7cbb8f1aa40a816b474fe6d17aa8c99aa79..b978603d1f9f9ceee34c2176d70c223ccb03fd25 100644 (file)
@@ -77,7 +77,7 @@ public class DatabaseHelper extends SQLiteOpenHelper
                TABLES.add(TABLE_VPN_PROFILE);
        }
 
-       DatabaseHelper(Context context)
+       public DatabaseHelper(Context context)
        {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
index 77cfa85510dab0a0e53cbcd7b6ae255752034df1..299d0a51a8825c140d9fcba2f3d6744696ed0b8a 100644 (file)
@@ -35,7 +35,7 @@ public class VpnProfileSource implements VpnProfileDataSource
        {
                mManagedConfigurationService = StrongSwanApplication.getInstance().getManagedConfigurationService();
 
-               vpnProfileSqlDataSource = new VpnProfileSqlDataSource(context);
+               vpnProfileSqlDataSource = new VpnProfileSqlDataSource();
 
                dataSources.add(vpnProfileSqlDataSource);
                dataSources.add(new VpnProfileManagedDataSource(context));
index e14fccae66bc96eb8a7a61b3345941b1d056744f..50ffc6297e1d3ad1ba03829a0fbfd16f5e7dedd8 100644 (file)
 package org.strongswan.android.data;
 
 import android.content.ContentValues;
-import android.content.Context;
 import android.database.Cursor;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 
+import org.strongswan.android.logic.StrongSwanApplication;
+
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
 public class VpnProfileSqlDataSource implements VpnProfileDataSource
 {
-       private DatabaseHelper mDbHelper;
+       private final DatabaseHelper mDbHelper;
+
        private SQLiteDatabase mDatabase;
-       private final Context mContext;
 
        /**
         * Construct a new VPN profile data source. The context is used to
         * 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
        public VpnProfileDataSource open() throws SQLException
        {
-               if (mDbHelper == null)
+               if (mDatabase == null)
                {
-                       mDbHelper = new DatabaseHelper(mContext);
                        mDatabase = mDbHelper.getWritableDatabase();
                }
                return this;
@@ -59,10 +57,9 @@ public class VpnProfileSqlDataSource implements VpnProfileDataSource
        @Override
        public void close()
        {
-               if (mDbHelper != null)
+               if (mDatabase != null)
                {
-                       mDbHelper.close();
-                       mDbHelper = null;
+                       mDatabase = null;
                }
        }
 
index 8d653624e17906807e0c0cd835ab1f892b804d37..58b55e033f8d9b53989d211b2d2a4e1c68d821d9 100644 (file)
@@ -26,6 +26,7 @@ import android.os.Handler;
 import android.os.Looper;
 import android.util.Log;
 
+import org.strongswan.android.data.DatabaseHelper;
 import org.strongswan.android.data.ManagedConfigurationService;
 import org.strongswan.android.security.LocalCertificateKeyStoreProvider;
 import org.strongswan.android.utils.Constants;
@@ -56,6 +57,8 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
 
        private ManagedConfigurationService mManagedConfigurationService;
 
+       private DatabaseHelper mDatabaseHelper;
+
        private final BroadcastReceiver mRestrictionsReceiver = new BroadcastReceiver()
        {
                @Override
@@ -78,6 +81,8 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
                StrongSwanApplication.mContext = getApplicationContext();
                StrongSwanApplication.mInstance = this;
 
+               mDatabaseHelper = new DatabaseHelper(mContext);
+
                mManagedConfigurationService = new ManagedConfigurationService(mContext);
                ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
        }
@@ -162,6 +167,14 @@ public class StrongSwanApplication extends Application implements DefaultLifecyc
                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/...
         * during installation.  On newer releases most are loaded in JNI_OnLoad.