aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-03-19 17:54:32 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2020-03-19 18:55:45 -0600
commit8451321a790d38f3a6932edc852cdda5c8469df9 (patch)
tree0f30e28b1ca9c7c458e56eb5817a8b042b566f6e /ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt
parentTunnelListFragment: cleanup list type (diff)
downloadwireguard-android-8451321a790d38f3a6932edc852cdda5c8469df9.tar.xz
wireguard-android-8451321a790d38f3a6932edc852cdda5c8469df9.zip
preferences: rewrite in kotlin
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt')
-rw-r--r--ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt57
1 files changed, 57 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt
new file mode 100644
index 00000000..6c5284ec
--- /dev/null
+++ b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.kt
@@ -0,0 +1,57 @@
+/*
+ * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+package com.wireguard.android.preference
+
+import android.content.ActivityNotFoundException
+import android.content.Context
+import android.content.Intent
+import android.net.Uri
+import android.util.AttributeSet
+import androidx.preference.Preference
+import com.wireguard.android.Application
+import com.wireguard.android.BuildConfig
+import com.wireguard.android.R
+import com.wireguard.android.backend.Backend
+import com.wireguard.android.backend.GoBackend
+import com.wireguard.android.backend.WgQuickBackend
+import java.util.Locale
+
+class VersionPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
+ private var versionSummary: String? = null
+
+ override fun getSummary() = versionSummary
+
+ override fun getTitle() = context.getString(R.string.version_title, BuildConfig.VERSION_NAME)
+
+ override fun onClick() {
+ val intent = Intent(Intent.ACTION_VIEW)
+ intent.data = Uri.parse("https://www.wireguard.com/")
+ try {
+ context.startActivity(intent)
+ } catch (_: ActivityNotFoundException) {
+ }
+ }
+
+ companion object {
+ private fun getBackendPrettyName(context: Context, backend: Backend) = when (backend) {
+ is WgQuickBackend -> context.getString(R.string.type_name_kernel_module)
+ is GoBackend -> context.getString(R.string.type_name_go_userspace)
+ else -> ""
+ }
+ }
+
+ init {
+ Application.getBackendAsync().thenAccept { backend: Backend ->
+ versionSummary = getContext().getString(R.string.version_summary_checking, getBackendPrettyName(context, backend).toLowerCase(Locale.ENGLISH))
+ Application.getAsyncWorker().supplyAsync(backend::getVersion).whenComplete { version, exception ->
+ versionSummary = if (exception == null)
+ getContext().getString(R.string.version_summary, getBackendPrettyName(context, backend), version)
+ else
+ getContext().getString(R.string.version_summary_unknown, getBackendPrettyName(context, backend).toLowerCase(Locale.ENGLISH))
+ notifyChanged()
+ }
+ }
+ }
+}