|
18 | 18 |
|
19 | 19 | package org.kiwix.kiwixmobile.core.downloader.downloadManager
|
20 | 20 |
|
| 21 | +import android.annotation.SuppressLint |
21 | 22 | import android.app.DownloadManager
|
22 | 23 | import android.content.Context
|
23 | 24 | import android.content.Intent
|
24 | 25 | import kotlinx.coroutines.CoroutineScope
|
25 | 26 | import kotlinx.coroutines.Dispatchers
|
26 | 27 | import kotlinx.coroutines.launch
|
27 | 28 | import org.kiwix.kiwixmobile.core.dao.DownloadRoomDao
|
28 |
| -import org.kiwix.kiwixmobile.core.dao.entities.DownloadRoomEntity |
29 | 29 | import org.kiwix.kiwixmobile.core.downloader.DownloadMonitor
|
30 | 30 | import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadNotificationManager.Companion.ACTION_CANCEL
|
31 | 31 | import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadNotificationManager.Companion.ACTION_PAUSE
|
32 | 32 | import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadNotificationManager.Companion.ACTION_QUERY_DOWNLOAD_STATUS
|
33 | 33 | import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadNotificationManager.Companion.ACTION_RESUME
|
| 34 | +import org.kiwix.kiwixmobile.core.extensions.registerReceiver |
| 35 | +import org.kiwix.kiwixmobile.core.zim_manager.ConnectivityBroadcastReceiver |
34 | 36 | import javax.inject.Inject
|
35 | 37 |
|
36 | 38 | class DownloadManagerMonitor @Inject constructor(
|
37 | 39 | val downloadRoomDao: DownloadRoomDao,
|
38 |
| - private val context: Context |
| 40 | + private val context: Context, |
| 41 | + private val connectivityBroadcastReceiver: ConnectivityBroadcastReceiver |
39 | 42 | ) : DownloadMonitor, DownloadManagerBroadcastReceiver.Callback {
|
40 | 43 | private val lock = Any()
|
41 | 44 |
|
42 | 45 | init {
|
| 46 | + context.registerReceiver(connectivityBroadcastReceiver) |
| 47 | + startServiceIfActiveDownloads() |
| 48 | + trackNetworkState() |
| 49 | + } |
| 50 | + |
| 51 | + @SuppressLint("CheckResult") |
| 52 | + private fun trackNetworkState() { |
| 53 | + connectivityBroadcastReceiver.networkStates |
| 54 | + .distinctUntilChanged() |
| 55 | + .subscribe( |
| 56 | + { |
| 57 | + // Start the service when the network changes so that we can |
| 58 | + // track the progress accurately. |
| 59 | + startServiceIfActiveDownloads() |
| 60 | + }, |
| 61 | + Throwable::printStackTrace |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + private fun startServiceIfActiveDownloads() { |
43 | 66 | CoroutineScope(Dispatchers.IO).launch {
|
44 |
| - if (getActiveDownloads().isNotEmpty()) { |
| 67 | + if (downloadRoomDao.downloads().blockingFirst().isNotEmpty()) { |
45 | 68 | startService()
|
46 | 69 | }
|
47 | 70 | }
|
48 | 71 | }
|
49 | 72 |
|
50 |
| - private suspend fun getActiveDownloads(): List<DownloadRoomEntity> = |
51 |
| - downloadRoomDao.downloadRoomEntity().blockingFirst().filter { |
52 |
| - it.status != Status.PAUSED && it.status != Status.CANCELLED |
53 |
| - } |
54 |
| - |
55 | 73 | override fun downloadCompleteOrCancelled(intent: Intent) {
|
56 | 74 | synchronized(lock) {
|
57 | 75 | intent.extras?.let {
|
|
0 commit comments