2020-03-20 15:36:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2017-2019 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.annotation.RequiresApi
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2020-09-15 23:30:20 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2020-03-20 15:36:48 +01:00
|
|
|
import com.wireguard.android.Application
|
|
|
|
import com.wireguard.android.QuickTileService
|
|
|
|
import com.wireguard.android.R
|
|
|
|
import com.wireguard.android.backend.Tunnel
|
|
|
|
import com.wireguard.android.util.ErrorMessages
|
2020-09-14 19:46:49 +02:00
|
|
|
import kotlinx.coroutines.launch
|
2020-03-20 15:36:48 +01:00
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.N)
|
|
|
|
class TunnelToggleActivity : AppCompatActivity() {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
val tunnel = Application.getTunnelManager().lastUsedTunnel ?: return
|
2020-09-16 00:00:31 +02:00
|
|
|
lifecycleScope.launch {
|
2020-09-14 19:46:49 +02:00
|
|
|
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))
|
2020-03-20 15:36:48 +01:00
|
|
|
finishAffinity()
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 10:37:21 +02:00
|
|
|
|
2020-03-20 15:36:48 +01:00
|
|
|
companion object {
|
2020-03-29 07:19:02 +02:00
|
|
|
private const val TAG = "WireGuard/TunnelToggleActivity"
|
2020-03-20 15:36:48 +01:00
|
|
|
}
|
|
|
|
}
|