Skip to content

Commit

Permalink
Replacing the fetch library with Android's DownloadManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaliDeveloper authored and MohitMaliFtechiz committed Jul 23, 2024
1 parent 974c7d4 commit e8978e1
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.base.BaseFragment
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
import org.kiwix.kiwixmobile.core.downloader.Downloader
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.hasNotificationPermission
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isManageExternalStoragePermissionGranted
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.navigate
Expand Down Expand Up @@ -89,6 +90,7 @@ import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.SelectFolder
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.YesNoDialog.WifiOnly
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.getPathFromUri
import org.kiwix.kiwixmobile.databinding.FragmentDestinationDownloadBinding
import org.kiwix.kiwixmobile.zimManager.Fat32Checker
import org.kiwix.kiwixmobile.zimManager.NetworkState
import org.kiwix.kiwixmobile.zimManager.ZimManageViewModel
import org.kiwix.kiwixmobile.zimManager.libraryView.AvailableSpaceCalculator
Expand Down Expand Up @@ -209,6 +211,26 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
}
}
)
// <book />
val libraryBookEntity = LibraryNetworkEntity.Book().apply {
id = "6a5413cd-0d96-7288-9c1b-5beda035e472"
size = "170628"
url = "https://download.kiwix.org/zim/zimit/100r.co_en_all_2024-06.zim.meta4"
mediaCount = "1433"
articleCount = "320"
favicon =
"iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAL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+QjOwZpd17omMrrd5KJncrrN7Fnef1aZGJV5PL6l92nK0roniRX8Hojvv2WTPY6dBq/Ir8Pa0CJ33xc8FpHJsXvkO3Fm9b9yEZ3BWw2ueCbni+QC77pmewnYo3oIhOxetrfdXd5zZhobrRsvw/YuhLtTacfyj/6ugOeqKvxltAvJWGparEv+WQS2fuQE7iE9SKXItPYGnJdtsVrdQ1Y4bm6WJ7U9gP52K1I6+lV5hXIQ6xDPjkRuwlF2hGI2PMQmVYgD7QXcsVtyLTS28T0km8+Iv8g9JKvFZUxSyghXOM15liO4xDZd5Jfx05FpaCtKBaAF0mvjVL7T69Zq8uw//dr1gPKsP/6mvUjKB9ZQg7jp6Ps7wkysc9DV71HyenEZUhrPyYrYDZ6CXEXOvIuiJyvkDXbC2WVa8mvT/dH9ajVKOKDCPs4Kk9ONdaIqibLcWl+C5kUy5GtxTT5YfI1Mz23IhcYsa8gc4vYmX4idjRKGiM2jPZJZZuV9HDAtlu25QHbXH7bL4UnkThWJloClsY1+TMVXCETtpJscrs76NcU+sX/myjHCsTDjuTlTESKN8m+dia6x65FNyJQxG5C5EuB5VbyW/mUWqSI/Siy3+7kdwvzvGgt+d9tbvZcDchs30XFNJCfX4ssIhH2BMv6FjDlXw/HDkTdtlgLAAAAAElFTkSuQmCC"
title = "100 Rabbits"
description = "Research and test low-tech solutions, and document findings"
language = "eng"
creator = "-"
publisher = "openZIM"
bookName = "100r.co_en_all"
tags = "_ftindex:yes;preppers;_category:other;_pictures:yes;_videos:yes;_details:yes"
date = "2024-06-24"
faviconMimeType = "image/png"
}
downloader.download(libraryBookEntity)
}

private fun setupMenu() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ import org.kiwix.kiwixmobile.core.downloader.DownloadRequester
import org.kiwix.kiwixmobile.core.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.core.downloader.model.DownloadRequest
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.core.extensions.deleteFile
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
import java.io.File
import javax.inject.Inject

