Skip to content

Commit

Permalink
Merge pull request #3999 from kiwix/Fixes#3997
Browse files Browse the repository at this point in the history
Fixed: Opening with Kiwix a ZIM file (just after download) from Firefox fails.
  • Loading branch information
kelson42 authored Sep 21, 2024
2 parents 007e12c + 7078aad commit 7e6e826
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package org.kiwix.kiwixmobile.deeplinks

import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.core.content.FileProvider
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.accessibility.AccessibilityChecks
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Before
Expand All @@ -31,11 +35,13 @@ import org.junit.Test
import org.junit.jupiter.api.fail
import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.R.string
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.page.history.navigationHistory
import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
Expand Down Expand Up @@ -76,6 +82,7 @@ class DeepLinksTest : BaseActivityTest() {
loadZimFileInApplicationAndReturnSchemeTypeUri("file")?.let {
// Launch the activity to test the deep link
ActivityScenario.launch<KiwixMainActivity>(createDeepLinkIntent(it)).onActivity {}
clickOnCopy()
navigationHistory {
checkZimFileLoadedSuccessful(R.id.readerFragment)
assertZimFileLoaded() // check if the zim file successfully loaded
Expand All @@ -87,11 +94,20 @@ class DeepLinksTest : BaseActivityTest() {
}
}

private fun clickOnCopy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
testFlakyView({
onView(withText(string.action_copy)).perform(click())
})
}
}

@Test
fun contentTypeDeepLinkTest() {
loadZimFileInApplicationAndReturnSchemeTypeUri("content")?.let {
// Launch the activity to test the deep link
ActivityScenario.launch<KiwixMainActivity>(createDeepLinkIntent(it)).onActivity {}
clickOnCopy()
navigationHistory {
checkZimFileLoadedSuccessful(R.id.readerFragment)
assertZimFileLoaded() // check if the zim file successfully loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
}
}
)
showCopyMoveDialogForOpenedZimFileFromStorage()
}

private fun showCopyMoveDialogForOpenedZimFileFromStorage() {
val args = LocalLibraryFragmentArgs.fromBundle(requireArguments())
if (args.zimFileUri.isNotEmpty()) {
handleSelectedFileUri(args.zimFileUri.toUri())
}
requireArguments().clear()
}

private fun setUpSwipeRefreshLayout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import org.kiwix.kiwixmobile.core.R.string
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.navigate
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggle
import org.kiwix.kiwixmobile.core.extensions.canReadFile
import org.kiwix.kiwixmobile.core.extensions.coreMainActivity
Expand Down Expand Up @@ -309,13 +310,13 @@ class KiwixReaderFragment : CoreReaderFragment() {
when (it.scheme) {
"file" -> {
Handler(Looper.getMainLooper()).postDelayed({
openAndSaveZimFileInLocalLibrary(it.toFile())
handleZimFileUri(it)
}, 300)
}

"content" -> {
Handler(Looper.getMainLooper()).postDelayed({
getZimFileFromUri(it)?.let(::openAndSaveZimFileInLocalLibrary)
handleZimFileUri(it)
}, 300)
}

Expand All @@ -325,6 +326,19 @@ class KiwixReaderFragment : CoreReaderFragment() {
return ShouldCall
}

private fun handleZimFileUri(uri: Uri) {
if (sharedPreferenceUtil?.isPlayStoreBuildWithAndroid11OrAbove() == true) {
clearIntentDataAndAction()
requireActivity().navigate(
KiwixReaderFragmentDirections.actionNavigationReaderToNavigationLibrary()
.apply { zimFileUri = "$uri" }
)
} else {
val file = if (uri.scheme == "file") uri.toFile() else getZimFileFromUri(uri)
file?.let(::openAndSaveZimFileInLocalLibrary)
}
}

private fun openAndSaveZimFileInLocalLibrary(file: File) {
val zimReaderSource = ZimReaderSource(file)
if (zimReaderSource.canOpenInLibkiwix()) {
Expand All @@ -342,9 +356,14 @@ class KiwixReaderFragment : CoreReaderFragment() {
} else {
activity.toast(R.string.cannot_open_file)
}
clearIntentDataAndAction()
}

private fun clearIntentDataAndAction() {
// if used once then clear it to avoid affecting any other functionality
// of the application.
requireActivity().intent.action = null
requireActivity().intent.data = null
}

private fun getZimFileFromUri(
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/navigation/kiwix_nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
<action
android:id="@+id/action_libraryFragment_to_localFileTransferFragment"
app:destination="@id/localFileTransferFragment" />
<argument
android:name="zimFileUri"
android:defaultValue=""
app:argType="string" />
</fragment>

<fragment
Expand Down

0 comments on commit 7e6e826

Please sign in to comment.