Skip to content

Commit 2954a8e

Browse files
authored
Merge pull request #644 from Kommunicate-io/development
Release 2.14.4
2 parents 4dd55f5 + 804fd18 commit 2954a8e

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

kommunicate/src/main/java/io/kommunicate/commons/commons/core/utils/DBUtils.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,26 @@ public static boolean existsColumnInTable(SQLiteDatabase inDatabase, String inTa
5656

5757
public static boolean isDatabaseEncrypted(Context context, String dbName) {
5858
String appId = MobiComKitClientService.getApplicationKey(AppContextService.getContext(context));
59+
60+
// 💡 DEFENSIVE CHECK: Prevent crash if appId is null
61+
if (appId == null || appId.isEmpty()) {
62+
Log.e("DatabaseCheck", "Application ID is missing. Kommunicate SDK may not be initialized.");
63+
return false;
64+
}
65+
5966
File dbFile = context.getDatabasePath(dbName);
67+
if (!dbFile.exists()) {
68+
return false; // Database file doesn't exist, so it's not encrypted.
69+
}
6070

61-
// Attempt to open the database with the given password
6271
SQLiteDatabase db = null;
6372
try {
64-
// Corrected method call with byte array for password
73+
// Now it's safe to call getBytes()
6574
db = SQLiteDatabase.openDatabase(dbFile.getPath(), appId.getBytes(), null, SQLiteDatabase.OPEN_READONLY, null, null);
6675
db.close();
67-
return true;
68-
} catch (SQLiteException e) { // Updated exception package
69-
// This exception is thrown if the password is wrong or the DB is not encrypted,
70-
// which for this check, means it's not encrypted with the given key.
76+
return true; // Successfully opened with the key, so it's encrypted.
77+
} catch (SQLiteException e) {
78+
// This is expected if the password (appId) is wrong or DB isn't encrypted.
7179
return false;
7280
} finally {
7381
if (db != null && db.isOpen()) {

kommunicate/src/main/java/io/kommunicate/database/DatabaseMigrationHelper.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ object DatabaseMigrationHelper {
3232
val password =
3333
MobiComKitClientService.getApplicationKey(AppContextService.getContext(context))
3434

35+
// Check added to prevent crash in case of null or empty application key
36+
if (password.isNullOrEmpty()) {
37+
System.err.println("Migration failed: Application Key is missing. Is the SDK initialized?")
38+
return
39+
}
40+
3541
// Load SQLCipher libraries
3642
System.loadLibrary("sqlcipher")
3743

kommunicate/src/main/java/io/kommunicate/devkit/api/conversation/ChatIntentService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public void onCreate() {
4848

4949
@Override
5050
protected void onHandleWork(@NonNull Intent intent) {
51+
52+
// If the service is started by the system before a user has logged in,
53+
// the SDK won't have the necessary credentials. Abort early to prevent crashes.
54+
if (!MobiComUserPreference.getInstance(this).isLoggedIn()) {
55+
Utils.printLog(this, "ChatIntentService", "Service triggered, but user is not logged in. Aborting work.");
56+
return;
57+
}
58+
5159
boolean connectivityChange = intent.getBooleanExtra(AL_SYNC_ON_CONNECTIVITY, false);
5260
boolean timeChangeReceiver = intent.getBooleanExtra(AL_TIME_CHANGE_RECEIVER, false);
5361

0 commit comments

Comments
 (0)