fix(service): запрос VPN-разрешения при старте из виджета и тайла

This commit is contained in:
samosvalishe committed 2026-08-25 16:29:07 +03:00
1 parent bb0a40ba9b
commit 29dc9a1999
5 files changed
+93 -20

No files matched your search

+1 -1
View File
@@ -74,7 +74,7 @@
android:resource="@xml/shortcuts" />
</activity>
<!-- Трамплин App Shortcuts (long-press иконки): шлёт START/STOP в ProxyReceiver. -->
<!-- Трамплин внешних входов (ярлыки, виджет, тайл): START с запросом согласия на VPN. -->
<activity
android:name=".service.ProxyShortcutActivity"
android:exported="true"
@@ -1,21 +1,69 @@
package com.freeturn.app.service
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.VpnService
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import com.freeturn.app.R
import com.freeturn.app.data.AppPreferences
import com.freeturn.app.domain.proxy.ProxyServiceLauncher
import com.freeturn.app.domain.proxy.ProxyStore
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
/**
* Прозрачный трамплин для App Shortcuts (long-press иконки). Launcher запускает
* только activity - шлём START/STOP в [ProxyReceiver] и сразу закрываемся.
* Прозрачный трамплин внешних входов (ярлыки, виджет, тайл): единственное место, где
* START может спросить согласие на VPN - системный диалог поднимается только из activity,
* а без него в WG-режиме `establish()` вернёт null и сессия умрёт с "откройте приложение".
*/
class ProxyShortcutActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
when (intent?.action) {
ProxyActions.START, ProxyActions.STOP -> sendBroadcast(
Intent(this, ProxyReceiver::class.java).setAction(intent.action)
)
}
class ProxyShortcutActivity : ComponentActivity() {
private val prefs: AppPreferences by inject()
private val launcher: ProxyServiceLauncher by inject()
private val consent = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) launcher.start()
else ProxyStore.fail(getString(R.string.notif_proxy_vpn_denied))
finish()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState != null) return
when (intent?.action) {
ProxyActions.START -> lifecycleScope.launch { startWithConsent() }
ProxyActions.STOP -> {
launcher.stop()
finish()
}
else -> finish()
}
}
private suspend fun startWithConsent() {
val vpnIntent = if (prefs.clientConfigFlow.first().wireGuardActive) {
VpnService.prepare(this)
} else null
if (vpnIntent == null) {
launcher.start()
finish()
} else {
consent.launch(vpnIntent)
}
}
companion object {
fun startIntent(context: Context): Intent =
Intent(context, ProxyShortcutActivity::class.java)
.setAction(ProxyActions.START)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
}
@@ -1,7 +1,9 @@
package com.freeturn.app.service
import android.app.PendingIntent
import android.content.Intent
import android.graphics.drawable.Icon
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import com.freeturn.app.R
@@ -45,10 +47,25 @@ class ProxyTileService : TileService() {
override fun onClick() {
super.onClick()
val action = if (running) ProxyActions.STOP else ProxyActions.START
sendBroadcast(Intent(this, ProxyReceiver::class.java).setAction(action))
if (running) {
sendBroadcast(Intent(this, ProxyReceiver::class.java).setAction(ProxyActions.STOP))
return
}
// START - через activity-трамплин: согласие на VPN спрашивается только оттуда.
val intent = ProxyShortcutActivity.startIntent(this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startActivityAndCollapse(
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
)
} else {
startActivityAndCollapseLegacy(intent)
}
}
/** PendingIntent-перегрузка появилась только в API 34, ниже другого способа свернуть шторку нет. */
@Suppress("DEPRECATION")
private fun startActivityAndCollapseLegacy(intent: Intent) = startActivityAndCollapse(intent)
private fun render() {
val tile = qsTile ?: return
tile.state = if (running) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
@@ -111,16 +111,23 @@ class ProxyWidgetProvider : AppWidgetProvider(), KoinComponent {
views.setContentDescription(R.id.widget_toggle, context.getString(R.string.widget_action_start))
}
val toggleAction = if (running) ProxyActions.STOP else ProxyActions.START
views.setOnClickPendingIntent(
R.id.widget_toggle,
// START - через activity-трамплин: согласие на VPN спрашивается только оттуда.
val toggle = if (running) {
PendingIntent.getBroadcast(
context,
if (running) 1 else 2,
Intent(context, ProxyReceiver::class.java).setAction(toggleAction),
1,
Intent(context, ProxyReceiver::class.java).setAction(ProxyActions.STOP),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
} else {
PendingIntent.getActivity(
context,
2,
ProxyShortcutActivity.startIntent(context),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}
views.setOnClickPendingIntent(R.id.widget_toggle, toggle)
views.setOnClickPendingIntent(
R.id.widget_body,
+1
View File
@@ -322,6 +322,7 @@
<string name="notif_proxy_connect_error">Ошибка подключения</string>
<string name="notif_proxy_wireguard_error">Ошибка WireGuard</string>
<string name="notif_proxy_vpn_permission">Нужно разрешение VPN - откройте приложение</string>
<string name="notif_proxy_vpn_denied">Разрешение VPN не выдано - прокси не запущен</string>
<!-- CaptchaWebViewDialog -->
<string name="captcha_title">Проверка VK</string>