Skip to content

Commit

Permalink
fix: resolve default collapsed state of speed display notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Restia-Ashbell committed Sep 19, 2024
1 parent a138218 commit 0a75b25
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 42 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ class SagerNet() : Application(),
NotificationChannel(
"service-vpn",
application.getText(R.string.service_vpn),
if (Build.VERSION.SDK_INT >= 28) NotificationManager.IMPORTANCE_MIN
else NotificationManager.IMPORTANCE_LOW
NotificationManager.IMPORTANCE_LOW
), // #1355
NotificationChannel(
"service-proxy",
Expand Down
68 changes: 28 additions & 40 deletions app/src/main/java/io/nekohasekai/sagernet/bg/ServiceNotification.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ class ServiceNotification(
), service.getString(
R.string.speed, Formatter.formatFileSize(service, stats.rxRateProxy)
), service.getString(
R.string.speed,
Formatter.formatFileSize(service, stats.txRateDirect)
R.string.speed, Formatter.formatFileSize(service, stats.txRateDirect)
), service.getString(
R.string.speed,
Formatter.formatFileSize(service, stats.rxRateDirect)
R.string.speed, Formatter.formatFileSize(service, stats.rxRateDirect)
)
)
it.setStyle(NotificationCompat.BigTextStyle().bigText(speedDetail))
Expand Down Expand Up @@ -100,7 +98,6 @@ class ServiceNotification(
}

suspend fun postNotificationWakeLockStatus(acquired: Boolean) {
updateActions()
useBuilder {
it.priority =
if (acquired) NotificationCompat.PRIORITY_HIGH else NotificationCompat.PRIORITY_LOW
Expand All @@ -111,14 +108,36 @@ class ServiceNotification(
private val showDirectSpeed = DataStore.showDirectSpeed

private val builder = NotificationCompat.Builder(service as Context, channel)
.setWhen(0)
.setShowWhen(false)
.setOngoing(true)
.setTicker(service.getString(R.string.forward_success))
.setContentTitle(title)
.setOnlyAlertOnce(true)
.setContentIntent(SagerNet.configureIntent(service))
.setSmallIcon(R.drawable.ic_service_active)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(if (visible) NotificationCompat.PRIORITY_LOW else NotificationCompat.PRIORITY_MIN)
.addAction(
NotificationCompat.Action.Builder(
0, service.getText(R.string.stop), PendingIntent.getBroadcast(
service, 0, Intent(Action.CLOSE).setPackage(service.packageName), flags
)
).setShowsUserInterface(false).build()
)
.addAction(
NotificationCompat.Action.Builder(
0, service.getString(R.string.action_switch), PendingIntent.getActivity(
service, 0, Intent(service, SwitchActivity::class.java), flags
)
).setShowsUserInterface(false).build()
)
.addAction(
NotificationCompat.Action.Builder(
0, service.getString(R.string.reset_connections),
PendingIntent.getBroadcast(
service, 0, Intent(Action.RESET_UPSTREAM_CONNECTIONS), flags
)
).setShowsUserInterface(false).build()
)

private val buildLock = Mutex()

Expand All @@ -141,40 +160,10 @@ class ServiceNotification(
})

runOnMainDispatcher {
updateActions()
show()
}
}

private suspend fun updateActions() {
service as Context
useBuilder {
it.clearActions()

val closeAction = NotificationCompat.Action.Builder(
0, service.getText(R.string.stop), PendingIntent.getBroadcast(
service, 0, Intent(Action.CLOSE).setPackage(service.packageName), flags
)
).setShowsUserInterface(false).build()
it.addAction(closeAction)

val switchAction = NotificationCompat.Action.Builder(
0, service.getString(R.string.action_switch), PendingIntent.getActivity(
service, 0, Intent(service, SwitchActivity::class.java), flags
)
).setShowsUserInterface(false).build()
it.addAction(switchAction)

val resetUpstreamAction = NotificationCompat.Action.Builder(
0, service.getString(R.string.reset_connections),
PendingIntent.getBroadcast(
service, 0, Intent(Action.RESET_UPSTREAM_CONNECTIONS), flags
)
).setShowsUserInterface(false).build()
it.addAction(resetUpstreamAction)
}
}

override fun onReceive(context: Context, intent: Intent) {
if (service.data.state == BaseService.State.Connected) {
listenPostSpeed = intent.action == Intent.ACTION_SCREEN_ON
Expand All @@ -185,9 +174,8 @@ class ServiceNotification(
private suspend fun show() =
useBuilder { (service as Service).startForeground(notificationId, it.build()) }

private suspend fun update() = useBuilder {
NotificationManagerCompat.from(service as Service).notify(notificationId, it.build())
}
private suspend fun update() =
useBuilder {NotificationManagerCompat.from(service as Service).notify(notificationId, it.build())}

fun destroy() {
listenPostSpeed = false
Expand Down

0 comments on commit 0a75b25

Please sign in to comment.