@@ -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 ()) {
0 commit comments