Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Opening with Kiwix a ZIM file (just after download) from Firefox fails. #3999

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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 @@
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 @@
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 @@
} else {
activity.toast(R.string.cannot_open_file)
}
clearIntentDataAndAction()
}

Check warning on line 360 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#L359-L360

Added lines #L359 - L360 were not covered by tests

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
Loading