Add cancel action to batch-test notification (#6009)

This commit is contained in:
eliotcougar authored and GitHub committed 2026-08-03 20:42:04 +08:00
1 parent 19b0c9a2c3
commit 31dfaeed6c
3 files changed
+31 -8

No files matched your search

@@ -77,15 +77,18 @@ object NotificationHelper {
* @param channelType The notification channel type
* @param title The notification title
* @param content The notification content text
* @param action An optional action retained when the notification is updated
*/
fun startForeground(
service: Service,
channelType: NotificationChannelType,
title: String,
content: String
content: String,
action: NotificationCompat.Action? = null
) {
ensureChannelCreated(channelType, service)
val builder = buildNotificationBuilder(channelType, service, title, content)
val builder = buildNotificationBuilder(channelType, service, title, content, action)
builderCache[channelType.notificationId] = builder
service.startForeground(channelType.notificationId, builder.build())
}
@@ -141,7 +144,8 @@ object NotificationHelper {
channelType: NotificationChannelType,
context: Context,
title: String,
content: String
content: String,
action: NotificationCompat.Action? = null
): NotificationCompat.Builder {
val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelType.channelId
@@ -158,5 +162,6 @@ object NotificationHelper {
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.apply { action?.let(::addAction) }
}
}
}
@@ -1,8 +1,10 @@
package com.v2ray.ang.service
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.core.CoreNativeManager
@@ -21,6 +23,23 @@ class CoreTestService : Service() {
// manage active batch workers so each batch is independent and cancellable
private val activeWorkers = Collections.synchronizedList(mutableListOf<RealPingWorkerService>())
private val cancelAction by lazy {
val intent = Intent(this, CoreTestService::class.java).putExtra(
"content",
TestServiceMessage(AppConfig.MSG_MEASURE_CONFIG_CANCEL)
)
val pendingIntent = PendingIntent.getService(
this,
NotificationChannelType.CORE_TEST.notificationId,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
NotificationCompat.Action.Builder(
R.drawable.ic_stop_24dp,
getString(android.R.string.cancel),
pendingIntent
).build()
}
/**
* Initializes the V2Ray environment.
@@ -64,7 +83,8 @@ class CoreTestService : Service() {
this,
NotificationChannelType.CORE_TEST,
getString(R.string.app_name),
getString(R.string.title_real_ping_all_server)
getString(R.string.title_real_ping_all_server),
cancelAction
)
val message = intent?.serializable<TestServiceMessage>("content")
if (message == null) {
@@ -140,9 +140,7 @@ class MainViewModel(
}
is MainServiceEvent.MeasureConfigFinish -> {
if (event.finishedCount == "0") {
onTestsFinished()
}
onTestsFinished()
}
}
}