diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-03 18:20:44 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-27 16:21:52 +0000 |
| commit | 6944f2362db6b6c85d2d1bb8f41d95f26f109044 (patch) | |
| tree | 9f03f8c40e8991e71d2a997dfd5e0853126c3b66 /android/src | |
| parent | fd4f89120c95021f4d150640bec891e2387657db (diff) | |
| download | mullvadvpn-6944f2362db6b6c85d2d1bb8f41d95f26f109044.tar.xz mullvadvpn-6944f2362db6b6c85d2d1bb8f41d95f26f109044.zip | |
Create `UrlButton` widget
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/UrlButton.kt | 100 | ||||
| -rw-r--r-- | android/src/main/res/values/attrs.xml | 4 |
2 files changed, 104 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/UrlButton.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/UrlButton.kt new file mode 100644 index 0000000000..bc2b28eb5a --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/UrlButton.kt @@ -0,0 +1,100 @@ +package net.mullvad.mullvadvpn.ui.widget + +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.util.AttributeSet +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.async +import net.mullvad.mullvadvpn.R +import net.mullvad.mullvadvpn.service.MullvadDaemon +import net.mullvad.mullvadvpn.util.JobTracker + +class UrlButton : Button { + private lateinit var daemon: MullvadDaemon + + private var shouldEnable = true + + var url: String? = null + var withToken = false + + constructor(context: Context) : super(context) {} + + constructor(context: Context, attributes: AttributeSet) : super(context, attributes) { + loadAttributes(attributes) + } + + constructor(context: Context, attributes: AttributeSet, defaultStyleAttribute: Int) : + super(context, attributes, defaultStyleAttribute) { + loadAttributes(attributes) + } + + constructor( + context: Context, + attributes: AttributeSet, + defaultStyleAttribute: Int, + defaultStyleResource: Int + ) : super(context, attributes, defaultStyleAttribute, defaultStyleResource) { + loadAttributes(attributes) + } + + init { + super.setEnabled(false) + } + + fun prepare(daemon: MullvadDaemon, jobTracker: JobTracker, jobName: String = "fetchUrl") { + synchronized(this) { + super.setEnabled(shouldEnable) + + this.daemon = daemon + + setOnClickAction(jobName, jobTracker) { + super.setEnabled(false) + + context.startActivity(buildIntent(jobTracker)) + + super.setEnabled(true) + } + } + } + + override fun setEnabled(enabled: Boolean) { + synchronized(this) { + shouldEnable = enabled + + if (!withToken || this::daemon.isInitialized) { + super.setEnabled(enabled) + } + } + } + + private fun loadAttributes(attributes: AttributeSet) { + val styleableId = R.styleable.UrlButton + + context.theme.obtainStyledAttributes(attributes, styleableId, 0, 0).apply { + try { + url = getString(R.styleable.UrlButton_url) + withToken = getBoolean(R.styleable.UrlButton_withToken, false) + } finally { + recycle() + } + } + } + + private suspend fun buildIntent(jobTracker: JobTracker): Intent { + val buildIntent = GlobalScope.async(Dispatchers.Default) { + val uri = if (withToken) { + Uri.parse(url + "?token=" + daemon.getWwwAuthToken()) + } else { + Uri.parse(url) + } + + Intent(Intent.ACTION_VIEW, uri) + } + + jobTracker.newJob(buildIntent) + + return buildIntent.await() + } +} diff --git a/android/src/main/res/values/attrs.xml b/android/src/main/res/values/attrs.xml index 20fd1f553e..e97d6cd591 100644 --- a/android/src/main/res/values/attrs.xml +++ b/android/src/main/res/values/attrs.xml @@ -11,4 +11,8 @@ <enum name="showSpinner" value="2"/> </attr> </declare-styleable> + <declare-styleable name="UrlButton"> + <attr name="url" format="reference|string"/> + <attr name="withToken" format="boolean"/> + </declare-styleable> </resources> |
