The difference between the usage of 21 and 24 is not big enough to hold on to all the baggage from API 21 Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
70 lines
2.7 KiB
Kotlin
70 lines
2.7 KiB
Kotlin
/*
|
|
* Copyright © 2017-2025 WireGuard LLC. All Rights Reserved.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
package com.wireguard.android.activity
|
|
|
|
import android.content.ComponentName
|
|
import android.os.Build
|
|
import android.os.Bundle
|
|
import android.service.quicksettings.TileService
|
|
import android.util.Log
|
|
import android.widget.Toast
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.annotation.RequiresApi
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.lifecycle.lifecycleScope
|
|
import com.wireguard.android.Application
|
|
import com.wireguard.android.QuickTileService
|
|
import com.wireguard.android.R
|
|
import com.wireguard.android.backend.GoBackend
|
|
import com.wireguard.android.backend.Tunnel
|
|
import com.wireguard.android.util.ErrorMessages
|
|
import kotlinx.coroutines.launch
|
|
|
|
class TunnelToggleActivity : AppCompatActivity() {
|
|
private val permissionActivityResultLauncher =
|
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { toggleTunnelWithPermissionsResult() }
|
|
|
|
private fun toggleTunnelWithPermissionsResult() {
|
|
val tunnel = Application.getTunnelManager().lastUsedTunnel ?: return
|
|
lifecycleScope.launch {
|
|
try {
|
|
tunnel.setStateAsync(Tunnel.State.TOGGLE)
|
|
} catch (e: Throwable) {
|
|
TileService.requestListeningState(this@TunnelToggleActivity, ComponentName(this@TunnelToggleActivity, QuickTileService::class.java))
|
|
val error = ErrorMessages[e]
|
|
val message = getString(R.string.toggle_error, error)
|
|
Log.e(TAG, message, e)
|
|
Toast.makeText(this@TunnelToggleActivity, message, Toast.LENGTH_LONG).show()
|
|
finishAffinity()
|
|
return@launch
|
|
}
|
|
TileService.requestListeningState(this@TunnelToggleActivity, ComponentName(this@TunnelToggleActivity, QuickTileService::class.java))
|
|
finishAffinity()
|
|
}
|
|
}
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
lifecycleScope.launch {
|
|
if (Application.getBackend() is GoBackend) {
|
|
try {
|
|
val intent = GoBackend.VpnService.prepare(this@TunnelToggleActivity)
|
|
if (intent != null) {
|
|
permissionActivityResultLauncher.launch(intent)
|
|
return@launch
|
|
}
|
|
} catch (e: Exception) {
|
|
Toast.makeText(this@TunnelToggleActivity, ErrorMessages[e], Toast.LENGTH_LONG).show()
|
|
}
|
|
}
|
|
toggleTunnelWithPermissionsResult()
|
|
}
|
|
}
|
|
|
|
companion object {
|
|
private const val TAG = "WireGuard/TunnelToggleActivity"
|
|
}
|
|
}
|