From 213fec0a359dd3a04ee3aa62a697b5f548def856 Mon Sep 17 00:00:00 2001 From: Aleem Azhar Date: Wed, 18 Sep 2024 12:40:54 +0500 Subject: [PATCH 1/5] Crash after launch fixed --- .../fhircore/engine/util/SecureSharedPreference.kt | 13 ++++++++++++- android/quest/src/main/AndroidManifest.xml | 2 ++ android/quest/src/main/res/xml/backup_rules.xml | 4 ++++ .../src/main/res/xml/data_extraction_rules.xml | 6 ++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 android/quest/src/main/res/xml/backup_rules.xml create mode 100644 android/quest/src/main/res/xml/data_extraction_rules.xml diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt index 0a237208b7..2ac97c81e6 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt @@ -17,6 +17,7 @@ package org.smartregister.fhircore.engine.util import android.content.Context +import android.content.SharedPreferences import androidx.core.content.edit import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey @@ -34,8 +35,18 @@ import org.smartregister.fhircore.engine.util.extension.encodeJson @Singleton class SecureSharedPreference @Inject constructor(@ApplicationContext val context: Context) { + private val secureSharedPreferences: SharedPreferences by lazy { + initEncryptedSharedPreferences() + } + + private fun initEncryptedSharedPreferences() = + runCatching { createEncryptedSharedPreferences() } + .getOrElse { + resetSharedPrefs() + createEncryptedSharedPreferences() + } - private val secureSharedPreferences = + private fun createEncryptedSharedPreferences() = EncryptedSharedPreferences.create( context, SECURE_STORAGE_FILE_NAME, diff --git a/android/quest/src/main/AndroidManifest.xml b/android/quest/src/main/AndroidManifest.xml index ff169b7773..db31e08c28 100644 --- a/android/quest/src/main/AndroidManifest.xml +++ b/android/quest/src/main/AndroidManifest.xml @@ -20,6 +20,8 @@ android:theme="@style/AppTheme.NoActionBar" android:usesCleartextTraffic="false" android:windowSoftInputMode="adjustResize" + android:fullBackupContent="@xml/backup_rules" + android:dataExtractionRules="@xml/data_extraction_rules" tools:ignore="UnusedAttribute,LockedOrientationActivity" tools:replace="android:theme"> + + + diff --git a/android/quest/src/main/res/xml/data_extraction_rules.xml b/android/quest/src/main/res/xml/data_extraction_rules.xml new file mode 100644 index 0000000000..b95dbd6b7b --- /dev/null +++ b/android/quest/src/main/res/xml/data_extraction_rules.xml @@ -0,0 +1,6 @@ + + + + + + From 30817cd9956af59030f84839e5a071541867bff9 Mon Sep 17 00:00:00 2001 From: Aleem Azhar Date: Wed, 18 Sep 2024 13:43:09 +0500 Subject: [PATCH 2/5] Bump version --- android/buildSrc/src/main/kotlin/BuildConfigs.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/buildSrc/src/main/kotlin/BuildConfigs.kt b/android/buildSrc/src/main/kotlin/BuildConfigs.kt index 78176a7a61..87d4f5517d 100644 --- a/android/buildSrc/src/main/kotlin/BuildConfigs.kt +++ b/android/buildSrc/src/main/kotlin/BuildConfigs.kt @@ -2,7 +2,7 @@ object BuildConfigs { const val minSdk = 26 const val compileSdk = 34 const val targetSdk = 34 - const val versionCode = 11 + const val versionCode = 12 const val versionName = "2.0.0" const val applicationId = "org.smartregister.opensrp" const val jvmToolchain = 17 From d3e997c155893dd53aea214da2d00c25bc62558d Mon Sep 17 00:00:00 2001 From: Aleem Azhar Date: Wed, 18 Sep 2024 13:58:09 +0500 Subject: [PATCH 3/5] reset version code --- android/buildSrc/src/main/kotlin/BuildConfigs.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/buildSrc/src/main/kotlin/BuildConfigs.kt b/android/buildSrc/src/main/kotlin/BuildConfigs.kt index 87d4f5517d..78176a7a61 100644 --- a/android/buildSrc/src/main/kotlin/BuildConfigs.kt +++ b/android/buildSrc/src/main/kotlin/BuildConfigs.kt @@ -2,7 +2,7 @@ object BuildConfigs { const val minSdk = 26 const val compileSdk = 34 const val targetSdk = 34 - const val versionCode = 12 + const val versionCode = 11 const val versionName = "2.0.0" const val applicationId = "org.smartregister.opensrp" const val jvmToolchain = 17 From badf1375b29cac7825aa4c426f223e06bc7001dc Mon Sep 17 00:00:00 2001 From: Aleem Azhar Date: Wed, 18 Sep 2024 15:08:15 +0500 Subject: [PATCH 4/5] Backup disabled --- .../fhircore/engine/di/FhirEngineModule.kt | 2 +- android/quest/src/main/AndroidManifest.xml | 5 +++-- android/quest/src/main/res/xml/backup_rules.xml | 4 ---- .../src/main/res/xml/data_extraction_rules.xml | 13 ++++++++++++- 4 files changed, 16 insertions(+), 8 deletions(-) delete mode 100644 android/quest/src/main/res/xml/backup_rules.xml diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt index f39ef440b6..2b6afdaab7 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirEngineModule.kt @@ -55,7 +55,7 @@ class FhirEngineModule { FhirEngineProvider.init( FhirEngineConfiguration( enableEncryptionIfSupported = !BuildConfig.DEBUG, - databaseErrorStrategy = DatabaseErrorStrategy.UNSPECIFIED, + databaseErrorStrategy = DatabaseErrorStrategy.RECREATE_AT_OPEN, ServerConfiguration( baseUrl = configService.provideAuthConfiguration().fhirServerBaseUrl, authenticator = tokenAuthenticator, diff --git a/android/quest/src/main/AndroidManifest.xml b/android/quest/src/main/AndroidManifest.xml index db31e08c28..2c2d7676f4 100644 --- a/android/quest/src/main/AndroidManifest.xml +++ b/android/quest/src/main/AndroidManifest.xml @@ -20,10 +20,11 @@ android:theme="@style/AppTheme.NoActionBar" android:usesCleartextTraffic="false" android:windowSoftInputMode="adjustResize" - android:fullBackupContent="@xml/backup_rules" + android:allowBackup="false" + android:fullBackupContent="false" android:dataExtractionRules="@xml/data_extraction_rules" tools:ignore="UnusedAttribute,LockedOrientationActivity" - tools:replace="android:theme"> + tools:replace="android:allowBackup,android:theme"> diff --git a/android/quest/src/main/res/xml/backup_rules.xml b/android/quest/src/main/res/xml/backup_rules.xml deleted file mode 100644 index e2c93b606e..0000000000 --- a/android/quest/src/main/res/xml/backup_rules.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/android/quest/src/main/res/xml/data_extraction_rules.xml b/android/quest/src/main/res/xml/data_extraction_rules.xml index b95dbd6b7b..805fc3affa 100644 --- a/android/quest/src/main/res/xml/data_extraction_rules.xml +++ b/android/quest/src/main/res/xml/data_extraction_rules.xml @@ -1,6 +1,17 @@ - + + + + + + + + + + + + From 3530a330dae7fa57fdad40ac5c5f50f098eda84e Mon Sep 17 00:00:00 2001 From: Aleem Azhar Date: Fri, 20 Sep 2024 15:29:53 +0500 Subject: [PATCH 5/5] Added the unit tests --- .../engine/util/SecureSharedPreference.kt | 6 ++-- .../engine/util/SecureSharedPreferenceTest.kt | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt index 2ac97c81e6..9fcdc526e8 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt @@ -39,14 +39,16 @@ class SecureSharedPreference @Inject constructor(@ApplicationContext val context initEncryptedSharedPreferences() } - private fun initEncryptedSharedPreferences() = + @VisibleForTesting + fun initEncryptedSharedPreferences() = runCatching { createEncryptedSharedPreferences() } .getOrElse { resetSharedPrefs() createEncryptedSharedPreferences() } - private fun createEncryptedSharedPreferences() = + @VisibleForTesting + fun createEncryptedSharedPreferences() = EncryptedSharedPreferences.create( context, SECURE_STORAGE_FILE_NAME, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/util/SecureSharedPreferenceTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/util/SecureSharedPreferenceTest.kt index eb0a10a63a..b015f3464c 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/util/SecureSharedPreferenceTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/util/SecureSharedPreferenceTest.kt @@ -48,6 +48,34 @@ internal class SecureSharedPreferenceTest : RobolectricTest() { secureSharedPreference = spyk(SecureSharedPreference(application)) } + @Test + fun testInitEncryptedSharedPreferences() { + val result = secureSharedPreference.initEncryptedSharedPreferences() + Assert.assertNotNull(result) + } + + @Test + fun testInitEncryptedSharedPreferencesHandlesException() { + every { secureSharedPreference.createEncryptedSharedPreferences() } throws + RuntimeException("Exception") andThenAnswer + { + callOriginal() + } + + val result = secureSharedPreference.initEncryptedSharedPreferences() + + Assert.assertNotNull(result) + + verify(exactly = 2) { secureSharedPreference.createEncryptedSharedPreferences() } + verify(exactly = 1) { secureSharedPreference.resetSharedPrefs() } + } + + @Test + fun testCreateEncryptedSharedPreferences() { + val result = secureSharedPreference.createEncryptedSharedPreferences() + Assert.assertNotNull(result) + } + @Test fun testSaveCredentialsAndRetrieveSessionToken() { secureSharedPreference.saveCredentials(username = "userName", password = "!@#$".toCharArray())