diff --git a/kommunicate/src/main/java/io/kommunicate/commons/commons/core/utils/DBUtils.java b/kommunicate/src/main/java/io/kommunicate/commons/commons/core/utils/DBUtils.java index 27dcf3d4..d90ffba6 100755 --- a/kommunicate/src/main/java/io/kommunicate/commons/commons/core/utils/DBUtils.java +++ b/kommunicate/src/main/java/io/kommunicate/commons/commons/core/utils/DBUtils.java @@ -56,18 +56,26 @@ public static boolean existsColumnInTable(SQLiteDatabase inDatabase, String inTa public static boolean isDatabaseEncrypted(Context context, String dbName) { String appId = MobiComKitClientService.getApplicationKey(AppContextService.getContext(context)); + + // 💡 DEFENSIVE CHECK: Prevent crash if appId is null + if (appId == null || appId.isEmpty()) { + Log.e("DatabaseCheck", "Application ID is missing. Kommunicate SDK may not be initialized."); + return false; + } + File dbFile = context.getDatabasePath(dbName); + if (!dbFile.exists()) { + return false; // Database file doesn't exist, so it's not encrypted. + } - // Attempt to open the database with the given password SQLiteDatabase db = null; try { - // Corrected method call with byte array for password + // Now it's safe to call getBytes() db = SQLiteDatabase.openDatabase(dbFile.getPath(), appId.getBytes(), null, SQLiteDatabase.OPEN_READONLY, null, null); db.close(); - return true; - } catch (SQLiteException e) { // Updated exception package - // This exception is thrown if the password is wrong or the DB is not encrypted, - // which for this check, means it's not encrypted with the given key. + return true; // Successfully opened with the key, so it's encrypted. + } catch (SQLiteException e) { + // This is expected if the password (appId) is wrong or DB isn't encrypted. return false; } finally { if (db != null && db.isOpen()) { diff --git a/kommunicate/src/main/java/io/kommunicate/database/DatabaseMigrationHelper.kt b/kommunicate/src/main/java/io/kommunicate/database/DatabaseMigrationHelper.kt index d9578c2d..58c7635a 100644 --- a/kommunicate/src/main/java/io/kommunicate/database/DatabaseMigrationHelper.kt +++ b/kommunicate/src/main/java/io/kommunicate/database/DatabaseMigrationHelper.kt @@ -32,6 +32,12 @@ object DatabaseMigrationHelper { val password = MobiComKitClientService.getApplicationKey(AppContextService.getContext(context)) + // Check added to prevent crash in case of null or empty application key + if (password.isNullOrEmpty()) { + System.err.println("Migration failed: Application Key is missing. Is the SDK initialized?") + return + } + // Load SQLCipher libraries System.loadLibrary("sqlcipher")