From 5630f96951c468a31825303235ddaef2718caf5d Mon Sep 17 00:00:00 2001 From: MohitMali Date: Mon, 18 Apr 2022 18:06:22 +0530 Subject: [PATCH 1/8] File picker added --- .../library/LocalLibraryFragment.kt | 62 +++++++++++++++++++ .../layout/fragment_destination_library.xml | 12 ++++ .../kiwix/kiwixmobile/core/utils/Constants.kt | 6 ++ .../kiwixmobile/core/utils/files/FileUtils.kt | 24 +++++++ core/src/main/res/values/strings.xml | 1 + 5 files changed, 105 insertions(+) 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 e97562289..b08499dee 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 @@ -19,7 +19,10 @@ package org.kiwix.kiwixmobile.nav.destination.library import android.Manifest +import android.content.ActivityNotFoundException +import android.content.Intent import android.content.pm.PackageManager +import android.net.Uri import android.os.Build import android.os.Bundle import android.os.Environment @@ -33,6 +36,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode import androidx.appcompat.widget.Toolbar import androidx.core.content.ContextCompat +import androidx.core.net.toUri import androidx.fragment.app.FragmentActivity import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider @@ -42,6 +46,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.fragment_destination_library.file_management_no_files import kotlinx.android.synthetic.main.fragment_destination_library.go_to_downloads_button_no_files +import kotlinx.android.synthetic.main.fragment_destination_library.select_file import kotlinx.android.synthetic.main.fragment_destination_library.zim_swiperefresh import kotlinx.android.synthetic.main.fragment_destination_library.zimfilelist import org.kiwix.kiwixmobile.R @@ -53,11 +58,13 @@ import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.navigateToSettings +import org.kiwix.kiwixmobile.core.utils.FILE_SELECT_CODE import org.kiwix.kiwixmobile.core.utils.LanguageUtils import org.kiwix.kiwixmobile.core.utils.REQUEST_STORAGE_PERMISSION import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog +import org.kiwix.kiwixmobile.core.utils.files.FileUtils import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BookOnDiskDelegate import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem @@ -67,6 +74,7 @@ import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.Re import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestNavigateTo import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestSelect import org.kiwix.kiwixmobile.zim_manager.fileselect_view.FileSelectListState +import java.io.File import javax.inject.Inject private const val WAS_IN_ACTION_MODE = "WAS_IN_ACTION_MODE" @@ -139,6 +147,60 @@ class LocalLibraryFragment : BaseFragment() { go_to_downloads_button_no_files.setOnClickListener { offerAction(FileSelectActions.UserClickedDownloadBooksButton) } + + select_file.setOnClickListener { + showFileChooser() + } + } + + private fun showFileChooser() { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + try { + startActivityForResult( + Intent.createChooser(intent, "Select a zim file"), + FILE_SELECT_CODE + ) + } catch (ex: ActivityNotFoundException) { + ex.printStackTrace() + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + when (requestCode) { + FILE_SELECT_CODE -> { + data?.data?.let(::isValidFile) + } + else -> super.onActivityResult(requestCode, resultCode, data) + } + } + + private fun isValidFile(uri: Uri) { + val filePath = FileUtils.getLocalFilePathByUri( + requireActivity().applicationContext, uri + ) + if (filePath == null || !File(filePath).exists()) { + activity.toast(R.string.error_file_not_found) + return + } + val file = File(filePath) + if (!FileUtils.isValidFile(file.path)) { + activity.toast(R.string.error_file_invalid) + return + } + navigateToReaderFragment(file) + } + + private fun navigateToReaderFragment(file: File) { + if (!file.canRead()) { + activity.toast(R.string.error_file_not_found) + } else { + activity?.navigate( + LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader() + .apply { zimFileUri = file.toUri().toString() } + ) + } } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { diff --git a/app/src/main/res/layout/fragment_destination_library.xml b/app/src/main/res/layout/fragment_destination_library.xml index 4cb7694fc..388792cd2 100644 --- a/app/src/main/res/layout/fragment_destination_library.xml +++ b/app/src/main/res/layout/fragment_destination_library.xml @@ -77,5 +77,17 @@ app:layout_constraintStart_toStartOf="@+id/file_management_no_files" app:layout_constraintTop_toBottomOf="@+id/file_management_no_files" /> + diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.kt index 308849cc3..30e49ae18 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/Constants.kt @@ -23,6 +23,7 @@ const val CONTACT_EMAIL_ADDRESS = "android@kiwix.org" // Request stuff const val REQUEST_STORAGE_PERMISSION = 1 const val REQUEST_WRITE_STORAGE_PERMISSION_ADD_NOTE = 3 +const val REQUEST_SELECT_FOLDER_PERMISSION = 4 // Tags const val TAG_FILE_SEARCHED = "searchedarticle" @@ -37,3 +38,8 @@ const val TAG_FROM_TAB_SWITCHER = "fromtabswitcher" const val EXTRA_IS_WIDGET_VOICE = "isWidgetVoice" const val HOTSPOT_SERVICE_CHANNEL_ID = "hotspotService" const val OLD_PROVIDER_DOMAIN = "org.kiwix.zim.base" + +// For Storage select dialog +const val INTERNAL_SELECT_POSITION = 0 +const val EXTERNAL_SELECT_POSITION = 1 +const val FILE_SELECT_CODE = 5 diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index dadb75e95..cc076714c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -23,12 +23,14 @@ import android.net.Uri import android.os.Environment import android.provider.DocumentsContract import android.util.Log +import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.downloader.ChunkUtils import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.core.extensions.get import java.io.BufferedReader import java.io.File import java.io.IOException +import java.lang.Exception import java.util.ArrayList object FileUtils { @@ -99,6 +101,11 @@ object FileUtils { if (documentId[0] == "primary") { return "${Environment.getExternalStorageDirectory()}/${documentId[1]}" } + return try { + "${getSdCardMainPath(context)}/${documentId[1]}" + } catch (e: Exception) { + null + } } else if ("com.android.providers.downloads.documents" == uri.authority) return try { documentProviderContentQuery(context, uri) @@ -228,4 +235,21 @@ object FileUtils { } catch (e: IOException) { "".also { e.printStackTrace() } } + + @JvmStatic fun isValidFile(filePath: String): Boolean { + if (filePath.endsWith(".zim") || filePath.endsWith(".zimaa")) { + return true + } + return false + } + + @JvmStatic fun getSdCardMainPath(context: Context): String { + var path = "${context.getExternalFilesDirs("")[1]}" + val separator: String = context.getString(R.string.android_directory_seperator) + val sepPos = path.indexOf(separator) + if (sepPos != -1) { + path = path.substring(0, sepPos) + } + return path + } } diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index e8b22ef18..cb33da20a 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -295,6 +295,7 @@ In order to access all the zim files across device we need to have All Files Permission Allowed Not allowed + /Android @string/on @string/off From b305ef0905d6bd217d3a2e35ed43cdc87b31de8d Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 21 Apr 2022 17:42:10 +0530 Subject: [PATCH 2/8] Changes after review --- .../library/LocalLibraryFragment.kt | 3 ++- app/src/main/res/values/strings.xml | 1 + .../core/extensions/StringExtensions.kt | 22 +++++++++++++++++++ .../kiwixmobile/core/utils/files/FileUtils.kt | 15 +++++-------- 4 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt 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 b08499dee..5b1c6bd44 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 @@ -32,6 +32,7 @@ import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode import androidx.appcompat.widget.Toolbar @@ -163,7 +164,7 @@ class LocalLibraryFragment : BaseFragment() { FILE_SELECT_CODE ) } catch (ex: ActivityNotFoundException) { - ex.printStackTrace() + activity.toast(resources.getString(R.string.no_app_found_to_open), Toast.LENGTH_SHORT) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 96761d289..f9c519e82 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,4 +6,5 @@ Failed to open file\nPlease try looking for this file in the Device Tab of your Library Send Files Receive Files + No app found to select zim file! diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt new file mode 100644 index 000000000..47701033f --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt @@ -0,0 +1,22 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.extensions + +fun String.getStringBefore(suffix: String): String = + this.substringBefore(suffix) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index cc076714c..b66c19932 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -27,6 +27,7 @@ import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.downloader.ChunkUtils import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.core.extensions.get +import org.kiwix.kiwixmobile.core.extensions.getStringBefore import java.io.BufferedReader import java.io.File import java.io.IOException @@ -103,7 +104,7 @@ object FileUtils { } return try { "${getSdCardMainPath(context)}/${documentId[1]}" - } catch (e: Exception) { + } catch (ignore: Exception) { null } } else if ("com.android.providers.downloads.documents" == uri.authority) @@ -243,13 +244,7 @@ object FileUtils { return false } - @JvmStatic fun getSdCardMainPath(context: Context): String { - var path = "${context.getExternalFilesDirs("")[1]}" - val separator: String = context.getString(R.string.android_directory_seperator) - val sepPos = path.indexOf(separator) - if (sepPos != -1) { - path = path.substring(0, sepPos) - } - return path - } + @JvmStatic fun getSdCardMainPath(context: Context): String = + "${context.getExternalFilesDirs("")[1]}" + .getStringBefore(context.getString(R.string.android_directory_seperator)) } From 578f3467ce3d25bc4e8ad06e2aa7fc06f9dcfb94 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Fri, 22 Apr 2022 11:37:56 +0530 Subject: [PATCH 3/8] Changes after review --- .../library/LocalLibraryFragment.kt | 12 +++++----- .../core/extensions/StringExtensions.kt | 22 ------------------- .../kiwixmobile/core/utils/files/FileUtils.kt | 11 +++------- core/src/main/res/values/strings.xml | 1 + 4 files changed, 11 insertions(+), 35 deletions(-) delete mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt 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 5b1c6bd44..2498efcb9 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 @@ -155,9 +155,11 @@ class LocalLibraryFragment : BaseFragment() { } private fun showFileChooser() { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.type = "*/*" - intent.addCategory(Intent.CATEGORY_OPENABLE) + val intent = Intent().apply { + action = Intent.ACTION_GET_CONTENT + type = "*/*" + addCategory(Intent.CATEGORY_OPENABLE) + } try { startActivityForResult( Intent.createChooser(intent, "Select a zim file"), @@ -186,7 +188,7 @@ class LocalLibraryFragment : BaseFragment() { return } val file = File(filePath) - if (!FileUtils.isValidFile(file.path)) { + if (!FileUtils.isValidZimFile(file.path)) { activity.toast(R.string.error_file_invalid) return } @@ -195,7 +197,7 @@ class LocalLibraryFragment : BaseFragment() { private fun navigateToReaderFragment(file: File) { if (!file.canRead()) { - activity.toast(R.string.error_file_not_found) + activity.toast(R.string.unable_to_read_file) } else { activity?.navigate( LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt deleted file mode 100644 index 47701033f..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/StringExtensions.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2022 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package org.kiwix.kiwixmobile.core.extensions - -fun String.getStringBefore(suffix: String): String = - this.substringBefore(suffix) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index b66c19932..f307bfa29 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -27,7 +27,6 @@ import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.downloader.ChunkUtils import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book import org.kiwix.kiwixmobile.core.extensions.get -import org.kiwix.kiwixmobile.core.extensions.getStringBefore import java.io.BufferedReader import java.io.File import java.io.IOException @@ -237,14 +236,10 @@ object FileUtils { "".also { e.printStackTrace() } } - @JvmStatic fun isValidFile(filePath: String): Boolean { - if (filePath.endsWith(".zim") || filePath.endsWith(".zimaa")) { - return true - } - return false - } + @JvmStatic fun isValidZimFile(filePath: String): Boolean = + (filePath.endsWith(".zim") || filePath.endsWith(".zimaa")) @JvmStatic fun getSdCardMainPath(context: Context): String = "${context.getExternalFilesDirs("")[1]}" - .getStringBefore(context.getString(R.string.android_directory_seperator)) + .substringBefore(context.getString(R.string.android_directory_seperator)) } diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index cb33da20a..69c578c26 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -45,6 +45,7 @@ Enter this ip address into your browser to access the server %s Share URL via other applications Error: The selected ZIM file could not be found. + Unable to read this zim file! Unable to open zim file Error: The selected file is not a valid ZIM file. Error: Loading article (Url: %1$s) failed. From 3a80d0699d60e07e952a8f75d481ab57d4f9f27e Mon Sep 17 00:00:00 2001 From: MohitMali Date: Fri, 22 Apr 2022 11:44:28 +0530 Subject: [PATCH 4/8] Changes after review --- .../kiwixmobile/nav/destination/library/LocalLibraryFragment.kt | 2 +- core/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 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 2498efcb9..f082ee335 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 @@ -197,7 +197,7 @@ class LocalLibraryFragment : BaseFragment() { private fun navigateToReaderFragment(file: File) { if (!file.canRead()) { - activity.toast(R.string.unable_to_read_file) + activity.toast(R.string.unable_to_read_zim_file) } else { activity?.navigate( LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader() diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 69c578c26..388c0ecb1 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -45,7 +45,7 @@ Enter this ip address into your browser to access the server %s Share URL via other applications Error: The selected ZIM file could not be found. - Unable to read this zim file! + Unable to read this zim file! Unable to open zim file Error: The selected file is not a valid ZIM file. Error: Loading article (Url: %1$s) failed. From bcd49946fa4f1914184b1761957e96cd17a4c9a3 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Mon, 25 Apr 2022 14:38:35 +0530 Subject: [PATCH 5/8] fixing merging issue --- .../nav/destination/library/LocalLibraryFragment.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 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 f082ee335..fde73d628 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 @@ -173,13 +173,16 @@ class LocalLibraryFragment : BaseFragment() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { when (requestCode) { FILE_SELECT_CODE -> { - data?.data?.let(::isValidFile) + data?.data + ?.let(::validateFileFromUriAndNavigateToReaderFragment) } else -> super.onActivityResult(requestCode, resultCode, data) } } - private fun isValidFile(uri: Uri) { + private fun validateFileFromUriAndNavigateToReaderFragment( + uri: Uri + ) { val filePath = FileUtils.getLocalFilePathByUri( requireActivity().applicationContext, uri ) @@ -192,10 +195,7 @@ class LocalLibraryFragment : BaseFragment() { activity.toast(R.string.error_file_invalid) return } - navigateToReaderFragment(file) - } - private fun navigateToReaderFragment(file: File) { if (!file.canRead()) { activity.toast(R.string.unable_to_read_zim_file) } else { From ebe8641ec76eee234260e13a66fa148b43af805a Mon Sep 17 00:00:00 2001 From: MohitMali Date: Mon, 25 Apr 2022 14:50:24 +0530 Subject: [PATCH 6/8] seperate two method and chaning name --- .../library/LocalLibraryFragment.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 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 fde73d628..c5790905c 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 @@ -173,29 +173,34 @@ class LocalLibraryFragment : BaseFragment() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { when (requestCode) { FILE_SELECT_CODE -> { - data?.data - ?.let(::validateFileFromUriAndNavigateToReaderFragment) + data?.data?.let { uri -> + getZimFileFromUri(uri)?.let(::navigateToReaderFragment) + } } else -> super.onActivityResult(requestCode, resultCode, data) } } - private fun validateFileFromUriAndNavigateToReaderFragment( + private fun getZimFileFromUri( uri: Uri - ) { + ): File? { val filePath = FileUtils.getLocalFilePathByUri( requireActivity().applicationContext, uri ) if (filePath == null || !File(filePath).exists()) { activity.toast(R.string.error_file_not_found) - return + return null } val file = File(filePath) - if (!FileUtils.isValidZimFile(file.path)) { + return if (!FileUtils.isValidZimFile(file.path)) { activity.toast(R.string.error_file_invalid) - return + null + } else { + file } + } + private fun navigateToReaderFragment(file: File) { if (!file.canRead()) { activity.toast(R.string.unable_to_read_zim_file) } else { From 409366914356da585036d407f6c145d480217aec Mon Sep 17 00:00:00 2001 From: MohitMali Date: Mon, 25 Apr 2022 15:49:08 +0530 Subject: [PATCH 7/8] remove translate --- app/src/main/res/values/strings.xml | 2 +- core/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f9c519e82..5826857a6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,5 +6,5 @@ Failed to open file\nPlease try looking for this file in the Device Tab of your Library Send Files Receive Files - No app found to select zim file! + No app found to select zim file! diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 388c0ecb1..e4ee868d8 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -45,7 +45,7 @@ Enter this ip address into your browser to access the server %s Share URL via other applications Error: The selected ZIM file could not be found. - Unable to read this zim file! + Unable to read this zim file! Unable to open zim file Error: The selected file is not a valid ZIM file. Error: Loading article (Url: %1$s) failed. From 7e7108b54f47fbcd978f596c01b425e3340cd518 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Tue, 26 Apr 2022 15:19:36 +0530 Subject: [PATCH 8/8] removing uncessary paranthesis --- .../java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index f307bfa29..e1768174f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -237,7 +237,7 @@ object FileUtils { } @JvmStatic fun isValidZimFile(filePath: String): Boolean = - (filePath.endsWith(".zim") || filePath.endsWith(".zimaa")) + filePath.endsWith(".zim") || filePath.endsWith(".zimaa") @JvmStatic fun getSdCardMainPath(context: Context): String = "${context.getExternalFilesDirs("")[1]}"