Skip to content

Commit

Permalink
Updating the current download item on OnlineLibraryScreen if a downlo…
Browse files Browse the repository at this point in the history
…ad completes or cancelled
  • Loading branch information
MohitMaliDeveloper authored and MohitMaliFtechiz committed Jul 23, 2024
1 parent e8978e1 commit 5b651f8
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
date = "2024-06-24"
faviconMimeType = "image/png"
}
downloader.download(libraryBookEntity)
// downloader.download(libraryBookEntity)
}

private fun setupMenu() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.dao

import androidx.room.Dao
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import javax.inject.Inject

// @Dao
// abstract class DownloadRoomDao @Inject constructor(
// private val newBookDao: NewBookDao,
// private val sharedPreferenceUtil: SharedPreferenceUtil
// ) {
// }
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class FetchDownloadDao @Inject constructor(
}
}

private fun getEntityFor(downloadId: Int) =
fun getEntityFor(downloadId: Int) =
box.query {
equal(FetchDownloadEntity_.downloadId, downloadId)
}.find().getOrNull(0)
Expand All @@ -89,9 +89,9 @@ class FetchDownloadDao @Inject constructor(

fun delete(downloadId: Long) {
// remove the previous file from storage since we have cancelled the download.
// getEntityFor(downloadId.toInt())?.file?.let {
// File(it).deleteFile()
// }
getEntityFor(downloadId.toInt())?.file?.let {
File(it).deleteFile()
}
box.query {
equal(FetchDownloadEntity_.downloadId, downloadId)
}.remove()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.kiwix.kiwixmobile.core.di.modules.MutexModule
import org.kiwix.kiwixmobile.core.di.modules.NetworkModule
import org.kiwix.kiwixmobile.core.di.modules.SearchModule
import org.kiwix.kiwixmobile.core.downloader.Downloader
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerBroadcastReceiver
import org.kiwix.kiwixmobile.core.error.ErrorActivity
import org.kiwix.kiwixmobile.core.main.KiwixWebView
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
Expand Down Expand Up @@ -114,6 +115,8 @@ interface CoreComponent {
fun searchResultGenerator(): SearchResultGenerator
fun mutex(): Mutex

fun downloadManagerBroadCastReceiver(): DownloadManagerBroadcastReceiver

fun inject(application: CoreApp)
fun inject(kiwixWebView: KiwixWebView)
fun inject(storageSelectDialog: StorageSelectDialog)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import android.content.Context
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.qr.GenerateQR
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudNotificationManger
import org.kiwix.kiwixmobile.core.webserver.KiwixServer
import org.kiwix.kiwixmobile.core.webserver.WebServerHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +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.DownloadManagerBroadcastReceiver
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerMonitor
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerRequester
import org.kiwix.kiwixmobile.core.downloader.fetch.FetchDownloadNotificationManager
import org.kiwix.kiwixmobile.core.utils.CONNECT_TIME_OUT
Expand Down Expand Up @@ -106,4 +108,14 @@ object DownloaderModule {
sharedPreferenceUtil,
fetchDownloadDao
)

@Provides
@Singleton
fun provideDownloadManagerCallback(downloadManagerMonitor: DownloadManagerMonitor)
: DownloadManagerBroadcastReceiver.Callback = downloadManagerMonitor

@Provides
@Singleton
fun providesDownloadManagerBroadcastReceiver(callback: DownloadManagerBroadcastReceiver.Callback)
: DownloadManagerBroadcastReceiver = DownloadManagerBroadcastReceiver(callback)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.kiwix.kiwixmobile.core.downloader.downloadManager
import android.app.DownloadManager.ACTION_DOWNLOAD_COMPLETE
import android.content.Context
import android.content.Intent
import android.util.Log
import org.kiwix.kiwixmobile.core.base.BaseBroadcastReceiver
import javax.inject.Inject

Expand All @@ -30,9 +31,10 @@ class DownloadManagerBroadcastReceiver @Inject constructor(private val callback:
override val action: String = ACTION_DOWNLOAD_COMPLETE

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

interface Callback {
fun downloadInformation()
fun downloadInformation(intent: Intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.kiwix.kiwixmobile.core.downloader.downloadManager

import android.app.DownloadManager
import android.content.Intent
import android.util.Log
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import org.kiwix.kiwixmobile.core.dao.FetchDownloadDao
Expand All @@ -31,8 +33,85 @@ class DownloadManagerMonitor @Inject constructor(
) :
DownloadMonitor, DownloadManagerBroadcastReceiver.Callback {
private val updater = PublishSubject.create<() -> Unit>()
private val lock = Any()

override fun downloadInformation() {}
override fun downloadInformation(intent: Intent) {
synchronized(lock) {
intent.extras?.let {
val downloadedFileId = it.getLong(DownloadManager.EXTRA_DOWNLOAD_ID, -1L)
if (downloadedFileId != -1L) {
val query = DownloadManager.Query().setFilterById(downloadedFileId)
val cursor = downloadManager.query(query).also { cursor ->
Log.e("HELLO", "downloadInformation: ${cursor.moveToFirst()}")
}
if (cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
val status = cursor.getInt(columnIndex)
val columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
val reason = cursor.getInt(columnReason)

when (status) {
DownloadManager.STATUS_FAILED -> {
var failedReason = ""
when (reason) {
DownloadManager.ERROR_CANNOT_RESUME -> failedReason = "ERROR_CANNOT_RESUME"
DownloadManager.ERROR_DEVICE_NOT_FOUND -> failedReason = "ERROR_DEVICE_NOT_FOUND"
DownloadManager.ERROR_FILE_ALREADY_EXISTS -> failedReason =
"ERROR_FILE_ALREADY_EXISTS"

DownloadManager.ERROR_FILE_ERROR -> failedReason = "ERROR_FILE_ERROR"
DownloadManager.ERROR_HTTP_DATA_ERROR -> failedReason = "ERROR_HTTP_DATA_ERROR"
DownloadManager.ERROR_INSUFFICIENT_SPACE -> failedReason =
"ERROR_INSUFFICIENT_SPACE"

DownloadManager.ERROR_TOO_MANY_REDIRECTS -> failedReason =
"ERROR_TOO_MANY_REDIRECTS"

DownloadManager.ERROR_UNHANDLED_HTTP_CODE -> failedReason =
"ERROR_UNHANDLED_HTTP_CODE"

DownloadManager.ERROR_UNKNOWN -> failedReason = "ERROR_UNKNOWN"
}
Log.e("STATUS", "FAILED: $failedReason")
}

DownloadManager.STATUS_PAUSED -> {
var pausedReason = ""

when (reason) {
DownloadManager.PAUSED_QUEUED_FOR_WIFI -> pausedReason = "PAUSED_QUEUED_FOR_WIFI"
DownloadManager.PAUSED_UNKNOWN -> pausedReason = "PAUSED_UNKNOWN"
DownloadManager.PAUSED_WAITING_FOR_NETWORK -> pausedReason =
"PAUSED_WAITING_FOR_NETWORK"

DownloadManager.PAUSED_WAITING_TO_RETRY -> pausedReason =
"PAUSED_WAITING_TO_RETRY"
}
Log.e("STATUS", "STATUS_PAUSED: $pausedReason")
}

DownloadManager.STATUS_PENDING -> Log.e(
"STATUS",
"STATUS_PENDING"
)

DownloadManager.STATUS_RUNNING -> Log.e("STATUS", "STATUS_RUNNING:")

DownloadManager.STATUS_SUCCESSFUL -> {
Log.e("STATUS", "STATUS_SUCCESSFUL:")
// updater.onNext { fetchDownloadDao.update(download) }
downloadManager.remove(downloadedFileId)
}
}
} else {
// the download is cancelled
updater.onNext { fetchDownloadDao.delete(downloadedFileId) }
}
Log.e("HELLO", "downloadInformation: $downloadedFileId")
}
}
}
}

init {
updater.subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.kiwix.kiwixmobile.core.BuildConfig
import org.kiwix.kiwixmobile.core.CoreApp
import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToLibkiwixMigrator
import org.kiwix.kiwixmobile.core.data.remote.ObjectBoxToRoomMigrator
import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerBroadcastReceiver
import org.kiwix.kiwixmobile.core.error.ErrorActivity
import org.kiwix.kiwixmobile.core.extensions.browserIntent
import org.kiwix.kiwixmobile.core.extensions.getToolbarNavigationIcon
import org.kiwix.kiwixmobile.core.extensions.registerReceiver
import org.kiwix.kiwixmobile.core.extensions.setToolTipWithContentDescription
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.search.NAV_ARG_SEARCH_STRING
Expand Down Expand Up @@ -95,6 +98,9 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
@Inject lateinit var objectBoxToLibkiwixMigrator: ObjectBoxToLibkiwixMigrator
@Inject lateinit var objectBoxToRoomMigrator: ObjectBoxToRoomMigrator

@Inject
lateinit var downloadManagerBroadcastReceiver: DownloadManagerBroadcastReceiver

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.KiwixTheme)
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -125,6 +131,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
CoroutineScope(Dispatchers.IO).launch {
objectBoxToRoomMigrator.migrateObjectBoxDataToRoom()
}
downloadManagerBroadcastReceiver.let(::registerReceiver)
}

@Suppress("DEPRECATION")
Expand All @@ -141,6 +148,11 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
}
}

override fun onDestroy() {
downloadManagerBroadcastReceiver.let(::unregisterReceiver)
super.onDestroy()
}

open fun configureActivityBasedOn(destination: NavDestination) {
if (destination.id !in topLevelDestinations) {
handleDrawerOnNavigation()
Expand Down

0 comments on commit 5b651f8

Please sign in to comment.