notification off

This commit is contained in:
2026-03-18 20:32:33 +07:00
parent 8144d64f58
commit 8ffc14dfb0
3 changed files with 67 additions and 39 deletions
@@ -54,19 +54,16 @@ class TunVpnService : VpnService() {
private var activeSession: Session? = null
private var vpnInterface: ParcelFileDescriptor? = null
override fun onCreate() {
super.onCreate()
Log.d(TAG, "TunVpnService created")
companion object {
const val ACTION_STOP_VPN = "app.netrunner.vpn.STOP_FROM_NOTIFICATION"
@Volatile
var isServiceRunning = false
}
companion object {
var isRunning: Boolean = false
private set
// Метод для внешнего контроля (опционально)
fun setStatus(running: Boolean) {
isRunning = running
}
override fun onCreate() {
super.onCreate()
isServiceRunning = true
Log.d(TAG, "TunVpnService created")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -97,7 +94,7 @@ class TunVpnService : VpnService() {
}
}
"STOP_VPN" -> {
"STOP_VPN", ACTION_STOP_VPN -> {
Log.d(TAG, "STOP_VPN received")
stopServiceProperly()
}
@@ -172,7 +169,6 @@ class TunVpnService : VpnService() {
activeSession = manager.startMobile("$ip:$port", fd, cachePath)
triggerVibration()
sendStatusBroadcast("connected")
isRunning = true
Log.d(TAG, "Session started successfully")
} catch (e: Exception) {
@@ -190,7 +186,6 @@ class TunVpnService : VpnService() {
sendStatusBroadcast("idle")
activeSession?.stop()
activeSession = null
isRunning = false
try {
vpnInterface?.close()
@@ -208,6 +203,15 @@ class TunVpnService : VpnService() {
val channelName = "Netrunner VPN Service"
val notificationManager = getSystemService(android.app.NotificationManager::class.java)
val stopIntent = Intent(this, TunVpnService::class.java).apply {
action = ACTION_STOP_VPN
}
val stopPendingIntent = android.app.PendingIntent.getService(
this, 1, stopIntent,
android.app.PendingIntent.FLAG_IMMUTABLE or android.app.PendingIntent.FLAG_UPDATE_CURRENT
)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val channel = android.app.NotificationChannel(
channelId, channelName, android.app.NotificationManager.IMPORTANCE_DEFAULT
@@ -219,18 +223,25 @@ class TunVpnService : VpnService() {
}
// Создаем Intent, чтобы при клике на уведомление открывалось приложение
val pendingIntent = android.app.PendingIntent.getActivity(
val contentPendingIntent = android.app.PendingIntent.getActivity(
this, 0,
packageManager.getLaunchIntentForPackage(packageName),
android.app.PendingIntent.FLAG_IMMUTABLE
)
val stopAction = android.app.Notification.Action.Builder(
android.R.drawable.ic_menu_close_clear_cancel, // Иконка
"Отключить", // Текст
stopPendingIntent // Действие
).build()
val notification = android.app.Notification.Builder(this, channelId)
.setContentTitle("Netrunner VPN")
.setContentText("Защита активна")
.setSmallIcon(android.R.drawable.ic_dialog_info) // Стандартная системная иконка
.setContentIntent(pendingIntent)
.setContentIntent(contentPendingIntent)
.setOngoing(true)
.addAction(stopAction)
.setCategory(android.app.Notification.CATEGORY_SERVICE)
.build()
@@ -262,9 +273,9 @@ class TunVpnService : VpnService() {
}
override fun onDestroy() {
isRunning = false
Log.d(TAG, "TunVpnService destroyed")
sendStatusBroadcast("idle")
isServiceRunning = false
super.onDestroy()
}
}
@@ -499,14 +510,31 @@ class VpnPlugin(private val activity: Activity) : Plugin(activity) {
invoke.resolve()
}
private fun isVpnProcessRunning(): Boolean {
val manager = activity.getSystemService(android.content.Context.ACTIVITY_SERVICE) as android.app.ActivityManager
val runningProcesses = manager.runningAppProcesses ?: return false
val vpnProcessName = "${activity.packageName}:vpn"
for (processInfo in runningProcesses) {
if (processInfo.processName == vpnProcessName) {
return true
}
}
return false
}
@Command
fun checkActualVpnStatus(invoke: Invoke) {
val status = if (TunVpnService.isRunning) "connected" else "idle"
val hasPermission = VpnService.prepare(activity) == null
val isRunning = isVpnProcessRunning();
val status = if (hasPermission && isRunning) "connected" else "idle"
val res = JSObject()
res.put("status", status)
Log.d(TAG, "Checking status: permission=$hasPermission, running=$isRunning -> result=$status")
invoke.resolve(res)
}
}