diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b73f94f..3db30da 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -74,7 +74,7 @@ android:resource="@xml/shortcuts" /> - + 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) + } } diff --git a/app/src/main/java/com/freeturn/app/service/ProxyTileService.kt b/app/src/main/java/com/freeturn/app/service/ProxyTileService.kt index 753e415..2edd0da 100644 --- a/app/src/main/java/com/freeturn/app/service/ProxyTileService.kt +++ b/app/src/main/java/com/freeturn/app/service/ProxyTileService.kt @@ -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 diff --git a/app/src/main/java/com/freeturn/app/service/ProxyWidgetProvider.kt b/app/src/main/java/com/freeturn/app/service/ProxyWidgetProvider.kt index 0c2b325..82b2042 100644 --- a/app/src/main/java/com/freeturn/app/service/ProxyWidgetProvider.kt +++ b/app/src/main/java/com/freeturn/app/service/ProxyWidgetProvider.kt @@ -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, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7eeb667..278dddd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -322,6 +322,7 @@ Ошибка подключения Ошибка WireGuard Нужно разрешение VPN - откройте приложение + Разрешение VPN не выдано - прокси не запущен Проверка VK