diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt b/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt index c6e06b7..208f776 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt @@ -21,6 +21,7 @@ import io.nekohasekai.libbox.PlatformInterface import io.nekohasekai.libbox.SystemProxyStatus import io.nekohasekai.sfa.Application import io.nekohasekai.sfa.BuildConfig +import io.nekohasekai.sfa.R import io.nekohasekai.sfa.constant.Action import io.nekohasekai.sfa.constant.Alert import io.nekohasekai.sfa.constant.Status @@ -122,6 +123,10 @@ class BoxService( private var lastProfileName = "" private suspend fun startService(delayStart: Boolean = false) { try { + withContext(Dispatchers.Main) { + notification.show(lastProfileName, R.string.status_starting) + } + val selectedProfileId = Settings.selectedProfile if (selectedProfileId == -1L) { stopAndAlert(Alert.EmptyConfiguration) @@ -142,6 +147,7 @@ class BoxService( lastProfileName = profile.name withContext(Dispatchers.Main) { + notification.show(lastProfileName, R.string.status_starting) binder.broadcast { it.onServiceResetLogs(listOf()) } @@ -166,10 +172,10 @@ class BoxService( boxService = newService commandServer?.setService(boxService) status.postValue(Status.Started) - withContext(Dispatchers.Main) { - notification.show(lastProfileName) + notification.show(lastProfileName, R.string.status_started) } + notification.start() } catch (e: Exception) { stopAndAlert(Alert.StartService, e.message) return diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt b/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt index 271e652..74b9aa4 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt @@ -9,6 +9,7 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.Build +import androidx.annotation.StringRes import androidx.core.app.NotificationCompat import androidx.core.app.ServiceCompat import androidx.lifecycle.MutableLiveData @@ -76,7 +77,7 @@ class ServiceNotification( } } - suspend fun show(lastProfileName: String) { + fun show(lastProfileName: String, @StringRes contentTextId: Int) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Application.notification.createNotificationChannel( NotificationChannel( @@ -87,14 +88,15 @@ class ServiceNotification( service.startForeground( notificationId, notificationBuilder .setContentTitle(lastProfileName.takeIf { it.isNotBlank() } ?: "sing-box") - .setContentText("service started").build() + .setContentText(service.getString(contentTextId)).build() ) - withContext(Dispatchers.IO) { - if (Settings.dynamicNotification) { - commandClient.connect() - withContext(Dispatchers.Main) { - registerReceiver() - } + } + + suspend fun start() { + if (Settings.dynamicNotification) { + commandClient.connect() + withContext(Dispatchers.Main) { + registerReceiver() } } }