Исправлено много варнингов
This commit is contained in:
@@ -3,7 +3,9 @@ package com.github.nullptroma.wallenc.app.tasks
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.IBinder
|
||||
import android.view.View
|
||||
@@ -52,7 +54,14 @@ class TaskPipelineForegroundService : Service() {
|
||||
private val serviceJob = SupervisorJob()
|
||||
private val serviceScope = CoroutineScope(serviceJob + Dispatchers.Main.immediate)
|
||||
|
||||
override fun onBind(intent: android.content.Intent?): IBinder? = null
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
if (intent?.action == ACTION_CANCEL_ALL_TASKS && ::orchestrator.isInitialized) {
|
||||
orchestrator.cancelAll()
|
||||
}
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
override fun onCreate() {
|
||||
@@ -133,6 +142,11 @@ class TaskPipelineForegroundService : Service() {
|
||||
.setSmallIcon(android.R.drawable.stat_sys_download)
|
||||
.setOngoing(true)
|
||||
.setOnlyAlertOnce(true)
|
||||
.addAction(
|
||||
0,
|
||||
getString(R.string.task_notification_cancel),
|
||||
cancelAllTasksPendingIntent(),
|
||||
)
|
||||
.build()
|
||||
|
||||
private fun buildAccumulatedNotification(tasks: List<TaskForegroundItem>): Notification {
|
||||
@@ -141,19 +155,44 @@ class TaskPipelineForegroundService : Service() {
|
||||
applyNotificationTemplateTextColor(big)
|
||||
bindTaskRows(big, sorted)
|
||||
|
||||
val collapsedSubtext = when {
|
||||
sorted.isEmpty() ->
|
||||
getString(R.string.task_notification_preparing)
|
||||
|
||||
sorted.all { it.progress?.fraction == null } ->
|
||||
getString(R.string.task_notification_indeterminate)
|
||||
|
||||
else -> resources.getQuantityString(
|
||||
R.plurals.task_notification_group_subtext,
|
||||
sorted.size,
|
||||
sorted.size,
|
||||
)
|
||||
}
|
||||
|
||||
return NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle(getString(R.string.task_notification_title))
|
||||
.setContentText(
|
||||
getString(R.string.task_notification_group_subtext, sorted.size),
|
||||
)
|
||||
.setContentText(collapsedSubtext)
|
||||
.setSmallIcon(android.R.drawable.stat_sys_download)
|
||||
.setOngoing(true)
|
||||
.setOnlyAlertOnce(true)
|
||||
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
|
||||
.setCustomBigContentView(big)
|
||||
.addAction(
|
||||
0,
|
||||
getString(R.string.task_notification_cancel),
|
||||
cancelAllTasksPendingIntent(),
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun cancelAllTasksPendingIntent(): PendingIntent =
|
||||
PendingIntent.getService(
|
||||
this,
|
||||
0,
|
||||
Intent(this, TaskPipelineForegroundService::class.java).setAction(ACTION_CANCEL_ALL_TASKS),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
||||
)
|
||||
|
||||
/**
|
||||
* Uses only [android.R.attr.textColor] from [android.R.style.TextAppearance_Material_Notification_Title]
|
||||
* so line size stays from layout (13sp) while color matches system notification title text.
|
||||
@@ -199,7 +238,7 @@ class TaskPipelineForegroundService : Service() {
|
||||
showTaskRow(remoteViews, i, sorted[i])
|
||||
}
|
||||
val remaining = n - (MAX_TASK_ROWS - 1)
|
||||
showOverflowRow(remoteViews, MAX_TASK_ROWS - 1, remaining)
|
||||
showOverflowRow(remoteViews, remaining)
|
||||
}
|
||||
advanceIndeterminateDotPhasesAfterBind(sorted, n)
|
||||
}
|
||||
@@ -254,14 +293,19 @@ class TaskPipelineForegroundService : Service() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun showOverflowRow(remoteViews: RemoteViews, index: Int, remainingCount: Int) {
|
||||
remoteViews.setViewVisibility(TASK_ROW_IDS[index], View.VISIBLE)
|
||||
private fun showOverflowRow(remoteViews: RemoteViews, remainingCount: Int) {
|
||||
val overflowIndex = MAX_TASK_ROWS - 1
|
||||
remoteViews.setViewVisibility(TASK_ROW_IDS[overflowIndex], View.VISIBLE)
|
||||
remoteViews.setTextViewText(
|
||||
TASK_TITLE_IDS[index],
|
||||
getString(R.string.task_notification_more_tasks, remainingCount),
|
||||
TASK_TITLE_IDS[overflowIndex],
|
||||
resources.getQuantityString(
|
||||
R.plurals.task_notification_more_tasks,
|
||||
remainingCount,
|
||||
remainingCount,
|
||||
),
|
||||
)
|
||||
remoteViews.setViewVisibility(TASK_LABEL_BAR_ROW_IDS[index], View.GONE)
|
||||
remoteViews.setTextViewText(TASK_SUBTITLE_IDS[index], "")
|
||||
remoteViews.setViewVisibility(TASK_LABEL_BAR_ROW_IDS[overflowIndex], View.GONE)
|
||||
remoteViews.setTextViewText(TASK_SUBTITLE_IDS[overflowIndex], "")
|
||||
}
|
||||
|
||||
private fun indeterminateSubtitleWithDots(taskId: TaskId, baseLabel: String): String {
|
||||
@@ -276,6 +320,9 @@ class TaskPipelineForegroundService : Service() {
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ACTION_CANCEL_ALL_TASKS =
|
||||
"com.github.nullptroma.wallenc.action.CANCEL_ALL_TASKS"
|
||||
|
||||
private const val CHANNEL_ID = "wallenc_task_pipeline"
|
||||
private const val FOREGROUND_NOTIFICATION_ID = 1001
|
||||
|
||||
|
||||
11
app/src/main/res/values/plurals.xml
Normal file
11
app/src/main/res/values/plurals.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<plurals name="task_notification_group_subtext">
|
||||
<item quantity="one">%d task running</item>
|
||||
<item quantity="other">%d tasks running</item>
|
||||
</plurals>
|
||||
<plurals name="task_notification_more_tasks">
|
||||
<item quantity="one">+%d more</item>
|
||||
<item quantity="other">+%d more</item>
|
||||
</plurals>
|
||||
</resources>
|
||||
@@ -5,6 +5,4 @@
|
||||
<string name="task_notification_preparing">Preparing…</string>
|
||||
<string name="task_notification_indeterminate">Working…</string>
|
||||
<string name="task_notification_cancel">Cancel</string>
|
||||
<string name="task_notification_group_subtext">%d tasks running</string>
|
||||
<string name="task_notification_more_tasks">+%d more</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user