From 4e86558b0e431ab5d9a5b5ca92764a43b8415bdb Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Fri, 20 Sep 2024 21:45:17 +0530 Subject: [PATCH 1/2] Fixed: Opening with Kiwix a ZIM file (just after download) from Firefox fails. * Fixed an issue where the ZIM file would not load when opened directly from the file manager. * Fixed the issue with opening a ZIM file from the browser's download screen. --- .../library/LocalLibraryFragment.kt | 9 ++++++++ .../destination/reader/KiwixReaderFragment.kt | 23 +++++++++++++++++-- .../main/res/navigation/kiwix_nav_graph.xml | 4 ++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt index 56cb3bf6ef..3fd12bdee7 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/LocalLibraryFragment.kt @@ -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() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt index 302822c4d2..692376f8f2 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/reader/KiwixReaderFragment.kt @@ -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 @@ -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) } @@ -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()) { @@ -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( diff --git a/app/src/main/res/navigation/kiwix_nav_graph.xml b/app/src/main/res/navigation/kiwix_nav_graph.xml index 8811982eab..6c0931027f 100644 --- a/app/src/main/res/navigation/kiwix_nav_graph.xml +++ b/app/src/main/res/navigation/kiwix_nav_graph.xml @@ -81,6 +81,10 @@ + Date: Fri, 20 Sep 2024 22:22:35 +0530 Subject: [PATCH 2/2] We improved the DeppLinksTest to test this functionality, so that we can avoid this type of error in the future. --- .../kiwix/kiwixmobile/deeplinks/DeepLinksTest.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/deeplinks/DeepLinksTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/deeplinks/DeepLinksTest.kt index 84becb2992..eaa3d80aca 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/deeplinks/DeepLinksTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/deeplinks/DeepLinksTest.kt @@ -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 @@ -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 @@ -76,6 +82,7 @@ class DeepLinksTest : BaseActivityTest() { loadZimFileInApplicationAndReturnSchemeTypeUri("file")?.let { // Launch the activity to test the deep link ActivityScenario.launch(createDeepLinkIntent(it)).onActivity {} + clickOnCopy() navigationHistory { checkZimFileLoadedSuccessful(R.id.readerFragment) assertZimFileLoaded() // check if the zim file successfully loaded @@ -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(createDeepLinkIntent(it)).onActivity {} + clickOnCopy() navigationHistory { checkZimFileLoadedSuccessful(R.id.readerFragment) assertZimFileLoaded() // check if the zim file successfully loaded