warnings fix and custom vibration
This commit is contained in:
@@ -77,7 +77,7 @@ class TunVpnService : VpnService() {
|
||||
|
||||
try {
|
||||
activeSession = SessionManager().startMobile("$ip:$port", fd, cacheDir.absolutePath)
|
||||
triggerVibration()
|
||||
triggerVibration(true)
|
||||
sendStatusBroadcast("connected")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Rust fail", e)
|
||||
@@ -92,6 +92,7 @@ class TunVpnService : VpnService() {
|
||||
vpnInterface = null
|
||||
isServiceRunning = false
|
||||
sendStatusBroadcast("idle")
|
||||
triggerVibration(false)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
@@ -104,32 +105,108 @@ class TunVpnService : VpnService() {
|
||||
|
||||
private fun startForegroundNotification() {
|
||||
val channelId = "vpn_channel"
|
||||
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
val nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
nm.createNotificationChannel(NotificationChannel(channelId, "VPN Service", NotificationManager.IMPORTANCE_LOW))
|
||||
nm.createNotificationChannel(
|
||||
NotificationChannel(channelId, "VPN Service", NotificationManager.IMPORTANCE_LOW)
|
||||
)
|
||||
}
|
||||
|
||||
val stopPI = PendingIntent.getService(this, 1, Intent(this, TunVpnService::class.java).apply { action = ACTION_STOP_VPN },
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val stopPI = PendingIntent.getService(
|
||||
this, 1,
|
||||
Intent(this, TunVpnService::class.java).apply { action = ACTION_STOP_VPN },
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
|
||||
val contentPI = PendingIntent.getActivity(this, 0, packageManager.getLaunchIntentForPackage(packageName), PendingIntent.FLAG_IMMUTABLE)
|
||||
val contentPI = PendingIntent.getActivity(
|
||||
this, 0,
|
||||
packageManager.getLaunchIntentForPackage(packageName)!!,
|
||||
PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
val notification = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(this, channelId) else Notification.Builder(this))
|
||||
.setContentTitle("Netrunner VPN").setContentText("Защита активна")
|
||||
val stopIcon = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
android.graphics.drawable.Icon.createWithResource(this, android.R.drawable.ic_menu_close_clear_cancel)
|
||||
} else {
|
||||
null // Для совсем древних версий можно передать null или оставить старый метод
|
||||
}
|
||||
|
||||
val stopAction = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && stopIcon != null) {
|
||||
Notification.Action.Builder(
|
||||
stopIcon,
|
||||
"Отключить",
|
||||
stopPI
|
||||
).build()
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
Notification.Action(
|
||||
android.R.drawable.ic_menu_close_clear_cancel,
|
||||
"Отключить",
|
||||
stopPI
|
||||
)
|
||||
}
|
||||
|
||||
val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
Notification.Builder(this, channelId)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
Notification.Builder(this)
|
||||
}
|
||||
|
||||
val notification = builder
|
||||
.setContentTitle("Netrunner VPN")
|
||||
.setContentText("Защита активна")
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_info)
|
||||
.setContentIntent(contentPI).setOngoing(true)
|
||||
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Отключить", stopPI)
|
||||
.setContentIntent(contentPI)
|
||||
.setOngoing(true)
|
||||
.addAction(stopAction) // Используем новый объект Action
|
||||
.build()
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||
startForeground(1, notification, android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
|
||||
} else startForeground(1, notification)
|
||||
} else {
|
||||
startForeground(1, notification)
|
||||
}
|
||||
}
|
||||
|
||||
private fun triggerVibration() {
|
||||
val v = getSystemService(VIBRATOR_SERVICE) as Vibrator
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) v.vibrate(VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE))
|
||||
else @Suppress("DEPRECATION") v.vibrate(50)
|
||||
private fun triggerVibration(isAscending: Boolean) {
|
||||
val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
val vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
|
||||
vibratorManager.defaultVibrator
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val fixedDelay = 30L
|
||||
|
||||
val stepCount = 23
|
||||
|
||||
val timings = LongArray(stepCount * 2)
|
||||
val amplitudes = IntArray(stepCount * 2)
|
||||
|
||||
for (i in 0 until stepCount) {
|
||||
val progress = i.toFloat() / (stepCount - 1)
|
||||
|
||||
val activeTime = if (isAscending) {
|
||||
(10 + (progress * 50)).toLong()
|
||||
} else {
|
||||
(60 - (progress * 50)).toLong()
|
||||
}
|
||||
|
||||
timings[i * 2] = fixedDelay
|
||||
timings[i * 2 + 1] = activeTime
|
||||
|
||||
amplitudes[i * 2] = 0
|
||||
amplitudes[i * 2 + 1] = 255
|
||||
}
|
||||
|
||||
vibrator.vibrate(VibrationEffect.createWaveform(timings, amplitudes, -1))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
vibrator.vibrate(1500)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
Reference in New Issue
Block a user