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
|
|
|
|
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
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.N)
|
|
|
|
class TunnelToggleActivity : AppCompatActivity() {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
val tunnel = Application.getTunnelManager().lastUsedTunnel ?: return
|
2020-03-27 05:26:53 +01:00
|
|
|
tunnel.setStateAsync(Tunnel.State.TOGGLE).whenComplete { _, t ->
|
2020-03-20 15:36:48 +01:00
|
|
|
TileService.requestListeningState(this, ComponentName(this, QuickTileService::class.java))
|
|
|
|
onToggleFinished(t)
|
|
|
|
finishAffinity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onToggleFinished(throwable: Throwable?) {
|
|
|
|
if (throwable == null) return
|
2020-03-27 10:46:30 +01:00
|
|
|
val error = ErrorMessages[throwable]
|
2020-03-20 15:36:48 +01:00
|
|
|
val message = getString(R.string.toggle_error, error)
|
|
|
|
Log.e(TAG, message, throwable)
|
|
|
|
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
2020-03-29 06:54:26 +02:00
|
|
|
private val TAG = "WireGuard/${TunnelToggleActivity::class.simpleName}"
|
2020-03-20 15:36:48 +01:00
|
|
|
}
|
|
|
|
}
|