#1386 stop notifications vibrating/making sounds

This commit is contained in:
Sean Mac Gillicuddy 2019-09-04 12:00:14 +01:00
parent f7f4d6e1f1
commit 5b716b39b2
7 changed files with 133 additions and 24 deletions

View File

@ -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;

View File

@ -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)
}

View File

@ -15,7 +15,7 @@
* 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.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
}
}

View File

@ -0,0 +1,105 @@
/*
* Kiwix Android
* Copyright (C) 2018 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.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)
}
}

View File

@ -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,

View File

@ -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", "");
}
}

View File

@ -27,14 +27,6 @@
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/downloadProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:padding="@dimen/download_progress_padding" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
@ -51,6 +43,14 @@
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Description" />
<ProgressBar
android:id="@+id/downloadProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:padding="@dimen/download_progress_padding" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"