From 9d574e381ab2fd71e156e6b6025e5a8bd538477c Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Sat, 28 Oct 2023 13:02:20 +0530 Subject: [PATCH 01/10] fix crash on api below 24 --- .../caching/testing/FakeAssetRepository.kt | 43 +++++++++++++++---- .../util/locale/AndroidLocaleFactory.kt | 29 ++++++++++--- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index 0b751a91339..146ec03a8f7 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -1,5 +1,6 @@ package org.oppia.android.util.caching.testing +import android.os.Build import com.google.protobuf.MessageLite import org.oppia.android.util.caching.AssetRepository import org.oppia.android.util.caching.AssetRepositoryImpl @@ -66,17 +67,43 @@ class FakeAssetRepository @Inject constructor( } private fun loadTextFile(assetName: String): String { - return trackedAssets.computeIfAbsent(assetName) { - prodImpl.loadTextFileFromLocalAssets(assetName) - } as? String ?: error("Asset doesn't exist: $assetName") + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + trackedAssets.computeIfAbsent(assetName) { + prodImpl.loadTextFileFromLocalAssets(assetName) + } as? String ?: error("Asset doesn't exist: $assetName") + } else { + val textData = trackedAssets[assetName] + return if(textData != null){ + textData as? String ?: error("Asset doesn't exist: $assetName") + } else{ + val dataFromFile = prodImpl.loadTextFileFromLocalAssets(assetName) + trackedAssets.putIfAbsent(assetName , dataFromFile) + dataFromFile as? String ?: error("Asset doesn't exist: $assetName") + } + } } private fun loadProtoFile(assetName: String, defaultMessage: T): T? { - return trackedAssets.computeIfAbsent(assetName) { - prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) - }?.let { protoAsset -> - @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. - protoAsset as? T + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + trackedAssets.computeIfAbsent(assetName) { + prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) + }?.let { protoAsset -> + @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. + protoAsset as? T + } + } else { + val protoFile = trackedAssets[assetName] + if(protoFile != null){ + @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. + return protoFile as? T + } + else{ + val loadedProtoFile = prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) + if (loadedProtoFile != null) { + trackedAssets.putIfAbsent(assetName , loadedProtoFile) + } + loadedProtoFile + } } } diff --git a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt index ffe3e186646..22a266b897a 100644 --- a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt +++ b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt @@ -55,13 +55,28 @@ class AndroidLocaleFactory @Inject constructor( */ fun createAndroidLocale(localeContext: OppiaLocaleContext): Locale { // Note: computeIfAbsent is used here instead of getOrPut to ensure atomicity across multiple - // threads calling into this create function. - return memoizedLocales.computeIfAbsent(localeContext) { - val chooser = profileChooserSelector.findBestChooser(localeContext) - val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) - val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) - val proposal = chooser.findBestProposal(primaryLocaleSource, fallbackLocaleSource) - return@computeIfAbsent proposal.computedLocale + // threads calling into this create function. ( computeIfAbsent is introduced in API Level 24 ) + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + memoizedLocales.computeIfAbsent(localeContext) { + val chooser = profileChooserSelector.findBestChooser(localeContext) + val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) + val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) + val proposal = chooser.findBestProposal(primaryLocaleSource, fallbackLocaleSource) + return@computeIfAbsent proposal.computedLocale + } + } else { + // Note : Using get/PutIfAbsent For API Level below 24 as computeIfAbsent is introduced in API Level 24 + val locale = memoizedLocales[localeContext] + return if(locale != null){ + locale + } else{ + val chooser = profileChooserSelector.findBestChooser(localeContext) + val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) + val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) + val proposal = chooser.findBestProposal(primaryLocaleSource, fallbackLocaleSource) + memoizedLocales.putIfAbsent(localeContext , proposal.computedLocale) + proposal.computedLocale + } } } From c4f9e49626f5aa5246fd90d7248a17d0deccef74 Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Sat, 28 Oct 2023 13:05:32 +0530 Subject: [PATCH 02/10] fix crash on api level below 24 --- .../util/caching/testing/FakeAssetRepository.kt | 15 +++++++-------- .../android/util/locale/AndroidLocaleFactory.kt | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index 146ec03a8f7..5c019afaf90 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -73,11 +73,11 @@ class FakeAssetRepository @Inject constructor( } as? String ?: error("Asset doesn't exist: $assetName") } else { val textData = trackedAssets[assetName] - return if(textData != null){ + return if (textData != null) { textData as? String ?: error("Asset doesn't exist: $assetName") - } else{ + } else { val dataFromFile = prodImpl.loadTextFileFromLocalAssets(assetName) - trackedAssets.putIfAbsent(assetName , dataFromFile) + trackedAssets.putIfAbsent(assetName, dataFromFile) dataFromFile as? String ?: error("Asset doesn't exist: $assetName") } } @@ -93,14 +93,13 @@ class FakeAssetRepository @Inject constructor( } } else { val protoFile = trackedAssets[assetName] - if(protoFile != null){ + if (protoFile != null) { @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. return protoFile as? T - } - else{ - val loadedProtoFile = prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) + } else { + val loadedProtoFile = prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) if (loadedProtoFile != null) { - trackedAssets.putIfAbsent(assetName , loadedProtoFile) + trackedAssets.putIfAbsent(assetName, loadedProtoFile) } loadedProtoFile } diff --git a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt index 22a266b897a..7b1ba053a96 100644 --- a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt +++ b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt @@ -67,14 +67,14 @@ class AndroidLocaleFactory @Inject constructor( } else { // Note : Using get/PutIfAbsent For API Level below 24 as computeIfAbsent is introduced in API Level 24 val locale = memoizedLocales[localeContext] - return if(locale != null){ + return if (locale != null) { locale - } else{ + } else { val chooser = profileChooserSelector.findBestChooser(localeContext) val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) val proposal = chooser.findBestProposal(primaryLocaleSource, fallbackLocaleSource) - memoizedLocales.putIfAbsent(localeContext , proposal.computedLocale) + memoizedLocales.putIfAbsent(localeContextg, proposal.computedLocale) proposal.computedLocale } } From bc0b31d91575b9de6a0c993b07475ad339f98626 Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Sat, 28 Oct 2023 13:06:24 +0530 Subject: [PATCH 03/10] fix app not opening on api level below 24 --- .../java/org/oppia/android/util/locale/AndroidLocaleFactory.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt index 7b1ba053a96..c2bda858f2d 100644 --- a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt +++ b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt @@ -74,7 +74,7 @@ class AndroidLocaleFactory @Inject constructor( val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) val proposal = chooser.findBestProposal(primaryLocaleSource, fallbackLocaleSource) - memoizedLocales.putIfAbsent(localeContextg, proposal.computedLocale) + memoizedLocales.putIfAbsent(localeContext, proposal.computedLocale) proposal.computedLocale } } From e73db32f6fdf523ee1d44fe17027e13a5348ba9d Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Wed, 1 Nov 2023 10:09:59 +0530 Subject: [PATCH 04/10] fix crash on api below 24 : optimize code for use of nested else branches --- .../caching/testing/FakeAssetRepository.kt | 32 +++++++++---------- .../util/locale/AndroidLocaleFactory.kt | 6 ++-- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index 5c019afaf90..8cdd90abc06 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -72,14 +72,14 @@ class FakeAssetRepository @Inject constructor( prodImpl.loadTextFileFromLocalAssets(assetName) } as? String ?: error("Asset doesn't exist: $assetName") } else { - val textData = trackedAssets[assetName] - return if (textData != null) { - textData as? String ?: error("Asset doesn't exist: $assetName") - } else { - val dataFromFile = prodImpl.loadTextFileFromLocalAssets(assetName) - trackedAssets.putIfAbsent(assetName, dataFromFile) - dataFromFile as? String ?: error("Asset doesn't exist: $assetName") + val textData = trackedAssets[assetName] ?: synchronized(trackedAssets) { + trackedAssets[assetName] ?: run { + val dataFromFile = prodImpl.loadTextFileFromLocalAssets(assetName) + trackedAssets[assetName] = dataFromFile + dataFromFile + } } + return textData as? String ?: error("Asset doesn't exist: $assetName") } } @@ -92,17 +92,17 @@ class FakeAssetRepository @Inject constructor( protoAsset as? T } } else { - val protoFile = trackedAssets[assetName] - if (protoFile != null) { - @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. - return protoFile as? T - } else { - val loadedProtoFile = prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) - if (loadedProtoFile != null) { - trackedAssets.putIfAbsent(assetName, loadedProtoFile) + val protoFile = trackedAssets[assetName] ?: synchronized(trackedAssets) { + trackedAssets[assetName] ?: kotlin.run { + val loadedProtoFile = prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) + if (loadedProtoFile != null) { + trackedAssets.putIfAbsent(assetName, loadedProtoFile) + } + loadedProtoFile } - loadedProtoFile } + @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. + return protoFile as? T } } diff --git a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt index c2bda858f2d..31e7bb13681 100644 --- a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt +++ b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt @@ -66,10 +66,7 @@ class AndroidLocaleFactory @Inject constructor( } } else { // Note : Using get/PutIfAbsent For API Level below 24 as computeIfAbsent is introduced in API Level 24 - val locale = memoizedLocales[localeContext] - return if (locale != null) { - locale - } else { + val locale = memoizedLocales[localeContext] ?: synchronized(memoizedLocales) { val chooser = profileChooserSelector.findBestChooser(localeContext) val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) @@ -77,6 +74,7 @@ class AndroidLocaleFactory @Inject constructor( memoizedLocales.putIfAbsent(localeContext, proposal.computedLocale) proposal.computedLocale } + return locale } } From f57c3ff1d0144c6e799a5d97c29b4a50d5fb6871 Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Fri, 3 Nov 2023 11:57:52 +0530 Subject: [PATCH 05/10] fix crash on api below 24 : optimize code implementation as per suggestions --- .../caching/testing/FakeAssetRepository.kt | 37 ++++--------------- .../util/locale/AndroidLocaleFactory.kt | 20 +++------- 2 files changed, 12 insertions(+), 45 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index 8cdd90abc06..d2de64ee658 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -67,43 +67,20 @@ class FakeAssetRepository @Inject constructor( } private fun loadTextFile(assetName: String): String { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - trackedAssets.computeIfAbsent(assetName) { + return (trackedAssets[assetName] ?: synchronized(trackedAssets) { + trackedAssets.getOrPut(assetName) { prodImpl.loadTextFileFromLocalAssets(assetName) - } as? String ?: error("Asset doesn't exist: $assetName") - } else { - val textData = trackedAssets[assetName] ?: synchronized(trackedAssets) { - trackedAssets[assetName] ?: run { - val dataFromFile = prodImpl.loadTextFileFromLocalAssets(assetName) - trackedAssets[assetName] = dataFromFile - dataFromFile - } } - return textData as? String ?: error("Asset doesn't exist: $assetName") - } + }) as? String ?: error("Asset doesn't exist: $assetName") } private fun loadProtoFile(assetName: String, defaultMessage: T): T? { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - trackedAssets.computeIfAbsent(assetName) { + @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. + return (trackedAssets[assetName] ?: synchronized(trackedAssets) { + trackedAssets.getOrPut(assetName) { prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) - }?.let { protoAsset -> - @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. - protoAsset as? T - } - } else { - val protoFile = trackedAssets[assetName] ?: synchronized(trackedAssets) { - trackedAssets[assetName] ?: kotlin.run { - val loadedProtoFile = prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) - if (loadedProtoFile != null) { - trackedAssets.putIfAbsent(assetName, loadedProtoFile) - } - loadedProtoFile - } } - @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. - return protoFile as? T - } + }) as? T } private fun fetchLoadedProtoFile(assetName: String) = trackedAssets[assetName] as? MessageLite diff --git a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt index 31e7bb13681..ca6ac4056d3 100644 --- a/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt +++ b/utility/src/main/java/org/oppia/android/util/locale/AndroidLocaleFactory.kt @@ -54,27 +54,17 @@ class AndroidLocaleFactory @Inject constructor( * @return the best [Locale] to match the provided [localeContext] */ fun createAndroidLocale(localeContext: OppiaLocaleContext): Locale { - // Note: computeIfAbsent is used here instead of getOrPut to ensure atomicity across multiple - // threads calling into this create function. ( computeIfAbsent is introduced in API Level 24 ) - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - memoizedLocales.computeIfAbsent(localeContext) { + // Note: Here we acquire lock on memoizedLocales if the Locale object is not created yet , + // this is to avoid unnecessary computation for the same object when the function is called + // by multiple threads + return memoizedLocales[localeContext] ?: synchronized(memoizedLocales) { + memoizedLocales.getOrPut(localeContext) { val chooser = profileChooserSelector.findBestChooser(localeContext) val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) val proposal = chooser.findBestProposal(primaryLocaleSource, fallbackLocaleSource) - return@computeIfAbsent proposal.computedLocale - } - } else { - // Note : Using get/PutIfAbsent For API Level below 24 as computeIfAbsent is introduced in API Level 24 - val locale = memoizedLocales[localeContext] ?: synchronized(memoizedLocales) { - val chooser = profileChooserSelector.findBestChooser(localeContext) - val primaryLocaleSource = LocaleSource.createFromPrimary(localeContext) - val fallbackLocaleSource = LocaleSource.createFromFallback(localeContext) - val proposal = chooser.findBestProposal(primaryLocaleSource, fallbackLocaleSource) - memoizedLocales.putIfAbsent(localeContext, proposal.computedLocale) proposal.computedLocale } - return locale } } From 5a983cb8a52493173e9d936e2e92c53a9f66f8ba Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Fri, 3 Nov 2023 11:59:55 +0530 Subject: [PATCH 06/10] fix crash on api below 24 : optimize code implementation as per suggestions --- .../caching/testing/FakeAssetRepository.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index d2de64ee658..f4116b5d6e7 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -1,6 +1,5 @@ package org.oppia.android.util.caching.testing -import android.os.Build import com.google.protobuf.MessageLite import org.oppia.android.util.caching.AssetRepository import org.oppia.android.util.caching.AssetRepositoryImpl @@ -67,20 +66,25 @@ class FakeAssetRepository @Inject constructor( } private fun loadTextFile(assetName: String): String { - return (trackedAssets[assetName] ?: synchronized(trackedAssets) { - trackedAssets.getOrPut(assetName) { - prodImpl.loadTextFileFromLocalAssets(assetName) + return ( + trackedAssets[assetName] ?: synchronized(trackedAssets) { + trackedAssets.getOrPut(assetName) { + prodImpl.loadTextFileFromLocalAssets(assetName) + } } - }) as? String ?: error("Asset doesn't exist: $assetName") + ) as? String ?: error("Asset doesn't exist: $assetName") + } private fun loadProtoFile(assetName: String, defaultMessage: T): T? { @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. - return (trackedAssets[assetName] ?: synchronized(trackedAssets) { - trackedAssets.getOrPut(assetName) { - prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) + return ( + trackedAssets[assetName] ?: synchronized(trackedAssets) { + trackedAssets.getOrPut(assetName) { + prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) + } } - }) as? T + ) as? T } private fun fetchLoadedProtoFile(assetName: String) = trackedAssets[assetName] as? MessageLite From f06b856bb85c1a64fa5545e06836922d1af66d23 Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Fri, 3 Nov 2023 12:01:13 +0530 Subject: [PATCH 07/10] fix crash on api below 24 : optimize code implementation as per suggestions --- .../oppia/android/util/caching/testing/FakeAssetRepository.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index f4116b5d6e7..e706075d700 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -73,7 +73,6 @@ class FakeAssetRepository @Inject constructor( } } ) as? String ?: error("Asset doesn't exist: $assetName") - } private fun loadProtoFile(assetName: String, defaultMessage: T): T? { From 1cfb2f7b2328df04331447a4f3d6bafcd2c44cb6 Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Sat, 4 Nov 2023 06:28:36 +0530 Subject: [PATCH 08/10] fix crash on api below 24 : optimize code implementation as per suggestions --- .../oppia/android/util/caching/testing/FakeAssetRepository.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index e706075d700..037280fd7e8 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -76,7 +76,7 @@ class FakeAssetRepository @Inject constructor( } private fun loadProtoFile(assetName: String, defaultMessage: T): T? { - @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit. + @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit return ( trackedAssets[assetName] ?: synchronized(trackedAssets) { trackedAssets.getOrPut(assetName) { From 09dd97693955717369fd1b8e329c18b2bf5d6a70 Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Tue, 7 Nov 2023 14:05:16 +0530 Subject: [PATCH 09/10] fix crash on api 23 : fix failing of tests --- .../util/caching/testing/FakeAssetRepository.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index 037280fd7e8..87e2d3ab80a 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -68,8 +68,9 @@ class FakeAssetRepository @Inject constructor( private fun loadTextFile(assetName: String): String { return ( trackedAssets[assetName] ?: synchronized(trackedAssets) { - trackedAssets.getOrPut(assetName) { - prodImpl.loadTextFileFromLocalAssets(assetName) + prodImpl.loadTextFileFromLocalAssets(assetName)?.let { loadedFile -> + trackedAssets.putIfAbsent(assetName, loadedFile) + loadedFile } } ) as? String ?: error("Asset doesn't exist: $assetName") @@ -79,8 +80,9 @@ class FakeAssetRepository @Inject constructor( @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit return ( trackedAssets[assetName] ?: synchronized(trackedAssets) { - trackedAssets.getOrPut(assetName) { - prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage) + prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage)?.let { loadedFile -> + trackedAssets.putIfAbsent(assetName, loadedFile) + loadedFile } } ) as? T From 136d8d6d3a8e4cebfa4f94919ba5993df8690931 Mon Sep 17 00:00:00 2001 From: Shaik-Sirajuddin Date: Wed, 8 Nov 2023 10:49:47 +0530 Subject: [PATCH 10/10] fix crash on api 23 : code optimization --- .../util/caching/testing/FakeAssetRepository.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt index 87e2d3ab80a..ea9bd7dbba8 100644 --- a/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt +++ b/utility/src/main/java/org/oppia/android/util/caching/testing/FakeAssetRepository.kt @@ -68,10 +68,11 @@ class FakeAssetRepository @Inject constructor( private fun loadTextFile(assetName: String): String { return ( trackedAssets[assetName] ?: synchronized(trackedAssets) { - prodImpl.loadTextFileFromLocalAssets(assetName)?.let { loadedFile -> - trackedAssets.putIfAbsent(assetName, loadedFile) - loadedFile - } + trackedAssets[assetName] ?: prodImpl.loadTextFileFromLocalAssets(assetName) + ?.let { loadedFile -> + trackedAssets.putIfAbsent(assetName, loadedFile) + loadedFile + } } ) as? String ?: error("Asset doesn't exist: $assetName") } @@ -80,7 +81,10 @@ class FakeAssetRepository @Inject constructor( @Suppress("UNCHECKED_CAST") // This should fail if the cast doesn't fit return ( trackedAssets[assetName] ?: synchronized(trackedAssets) { - prodImpl.maybeLoadProtoFromLocalAssets(assetName, defaultMessage)?.let { loadedFile -> + trackedAssets[assetName] ?: prodImpl.maybeLoadProtoFromLocalAssets( + assetName, + defaultMessage + )?.let { loadedFile -> trackedAssets.putIfAbsent(assetName, loadedFile) loadedFile }