class FetchDownloadDao @Inject constructor(
private val box: Box<FetchDownloadEntity>,
private val newBookDao: NewBookDao
private val newBookDao: NewBookDao,
private val sharedPreferenceUtil: SharedPreferenceUtil
) {

fun downloads(): Flowable<List<DownloadModel>> =
Expand All @@ -58,17 +62,17 @@ class FetchDownloadDao @Inject constructor(

fun update(download: Download) {
box.store.callInTx {
getEntityFor(download)?.let { dbEntity ->
getEntityFor(download.id)?.let { dbEntity ->
dbEntity.updateWith(download)
.takeIf { updatedEntity -> updatedEntity != dbEntity }
?.let(box::put)
}
}
}

private fun getEntityFor(download: Download) =
private fun getEntityFor(downloadId: Int) =
box.query {
equal(FetchDownloadEntity_.downloadId, download.id)
equal(FetchDownloadEntity_.downloadId, downloadId)
}.find().getOrNull(0)

fun getEntityForFileName(fileName: String) =
Expand All @@ -79,13 +83,17 @@ class FetchDownloadDao @Inject constructor(
)
}.findFirst()

fun insert(downloadId: Long, book: Book) {
box.put(FetchDownloadEntity(downloadId, book))
fun insert(downloadId: Long, book: Book, filePath: String?) {
box.put(FetchDownloadEntity(downloadId, book, filePath))
}

fun delete(download: Download) {
fun delete(downloadId: Long) {
// remove the previous file from storage since we have cancelled the download.
// getEntityFor(downloadId.toInt())?.file?.let {
// File(it).deleteFile()
// }
box.query {
equal(FetchDownloadEntity_.downloadId, download.id)
equal(FetchDownloadEntity_.downloadId, downloadId)
}.remove()
}

Expand All @@ -96,9 +104,11 @@ class FetchDownloadDao @Inject constructor(
) {
box.store.callInTx {
if (doesNotAlreadyExist(book)) {
val downloadRequest = DownloadRequest(url, book.title)
insert(
downloadRequester.enqueue(DownloadRequest(url)),
book = book
downloadRequester.enqueue(downloadRequest),
book = book,
filePath = downloadRequest.getDestinationFile(sharedPreferenceUtil).path
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ data class FetchDownloadEntity(
val favIcon: String,
val tags: String? = null
) {
constructor(downloadId: Long, book: Book) : this(
constructor(downloadId: Long, book: Book, file: String?) : this(
file = file,
downloadId = downloadId,
bookId = book.id,
title = book.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.kiwix.kiwixmobile.core.di.qualifiers.Computation
import org.kiwix.kiwixmobile.core.di.qualifiers.IO
import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread
import org.kiwix.kiwixmobile.core.downloader.DownloadMonitor
import org.kiwix.kiwixmobile.core.downloader.fetch.FetchDownloadMonitor
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerMonitor
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
import org.kiwix.kiwixmobile.core.utils.BookUtils
import javax.inject.Singleton
Expand Down Expand Up @@ -88,10 +88,15 @@ class ApplicationModule {
@Provides
fun provideComputationThread(): Scheduler = Schedulers.computation()

// @Provides
// @Singleton
// internal fun provideDownloadMonitor(fetchDownloadMonitor: FetchDownloadMonitor): DownloadMonitor =
// fetchDownloadMonitor

@Provides
@Singleton
internal fun provideDownloadMonitor(fetchDownloadMonitor: FetchDownloadMonitor): DownloadMonitor =
fetchDownloadMonitor
internal fun provideDownloadMonitor(downloadManagerMonitor: DownloadManagerMonitor)
: DownloadMonitor = downloadManagerMonitor

@Provides
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import dagger.Module
import dagger.Provides
import org.kiwix.kiwixmobile.core.di.CoreServiceScope
import org.kiwix.kiwixmobile.core.qr.GenerateQR
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerBroadcastReceiver
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerMonitor
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudNotificationManger
import org.kiwix.kiwixmobile.core.webserver.KiwixServer
import org.kiwix.kiwixmobile.core.webserver.WebServerHelper
Expand Down Expand Up @@ -75,4 +77,11 @@ class CoreServiceModule {
@Provides
@CoreServiceScope
fun providesGenerateQr(): GenerateQR = GenerateQR()
fun providesDownloadManagerBroadcastReceiver(callback: DownloadManagerBroadcastReceiver.Callback)
: DownloadManagerBroadcastReceiver = DownloadManagerBroadcastReceiver(callback)

@Provides
@CoreServiceScope
fun providesDownloadInformationCallback(downloadManagerMonitor: DownloadManagerMonitor)
: DownloadManagerBroadcastReceiver.Callback = downloadManagerMonitor
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.kiwix.kiwixmobile.core.dao.NewNoteDao
import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao
import org.kiwix.kiwixmobile.core.dao.entities.MyObjectBox
import org.kiwix.kiwixmobile.core.data.KiwixRoomDatabase
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import javax.inject.Singleton

@Module
Expand Down Expand Up @@ -71,9 +72,10 @@ open class DatabaseModule {

@Provides @Singleton fun providesFetchDownloadDao(
boxStore: BoxStore,
newBookDao: NewBookDao
newBookDao: NewBookDao,
sharedPreferenceUtil: SharedPreferenceUtil
): FetchDownloadDao =
FetchDownloadDao(boxStore.boxFor(), newBookDao)
FetchDownloadDao(boxStore.boxFor(), newBookDao, sharedPreferenceUtil)

@Singleton
@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.kiwix.kiwixmobile.core.di.modules

import android.app.DownloadManager
import android.content.Context
import com.tonyodev.fetch2.Fetch
import com.tonyodev.fetch2.Fetch.Impl
Expand All @@ -33,8 +34,8 @@ import org.kiwix.kiwixmobile.core.data.remote.KiwixService
import org.kiwix.kiwixmobile.core.downloader.DownloadRequester
import org.kiwix.kiwixmobile.core.downloader.Downloader
import org.kiwix.kiwixmobile.core.downloader.DownloaderImpl
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerRequester
import org.kiwix.kiwixmobile.core.downloader.fetch.FetchDownloadNotificationManager
import org.kiwix.kiwixmobile.core.downloader.fetch.FetchDownloadRequester
import org.kiwix.kiwixmobile.core.utils.CONNECT_TIME_OUT
import org.kiwix.kiwixmobile.core.utils.READ_TIME_OUT
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
Expand All @@ -51,10 +52,10 @@ object DownloaderModule {
kiwixService: KiwixService
): Downloader = DownloaderImpl(downloadRequester, downloadDao, kiwixService)

@Provides
@Singleton
fun providesDownloadRequester(fetch: Fetch, sharedPreferenceUtil: SharedPreferenceUtil):
DownloadRequester = FetchDownloadRequester(fetch, sharedPreferenceUtil)
// @Provides
// @Singleton
// fun providesDownloadRequester(fetch: Fetch, sharedPreferenceUtil: SharedPreferenceUtil):
// DownloadRequester = FetchDownloadRequester(fetch, sharedPreferenceUtil)

@Provides
@Singleton
Expand Down Expand Up @@ -93,4 +94,16 @@ object DownloaderModule {
@Singleton
fun provideFetchDownloadNotificationManager(context: Context, fetchDownloadDao: FetchDownloadDao):
FetchNotificationManager = FetchDownloadNotificationManager(context, fetchDownloadDao)

@Provides
@Singleton
fun providesDownloadRequester(
downloadManager: DownloadManager,
sharedPreferenceUtil: SharedPreferenceUtil,
fetchDownloadDao: FetchDownloadDao
): DownloadRequester = DownloadManagerRequester(
downloadManager,
sharedPreferenceUtil,
fetchDownloadDao
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.downloader.downloadManager

import android.app.DownloadManager.ACTION_DOWNLOAD_COMPLETE
import android.content.Context
import android.content.Intent
import org.kiwix.kiwixmobile.core.base.BaseBroadcastReceiver
import javax.inject.Inject

class DownloadManagerBroadcastReceiver @Inject constructor(private val callback: Callback) :
BaseBroadcastReceiver() {
// This broadcast will trigger when a download is completed or cancelled.
override val action: String = ACTION_DOWNLOAD_COMPLETE

override fun onIntentWithActionReceived(context: Context, intent: Intent) {
}

interface Callback {
fun downloadInformation()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.downloader.downloadManager

import android.app.DownloadManager
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.core.downloader.DownloadMonitor
import javax.inject.Inject

class DownloadManagerMonitor @Inject constructor(
private val downloadManager: DownloadManager,
private val fetchDownloadDao: FetchDownloadDao
) :
DownloadMonitor, DownloadManagerBroadcastReceiver.Callback {
private val updater = PublishSubject.create<() -> Unit>()

override fun downloadInformation() {}

init {
updater.subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(
{ it.invoke() },
Throwable::printStackTrace
)
}

override fun init() {
// empty method to so class does not get reported unused}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.downloader.downloadManager

import android.app.DownloadManager
import android.app.DownloadManager.Request
import android.net.Uri
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.core.downloader.DownloadRequester
import org.kiwix.kiwixmobile.core.downloader.model.DownloadRequest
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import javax.inject.Inject

class DownloadManagerRequester @Inject constructor(
private val downloadManager: DownloadManager,
private val sharedPreferenceUtil: SharedPreferenceUtil,
private val fetchDownloadDao: FetchDownloadDao
) : DownloadRequester {
override fun enqueue(downloadRequest: DownloadRequest): Long =
downloadManager.enqueue(downloadRequest.toDownloadManagerRequest(sharedPreferenceUtil))

override fun cancel(downloadId: Long) {
downloadManager.remove(downloadId).also {
fetchDownloadDao.delete(downloadId)
}
}

override fun retryDownload(downloadId: Long) {
}

override fun pauseResumeDownload(downloadId: Long, isPause: Boolean) {
}
}

private fun DownloadRequest.toDownloadManagerRequest(sharedPreferenceUtil: SharedPreferenceUtil) =
DownloadManager.Request(uri).apply {
setDestinationUri(Uri.fromFile(getDestinationFile(sharedPreferenceUtil)))
setAllowedNetworkTypes(
if (sharedPreferenceUtil.prefWifiOnly)
Request.NETWORK_WIFI
else
Request.NETWORK_MOBILE
)
setTitle(notificationTitle)
setAllowedOverMetered(true)
setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
}
Loading

0 comments on commit e8978e1

Please sign in to comment.