-
-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
* This was requested in #3453 and this PR is related to the Epub opening issue so we are introducing this here.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Kiwix Android | ||
* Copyright (c) 2024 Kiwix <android.kiwix.org> | ||
* 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 <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package org.kiwix.kiwixmobile.core.utils.dialog | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.util.Log | ||
import androidx.core.content.FileProvider | ||
import org.kiwix.kiwixmobile.core.R | ||
import org.kiwix.kiwixmobile.core.extensions.toast | ||
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer | ||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil | ||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.downloadFileFromUrl | ||
import java.io.File | ||
import javax.inject.Inject | ||
|
||
class DownloadOrOpenEpubAndPdfHandler @Inject constructor( | ||
private val activity: Activity, | ||
private val sharedPreferenceUtil: SharedPreferenceUtil, | ||
private val alertDialogShower: AlertDialogShower, | ||
private val zimReaderContainer: ZimReaderContainer | ||
) { | ||
|
||
fun showDownloadOrOpenEpubAndPdfDialog(url: String, documentType: String?) { | ||
alertDialogShower.show( | ||
KiwixDialog.DownloadOrOpenEpubAndPdf, | ||
{ openOrDownloadFile(url, documentType, true) }, | ||
{ openOrDownloadFile(url, documentType, false) }, | ||
{ } | ||
Check warning on line 45 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L41-L45
|
||
) | ||
} | ||
Check warning on line 47 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L47
|
||
|
||
private fun openOrDownloadFile(url: String, documentType: String?, openFile: Boolean) { | ||
downloadFileFromUrl( | ||
url, | ||
null, | ||
zimReaderContainer, | ||
sharedPreferenceUtil | ||
Check warning on line 54 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L51-L54
|
||
)?.let { savedFile -> | ||
if (openFile) { | ||
openFile(savedFile, documentType) | ||
Check warning on line 57 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L57
|
||
} else { | ||
activity.toast(activity.getString(R.string.save_media_saved, savedFile.name)).also { | ||
Log.e("DownloadOrOpenEpubAndPdf", "File downloaded at = ${savedFile.path}") | ||
} | ||
Check warning on line 61 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L59-L61
|
||
} | ||
} ?: run { | ||
activity.toast(R.string.save_media_error) | ||
} | ||
} | ||
Check warning on line 66 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L63-L66
|
||
|
||
private fun openFile(savedFile: File, documentType: String?) { | ||
if (savedFile.exists()) { | ||
val uri = FileProvider.getUriForFile( | ||
activity, | ||
"${activity.packageName}.fileprovider", | ||
savedFile | ||
Check warning on line 73 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L70-L73
|
||
) | ||
val intent = Intent(Intent.ACTION_VIEW).apply { | ||
setDataAndType(uri, documentType) | ||
flags = Intent.FLAG_ACTIVITY_NO_HISTORY | ||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) | ||
} | ||
Check warning on line 79 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L75-L79
|
||
if (intent.resolveActivity(activity.packageManager) != null) { | ||
activity.startActivity(intent) | ||
Check warning on line 81 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L81
|
||
} else { | ||
activity.toast(R.string.no_reader_application_installed) | ||
Check warning on line 83 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L83
|
||
} | ||
} | ||
} | ||
Check warning on line 86 in core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt Codecov / codecov/patchcore/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/DownloadOrOpenEpubAndPdfHandler.kt#L86
|
||
} |