diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ApplicationModule.java b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ApplicationModule.java index 7402619b2..02e7c8a41 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ApplicationModule.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ApplicationModule.java @@ -33,7 +33,7 @@ import org.kiwix.kiwixmobile.di.qualifiers.Computation; import org.kiwix.kiwixmobile.di.qualifiers.IO; import org.kiwix.kiwixmobile.di.qualifiers.MainThread; import org.kiwix.kiwixmobile.downloader.DownloadMonitor; -import org.kiwix.kiwixmobile.downloader.FetchDownloadMonitor; +import org.kiwix.kiwixmobile.downloader.fetch.FetchDownloadMonitor; import org.kiwix.kiwixmobile.utils.BookUtils; import org.kiwix.kiwixmobile.utils.LanguageUtils; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/DownloaderModule.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/DownloaderModule.kt index 00dcbb9c0..64ab1413e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/DownloaderModule.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/DownloaderModule.kt @@ -18,10 +18,10 @@ package org.kiwix.kiwixmobile.di.modules import android.content.Context -import com.tonyodev.fetch2.DefaultFetchNotificationManager import com.tonyodev.fetch2.Fetch import com.tonyodev.fetch2.Fetch.Impl import com.tonyodev.fetch2.FetchConfiguration +import com.tonyodev.fetch2.FetchNotificationManager import com.tonyodev.fetch2okhttp.OkHttpDownloader import dagger.Module import dagger.Provides @@ -32,6 +32,7 @@ import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao import org.kiwix.kiwixmobile.downloader.DownloadRequester import org.kiwix.kiwixmobile.downloader.Downloader import org.kiwix.kiwixmobile.downloader.DownloaderImpl +import org.kiwix.kiwixmobile.downloader.fetch.FetchDownloadNotificationManager import org.kiwix.kiwixmobile.downloader.fetch.FetchDownloadRequester import org.kiwix.kiwixmobile.utils.SharedPreferenceUtil import javax.inject.Singleton @@ -66,21 +67,25 @@ object DownloaderModule { @Singleton fun provideFetchConfiguration( context: Context, - okHttpDownloader: OkHttpDownloader + okHttpDownloader: OkHttpDownloader, + fetchNotificationManager: FetchNotificationManager ): FetchConfiguration = FetchConfiguration.Builder(context).apply { setDownloadConcurrentLimit(5) enableLogging(DEBUG) enableRetryOnNetworkGain(true) setHttpDownloader(okHttpDownloader) - setNotificationManager(object : DefaultFetchNotificationManager(context) { - override fun getFetchInstanceForNamespace(namespace: String) = - Fetch.getDefaultInstance() - }) + setNotificationManager(fetchNotificationManager) }.build().also(Impl::setDefaultInstanceConfiguration) @JvmStatic @Provides @Singleton fun provideOkHttpDownloader() = OkHttpDownloader(OkHttpClient.Builder().build()) + + @JvmStatic + @Provides + @Singleton + fun provideFetchDownloadNotificationManager(context: Context): FetchNotificationManager = + FetchDownloadNotificationManager(context) } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/FetchDownloadMonitor.kt b/app/src/main/java/org/kiwix/kiwixmobile/downloader/fetch/FetchDownloadMonitor.kt similarity index 96% rename from app/src/main/java/org/kiwix/kiwixmobile/downloader/FetchDownloadMonitor.kt rename to app/src/main/java/org/kiwix/kiwixmobile/downloader/fetch/FetchDownloadMonitor.kt index c137453be..e04f23e3e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/downloader/FetchDownloadMonitor.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/fetch/FetchDownloadMonitor.kt @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.kiwix.kiwixmobile.downloader +package org.kiwix.kiwixmobile.downloader.fetch import com.tonyodev.fetch2.Download import com.tonyodev.fetch2.Error @@ -25,6 +25,7 @@ import com.tonyodev.fetch2core.DownloadBlock import io.reactivex.schedulers.Schedulers import io.reactivex.subjects.PublishSubject import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao +import org.kiwix.kiwixmobile.downloader.DownloadMonitor import javax.inject.Inject class FetchDownloadMonitor @Inject constructor(fetch: Fetch, fetchDownloadDao: FetchDownloadDao) : @@ -104,10 +105,6 @@ class FetchDownloadMonitor @Inject constructor(fetch: Fetch, fetchDownloadDao: F init { fetch.addListener(fetchListener, true) - } - - override fun init() { - // empty method to so class does not get reported unused updater.subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe( { it.invoke() @@ -115,4 +112,8 @@ class FetchDownloadMonitor @Inject constructor(fetch: Fetch, fetchDownloadDao: F Throwable::printStackTrace ) } + + override fun init() { + // empty method to so class does not get reported unused + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/fetch/FetchDownloadNotificationManager.kt b/app/src/main/java/org/kiwix/kiwixmobile/downloader/fetch/FetchDownloadNotificationManager.kt new file mode 100644 index 000000000..6be106db2 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/fetch/FetchDownloadNotificationManager.kt @@ -0,0 +1,105 @@ +/* + * Kiwix Android + * Copyright (C) 2018 Kiwix + * + * 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 . + */ +package org.kiwix.kiwixmobile.downloader.fetch + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.media.AudioManager +import android.os.Build +import android.os.Build.VERSION_CODES +import androidx.annotation.RequiresApi +import androidx.core.app.NotificationCompat +import com.tonyodev.fetch2.DefaultFetchNotificationManager +import com.tonyodev.fetch2.DownloadNotification +import com.tonyodev.fetch2.Fetch +import com.tonyodev.fetch2.util.DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET +import org.kiwix.kiwixmobile.R +import org.kiwix.kiwixmobile.R.string + +class FetchDownloadNotificationManager(context: Context) : + DefaultFetchNotificationManager(context) { + override fun getFetchInstanceForNamespace(namespace: String) = Fetch.getDefaultInstance() + + override fun createNotificationChannels( + context: Context, + notificationManager: NotificationManager + ) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channelId = context.getString(R.string.fetch_notification_default_channel_id) + if (notificationManager.getNotificationChannel(channelId) == null) { + notificationManager.createNotificationChannel(createChannel(channelId, context)) + } + } + } + + override fun notify(groupId: Int) { + super.notify(groupId) + } + + override fun updateNotification( + notificationBuilder: NotificationCompat.Builder, + downloadNotification: DownloadNotification, + context: Context + ) { + val smallIcon = if (downloadNotification.isDownloading) { + android.R.drawable.stat_sys_download + } else { + android.R.drawable.stat_sys_download_done + } + notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setSmallIcon(smallIcon) + .setContentTitle(downloadNotification.title) + .setContentText(getSubtitleText(context, downloadNotification)) + .setOngoing(downloadNotification.isOnGoingNotification) + .setGroup(downloadNotification.groupId.toString()) + .setSound(null) + .setSound(null, AudioManager.STREAM_NOTIFICATION) + .setVibrate(null) + .setGroupSummary(false) + if (downloadNotification.isFailed || downloadNotification.isCompleted) { + notificationBuilder.setProgress(0, 0, false) + } else { + val progressIndeterminate = downloadNotification.progressIndeterminate + val maxProgress = if (downloadNotification.progressIndeterminate) 0 else 100 + val progress = if (downloadNotification.progress < 0) 0 else downloadNotification.progress + notificationBuilder.setProgress(maxProgress, progress, progressIndeterminate) + } + when { + downloadNotification.isDownloading || + downloadNotification.isPaused || + downloadNotification.isQueued -> { + notificationBuilder.setTimeoutAfter(getNotificationTimeOutMillis()) + } + else -> { + notificationBuilder.setTimeoutAfter(DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET) + } + } + } + + @RequiresApi(VERSION_CODES.O) + private fun createChannel(channelId: String, context: Context) = + NotificationChannel( + channelId, + context.getString(string.fetch_notification_default_channel_name), + NotificationManager.IMPORTANCE_DEFAULT + ).apply { + setSound(null, null) + enableVibration(false) + } +} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/model/DownloadModel.kt b/app/src/main/java/org/kiwix/kiwixmobile/downloader/model/DownloadModel.kt index 53b7a82d6..5739be5ea 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/downloader/model/DownloadModel.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/model/DownloadModel.kt @@ -35,7 +35,7 @@ data class DownloadModel( val progress: Int, val book: Book ) { - val fileNameFromUrl: String get() = StorageUtils.getFileNameFromUrl(book.url) + val fileNameFromUrl: String by lazy { StorageUtils.getFileNameFromUrl(book.url) } constructor(downloadEntity: FetchDownloadEntity) : this( downloadEntity.id, diff --git a/app/src/main/java/org/kiwix/kiwixmobile/utils/StorageUtils.java b/app/src/main/java/org/kiwix/kiwixmobile/utils/StorageUtils.java index a62b36cf3..7e757340c 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/utils/StorageUtils.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/utils/StorageUtils.java @@ -20,8 +20,6 @@ package org.kiwix.kiwixmobile.utils; public class StorageUtils { public static String getFileNameFromUrl(String url) { - String filename = NetworkUtils.getFileNameFromUrl(url); - filename = filename.replace(".meta4", ""); - return filename; + return NetworkUtils.getFileNameFromUrl(url).replace(".meta4", ""); } } diff --git a/app/src/main/res/layout/download_item.xml b/app/src/main/res/layout/download_item.xml index f99e66a32..0d71469b1 100644 --- a/app/src/main/res/layout/download_item.xml +++ b/app/src/main/res/layout/download_item.xml @@ -27,14 +27,6 @@ android:gravity="center" android:orientation="vertical"> - - + +