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 authored and kelson42 committed Feb 7, 2024
1 parent 1f07863 commit 2875037
Showing 1 changed file with 25 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 @@ -296,12 +296,8 @@ 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 ->
openZimFile(zimFile)

Check warning on line 300 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt#L300

Added line #L300 was not covered by tests
}
}

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

private fun getZimFileFromUri(
uri: Uri
): File? {
val filePath = FileUtils.getLocalFilePathByUri(
requireActivity().applicationContext, uri

Check warning on line 314 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt#L313-L314

Added lines #L313 - L314 were not covered by tests
)
if (filePath == null || !File(filePath).isFileExist()) {
activity.toast(R.string.error_file_not_found)
return null

Check warning on line 318 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt#L317-L318

Added lines #L317 - L318 were not covered by tests
}
val file = File(filePath)

Check warning on line 320 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt#L320

Added line #L320 was not covered by tests
return if (!FileUtils.isValidZimFile(file.path)) {
activity.toast(R.string.error_file_invalid)
null

Check warning on line 323 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt#L322-L323

Added lines #L322 - L323 were not covered by tests
} else if (!file.canReadFile()) {
activity.toast(R.string.cannot_open_file)
null

Check warning on line 326 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt#L325-L326

Added lines #L325 - L326 were not covered by tests
} else {
file

Check warning on line 328 in app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt#L328

Added line #L328 was not covered by tests
}
}

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

0 comments on commit 2875037

Please sign in to comment.