summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-03-20 20:06:48 +0530
committerJason A. Donenfeld <Jason@zx2c4.com>2020-03-20 22:22:56 -0600
commit04d0b819f60588bed60d8ccaf23b523c92fe2168 (patch)
treed9ea495d597a2a93bba6bf87fcd3f908396d1816 /ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt
parent85aa5fbd46aa7cae0b6b4bf4e97b52609646fb13 (diff)
Convert activity package to Kotlin
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt')
-rw-r--r--ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt44
1 files changed, 44 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt b/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt
new file mode 100644
index 00000000..928a1108
--- /dev/null
+++ b/ui/src/main/java/com/wireguard/android/activity/TunnelToggleActivity.kt
@@ -0,0 +1,44 @@
+/*
+ * 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
+ tunnel.setState(Tunnel.State.TOGGLE).whenComplete { _, t ->
+ TileService.requestListeningState(this, ComponentName(this, QuickTileService::class.java))
+ onToggleFinished(t)
+ finishAffinity()
+ }
+ }
+
+ private fun onToggleFinished(throwable: Throwable?) {
+ if (throwable == null) return
+ val error = ErrorMessages.get(throwable)
+ val message = getString(R.string.toggle_error, error)
+ Log.e(TAG, message, throwable)
+ Toast.makeText(this, message, Toast.LENGTH_LONG).show()
+ }
+
+ companion object {
+ private val TAG = "WireGuard/" + TunnelToggleActivity::class.java.simpleName
+ }
+}