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 2597209
Show file tree
Hide file tree
Showing 2 changed files with 46 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 @@ -58,8 +66,25 @@ class YuhaiinVpnService : VpnService() {
private val connectivity by lazy { application.getSystemService<ConnectivityManager>()!! }
private val notification by lazy { application.getSystemService<NotificationManager>()!! }
private val uidDumper = UidDumper()
private val speedNotifier = SpeedNotifier()
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 @@ -161,7 +186,7 @@ class YuhaiinVpnService : VpnService() {

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

state = State.CONNECTED
} catch (e: Exception) {
Expand Down Expand Up @@ -278,6 +303,7 @@ class YuhaiinVpnService : VpnService() {
app.start(Opts().apply {
mapStore = profile
savepath = getExternalFilesDir("yuhaiin").toString()
notifySpped = speedNotifier

tun = TUN().apply {
fd = mInterface!!.fd
Expand All @@ -293,7 +319,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 +331,23 @@ class YuhaiinVpnService : VpnService() {
)


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

inner class SpeedNotifier : NotifySpped {
override fun notifyEnable(): Boolean =
NotificationManagerCompat.from(this@YuhaiinVpnService).areNotificationsEnabled()

override fun notify(str: String) {
val notificationManagerCompat = NotificationManagerCompat.from(this@YuhaiinVpnService)

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
)
if (notificationManagerCompat.areNotificationsEnabled())
notificationManagerCompat.notify(
1,
notificationBuilder().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 2597209

Please sign in to comment.