diff --git a/native/android/app/src/cpp/DatabaseInitializerJNIHelper.cpp b/native/android/app/src/cpp/DatabaseInitializerJNIHelper.cpp new file mode 100644 --- /dev/null +++ b/native/android/app/src/cpp/DatabaseInitializerJNIHelper.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +namespace comm { +void DatabaseInitializerJNIHelper::initializeDatabaseManager( + facebook::jni::alias_ref jThis, + std::string sqliteFilePath) { + SQLiteQueryExecutor::initialize(sqliteFilePath); + DatabaseManager::initializeQueryExecutor(); +} + +void DatabaseInitializerJNIHelper::registerNatives() { + javaClassStatic()->registerNatives({ + makeNativeMethod( + "initializeDatabaseManager", + DatabaseInitializerJNIHelper::initializeDatabaseManager), + }); +} +} // namespace comm diff --git a/native/android/app/src/cpp/jsiInstaller.cpp b/native/android/app/src/cpp/jsiInstaller.cpp --- a/native/android/app/src/cpp/jsiInstaller.cpp +++ b/native/android/app/src/cpp/jsiInstaller.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -57,5 +58,6 @@ comm::ThreadOperationsJNIHelper::registerNatives(); comm::MessageOperationsUtilitiesJNIHelper::registerNatives(); comm::GlobalDBSingletonJNIHelper::registerNatives(); + comm::DatabaseInitializerJNIHelper::registerNatives(); }); } diff --git a/native/android/app/src/main/java/app/comm/android/MainApplication.java b/native/android/app/src/main/java/app/comm/android/MainApplication.java --- a/native/android/app/src/main/java/app/comm/android/MainApplication.java +++ b/native/android/app/src/main/java/app/comm/android/MainApplication.java @@ -3,8 +3,11 @@ import android.content.Context; import android.content.res.Configuration; import android.database.CursorWindow; +import android.util.Log; import androidx.annotation.NonNull; import androidx.multidex.MultiDexApplication; +import app.comm.android.fbjni.CommSecureStore; +import app.comm.android.fbjni.DatabaseInitializer; import app.comm.android.newarchitecture.MainApplicationReactNativeHost; import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; @@ -19,6 +22,7 @@ import expo.modules.ReactNativeHostWrapper; import io.invertase.firebase.messaging.RNFirebaseMessagingPackage; import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage; +import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.security.Security; @@ -82,6 +86,7 @@ Security.insertProviderAt(new org.conscrypt.OpenSSLProvider(), 1); SoLoader.init(this, /* native exopackage */ false); + this.initializeDatabase(); ApplicationLifecycleDispatcher.onApplicationCreate(this); try { Field field = CursorWindow.class.getDeclaredField("sCursorWindowSize"); @@ -99,4 +104,13 @@ super.onConfigurationChanged(newConfig); ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig); } + + private void initializeDatabase() { + File sqliteFile = + this.getApplicationContext().getDatabasePath("comm.sqlite"); + CommSecureStore.getInstance().initialize( + ExpoUtils.createExpoSecureStoreSupplier(this.getApplicationContext())); + + DatabaseInitializer.initializeDatabaseManager(sqliteFile.getPath()); + } } diff --git a/native/android/app/src/main/java/app/comm/android/fbjni/DatabaseInitializer.java b/native/android/app/src/main/java/app/comm/android/fbjni/DatabaseInitializer.java new file mode 100644 --- /dev/null +++ b/native/android/app/src/main/java/app/comm/android/fbjni/DatabaseInitializer.java @@ -0,0 +1,5 @@ +package app.comm.android.fbjni; + +public class DatabaseInitializer { + public static native void initializeDatabaseManager(String sqliteFilePath); +} diff --git a/native/cpp/CommonCpp/NativeModules/InternalModules/DatabaseInitializerJNIHelper.h b/native/cpp/CommonCpp/NativeModules/InternalModules/DatabaseInitializerJNIHelper.h new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/NativeModules/InternalModules/DatabaseInitializerJNIHelper.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace comm { +class DatabaseInitializerJNIHelper + : public facebook::jni::JavaClass { +public: + static auto constexpr kJavaDescriptor = + "Lapp/comm/android/fbjni/DatabaseInitializer;"; + static void initializeDatabaseManager( + facebook::jni::alias_ref jThis, + std::string sqliteFilePath); + static void registerNatives(); +}; +} // namespace comm