Skip to content

Commit

Permalink
add flow to notify
Browse files Browse the repository at this point in the history
  • Loading branch information
Asutorufa committed Sep 22, 2024
1 parent 81fc4df commit 559c1be
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ import android.os.ParcelFileDescriptor
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.getSystemService
import io.github.asutorufa.yuhaiin.BuildConfig
import io.github.asutorufa.yuhaiin.IYuhaiinVpnBinder
import io.github.asutorufa.yuhaiin.MainActivity
import io.github.asutorufa.yuhaiin.R
import kotlinx.serialization.json.Json
import yuhaiin.*
import yuhaiin.App
import yuhaiin.Closer
import yuhaiin.MapStore
import yuhaiin.NotifySpped
import yuhaiin.Opts
import yuhaiin.SocketProtect
import yuhaiin.TUN
import yuhaiin.Yuhaiin
import java.net.InetSocketAddress


Expand Down Expand Up @@ -60,6 +68,22 @@ class YuhaiinVpnService : VpnService() {
private val uidDumper = UidDumper()
private val app = App()

private fun notificationBuilder(): NotificationCompat.Builder {
return NotificationCompat.Builder(this, packageName)
.setContentTitle(resources.getString(R.string.yuhaiin_running))
.setContentText(String.format(getString(R.string.notify_msg), "VPN"))
.setSmallIcon(R.drawable.emoji_nature)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(
this,
0,
Intent(this, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
}

inner class VpnBinder : IYuhaiinVpnBinder.Stub() {
override fun isRunning() = state == State.CONNECTED
override fun stop() = this@YuhaiinVpnService.onRevoke()
Expand Down Expand Up @@ -160,8 +184,8 @@ class YuhaiinVpnService : VpnService() {
val store = Yuhaiin.mapStoreFromJson(intent?.getByteArrayExtra("profile")!!)

configure(store)
startNotification()
start(store)
startNotification("Default")

state = State.CONNECTED
} catch (e: Exception) {
Expand Down Expand Up @@ -278,6 +302,10 @@ class YuhaiinVpnService : VpnService() {
app.start(Opts().apply {
mapStore = profile
savepath = getExternalFilesDir("yuhaiin").toString()
notifySpped = SpeedNotifier(
notificationBuilder(),
NotificationManagerCompat.from(this@YuhaiinVpnService)
)

tun = TUN().apply {
fd = mInterface!!.fd
Expand All @@ -293,7 +321,7 @@ class YuhaiinVpnService : VpnService() {
})
}

private fun startNotification(name: String) {
private fun startNotification(name: String = "Default") {
// Notifications on Oreo and above need a channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
notification.createNotificationChannel(
Expand All @@ -305,22 +333,26 @@ class YuhaiinVpnService : VpnService() {
)


startForeground(1, notificationBuilder().build())
}

inner class SpeedNotifier(
private var builder: NotificationCompat.Builder,
private val notificationManagerCompat: NotificationManagerCompat
) : NotifySpped {

private val enabled = notificationManagerCompat.areNotificationsEnabled()

override fun notifyEnable(): Boolean = enabled

startForeground(
1,
NotificationCompat.Builder(this, packageName)
.setContentTitle(resources.getString(R.string.yuhaiin_running))
.setContentText(String.format(getString(R.string.notify_msg), name))
.setSmallIcon(R.drawable.emoji_nature)
.setContentIntent(
PendingIntent.getActivity(
this,
0,
Intent(this, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
override fun notify(str: String) {
if (enabled)
notificationManagerCompat.notify(
1,
builder.setContentText(str)
.build()
)
.build()
)
}

}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jul 24 19:04:35 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit 559c1be

Please sign in to comment.