AutoFixService fixes
This commit is contained in:
@@ -17,6 +17,7 @@ import dagger.hilt.android.AndroidEntryPoint
|
|||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.koitharu.kotatsu.R
|
import org.koitharu.kotatsu.R
|
||||||
import org.koitharu.kotatsu.alternatives.domain.AutoFixUseCase
|
import org.koitharu.kotatsu.alternatives.domain.AutoFixUseCase
|
||||||
|
import org.koitharu.kotatsu.alternatives.domain.AutoFixUseCase.NoAlternativesException
|
||||||
import org.koitharu.kotatsu.core.ErrorReporterReceiver
|
import org.koitharu.kotatsu.core.ErrorReporterReceiver
|
||||||
import org.koitharu.kotatsu.core.model.getTitle
|
import org.koitharu.kotatsu.core.model.getTitle
|
||||||
import org.koitharu.kotatsu.core.model.isNsfw
|
import org.koitharu.kotatsu.core.model.isNsfw
|
||||||
@@ -47,7 +48,7 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
notificationManager = NotificationManagerCompat.from(applicationContext)
|
notificationManager = NotificationManagerCompat.from(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun IntentJobContext.processIntent(intent: Intent) {
|
override suspend fun IntentJobContext.processIntent(intent: Intent) {
|
||||||
@@ -58,7 +59,7 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
val result = runCatchingCancellable {
|
val result = runCatchingCancellable {
|
||||||
autoFixUseCase.invoke(mangaId)
|
autoFixUseCase.invoke(mangaId)
|
||||||
}
|
}
|
||||||
if (applicationContext.checkNotificationPermission(CHANNEL_ID)) {
|
if (checkNotificationPermission(CHANNEL_ID)) {
|
||||||
val notification = buildNotification(startId, result)
|
val notification = buildNotification(startId, result)
|
||||||
notificationManager.notify(TAG, startId, notification)
|
notificationManager.notify(TAG, startId, notification)
|
||||||
}
|
}
|
||||||
@@ -67,7 +68,7 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun IntentJobContext.onError(error: Throwable) {
|
override fun IntentJobContext.onError(error: Throwable) {
|
||||||
if (applicationContext.checkNotificationPermission(CHANNEL_ID)) {
|
if (checkNotificationPermission(CHANNEL_ID)) {
|
||||||
val notification = runBlocking { buildNotification(startId, Result.failure(error)) }
|
val notification = runBlocking { buildNotification(startId, Result.failure(error)) }
|
||||||
notificationManager.notify(TAG, startId, notification)
|
notificationManager.notify(TAG, startId, notification)
|
||||||
}
|
}
|
||||||
@@ -75,7 +76,7 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
|
|
||||||
@SuppressLint("InlinedApi")
|
@SuppressLint("InlinedApi")
|
||||||
private fun startForeground(jobContext: IntentJobContext) {
|
private fun startForeground(jobContext: IntentJobContext) {
|
||||||
val title = applicationContext.getString(R.string.fixing_manga)
|
val title = getString(R.string.fixing_manga)
|
||||||
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_MIN)
|
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_MIN)
|
||||||
.setName(title)
|
.setName(title)
|
||||||
.setShowBadge(false)
|
.setShowBadge(false)
|
||||||
@@ -85,7 +86,7 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
.build()
|
.build()
|
||||||
notificationManager.createNotificationChannel(channel)
|
notificationManager.createNotificationChannel(channel)
|
||||||
|
|
||||||
val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
|
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
.setContentTitle(title)
|
.setContentTitle(title)
|
||||||
.setPriority(NotificationCompat.PRIORITY_MIN)
|
.setPriority(NotificationCompat.PRIORITY_MIN)
|
||||||
.setDefaults(0)
|
.setDefaults(0)
|
||||||
@@ -97,7 +98,7 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||||
.addAction(
|
.addAction(
|
||||||
appcompatR.drawable.abc_ic_clear_material,
|
appcompatR.drawable.abc_ic_clear_material,
|
||||||
applicationContext.getString(android.R.string.cancel),
|
getString(android.R.string.cancel),
|
||||||
jobContext.getCancelIntent(),
|
jobContext.getCancelIntent(),
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
@@ -110,7 +111,7 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun buildNotification(startId: Int, result: Result<Pair<Manga, Manga?>>): Notification {
|
private suspend fun buildNotification(startId: Int, result: Result<Pair<Manga, Manga?>>): Notification {
|
||||||
val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
|
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||||
.setDefaults(0)
|
.setDefaults(0)
|
||||||
.setSilent(true)
|
.setSilent(true)
|
||||||
@@ -119,17 +120,17 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
if (replacement != null) {
|
if (replacement != null) {
|
||||||
notification.setLargeIcon(
|
notification.setLargeIcon(
|
||||||
coil.execute(
|
coil.execute(
|
||||||
ImageRequest.Builder(applicationContext)
|
ImageRequest.Builder(this)
|
||||||
.data(replacement.coverUrl)
|
.data(replacement.coverUrl)
|
||||||
.mangaSourceExtra(replacement.source)
|
.mangaSourceExtra(replacement.source)
|
||||||
.build(),
|
.build(),
|
||||||
).toBitmapOrNull(),
|
).toBitmapOrNull(),
|
||||||
)
|
)
|
||||||
notification.setSubText(replacement.title)
|
notification.setSubText(replacement.title)
|
||||||
val intent = AppRouter.detailsIntent(applicationContext, replacement)
|
val intent = AppRouter.detailsIntent(this, replacement)
|
||||||
notification.setContentIntent(
|
notification.setContentIntent(
|
||||||
PendingIntentCompat.getActivity(
|
PendingIntentCompat.getActivity(
|
||||||
applicationContext,
|
this,
|
||||||
replacement.id.toInt(),
|
replacement.id.toInt(),
|
||||||
intent,
|
intent,
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT,
|
PendingIntent.FLAG_UPDATE_CURRENT,
|
||||||
@@ -143,35 +144,35 @@ class AutoFixService : CoroutineIntentService() {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
notification
|
notification
|
||||||
.setContentTitle(applicationContext.getString(R.string.fixed))
|
.setContentTitle(getString(R.string.fixed))
|
||||||
.setContentText(
|
.setContentText(
|
||||||
applicationContext.getString(
|
getString(
|
||||||
R.string.manga_replaced,
|
R.string.manga_replaced,
|
||||||
seed.title,
|
seed.title,
|
||||||
seed.source.getTitle(applicationContext),
|
seed.source.getTitle(this),
|
||||||
replacement.title,
|
replacement.title,
|
||||||
replacement.source.getTitle(applicationContext),
|
replacement.source.getTitle(this),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.setSmallIcon(R.drawable.ic_stat_done)
|
.setSmallIcon(R.drawable.ic_stat_done)
|
||||||
} else {
|
} else {
|
||||||
notification
|
notification
|
||||||
.setContentTitle(applicationContext.getString(R.string.fixing_manga))
|
.setContentTitle(getString(R.string.fixing_manga))
|
||||||
.setContentText(applicationContext.getString(R.string.no_fix_required, seed.title))
|
.setContentText(getString(R.string.no_fix_required, seed.title))
|
||||||
.setSmallIcon(android.R.drawable.stat_sys_warning)
|
.setSmallIcon(android.R.drawable.stat_sys_warning)
|
||||||
}
|
}
|
||||||
}.onFailure { error ->
|
}.onFailure { error ->
|
||||||
notification
|
notification
|
||||||
.setContentTitle(applicationContext.getString(R.string.error_occurred))
|
.setContentTitle(getString(R.string.error_occurred))
|
||||||
.setContentText(
|
.setContentText(
|
||||||
if (error is AutoFixUseCase.NoAlternativesException) {
|
if (error is NoAlternativesException) {
|
||||||
applicationContext.getString(R.string.no_alternatives_found, error.seed.manga.title)
|
getString(R.string.no_alternatives_found, error.seed.manga.title)
|
||||||
} else {
|
} else {
|
||||||
error.getDisplayMessage(applicationContext.resources)
|
error.getDisplayMessage(resources)
|
||||||
},
|
},
|
||||||
).setSmallIcon(android.R.drawable.stat_notify_error)
|
).setSmallIcon(android.R.drawable.stat_notify_error)
|
||||||
ErrorReporterReceiver.getNotificationAction(
|
ErrorReporterReceiver.getNotificationAction(
|
||||||
context = applicationContext,
|
context = this,
|
||||||
e = error,
|
e = error,
|
||||||
notificationId = startId,
|
notificationId = startId,
|
||||||
notificationTag = TAG,
|
notificationTag = TAG,
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.app.PendingIntent
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.BadParcelableException
|
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import androidx.core.app.PendingIntentCompat
|
import androidx.core.app.PendingIntentCompat
|
||||||
@@ -65,7 +64,7 @@ class ErrorReporterReceiver : BroadcastReceiver() {
|
|||||||
e: Throwable,
|
e: Throwable,
|
||||||
notificationId: Int,
|
notificationId: Int,
|
||||||
notificationTag: String?,
|
notificationTag: String?,
|
||||||
): PendingIntent? = try {
|
): PendingIntent? = runCatching {
|
||||||
val intent = Intent(context, ErrorReporterReceiver::class.java)
|
val intent = Intent(context, ErrorReporterReceiver::class.java)
|
||||||
intent.setAction(ACTION_REPORT)
|
intent.setAction(ACTION_REPORT)
|
||||||
intent.setData("err://${e.hashCode()}".toUri())
|
intent.setData("err://${e.hashCode()}".toUri())
|
||||||
@@ -73,9 +72,9 @@ class ErrorReporterReceiver : BroadcastReceiver() {
|
|||||||
intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId)
|
intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId)
|
||||||
intent.putExtra(EXTRA_NOTIFICATION_TAG, notificationTag)
|
intent.putExtra(EXTRA_NOTIFICATION_TAG, notificationTag)
|
||||||
PendingIntentCompat.getBroadcast(context, 0, intent, 0, false)
|
PendingIntentCompat.getBroadcast(context, 0, intent, 0, false)
|
||||||
} catch (e: BadParcelableException) {
|
}.onFailure { e ->
|
||||||
|
// probably cannot write exception as serializable
|
||||||
e.printStackTraceDebug()
|
e.printStackTraceDebug()
|
||||||
null
|
}.getOrNull()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user