-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing the fetch library with Android's DownloadManager.
- Loading branch information
1 parent
974c7d4
commit e8978e1
Showing
13 changed files
with
241 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadManagerBroadcastReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadManagerMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...in/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadManagerRequester.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.