Skip to content

Commit

Permalink
Fixed: Version 3.9.1 experienced occasional crashes when setting the …
Browse files Browse the repository at this point in the history
…SuggestionSearcher.

* The crashes occurred in the Play Store version due to insufficient permissions for opening a file via `fileDescriptor`. To address this issue, we have modified the implementation to use the `file` instead of `fileDescriptor`. This adjustment ensures smooth operation in the non-Play Store version where we have permission to open a file via its path. In the Play Store version, it will display a proper error message to the user, preventing errors thrown by libkiwix.
  • Loading branch information
MohitMaliDeveloper committed Feb 5, 2024
1 parent 9b3bfa4 commit 01e23cf
Showing 1 changed file with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggle
import org.kiwix.kiwixmobile.core.extensions.canReadFile
import org.kiwix.kiwixmobile.core.extensions.coreMainActivity
import org.kiwix.kiwixmobile.core.extensions.isFileExist
import org.kiwix.kiwixmobile.core.extensions.setBottomMarginToFragmentContainerView
Expand All @@ -56,7 +57,6 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.TAG_CURRENT_FILE
import org.kiwix.kiwixmobile.core.utils.TAG_KIWIX
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.getAssetFileDescriptorFromUri
import java.io.File

private const val HIDE_TAB_SWITCHER_DELAY: Long = 300
Expand Down Expand Up @@ -287,6 +287,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
newMainPageTab()
}

@Suppress("NestedBlockDepth")
override fun onNewIntent(
intent: Intent,
activity: AppCompatActivity
Expand All @@ -296,12 +297,12 @@ class KiwixReaderFragment : CoreReaderFragment() {
when (it.scheme) {
"file" -> openZimFile(it.toFile())
"content" -> {
// pass this uri to zimFileReader, which is necessary for saving
// notes, bookmarks, history, and reopening the same ZIM file after the app closes.
getAssetFileDescriptorFromUri(activity, it)?.let { assetFileDescriptor ->
openZimFile(null, assetFileDescriptor = assetFileDescriptor, filePath = "$it")
} ?: kotlin.run {
activity.toast(R.string.cannot_open_file)
getZimFileFromUri(it)?.let { zimFile ->
if (zimFile.canReadFile()) {
openZimFile(zimFile)
} else {
activity.toast(R.string.cannot_open_file)
}
}
}

Expand All @@ -311,6 +312,25 @@ class KiwixReaderFragment : CoreReaderFragment() {
return ShouldCall
}

private fun getZimFileFromUri(
uri: Uri
): File? {
val filePath = FileUtils.getLocalFilePathByUri(
requireActivity().applicationContext, uri
)
if (filePath == null || !File(filePath).isFileExist()) {
activity.toast(R.string.error_file_not_found)
return null
}
val file = File(filePath)
return if (!FileUtils.isValidZimFile(file.path)) {
activity.toast(R.string.error_file_invalid)
null
} else {
file
}
}

private fun setBottomMarginToNavHostContainer(margin: Int) {
coreMainActivity.navHostContainer
.setBottomMarginToFragmentContainerView(margin)
Expand Down

0 comments on commit 01e23cf

Please sign in to comment.